Bug 19322: Typo in UNIMARC field 140 plugin
[koha.git] / tools / batch_record_modification.pl
blobe9e307b314ddfdfc5d221ce38eb808b315dc671f
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;
35 use Koha::Virtualshelves;
37 my $input = new CGI;
38 our $dbh = C4::Context->dbh;
39 my $op = $input->param('op') // q|form|;
40 my $recordtype = $input->param('recordtype') // 'biblio';
41 my $mmtid = $input->param('marc_modification_template_id');
43 my ( @messages );
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
46 template_name => 'tools/batch_record_modification.tt',
47 query => $input,
48 type => "intranet",
49 flagsrequired => { tools => 'records_batchmod' },
50 });
52 $template->param( lists => scalar Koha::Virtualshelves->search([{ category => 1, owner => $loggedinuser }, { category => 2 }]) );
54 my $sessionID = $input->cookie("CGISESSID");
56 my $runinbackground = $input->param('runinbackground');
57 my $completedJobID = $input->param('completedJobID');
58 if ( $completedJobID ) {
59 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
60 my $report = $job->get('report');
61 my $messages = $job->get('messages');
62 $template->param(
63 report => $report,
64 messages => $messages,
65 view => 'report',
66 recordtype => $recordtype,
68 output_html_with_http_headers $input, $cookie, $template->output;
69 $job->clear();
70 exit;
73 my @templates = GetModificationTemplates( $mmtid );
74 unless ( @templates ) {
75 $op = 'error';
76 $template->param(
77 view => 'errors',
78 errors => ['no_template_defined'],
80 output_html_with_http_headers $input, $cookie, $template->output;
81 exit;
84 if ( $mmtid ) {
85 my @actions = GetModificationTemplateActions( $mmtid );
86 unless ( @actions ) {
87 $op = 'form';
88 push @messages, {
89 type => 'error',
90 code => 'no_action_defined_for_the_template',
91 mmtid => $mmtid,
96 if ( $op eq 'form' ) {
97 # Display the form
98 $template->param(
99 view => 'form',
101 } elsif ( $op eq 'list' ) {
102 # List all records to process
103 my ( @records, @record_ids );
104 if ( my $bib_list = $input->param('bib_list') ) {
105 # Come from the basket
106 @record_ids = split /\//, $bib_list;
107 $recordtype = 'biblio';
108 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
109 # A file of id is given
110 binmode $uploadfile, ':encoding(UTF-8)';
111 while ( my $content = <$uploadfile> ) {
112 next unless $content;
113 $content =~ s/[\r\n]*$//;
114 push @record_ids, $content if $content;
116 } elsif ( my $shelf_number = $input->param('shelf_number') ) {
117 my $shelf = Koha::Virtualshelves->find($shelf_number);
118 my $contents = $shelf->get_contents;
119 while ( my $content = $contents->next ) {
120 my $biblionumber = $content->biblionumber;
121 push @record_ids, $biblionumber;
123 } else {
124 # The user enters manually the list of id
125 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
128 for my $record_id ( uniq @record_ids ) {
129 if ( $recordtype eq 'biblio' ) {
130 # Retrieve biblio information
131 my $biblio = Koha::Biblios->find( $record_id );
132 unless ( $biblio ) {
133 push @messages, {
134 type => 'warning',
135 code => 'biblio_not_exists',
136 biblionumber => $record_id,
138 next;
140 push @records, $biblio;
141 } else {
142 # Retrieve authority information
143 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
144 unless ( $authority ) {
145 push @messages, {
146 type => 'warning',
147 code => 'authority_not_exists',
148 authid => $record_id,
150 next;
153 push @records, {
154 authid => $record_id,
155 summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
159 $template->param(
160 records => \@records,
161 mmtid => $mmtid,
162 view => 'list',
164 } elsif ( $op eq 'modify' ) {
165 # We want to modify selected records!
166 my @record_ids = $input->multi_param('record_id');
168 my ( $job );
169 if ( $runinbackground ) {
170 my $job_size = scalar( @record_ids );
171 $job = C4::BackgroundJob->new( $sessionID, "FIXME", '/cgi-bin/koha/tools/batch_record_modification.pl', $job_size );
172 my $job_id = $job->id;
173 if (my $pid = fork) {
174 $dbh->{InactiveDestroy} = 1;
176 my $reply = CGI->new("");
177 print $reply->header(-type => 'text/html');
178 print '{"jobID":"' . $job_id . '"}';
179 exit 0;
180 } elsif (defined $pid) {
181 close STDOUT;
182 } else {
183 warn "fork failed while attempting to run tools/batch_record_modification.pl as a background job";
184 exit 0;
188 my $report = {
189 total_records => 0,
190 total_success => 0,
192 my $progress = 0;
193 $dbh->{RaiseError} = 1;
194 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
195 $report->{total_records}++;
196 next unless $record_id;
198 if ( $recordtype eq 'biblio' ) {
199 # Biblios
200 my $biblionumber = $record_id;
202 # Finally, modify the biblio
203 my $error = eval {
204 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
205 ModifyRecordWithTemplate( $mmtid, $record );
206 my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
207 ModBiblio( $record, $biblionumber, $frameworkcode );
209 if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
210 push @messages, {
211 type => 'error',
212 code => 'biblio_not_modified',
213 biblionumber => $biblionumber,
214 error => ($@ ? $@ : $error),
216 } else {
217 push @messages, {
218 type => 'success',
219 code => 'biblio_modified',
220 biblionumber => $biblionumber,
222 $report->{total_success}++;
224 } else {
225 # Authorities
226 my $authid = $record_id;
227 my $error = eval {
228 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
229 my $record = $authority->record;
230 ModifyRecordWithTemplate( $mmtid, $record );
231 ModAuthority( $authid, $record, $authority->authtypecode );
233 if ( $error and $error != $authid or $@ ) {
234 push @messages, {
235 type => 'error',
236 code => 'authority_not_modified',
237 authid => $authid,
238 error => ($@ ? $@ : 0),
240 } else {
241 push @messages, {
242 type => 'success',
243 code => 'authority_modified',
244 authid => $authid,
246 $report->{total_success}++;
250 $job->set({
251 view => 'report',
252 report => $report,
253 messages => \@messages,
255 $job->progress( ++$progress ) if $runinbackground;
258 if ($runinbackground) {
259 $job->finish if defined $job;
260 } else {
261 $template->param(
262 view => 'report',
263 report => $report,
264 messages => \@messages,
269 $template->param(
270 messages => \@messages,
271 recordtype => $recordtype,
272 MarcModificationTemplatesLoop => \@templates,
275 output_html_with_http_headers $input, $cookie, $template->output;