Bug 30464: Make BatchUpdateAuthority update the index in one request
[koha.git] / Koha / BackgroundJob / BatchUpdateAuthority.pm
1 package Koha::BackgroundJob::BatchUpdateAuthority;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19 use JSON qw( decode_json encode_json );
20
21 use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate );
22 use C4::AuthoritiesMarc qw( ModAuthority );
23 use Koha::BackgroundJobs;
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::MetadataRecord::Authority;
26 use Koha::SearchEngine;
27 use Koha::SearchEngine::Indexer;
28
29 use base 'Koha::BackgroundJob';
30
31 =head1 NAME
32
33 Koha::BackgroundJob::BatchUpdateAuthority - Batch update authorities
34
35 This is a subclass of Koha::BackgroundJob.
36
37 =head1 API
38
39 =head2 Class methods
40
41 =head3 job_type
42
43 Define the job type of this job: batch_authority_record_modification
44
45 =cut
46
47 sub job_type {
48     return 'batch_authority_record_modification';
49 }
50
51 =head3 process
52
53 Process the modification.
54
55 =cut
56
57 sub process {
58     my ( $self, $args ) = @_;
59
60     if ( $self->status eq 'cancelled' ) {
61         return;
62     }
63
64     my $job_progress = 0;
65     $self->started_on(dt_from_string)
66         ->progress($job_progress)
67         ->status('started')
68         ->store;
69
70     my $mmtid = $args->{mmtid};
71     my @record_ids = @{ $args->{record_ids} };
72
73     my $report = {
74         total_records => scalar @record_ids,
75         total_success => 0,
76     };
77     my @messages;
78     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
79         next unless $record_id;
80         # Authorities
81         my $authid = $record_id;
82         my $error = eval {
83             my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
84             my $record = $authority->record;
85             ModifyRecordWithTemplate( $mmtid, $record );
86             ModAuthority( $authid, $record, $authority->authtypecode, { skip_record_index => 1 } );
87         };
88         if ( $error and $error != $authid or $@ ) {
89             push @messages, {
90                 type => 'error',
91                 code => 'authority_not_modified',
92                 authid => $authid,
93                 error => ($@ ? "$@" : 0),
94             };
95         } else {
96             push @messages, {
97                 type => 'success',
98                 code => 'authority_modified',
99                 authid => $authid,
100             };
101             $report->{total_success}++;
102         }
103         $self->progress( ++$job_progress )->store;
104     }
105
106     my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX });
107     $indexer->index_records( \@record_ids, "specialUpdate", "authorityserver" );
108
109     my $job_data = decode_json $self->data;
110     $job_data->{messages} = \@messages;
111     $job_data->{report} = $report;
112
113     $self->ended_on(dt_from_string)
114         ->data(encode_json $job_data);
115     $self->status('finished') if $self->status ne 'cancelled';
116     $self->store;
117
118 }
119
120 =head3 enqueue
121
122 Enqueue the new job
123
124 =cut
125
126 sub enqueue {
127     my ( $self, $args) = @_;
128
129     # TODO Raise exception instead
130     return unless exists $args->{mmtid};
131     return unless exists $args->{record_ids};
132
133     my $mmtid = $args->{mmtid};
134     my @record_ids = @{ $args->{record_ids} };
135
136     $self->SUPER::enqueue({
137         job_size => scalar @record_ids,
138         job_args => {mmtid => $mmtid, record_ids => \@record_ids,},
139         queue    => 'long_tasks',
140     });
141 }
142
143 1;