Bug 22800: (bug 22550 follow-up) Do not raw filter when not necessary (OPAC suggestions)
[koha.git] / tools / batch_record_modification.pl
blob2b7608469be4ff10d2caef85216d045a0bec890f
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 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;
114 } else {
115 # The user enters manually the list of id
116 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
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,
129 next;
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,
141 next;
144 push @records, {
145 authid => $record_id,
146 summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
150 $template->param(
151 records => \@records,
152 mmtid => $mmtid,
153 view => 'list',
155 } elsif ( $op eq 'modify' ) {
156 # We want to modify selected records!
157 my @record_ids = $input->multi_param('record_id');
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;
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;
179 my $report = {
180 total_records => 0,
181 total_success => 0,
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;
189 if ( $recordtype eq 'biblio' ) {
190 # Biblios
191 my $biblionumber = $record_id;
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 );
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),
207 } else {
208 push @messages, {
209 type => 'success',
210 code => 'biblio_modified',
211 biblionumber => $biblionumber,
213 $report->{total_success}++;
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 );
224 if ( $error and $error != $authid or $@ ) {
225 push @messages, {
226 type => 'error',
227 code => 'authority_not_modified',
228 authid => $authid,
229 error => ($@ ? $@ : 0),
231 } else {
232 push @messages, {
233 type => 'success',
234 code => 'authority_modified',
235 authid => $authid,
237 $report->{total_success}++;
241 $job->set({
242 view => 'report',
243 report => $report,
244 messages => \@messages,
246 $job->progress( ++$progress ) if $runinbackground;
249 if ($runinbackground) {
250 $job->finish if defined $job;
251 } else {
252 $template->param(
253 view => 'report',
254 report => $report,
255 messages => \@messages,
260 $template->param(
261 messages => \@messages,
262 recordtype => $recordtype,
263 MarcModificationTemplatesLoop => \@templates,
266 output_html_with_http_headers $input, $cookie, $template->output;