Bug 29697: Replace GetMarcBiblio occurrences with $biblio->metadata->record
[srvgit] / Koha / BackgroundJob / BatchUpdateBiblio.pm
1 package Koha::BackgroundJob::BatchUpdateBiblio;
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 Koha::Biblios;
22 use Koha::DateUtils qw( dt_from_string );
23 use Koha::Virtualshelves;
24 use Koha::SearchEngine;
25 use Koha::SearchEngine::Indexer;
26
27 use C4::Context;
28 use C4::Biblio;
29 use C4::MarcModificationTemplates;
30
31 use base 'Koha::BackgroundJob';
32
33 =head1 NAME
34
35 Koha::BackgroundJob::BatchUpdateBiblio - Batch update bibliographic records
36
37 This is a subclass of Koha::BackgroundJob.
38
39 =head1 API
40
41 =head2 Class methods
42
43 =head3 job_type
44
45 Define the job type of this job: batch_biblio_record_modification
46
47 =cut
48
49 sub job_type {
50     return 'batch_biblio_record_modification';
51 }
52
53 =head3 process
54
55 Process the modification.
56
57 =cut
58
59 sub process {
60     my ( $self, $args ) = @_;
61
62     if ( $self->status eq 'cancelled' ) {
63         return;
64     }
65
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
68
69     my $job_progress = 0;
70     $self->started_on(dt_from_string)
71         ->progress($job_progress)
72         ->status('started')
73         ->store;
74
75     my $mmtid = $args->{mmtid};
76     my @record_ids = @{ $args->{record_ids} };
77
78     my $report = {
79         total_records => scalar @record_ids,
80         total_success => 0,
81     };
82     my @messages;
83     RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
84
85         last if $self->get_from_storage->status eq 'cancelled';
86
87         next unless $biblionumber;
88
89         # Modify the biblio
90         my $error = eval {
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,
98             });
99         };
100         if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
101             push @messages, {
102                 type => 'error',
103                 code => 'biblio_not_modified',
104                 biblionumber => $biblionumber,
105                 error => ($@ ? "$@" : $error),
106             };
107         } else {
108             push @messages, {
109                 type => 'success',
110                 code => 'biblio_modified',
111                 biblionumber => $biblionumber,
112             };
113             $report->{total_success}++;
114         }
115         $self->progress( ++$job_progress )->store;
116     }
117
118     my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
119     $indexer->index_records( \@record_ids, "specialUpdate", "biblioserver" );
120
121     my $job_data = decode_json $self->data;
122     $job_data->{messages} = \@messages;
123     $job_data->{report} = $report;
124
125     $self->ended_on(dt_from_string)
126         ->data(encode_json $job_data);
127     $self->status('finished') if $self->status ne 'cancelled';
128     $self->store;
129 }
130
131 =head3 enqueue
132
133 Enqueue the new job
134
135 =cut
136
137 sub enqueue {
138     my ( $self, $args) = @_;
139
140     # TODO Raise exception instead
141     return unless exists $args->{mmtid};
142     return unless exists $args->{record_ids};
143
144     $self->SUPER::enqueue({
145         job_size => scalar @{$args->{record_ids}},
146         job_args => $args,
147         queue    => 'long_tasks',
148     });
149 }
150
151 =head3 additional_report
152
153 Pass the list of lists/virtual shelves the logged in user has write permissions.
154
155 It will enable the "add modified records to list" feature.
156
157 =cut
158
159 sub additional_report {
160     my ($self) = @_;
161
162     my $loggedinuser = C4::Context->userenv ? C4::Context->userenv->{'number'} : undef;
163     return {
164         lists => Koha::Virtualshelves->search(
165             [
166                 { public => 0, owner => $loggedinuser },
167                 { public => 1 }
168             ]
169         ),
170     };
171 }
172
173 1;