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>.
22 use Koha::DateUtils qw( dt_from_string );
23 use Koha::Virtualshelves;
24 use Koha::SearchEngine;
25 use Koha::SearchEngine::Indexer;
29 use C4::MarcModificationTemplates;
31 use base 'Koha::BackgroundJob';
35 Koha::BackgroundJob::BatchUpdateBiblio - Batch update bibliographic records
37 This is a subclass of Koha::BackgroundJob.
45 Define the job type of this job: batch_biblio_record_modification
50 return 'batch_biblio_record_modification';
55 Process the modification.
60 my ( $self, $args ) = @_;
62 if ( $self->status eq 'cancelled' ) {
66 # FIXME If the job has already been started, but started again (worker has been restart for instance)
67 # Then we will start from scratch and so double process the same records
70 $self->started_on(dt_from_string)
71 ->progress($job_progress)
75 my $mmtid = $args->{mmtid};
76 my @record_ids = @{ $args->{record_ids} };
79 total_records => scalar @record_ids,
83 RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
85 last if $self->get_from_storage->status eq 'cancelled';
87 next unless $biblionumber;
91 my $biblio = Koha::Biblios->find($biblionumber);
92 my $record = $biblio->metadata->record;
93 C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record );
94 my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
95 C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode, {
96 overlay_context => $args->{overlay_context},
97 skip_record_index => 1,
100 if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
103 code => 'biblio_not_modified',
104 biblionumber => $biblionumber,
105 error => ($@ ? "$@" : $error),
110 code => 'biblio_modified',
111 biblionumber => $biblionumber,
113 $report->{total_success}++;
115 $self->progress( ++$job_progress )->store;
118 my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
119 $indexer->index_records( \@record_ids, "specialUpdate", "biblioserver" );
121 my $json = JSON->new;
122 my $job_data = $json->decode($self->data);
123 $job_data->{messages} = \@messages;
124 $job_data->{report} = $report;
126 $self->ended_on(dt_from_string)
127 ->data($json->encode($job_data));
128 $self->status('finished') if $self->status ne 'cancelled';
139 my ( $self, $args) = @_;
141 # TODO Raise exception instead
142 return unless exists $args->{mmtid};
143 return unless exists $args->{record_ids};
145 $self->SUPER::enqueue({
146 job_size => scalar @{$args->{record_ids}},
148 queue => 'long_tasks',
152 =head3 additional_report
154 Pass the list of lists/virtual shelves the logged in user has write permissions.
156 It will enable the "add modified records to list" feature.
160 sub additional_report {
163 my $loggedinuser = C4::Context->userenv ? C4::Context->userenv->{'number'} : undef;
165 lists => Koha::Virtualshelves->search(
167 { public => 0, owner => $loggedinuser },