1 package Koha::BackgroundJob::BatchUpdateBiblio;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 use JSON qw( decode_json encode_json );
21 use Koha::DateUtils qw( dt_from_string );
22 use Koha::Virtualshelves;
23 use Koha::SearchEngine;
24 use Koha::SearchEngine::Indexer;
28 use C4::MarcModificationTemplates;
30 use base 'Koha::BackgroundJob';
34 Koha::BackgroundJob::BatchUpdateBiblio - Batch update bibliographic records
36 This is a subclass of Koha::BackgroundJob.
44 Define the job type of this job: batch_biblio_record_modification
49 return 'batch_biblio_record_modification';
54 Process the modification.
59 my ( $self, $args ) = @_;
61 if ( $self->status eq 'cancelled' ) {
65 # FIXME If the job has already been started, but started again (worker has been restart for instance)
66 # Then we will start from scratch and so double process the same records
69 $self->started_on(dt_from_string)
70 ->progress($job_progress)
74 my $mmtid = $args->{mmtid};
75 my @record_ids = @{ $args->{record_ids} };
78 total_records => scalar @record_ids,
82 RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
84 last if $self->get_from_storage->status eq 'cancelled';
86 next unless $biblionumber;
90 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
91 C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record );
92 my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
93 C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode, {
94 overlay_context => $args->{overlay_context},
95 skip_record_index => 1,
98 if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
101 code => 'biblio_not_modified',
102 biblionumber => $biblionumber,
103 error => ($@ ? "$@" : $error),
108 code => 'biblio_modified',
109 biblionumber => $biblionumber,
111 $report->{total_success}++;
113 $self->progress( ++$job_progress )->store;
116 my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
117 $indexer->index_records( \@record_ids, "specialUpdate", "biblioserver" );
119 my $job_data = decode_json $self->data;
120 $job_data->{messages} = \@messages;
121 $job_data->{report} = $report;
123 $self->ended_on(dt_from_string)
124 ->data(encode_json $job_data);
125 $self->status('finished') if $self->status ne 'cancelled';
136 my ( $self, $args) = @_;
138 # TODO Raise exception instead
139 return unless exists $args->{mmtid};
140 return unless exists $args->{record_ids};
142 $self->SUPER::enqueue({
143 job_size => scalar @{$args->{record_ids}},
145 queue => 'long_tasks',
149 =head3 additional_report
151 Pass the list of lists/virtual shelves the logged in user has write permissions.
153 It will enable the "add modified records to list" feature.
157 sub additional_report {
160 my $loggedinuser = C4::Context->userenv ? C4::Context->userenv->{'number'} : undef;
162 lists => Koha::Virtualshelves->search(
164 { public => 0, owner => $loggedinuser },