1 package Koha::BackgroundJob::BatchUpdateAuthority;
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>.
20 use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate );
21 use C4::AuthoritiesMarc qw( ModAuthority );
22 use Koha::BackgroundJobs;
23 use Koha::DateUtils qw( dt_from_string );
24 use Koha::MetadataRecord::Authority;
25 use Koha::SearchEngine;
26 use Koha::SearchEngine::Indexer;
28 use base 'Koha::BackgroundJob';
32 Koha::BackgroundJob::BatchUpdateAuthority - Batch update authorities
34 This is a subclass of Koha::BackgroundJob.
42 Define the job type of this job: batch_authority_record_modification
47 return 'batch_authority_record_modification';
52 Process the modification.
57 my ( $self, $args ) = @_;
59 if ( $self->status eq 'cancelled' ) {
64 $self->started_on(dt_from_string)
65 ->progress($job_progress)
69 my $mmtid = $args->{mmtid};
70 my @record_ids = @{ $args->{record_ids} };
73 total_records => scalar @record_ids,
77 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
78 next unless $record_id;
80 my $authid = $record_id;
82 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
83 my $record = $authority->record;
84 ModifyRecordWithTemplate( $mmtid, $record );
85 ModAuthority( $authid, $record, $authority->authtypecode, { skip_record_index => 1 } );
87 if ( $error and $error != $authid or $@ ) {
90 code => 'authority_not_modified',
92 error => ($@ ? "$@" : 0),
97 code => 'authority_modified',
100 $report->{total_success}++;
102 $self->progress( ++$job_progress )->store;
105 my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX });
106 $indexer->index_records( \@record_ids, "specialUpdate", "authorityserver" );
108 my $json = $self->json;
109 my $job_data = $json->decode($self->data);
110 $job_data->{messages} = \@messages;
111 $job_data->{report} = $report;
113 $self->ended_on(dt_from_string)
114 ->data($json->encode($job_data));
115 $self->status('finished') if $self->status ne 'cancelled';
127 my ( $self, $args) = @_;
129 # TODO Raise exception instead
130 return unless exists $args->{mmtid};
131 return unless exists $args->{record_ids};
133 my $mmtid = $args->{mmtid};
134 my @record_ids = @{ $args->{record_ids} };
136 $self->SUPER::enqueue({
137 job_size => scalar @record_ids,
138 job_args => {mmtid => $mmtid, record_ids => \@record_ids,},
139 queue => 'long_tasks',