From 5374bd2101d2e9a6da2cd834b039efedb6583b29 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Thu, 8 Jun 2023 17:31:28 +0200
Subject: [PATCH] Bug 33963: Remove C4::BackgroundJob

Finally! No more occurrences of this module, we can happily remove it!

Test plan:
git grep is your friend
---
 C4/BackgroundJob.pm                           | 325 ------------------
 .../apache-shared-intranet-plack.conf         |   1 -
 .../prog/js/background-job-progressbar.js     |  84 -----
 t/db_dependent/BackgroundJob.t                |  57 ---
 tools/background-job-progress.pl              |  56 ---
 5 files changed, 523 deletions(-)
 delete mode 100644 C4/BackgroundJob.pm
 delete mode 100644 koha-tmpl/intranet-tmpl/prog/js/background-job-progressbar.js
 delete mode 100755 t/db_dependent/BackgroundJob.t
 delete mode 100755 tools/background-job-progress.pl

diff --git a/C4/BackgroundJob.pm b/C4/BackgroundJob.pm
deleted file mode 100644
index 733f72e11b1..00000000000
--- a/C4/BackgroundJob.pm
+++ /dev/null
@@ -1,325 +0,0 @@
-package C4::BackgroundJob;
-
-# Copyright (C) 2007 LibLime
-# Galen Charlton <galen.charlton@liblime.com>
-#
-# This file is part of Koha.
-#
-# Koha is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 3 of the License, or
-# (at your option) any later version.
-#
-# Koha is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with Koha; if not, see <http://www.gnu.org/licenses>.
-
-use Modern::Perl;
-use C4::Context;
-use C4::Auth qw( get_session );
-use Digest::MD5;
-
-
-
-=head1 NAME
-
-C4::BackgroundJob - manage long-running jobs
-initiated from the web staff interface
-
-=head1 SYNOPSIS
-
- # start tracking a job
- my $job = C4::BackgroundJob->new($sessionID, $job_name, $job_invoker, $num_work_units);
- my $jobID = $job->id();
- $job->progress($work_units_processed);
- $job->finish($job_result_hashref);
-
- # get status and results of a job
- my $job = C4::BackgroundJob->fetch($sessionID, $jobID);
- my $max_work_units = $job->size();
- my $work_units_processed = $job->progress();
- my $job_status = $job->status();
- my $job_name = $job->name();
- my $job_invoker = $job->invoker();
- my $results_hashref = $job->results();
-
-This module manages tracking the progress and results
-of (potentially) long-running jobs initiated from 
-the staff user interface.  Such jobs can include
-batch MARC and patron record imports.
-
-=head1 METHODS
-
-=head2 new
-
- my $job = C4::BackgroundJob->new($sessionID, $job_name, $job_invoker, $num_work_units);
-
-Create a new job object and set its status to 'running'.  C<$num_work_units>
-should be a number representing the size of the job; the units of the
-job size are up to the caller and could be number of records, 
-number of bytes, etc.
-
-=cut
-
-sub new {
-    my $class = shift;
-    my ($sessionID, $job_name, $job_invoker, $num_work_units) = @_;
-
-    my $self = {};
-    $self->{'sessionID'} = $sessionID;
-    $self->{'name'} = $job_name;
-    $self->{'invoker'} = $job_invoker;
-    $self->{'size'} = $num_work_units;
-    $self->{'progress'} = 0;
-    $self->{'status'} = "running";
-    $self->{'jobID'} = Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
-    $self->{'extra_values'} = {};
-
-    bless $self, $class;
-    $self->_serialize();
-
-    return $self;
-}
-
-# store object in CGI session
-sub _serialize {
-    my $self = shift;
-
-    my $prefix = "job_" . $self->{'jobID'};
-    my $session = get_session($self->{'sessionID'});
-    $session->param($prefix, $self);
-    $session->flush();
-}
-
-=head2 id
-
- my $jobID = $job->id();
-
-Read-only accessor for job ID.
-
-=cut
-
-sub id {
-    my $self = shift;
-    return $self->{'jobID'};
-}
-
-=head2 name
-
- my $name = $job->name();
- $job->name($name);
-
-Read/write accessor for job name.
-
-=cut
-
-sub name {
-    my $self = shift;
-    if (@_) {
-        $self->{'name'} = shift;
-        $self->_serialize();
-    } else {
-        return $self->{'name'};
-    }
-}
-
-=head2 invoker
-
- my $invoker = $job->invoker();
-i $job->invoker($invoker);
-
-Read/write accessor for job invoker.
-
-=cut
-
-sub invoker {
-    my $self = shift;
-    if (@_) {
-        $self->{'invoker'} = shift;
-        $self->_serialize();
-    } else {
-        return $self->{'invoker'};
-    }
-}
-
-=head2 progress
-
- my $progress = $job->progress();
- $job->progress($progress);
-
-Read/write accessor for job progress.
-
-=cut
-
-sub progress {
-    my $self = shift;
-    if (@_) {
-        $self->{'progress'} = shift;
-        $self->_serialize();
-    } else {
-        return $self->{'progress'};
-    }
-}
-
-=head2 status
-
- my $status = $job->status();
-
-Read-only accessor for job status.
-
-=cut
-
-sub status {
-    my $self = shift;
-    return $self->{'status'};
-}
-
-=head2 size
-
- my $size = $job->size();
- $job->size($size);
-
-Read/write accessor for job size.
-
-=cut
-
-sub size {
-    my $self = shift;
-    if (@_) {
-        $self->{'size'} = shift;
-        $self->_serialize();
-    } else {
-        return $self->{'size'};
-    }
-}
-
-=head2 finish
-
- $job->finish($results_hashref);
-
-Mark the job as finished, setting its status to 'completed'.
-C<$results_hashref> should be a reference to a hash containing
-the results of the job.
-
-=cut
-
-sub finish {
-    my $self = shift;
-    my $results_hashref = shift;
-    $self->{'status'} = 'completed';
-    $self->{'results'} = $results_hashref;
-    $self->_serialize();
-}
-
-=head2 results
-
- my $results_hashref = $job->results();
-
-Retrieve the results of the current job.  Returns undef 
-if the job status is not 'completed'.
-
-=cut
-
-sub results {
-    my $self = shift;
-    return unless $self->{'status'} eq 'completed';
-    return $self->{'results'};
-}
-
-=head2 fetch
-
- my $job = C4::BackgroundJob->fetch($sessionID, $jobID);
-
-Retrieve a job that has been serialized to the database. 
-Returns C<undef> if the job does not exist in the current 
-session.
-
-=cut
-
-sub fetch {
-    my $class = shift;
-    my $sessionID = shift;
-    my $jobID = shift;
-
-    my $session = get_session($sessionID);
-    my $prefix = "job_$jobID";
-    unless (defined $session->param($prefix)) {
-        return;
-    }
-    my $self = $session->param($prefix);
-    bless $self, $class;
-    return $self;
-}
-
-=head2 set
-
-=over 4
-
-=item $job->set($hashref);
-
-=back
-
-Set some variables into the hashref.
-These variables can be retrieved using the get method.
-
-=cut
-
-sub set {
-    my ($self, $hashref) = @_;
-    while ( my ($k, $v) = each %$hashref ) {
-        $self->{extra_values}->{$k} = $v;
-    }
-    $self->_serialize();
-    return;
-}
-
-=head2 get
-
-=over 4
-
-=item $value = $job->get($key);
-
-=back
-
-Get a variable which has been previously stored with the set method.
-
-=cut
-
-sub get {
-    my ($self, $key) = @_;
-    return $self->{extra_values}->{$key};
-}
-
-
-=head2 clear
-
-=over 4
-
-=item $job->clear();
-
-=back
-
-Clear the job from the current session.
-
-=cut
-
-sub clear {
-    my $self = shift;
-    get_session($self->{sessionID})->clear('job_' . $self->{jobID});
-}
-
-
-1;
-__END__
-
-=head1 AUTHOR
-
-Koha Development Team <http://koha-community.org/>
-
-Galen Charlton <galen.charlton@liblime.com>
-
-=cut
diff --git a/debian/templates/apache-shared-intranet-plack.conf b/debian/templates/apache-shared-intranet-plack.conf
index 856a86f71fe..7f9f47e1e13 100644
--- a/debian/templates/apache-shared-intranet-plack.conf
+++ b/debian/templates/apache-shared-intranet-plack.conf
@@ -11,7 +11,6 @@
 
         # FIXME: These scripts should be fixed so they
         # don't break under plack/starman
