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