Bug 21579: Make showdiffmarc.pl work for authorities and biblios
[koha.git] / tools / batch_record_modification.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2013 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 3
10 # of the License, or (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General
18 # Public License along with Koha; if not, see
19 # <http://www.gnu.org/licenses>
20
21 use Modern::Perl;
22
23 use CGI;
24 use List::MoreUtils qw( uniq );
25
26 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::AuthoritiesMarc qw( BuildSummary ModAuthority );
29 use C4::BackgroundJob;
30 use C4::Biblio qw( GetMarcBiblio ModBiblio );
31 use C4::MarcModificationTemplates qw( GetModificationTemplateActions GetModificationTemplates ModifyRecordWithTemplate );
32
33 use Koha::Biblios;
34 use Koha::MetadataRecord::Authority;
35
36 my $input = new CGI;
37 our $dbh = C4::Context->dbh;
38 my $op = $input->param('op') // q|form|;
39 my $recordtype = $input->param('recordtype') // 'biblio';
40 my $mmtid = $input->param('marc_modification_template_id');
41
42 my ( @messages );
43
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
45         template_name => 'tools/batch_record_modification.tt',
46         query => $input,
47         type => "intranet",
48         authnotrequired => 0,
49         flagsrequired => { tools => 'records_batchmod' },
50 });
51
52
53 my $sessionID = $input->cookie("CGISESSID");
54
55 my $runinbackground = $input->param('runinbackground');
56 my $completedJobID = $input->param('completedJobID');
57 if ( $completedJobID ) {
58     my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
59     my $report = $job->get('report');
60     my $messages = $job->get('messages');
61     $template->param(
62         report => $report,
63         messages => $messages,
64         view => 'report',
65     );
66     output_html_with_http_headers $input, $cookie, $template->output;
67     $job->clear();
68     exit;
69 }
70
71 my @templates = GetModificationTemplates( $mmtid );
72 unless ( @templates ) {
73     $op = 'error';
74     $template->param(
75         view => 'errors',
76         errors => ['no_template_defined'],
77     );
78     output_html_with_http_headers $input, $cookie, $template->output;
79     exit;
80 }
81
82 if ( $mmtid ) {
83     my @actions = GetModificationTemplateActions( $mmtid );
84     unless ( @actions ) {
85         $op = 'form';
86         push @messages, {
87             type => 'error',
88             code => 'no_action_defined_for_the_template',
89             mmtid => $mmtid,
90         };
91     }
92 }
93
94 if ( $op eq 'form' ) {
95     # Display the form
96     $template->param(
97         view => 'form',
98     );
99 } elsif ( $op eq 'list' ) {
100     # List all records to process
101     my ( @records, @record_ids );
102     if ( my $bib_list = $input->param('bib_list') ) {
103         # Come from the basket
104         @record_ids = split /\//, $bib_list;
105         $recordtype = 'biblio';
106     } elsif ( my $uploadfile = $input->param('uploadfile') ) {
107         # A file of id is given
108         binmode $uploadfile, ':encoding(UTF-8)';
109         while ( my $content = <$uploadfile> ) {
110             next unless $content;
111             $content =~ s/[\r\n]*$//;
112             push @record_ids, $content if $content;
113         }
114     } else {
115         # The user enters manually the list of id
116         push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
117     }
118
119     for my $record_id ( uniq @record_ids ) {
120         if ( $recordtype eq 'biblio' ) {
121             # Retrieve biblio information
122             my $biblio = Koha::Biblios->find( $record_id );
123             unless ( $biblio ) {
124                 push @messages, {
125                     type => 'warning',
126                     code => 'biblio_not_exists',
127                     biblionumber => $record_id,
128                 };
129                 next;
130             }
131             push @records, $biblio;
132         } else {
133             # Retrieve authority information
134             my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
135             unless ( $authority ) {
136                 push @messages, {
137                     type => 'warning',
138                     code => 'authority_not_exists',
139                     authid => $record_id,
140                 };
141                 next;
142             }
143
144             push @records, {
145                 authid => $record_id,
146                 summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
147             };
148         }
149     }
150     $template->param(
151         records => \@records,
152         mmtid => $mmtid,
153         view => 'list',
154     );
155 } elsif ( $op eq 'modify' ) {
156     # We want to modify selected records!
157     my @record_ids = $input->multi_param('record_id');
158
159     my ( $job );
160     if ( $runinbackground ) {
161         my $job_size = scalar( @record_ids );
162         $job = C4::BackgroundJob->new( $sessionID, "FIXME", '/cgi-bin/koha/tools/batch_record_modification.pl', $job_size );
163         my $job_id = $job->id;
164         if (my $pid = fork) {
165             $dbh->{InactiveDestroy}  = 1;
166
167             my $reply = CGI->new("");
168             print $reply->header(-type => 'text/html');
169             print '{"jobID":"' . $job_id . '"}';
170             exit 0;
171         } elsif (defined $pid) {
172             close STDOUT;
173         } else {
174             warn "fork failed while attempting to run tools/batch_record_modification.pl as a background job";
175             exit 0;
176         }
177     }
178
179     my $report = {
180         total_records => 0,
181         total_success => 0,
182     };
183     my $progress = 0;
184     $dbh->{RaiseError} = 1;
185     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
186         $report->{total_records}++;
187         next unless $record_id;
188
189         if ( $recordtype eq 'biblio' ) {
190             # Biblios
191             my $biblionumber = $record_id;
192
193             # Finally, modify the biblio
194             my $error = eval {
195                 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
196                 ModifyRecordWithTemplate( $mmtid, $record );
197                 my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
198                 ModBiblio( $record, $biblionumber, $frameworkcode );
199             };
200             if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
201                 push @messages, {
202                     type => 'error',
203                     code => 'biblio_not_modified',
204                     biblionumber => $biblionumber,
205                     error => ($@ ? $@ : $error),
206                 };
207             } else {
208                 push @messages, {
209                     type => 'success',
210                     code => 'biblio_modified',
211                     biblionumber => $biblionumber,
212                 };
213                 $report->{total_success}++;
214             }
215         } else {
216             # Authorities
217             my $authid = $record_id;
218             my $error = eval {
219                 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
220                 my $record = $authority->record;
221                 ModifyRecordWithTemplate( $mmtid, $record );
222                 ModAuthority( $authid, $record, $authority->authtypecode );
223             };
224             if ( $error and $error != $authid or $@ ) {
225                 push @messages, {
226                     type => 'error',
227                     code => 'authority_not_modified',
228                     authid => $authid,
229                     error => ($@ ? $@ : 0),
230                 };
231             } else {
232                 push @messages, {
233                     type => 'success',
234                     code => 'authority_modified',
235                     authid => $authid,
236                 };
237                 $report->{total_success}++;
238             }
239         }
240
241         $job->set({
242             view => 'report',
243             report => $report,
244             messages => \@messages,
245         });
246         $job->progress( ++$progress ) if $runinbackground;
247     }
248
249     if ($runinbackground) {
250         $job->finish if defined $job;
251     } else {
252         $template->param(
253             view => 'report',
254             report => $report,
255             messages => \@messages,
256         );
257     }
258 }
259
260 $template->param(
261     messages => \@messages,
262     recordtype => $recordtype,
263     MarcModificationTemplatesLoop => \@templates,
264 );
265
266 output_html_with_http_headers $input, $cookie, $template->output;