-        ProxyPass "/cgi-bin/koha/tools/background-job-progress.pl" "!"
         ProxyPass "/cgi-bin/koha/tools/export.pl" "!"
         ProxyPass "/cgi-bin/koha/tools/upload-cover-image.pl" "!"
         ProxyPass "/cgi-bin/koha/svc/cataloguing/metasearch" "!"
diff --git a/koha-tmpl/intranet-tmpl/prog/js/background-job-progressbar.js b/koha-tmpl/intranet-tmpl/prog/js/background-job-progressbar.js
deleted file mode 100644
index 52ee7380e4a..00000000000
--- a/koha-tmpl/intranet-tmpl/prog/js/background-job-progressbar.js
+++ /dev/null
@@ -1,84 +0,0 @@
-var backgroundJobProgressTimer = 0;
-var jobID = '';
-var savedForm;
-var inBackgroundJobProgressTimer = false;
-function updateJobProgress() {
-    if (inBackgroundJobProgressTimer) {
-        return;
-    }
-    inBackgroundJobProgressTimer = true;
-    $.getJSON("/cgi-bin/koha/tools/background-job-progress.pl?jobID=" + jobID, function(json) {
-        var percentage = json.job_status == 'completed' ? 100 :
-                            json.job_size > 0              ? Math.floor(100 * json.progress / json.job_size) :
-                            100;
-        var bgproperty = (parseInt(percentage*2)-300)+"px 0px";
-        $("#jobprogress").css("background-position",bgproperty);
-        $("#jobprogresspercent").text(percentage);
-
-        if (percentage == 100) {
-            clearInterval(backgroundJobProgressTimer); // just in case form submission fails
-            completeJob();
-        }
-        inBackgroundJobProgressTimer = false;
-    });
-}
-
-function completeJob() {
-    savedForm.completedJobID.value = jobID;
-    savedForm.submit();
-}
-
-// submit a background job with data
-// supplied from form f and activate
-// progress indicator
-function submitBackgroundJob(f) {
-    // check for background field
-    if (f.runinbackground) {
-        // set value of this hidden field for
-        // use by CGI script
-        savedForm = f;
-        f.mainformsubmit.disabled = true;
-        f.runinbackground.value = 'true';
-
-        // gather up form submission
-        var inputs = [];
-        $(':input:enabled', f).each(function() {
-            if (this.type == 'radio' || this.type == 'checkbox') {
-                if (this.checked) {
-                    inputs.push(this.name + '=' + encodeURIComponent(this.value));
-                }
-            } else if (this.type == 'button') {
-                ; // do nothing
-            } else {
-                inputs.push(this.name + '=' + encodeURIComponent(this.value));
-            }
-
-        });
-
-        // and submit the request
-        $("#jobpanel").show();
-        $("#jobstatus").show();
-        $.ajax({
-            data: inputs.join('&'),
-            url: f.action,
-            dataType: 'json',
-            type: 'post',
-            success: function(json) {
-                jobID = json.jobID;
-                inBackgroundJobProgressTimer = false;
-                backgroundJobProgressTimer = setInterval("updateJobProgress()", 500);
-            },
-            error: function(xml, textStatus) {
-                humanMsg.displayMsg( '<p>' + __('Import of record(s) failed: ') + textStatus + '</p></br>'+xml.responseText, { className: 'humanError' } );
-            }
-
-        });
-
-    } else {
-        // background job support not enabled,
-        // so just do a normal form submission
-        f.submit();
-    }
-
-    return false;
-}
diff --git a/t/db_dependent/BackgroundJob.t b/t/db_dependent/BackgroundJob.t
deleted file mode 100755
index 459f3b28568..00000000000
--- a/t/db_dependent/BackgroundJob.t
+++ /dev/null
@@ -1,57 +0,0 @@
-#!/usr/bin/perl
-
-use Modern::Perl;
-use C4::Auth qw( get_session );
-use CGI qw ( -utf8 );
-use Test::More tests => 18;
-
-use Koha::Database;
-
-BEGIN {
-    use_ok('C4::BackgroundJob', qw( get id fetch name invoker progress status size set finish results clear ));
-}
-my $query = CGI->new;
-
-my $schema = Koha::Database->new->schema;
-$schema->storage->txn_begin;
-
-my $session = C4::Auth::get_session;
-$session->flush;
-my $sessionID = $session->id;
-my $job;
-ok( $job = C4::BackgroundJob->new($sessionID), "making job" );
-ok( $job->id, "fetching id number" );
-
-$job->name("George");
-is( $job->name, "George", "testing name" );
-
-$job->invoker("enjoys");
-is( $job->invoker, "enjoys", "testing invoker" );
-
-$job->progress("testing");
-is( $job->progress, "testing", "testing progress" );
-
-ok( $job->status, "testing status" );
-
-$job->size("56");
-is( $job->size, "56", "testing size" );
-
-ok( C4::BackgroundJob->fetch( $sessionID, $job->id ), "testing fetch" );
-$job->set( { key1 => 'value1', key2 => 'value2' } );
-is( $job->get('key1'), 'value1', 'fetched extra value for key key1' );
-is( $job->get('key2'), 'value2', 'fetched extra value for key key2' );
-
-$job->set( { size => 666 } );
-is( $job->size, "56", '->set() does not scribble over private object data' );
-
-$job->finish("finished");
-is( $job->status, 'completed', "testing finished" );
-
-ok( $job->results, 'Test if we have results' );
-
-my $second_job = C4::BackgroundJob->new( $sessionID, "making new job" );
-is( ref( C4::BackgroundJob->fetch( $sessionID, $job->id ) ),        "C4::BackgroundJob", 'job_$jobid should be a C4::BackgroundJob for uncleared job 1' );
-is( ref( C4::BackgroundJob->fetch( $sessionID, $second_job->id ) ), "C4::BackgroundJob", 'job_$jobid should be a C4::BackgroundJob for uncleared job 2' );
-$job->clear;
-is( C4::BackgroundJob->fetch( $sessionID, $job->id ), undef, 'After clearing it, job 1 should not exist anymore in the session' );
-is( ref( C4::BackgroundJob->fetch( $sessionID, $second_job->id ) ), "C4::BackgroundJob", 'After clear on job 1, job 2 should still be a C4::BackgroundJob' );
diff --git a/tools/background-job-progress.pl b/tools/background-job-progress.pl
deleted file mode 100755
index ae6df95e863..00000000000
--- a/tools/background-job-progress.pl
+++ /dev/null
@@ -1,56 +0,0 @@
-#!/usr/bin/perl
-
-# Copyright (C) 2007 LibLime
-#
-# This file is part of Koha.
-#
-# Koha is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 3 of the License, or
-# (at your option) any later version.
-#
-# Koha is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with Koha; if not, see <http://www.gnu.org/licenses>.
-
-use Modern::Perl;
-
-# standard or CPAN modules used
-use CGI qw ( -utf8 );
-use CGI::Session;
-use C4::Context;
-use C4::Auth qw( check_cookie_auth );
-use C4::BackgroundJob;
-use CGI::Cookie; # need to check cookies before
-                 # having CGI parse the POST request
-
-my $input = CGI->new;
-my %cookies = CGI::Cookie->fetch;
-my ($auth_status, $session) = check_cookie_auth($cookies{'CGISESSID'}->value, { tools => '*' });
-if ($auth_status ne "ok") {
-    my $reply = CGI->new("");
-    print $reply->header(-type => 'text/html');
-    print '{"progress":"0"}';
-    exit 0;
-}
-
-my $sessionID = $session->id;
-my $jobID = $input->param('jobID');
-my $job = C4::BackgroundJob->fetch($sessionID, $jobID);
-my $reported_progress = 0;
-my $job_size = 100;
-my $job_status = 'running';
-if (defined $job) {
-    $reported_progress = $job->progress();
-    $job_size = $job->size();
-    $job_status = $job->status();
-}
-
-my $reply = CGI->new;
-print $reply->header(-type => 'text/html');
-# response will be sent back as JSON
-print '{"progress":"' . $reported_progress . '","job_size":"' . $job_size . '","job_status":"' . $job_status . '"}';
-- 
2.25.1