Bug 18667: Show a diff view of SQL reports when converting
[koha.git] / tools / batch_record_modification.pl
blob89527fbcb3edb554c291456a94b5c0947e8b1c1c
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright 2013 BibLibre
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.
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.
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>
21 use Modern::Perl;
23 use CGI;
24 use List::MoreUtils qw( uniq );
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 );
33 use Koha::Biblios;
34 use Koha::MetadataRecord::Authority;
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');
42 my ( @messages );
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 });
53 my $sessionID = $input->cookie("CGISESSID");
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',
66 output_html_with_http_headers $input, $cookie, $template->output;
67 $job->clear();
68 exit;
71 my @templates = GetModificationTemplates( $mmtid );
72 unless ( @templates ) {
73 $op = 'error';
74 $template->param(
75 view => 'errors',
76 errors => ['no_template_defined'],
78 output_html_with_http_headers $input, $cookie, $template->output;
79 exit;
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,
94 if ( $op eq 'form' ) {
95 # Display the form
96 $template->param(
97 view => 'form',
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 while ( my $content = <$uploadfile> ) {
109 next unless $content;
110 $content =~ s/[\r\n]*$//;
111 push @record_ids, $content if $content;
113 } else {
114 # The user enters manually the list of id
115 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
118 for my $record_id ( uniq @record_ids ) {
119 if ( $recordtype eq 'biblio' ) {
120 # Retrieve biblio information
121 my $biblio = Koha::Biblios->find( $record_id );
122 unless ( $biblio ) {
123 push @messages, {
124 type => 'warning',
125 code => 'biblio_not_exists',
126 biblionumber => $record_id,
128 next;
130 push @records, $biblio;
131 } else {
132 # Retrieve authority information
133 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
134 unless ( $authority ) {
135 push @messages, {
136 type => 'warning',
137 code => 'authority_not_exists',
138 authid => $record_id,
140 next;
143 push @records, {
144 authid => $record_id,
145 summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
149 $template->param(
150 records => \@records,
151 mmtid => $mmtid,
152 view => 'list',
154 } elsif ( $op eq 'modify' ) {
155 # We want to modify selected records!
156 my @record_ids = $input->multi_param('record_id');
158 my ( $job );
159 if ( $runinbackground ) {
160 my $job_size = scalar( @record_ids );
161 $job = C4::BackgroundJob->new( $sessionID, "FIXME", '/cgi-bin/koha/tools/batch_record_modification.pl', $job_size );
162 my $job_id = $job->id;
163 if (my $pid = fork) {
164 $dbh->{InactiveDestroy} = 1;
166 my $reply = CGI->new("");
167 print $reply->header(-type => 'text/html');
168 print '{"jobID":"' . $job_id . '"}';
169 exit 0;
170 } elsif (defined $pid) {
171 close STDOUT;
172 } else {
173 warn "fork failed while attempting to run tools/batch_record_modification.pl as a background job";
174 exit 0;
178 my $report = {
179 total_records => 0,
180 total_success => 0,
182 my $progress = 0;
183 $dbh->{RaiseError} = 1;
184 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
185 $report->{total_records}++;
186 next unless $record_id;
188 if ( $recordtype eq 'biblio' ) {
189 # Biblios
190 my $biblionumber = $record_id;
192 # Finally, modify the biblio
193 my $error = eval {
194 my $record = GetMarcBiblio( $biblionumber );
195 ModifyRecordWithTemplate( $mmtid, $record );
196 my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
197 ModBiblio( $record, $biblionumber, $frameworkcode );
199 if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
200 push @messages, {
201 type => 'error',
202 code => 'biblio_not_modified',
203 biblionumber => $biblionumber,
204 error => ($@ ? $@ : $error),
206 } else {
207 push @messages, {
208 type => 'success',
209 code => 'biblio_modified',
210 biblionumber => $biblionumber,
212 $report->{total_success}++;
214 } else {
215 # Authorities
216 my $authid = $record_id;
217 my $error = eval {
218 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
219 my $record = $authority->record;
220 ModifyRecordWithTemplate( $mmtid, $record );
221 ModAuthority( $authid, $record, $authority->authtypecode );
223 if ( $error and $error != $authid or $@ ) {
224 push @messages, {
225 type => 'error',
226 code => 'authority_not_modified',
227 authid => $authid,
228 error => ($@ ? $@ : 0),
230 } else {
231 push @messages, {
232 type => 'success',
233 code => 'authority_modified',
234 authid => $authid,
236 $report->{total_success}++;
240 $job->set({
241 view => 'report',
242 report => $report,
243 messages => \@messages,
245 $job->progress( ++$progress ) if $runinbackground;
248 if ($runinbackground) {
249 $job->finish if defined $job;
250 } else {
251 $template->param(
252 view => 'report',
253 report => $report,
254 messages => \@messages,
259 $template->param(
260 messages => \@messages,
261 recordtype => $recordtype,
262 MarcModificationTemplatesLoop => \@templates,
265 output_html_with_http_headers $input, $cookie, $template->output;