The current searching in labels is a bit minimal, and current only does keyword searc...
[koha.git] / tools / manage-marc-import.pl
blob97ac5688920bbd6dd316a1bff34a4115090a89a2
1 #!/usr/bin/perl
3 # Copyright (C) 2007 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use strict;
22 # standard or CPAN modules used
23 use CGI;
24 use CGI::Cookie;
25 use MARC::File::USMARC;
27 # Koha modules used
28 use C4::Context;
29 use C4::Auth;
30 use C4::Input;
31 use C4::Output;
32 use C4::Biblio;
33 use C4::ImportBatch;
34 use C4::Matcher;
35 use C4::BackgroundJob;
36 use C4::Labels qw(add_batch);
38 my $script_name = "/cgi-bin/koha/tools/manage-marc-import.pl";
40 my $input = new CGI;
41 my $op = $input->param('op');
42 my $completedJobID = $input->param('completedJobID');
43 my $runinbackground = $input->param('runinbackground');
44 my $import_batch_id = $input->param('import_batch_id');
46 # record list displays
47 my $offset = $input->param('offset') || 0;
48 my $results_per_page = $input->param('results_per_page') || 25;
50 my ($template, $loggedinuser, $cookie)
51 = get_template_and_user({template_name => "tools/manage-marc-import.tmpl",
52 query => $input,
53 type => "intranet",
54 authnotrequired => 0,
55 flagsrequired => {tools => 'manage_staged_marc'},
56 debug => 1,
57 });
59 my %cookies = parse CGI::Cookie($cookie);
60 my $sessionID = $cookies{'CGISESSID'}->value;
61 my $dbh = C4::Context->dbh;
63 if ($op eq "create_labels") {
64 #create a batch of labels, then lose $op & $import_batch_id so we get back to import batch list.
65 my $label_batch_id = create_labelbatch_from_importbatch($import_batch_id);
66 $template->param( label_batch => $label_batch_id );
67 $op='';
68 $import_batch_id='';
70 if ($op) {
71 $template->param(script_name => $script_name, $op => 1);
72 } else {
73 $template->param(script_name => $script_name);
76 if ($op eq "") {
77 # displaying a list
78 if ($import_batch_id eq "") {
79 import_batches_list($template, $offset, $results_per_page);
80 } else {
81 import_biblios_list($template, $import_batch_id, $offset, $results_per_page);
83 } elsif ($op eq "commit-batch") {
84 if ($completedJobID) {
85 add_saved_job_results_to_template($template, $completedJobID);
86 } else {
87 commit_batch($template, $import_batch_id);
89 import_biblios_list($template, $import_batch_id, $offset, $results_per_page);
90 } elsif ($op eq "revert-batch") {
91 if ($completedJobID) {
92 add_saved_job_results_to_template($template, $completedJobID);
93 } else {
94 revert_batch($template, $import_batch_id);
96 import_biblios_list($template, $import_batch_id, $offset, $results_per_page);
97 } elsif ($op eq "clean-batch") {
99 } elsif ($op eq "redo-matching") {
100 my $new_matcher_id = $input->param('new_matcher_id');
101 my $current_matcher_id = $input->param('current_matcher_id');
102 my $overlay_action = $input->param('overlay_action');
103 my $nomatch_action = $input->param('nomatch_action');
104 my $item_action = $input->param('item_action');
105 redo_matching($template, $import_batch_id, $new_matcher_id, $current_matcher_id,
106 $overlay_action, $nomatch_action, $item_action);
107 import_biblios_list($template, $import_batch_id, $offset, $results_per_page);
110 output_html_with_http_headers $input, $cookie, $template->output;
112 exit 0;
114 sub redo_matching {
115 my ($template, $import_batch_id, $new_matcher_id, $current_matcher_id, $overlay_action, $nomatch_action, $item_action) = @_;
116 my $rematch_failed = 0;
117 return if not defined $new_matcher_id and not defined $current_matcher_id;
118 my $old_overlay_action = GetImportBatchOverlayAction($import_batch_id);
119 my $old_nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
120 my $old_item_action = GetImportBatchItemAction($import_batch_id);
121 return if $new_matcher_id == $current_matcher_id and
122 $old_overlay_action eq $overlay_action and
123 $old_nomatch_action eq $nomatch_action and
124 $old_item_action eq $item_action;
126 if ($old_overlay_action ne $overlay_action) {
127 SetImportBatchOverlayAction($import_batch_id, $overlay_action);
128 $template->param('changed_overlay_action' => 1);
130 if ($old_nomatch_action ne $nomatch_action) {
131 SetImportBatchNoMatchAction($import_batch_id, $nomatch_action);
132 $template->param('changed_nomatch_action' => 1);
134 if ($old_item_action ne $item_action) {
135 SetImportBatchItemAction($import_batch_id, $item_action);
136 $template->param('changed_item_action' => 1);
139 if ($new_matcher_id == $current_matcher_id) {
140 return;
143 my $num_with_matches = 0;
144 if (defined $new_matcher_id and $new_matcher_id ne "") {
145 my $matcher = C4::Matcher->fetch($new_matcher_id);
146 if (defined $matcher) {
147 $num_with_matches = BatchFindBibDuplicates($import_batch_id, $matcher);
148 SetImportBatchMatcher($import_batch_id, $new_matcher_id);
149 } else {
150 $rematch_failed = 1;
152 } else {
153 $num_with_matches = BatchFindBibDuplicates($import_batch_id, undef);
154 SetImportBatchMatcher($import_batch_id, undef);
155 SetImportBatchOverlayAction('create_new');
157 $template->param(rematch_failed => $rematch_failed);
158 $template->param(rematch_attempted => 1);
159 $template->param(num_with_matches => $num_with_matches);
162 sub create_labelbatch_from_importbatch {
163 my ($batch_id) = @_;
164 my @items = GetItemNumbersFromImportBatch($batch_id);
165 my $labelbatch = add_batch('labels',\@items);
166 return $labelbatch;
169 sub import_batches_list {
170 my ($template, $offset, $results_per_page) = @_;
171 my $batches = GetImportBatchRangeDesc($offset, $results_per_page);
173 my @list = ();
174 foreach my $batch (@$batches) {
175 push @list, {
176 import_batch_id => $batch->{'import_batch_id'},
177 num_biblios => $batch->{'num_biblios'},
178 num_items => $batch->{'num_items'},
179 upload_timestamp => $batch->{'upload_timestamp'},
180 import_status => $batch->{'import_status'},
181 file_name => $batch->{'file_name'},
182 comments => $batch->{'comments'}
185 $template->param(batch_list => \@list);
186 my $num_batches = GetNumberOfNonZ3950ImportBatches();
187 add_page_numbers($template, $offset, $results_per_page, $num_batches);
188 $template->param(offset => $offset);
189 $template->param(range_top => $offset + $results_per_page - 1);
190 $template->param(num_results => $num_batches);
191 $template->param(results_per_page => $results_per_page);
195 sub commit_batch {
196 my ($template, $import_batch_id) = @_;
198 my $job = undef;
199 $dbh->{AutoCommit} = 0;
200 my $callback = sub {};
201 if ($runinbackground) {
202 $job = put_in_background($import_batch_id);
203 $callback = progress_callback($job, $dbh);
205 my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) =
206 BatchCommitBibRecords($import_batch_id, 50, $callback);
207 $dbh->commit();
209 my $results = {
210 did_commit => 1,
211 num_added => $num_added,
212 num_updated => $num_updated,
213 num_items_added => $num_items_added,
214 num_items_errored => $num_items_errored,
215 num_ignored => $num_ignored
217 if ($runinbackground) {
218 $job->finish($results);
219 } else {
220 add_results_to_template($template, $results);
224 sub revert_batch {
225 my ($template, $import_batch_id) = @_;
227 $dbh->{AutoCommit} = 0;
228 my $job = undef;
229 my $callback = sub {};
230 if ($runinbackground) {
231 $job = put_in_background($import_batch_id);
232 $callback = progress_callback($job, $dbh);
234 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) =
235 BatchRevertBibRecords($import_batch_id, 50, $callback);
236 $dbh->commit();
238 my $results = {
239 did_revert => 1,
240 num_deleted => $num_deleted,
241 num_items_deleted => $num_items_deleted,
242 num_errors => $num_errors,
243 num_reverted => $num_reverted,
244 num_ignored => $num_ignored,
246 if ($runinbackground) {
247 $job->finish($results);
248 } else {
249 add_results_to_template($template, $results);
253 sub put_in_background {
254 my $import_batch_id = shift;
256 my $batch = GetImportBatch($import_batch_id);
257 my $job = C4::BackgroundJob->new($sessionID, $batch->{'file_name'}, $ENV{'SCRIPT_NAME'}, $batch->{'num_biblios'});
258 my $jobID = $job->id();
260 # fork off
261 if (my $pid = fork) {
262 # parent
263 # return job ID as JSON
265 # prevent parent exiting from
266 # destroying the kid's database handle
267 # FIXME: according to DBI doc, this may not work for Oracle
268 $dbh->{InactiveDestroy} = 1;
270 my $reply = CGI->new("");
271 print $reply->header(-type => 'text/html');
272 print "{ jobID: '$jobID' }";
273 exit 0;
274 } elsif (defined $pid) {
275 # child
276 # close STDOUT to signal to Apache that
277 # we're now running in the background
278 close STDOUT;
279 close STDERR;
280 } else {
281 # fork failed, so exit immediately
282 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
283 exit 0;
285 return $job;
288 sub progress_callback {
289 my $job = shift;
290 my $dbh = shift;
291 return sub {
292 my $progress = shift;
293 $job->progress($progress);
294 $dbh->commit();
298 sub add_results_to_template {
299 my $template = shift;
300 my $results = shift;
301 $template->param(map { $_ => $results->{$_} } keys %{ $results });
304 sub add_saved_job_results_to_template {
305 my $template = shift;
306 my $completedJobID = shift;
307 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
308 my $results = $job->results();
309 add_results_to_template($template, $results);
312 sub import_biblios_list {
313 my ($template, $import_batch_id, $offset, $results_per_page) = @_;
315 my $batch = GetImportBatch($import_batch_id);
316 my $biblios = GetImportBibliosRange($import_batch_id, $offset, $results_per_page);
317 my @list = ();
318 foreach my $biblio (@$biblios) {
319 my $citation = $biblio->{'title'};
320 $citation .= " $biblio->{'author'}" if $biblio->{'author'};
321 $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
322 $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
323 $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
324 $citation .= $biblio->{'issn'} if $biblio->{'issn'};
325 $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
326 my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
327 push @list, {
328 import_record_id => $biblio->{'import_record_id'},
329 citation => $citation,
330 status => $biblio->{'status'},
331 record_sequence => $biblio->{'record_sequence'},
332 overlay_status => $biblio->{'overlay_status'},
333 match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
334 match_citation => $#$match > -1 ? $match->[0]->{'title'} . ' ' . $match->[0]->{'author'} : '',
335 match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
338 my $num_biblios = $batch->{'num_biblios'};
339 $template->param(biblio_list => \@list);
340 add_page_numbers($template, $offset, $results_per_page, $num_biblios);
341 $template->param(offset => $offset);
342 $template->param(range_top => $offset + $results_per_page - 1);
343 $template->param(num_results => $num_biblios);
344 $template->param(results_per_page => $results_per_page);
345 $template->param(import_batch_id => $import_batch_id);
346 my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
347 $template->param("overlay_action_${overlay_action}" => 1);
348 $template->param(overlay_action => $overlay_action);
349 my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
350 $template->param("nomatch_action_${nomatch_action}" => 1);
351 $template->param(nomatch_action => $nomatch_action);
352 my $item_action = GetImportBatchItemAction($import_batch_id);
353 $template->param("item_action_${item_action}" => 1);
354 $template->param(item_action => $item_action);
355 batch_info($template, $batch);
359 sub batch_info {
360 my ($template, $batch) = @_;
361 $template->param(batch_info => 1);
362 $template->param(file_name => $batch->{'file_name'});
363 $template->param(comments => $batch->{'comments'});
364 $template->param(import_status => $batch->{'import_status'});
365 $template->param(upload_timestamp => $batch->{'upload_timestamp'});
366 $template->param(num_biblios => $batch->{'num_biblios'});
367 $template->param(num_items => $batch->{'num_biblios'});
368 if ($batch->{'num_biblios'} > 0) {
369 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
370 $template->param(can_commit => 1);
372 if ($batch->{'import_status'} eq 'imported') {
373 $template->param(can_revert => 1);
376 if (defined $batch->{'matcher_id'}) {
377 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
378 if (defined $matcher) {
379 $template->param('current_matcher_id' => $batch->{'matcher_id'});
380 $template->param('current_matcher_code' => $matcher->code());
381 $template->param('current_matcher_description' => $matcher->description());
384 add_matcher_list($batch->{'matcher_id'});
387 sub add_matcher_list {
388 my $current_matcher_id = shift;
389 my @matchers = C4::Matcher::GetMatcherList();
390 if (defined $current_matcher_id) {
391 for (my $i = 0; $i <= $#matchers; $i++) {
392 if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
393 $matchers[$i]->{'selected'} = 1;
397 $template->param(available_matchers => \@matchers);
400 sub add_page_numbers {
401 my ($template, $offset, $results_per_page, $total_results) = @_;
402 my $max_pages = POSIX::ceil($total_results / $results_per_page);
403 return if $max_pages < 2;
404 my $current_page = int($offset / $results_per_page) + 1;
405 my @pages = ();
406 for (my $i = 1; $i <= $max_pages; $i++) {
407 push @pages, {
408 page_number => $i,
409 current_page => ($current_page == $i) ? 1 : 0,
410 offset => ($i - 1) * $results_per_page
413 $template->param(pages => \@pages);