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
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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 # standard or CPAN modules used
26 use MARC
::File
::USMARC
;
32 use C4
::AuthoritiesMarc
;
37 use C4
::BackgroundJob
;
38 use C4
::Labels
::Batch
;
40 my $script_name = "/cgi-bin/koha/tools/manage-marc-import.pl";
43 my $op = $input->param('op') || '';
44 my $completedJobID = $input->param('completedJobID');
45 our $runinbackground = $input->param('runinbackground');
46 my $import_batch_id = $input->param('import_batch_id') || '';
48 # record list displays
49 my $offset = $input->param('offset') || 0;
50 my $results_per_page = $input->param('results_per_page') || 25;
52 my ($template, $loggedinuser, $cookie)
53 = get_template_and_user
({template_name
=> "tools/manage-marc-import.tmpl",
57 flagsrequired
=> {tools
=> 'manage_staged_marc'},
61 my %cookies = parse CGI
::Cookie
($cookie);
62 our $sessionID = $cookies{'CGISESSID'}->value;
63 our $dbh = C4
::Context
->dbh;
65 # Frameworks selection loop
67 my $frameworks = getframeworks
;
69 while ( my ($key, $value) = each %$frameworks ) {
70 push @
$arrayref, { value
=> $key, label
=> $value->{frameworktext
} };
72 $template->param( frameworks
=> $arrayref );
75 if ($op eq "create_labels") {
76 #create a batch of labels, then lose $op & $import_batch_id so we get back to import batch list.
77 my $label_batch_id = create_labelbatch_from_importbatch
($import_batch_id);
78 if ($label_batch_id == -1) {
79 $template->param( label_batch_msg
=> "Error attempting to create label batch. Please ask your system administrator to check the log for more details.",
80 message_type
=> 'alert',
84 $template->param( label_batch_msg
=> "Label batch #$label_batch_id created.",
85 message_type
=> 'dialog',
92 $template->param(script_name
=> $script_name, $op => 1);
94 $template->param(script_name
=> $script_name);
99 if ($import_batch_id eq '') {
100 import_batches_list
($template, $offset, $results_per_page);
102 import_records_list
($template, $import_batch_id, $offset, $results_per_page);
104 } elsif ($op eq "commit-batch") {
105 if ($completedJobID) {
106 add_saved_job_results_to_template
($template, $completedJobID);
108 my $framework = $input->param('framework');
109 commit_batch
($template, $import_batch_id, $framework);
111 import_records_list
($template, $import_batch_id, $offset, $results_per_page);
112 } elsif ($op eq "revert-batch") {
113 if ($completedJobID) {
114 add_saved_job_results_to_template
($template, $completedJobID);
116 revert_batch
($template, $import_batch_id);
118 import_records_list
($template, $import_batch_id, $offset, $results_per_page);
119 } elsif ($op eq "clean-batch") {
120 CleanBatch
($import_batch_id);
121 import_batches_list
($template, $offset, $results_per_page);
124 import_batch_id
=> $import_batch_id,
126 } elsif ($op eq "redo-matching") {
127 my $new_matcher_id = $input->param('new_matcher_id');
128 my $current_matcher_id = $input->param('current_matcher_id');
129 my $overlay_action = $input->param('overlay_action');
130 my $nomatch_action = $input->param('nomatch_action');
131 my $item_action = $input->param('item_action');
132 redo_matching
($template, $import_batch_id, $new_matcher_id, $current_matcher_id,
133 $overlay_action, $nomatch_action, $item_action);
134 import_records_list
($template, $import_batch_id, $offset, $results_per_page);
137 output_html_with_http_headers
$input, $cookie, $template->output;
142 my ($template, $import_batch_id, $new_matcher_id, $current_matcher_id, $overlay_action, $nomatch_action, $item_action) = @_;
143 my $rematch_failed = 0;
144 return if not defined $new_matcher_id and not defined $current_matcher_id;
145 my $old_overlay_action = GetImportBatchOverlayAction
($import_batch_id);
146 my $old_nomatch_action = GetImportBatchNoMatchAction
($import_batch_id);
147 my $old_item_action = GetImportBatchItemAction
($import_batch_id);
148 return if $new_matcher_id eq $current_matcher_id and
149 $old_overlay_action eq $overlay_action and
150 $old_nomatch_action eq $nomatch_action and
151 $old_item_action eq $item_action;
153 if ($old_overlay_action ne $overlay_action) {
154 SetImportBatchOverlayAction
($import_batch_id, $overlay_action);
155 $template->param('changed_overlay_action' => 1);
157 if ($old_nomatch_action ne $nomatch_action) {
158 SetImportBatchNoMatchAction
($import_batch_id, $nomatch_action);
159 $template->param('changed_nomatch_action' => 1);
161 if ($old_item_action ne $item_action) {
162 SetImportBatchItemAction
($import_batch_id, $item_action);
163 $template->param('changed_item_action' => 1);
166 my $num_with_matches = 0;
167 if (defined $new_matcher_id and $new_matcher_id ne "") {
168 my $matcher = C4
::Matcher
->fetch($new_matcher_id);
169 if (defined $matcher) {
170 $num_with_matches = BatchFindDuplicates
($import_batch_id, $matcher);
171 SetImportBatchMatcher
($import_batch_id, $new_matcher_id);
176 $num_with_matches = BatchFindDuplicates
($import_batch_id, undef);
177 SetImportBatchMatcher
($import_batch_id, undef);
178 SetImportBatchOverlayAction
('create_new');
180 $template->param(rematch_failed
=> $rematch_failed);
181 $template->param(rematch_attempted
=> 1);
182 $template->param(num_with_matches
=> $num_with_matches);
185 sub create_labelbatch_from_importbatch
{
188 my $branch_code = C4
::Context
->userenv->{'branch'};
189 my $batch = C4
::Labels
::Batch
->new(branch_code
=> $branch_code);
190 my @items = GetItemNumbersFromImportBatch
($batch_id);
191 if (grep{$_ == 0} @items) {
192 warn sprintf('create_labelbatch_from_importbatch() : Call to C4::ImportBatch::GetItemNumbersFromImportBatch returned no item number(s) from import batch #%s.', $batch_id);
195 foreach my $item_number (@items) {
196 $err = $batch->add_item($item_number);
198 warn sprintf('create_labelbatch_from_importbatch() : Error attempting to add item #%s of import batch #%s to label batch.', $item_number, $batch_id);
202 return $batch->get_attr('batch_id');
205 sub import_batches_list
{
206 my ($template, $offset, $results_per_page) = @_;
207 my $batches = GetImportBatchRangeDesc
($offset, $results_per_page);
210 foreach my $batch (@
$batches) {
212 import_batch_id
=> $batch->{'import_batch_id'},
213 num_records
=> $batch->{'num_records'},
214 num_items
=> $batch->{'num_items'},
215 upload_timestamp
=> $batch->{'upload_timestamp'},
216 import_status
=> $batch->{'import_status'},
217 file_name
=> $batch->{'file_name'} || "($batch->{'batch_type'})",
218 comments
=> $batch->{'comments'},
219 can_clean
=> ($batch->{'import_status'} ne 'cleaned') ?
1 : 0,
220 record_type
=> $batch->{'record_type'},
223 $template->param(batch_list
=> \
@list);
224 my $num_batches = GetNumberOfNonZ3950ImportBatches
();
225 add_page_numbers
($template, $offset, $results_per_page, $num_batches);
226 $template->param(offset
=> $offset);
227 $template->param(range_top
=> $offset + $results_per_page - 1);
228 $template->param(num_results
=> $num_batches);
229 $template->param(results_per_page
=> $results_per_page);
234 my ($template, $import_batch_id, $framework) = @_;
237 $dbh->{AutoCommit
} = 0;
238 my $callback = sub {};
239 if ($runinbackground) {
240 $job = put_in_background
($import_batch_id);
241 $callback = progress_callback
($job, $dbh);
243 my ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored) =
244 BatchCommitRecords
($import_batch_id, $framework, 50, $callback);
249 num_added
=> $num_added,
250 num_updated
=> $num_updated,
251 num_items_added
=> $num_items_added,
252 num_items_replaced
=> $num_items_replaced,
253 num_items_errored
=> $num_items_errored,
254 num_ignored
=> $num_ignored
256 if ($runinbackground) {
257 $job->finish($results);
259 add_results_to_template
($template, $results);
264 my ($template, $import_batch_id) = @_;
266 $dbh->{AutoCommit
} = 0;
268 my $callback = sub {};
269 if ($runinbackground) {
270 $job = put_in_background
($import_batch_id);
271 $callback = progress_callback
($job, $dbh);
273 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) =
274 BatchRevertRecords
($import_batch_id, 50, $callback);
279 num_deleted
=> $num_deleted,
280 num_items_deleted
=> $num_items_deleted,
281 num_errors
=> $num_errors,
282 num_reverted
=> $num_reverted,
283 num_ignored
=> $num_ignored,
285 if ($runinbackground) {
286 $job->finish($results);
288 add_results_to_template
($template, $results);
292 sub put_in_background
{
293 my $import_batch_id = shift;
295 my $batch = GetImportBatch
($import_batch_id);
296 my $job = C4
::BackgroundJob
->new($sessionID, $batch->{'file_name'}, $ENV{'SCRIPT_NAME'}, $batch->{'num_records'});
297 my $jobID = $job->id();
300 if (my $pid = fork) {
302 # return job ID as JSON
304 # prevent parent exiting from
305 # destroying the kid's database handle
306 # FIXME: according to DBI doc, this may not work for Oracle
307 $dbh->{InactiveDestroy
} = 1;
309 my $reply = CGI
->new("");
310 print $reply->header(-type
=> 'text/html');
311 print '{"jobID":"' . $jobID . '"}';
313 } elsif (defined $pid) {
315 # close STDOUT to signal to Apache that
316 # we're now running in the background
320 # fork failed, so exit immediately
321 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
327 sub progress_callback
{
331 my $progress = shift;
332 $job->progress($progress);
337 sub add_results_to_template
{
338 my $template = shift;
340 $template->param(map { $_ => $results->{$_} } keys %{ $results });
343 sub add_saved_job_results_to_template
{
344 my $template = shift;
345 my $completedJobID = shift;
346 my $job = C4
::BackgroundJob
->fetch($sessionID, $completedJobID);
347 my $results = $job->results();
348 add_results_to_template
($template, $results);
351 sub import_records_list
{
352 my ($template, $import_batch_id, $offset, $results_per_page) = @_;
354 my $batch = GetImportBatch
($import_batch_id);
355 my $records = GetImportRecordsRange
($import_batch_id, $offset, $results_per_page);
357 foreach my $record (@
$records) {
358 my $citation = $record->{'title'} || $record->{'authorized_heading'};
359 $citation .= " $record->{'author'}" if $record->{'author'};
360 $citation .= " (" if $record->{'issn'} or $record->{'isbn'};
361 $citation .= $record->{'isbn'} if $record->{'isbn'};
362 $citation .= ", " if $record->{'issn'} and $record->{'isbn'};
363 $citation .= $record->{'issn'} if $record->{'issn'};
364 $citation .= ")" if $record->{'issn'} or $record->{'isbn'};
366 my $match = GetImportRecordMatches
($record->{'import_record_id'}, 1);
367 my $match_citation = '';
370 if ($match->[0]->{'record_type'} eq 'biblio') {
371 $match_citation .= $match->[0]->{'title'} if defined($match->[0]->{'title'});
372 $match_citation .= ' ' . $match->[0]->{'author'} if defined($match->[0]->{'author'});
373 $match_id = $match->[0]->{'biblionumber'};
374 } elsif ($match->[0]->{'record_type'} eq 'auth') {
375 if (defined($match->[0]->{'authorized_heading'})) {
376 $match_citation .= $match->[0]->{'authorized_heading'};
377 $match_id = $match->[0]->{'candidate_match_id'};
383 { import_record_id
=> $record->{'import_record_id'},
384 final_match_id
=> $record->{'matched_biblionumber'} || $record->{'matched_authid'},
385 citation
=> $citation,
386 status
=> $record->{'status'},
387 record_sequence
=> $record->{'record_sequence'},
388 overlay_status
=> $record->{'overlay_status'},
389 # Sorry about the match_id being from the "biblionumber" field;
390 # as it turns out, any match id will go in biblionumber
391 match_id
=> $match_id,
392 match_citation
=> $match_citation,
393 match_score
=> $#$match > -1 ? $match->[0]->{'score'} : 0,
394 record_type
=> $record->{'record_type'},
397 my $num_records = $batch->{'num_records'};
398 $template->param(record_list
=> \
@list);
399 add_page_numbers
($template, $offset, $results_per_page, $num_records);
400 $template->param(offset
=> $offset);
401 $template->param(range_top
=> $offset + $results_per_page - 1);
402 $template->param(num_results
=> $num_records);
403 $template->param(results_per_page
=> $results_per_page);
404 $template->param(import_batch_id
=> $import_batch_id);
405 my $overlay_action = GetImportBatchOverlayAction
($import_batch_id);
406 $template->param("overlay_action_${overlay_action}" => 1);
407 $template->param(overlay_action
=> $overlay_action);
408 my $nomatch_action = GetImportBatchNoMatchAction
($import_batch_id);
409 $template->param("nomatch_action_${nomatch_action}" => 1);
410 $template->param(nomatch_action
=> $nomatch_action);
411 my $item_action = GetImportBatchItemAction
($import_batch_id);
412 $template->param("item_action_${item_action}" => 1);
413 $template->param(item_action
=> $item_action);
414 batch_info
($template, $batch);
419 my ($template, $batch) = @_;
420 $template->param(batch_info
=> 1);
421 $template->param(file_name
=> $batch->{'file_name'});
422 $template->param(comments
=> $batch->{'comments'});
423 $template->param(import_status
=> $batch->{'import_status'});
424 $template->param(upload_timestamp
=> $batch->{'upload_timestamp'});
425 $template->{VARS
}->{'record_type'} = $batch->{'record_type'};
426 $template->param(num_records
=> $batch->{'num_records'});
427 $template->param(num_items
=> $batch->{'num_items'});
428 if ($batch->{'import_status'} ne 'cleaned') {
429 $template->param(can_clean
=> 1);
431 if ($batch->{'num_records'} > 0) {
432 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
433 $template->param(can_commit
=> 1);
435 if ($batch->{'import_status'} eq 'imported') {
436 $template->param(can_revert
=> 1);
439 if (defined $batch->{'matcher_id'}) {
440 my $matcher = C4
::Matcher
->fetch($batch->{'matcher_id'});
441 if (defined $matcher) {
442 $template->param('current_matcher_id' => $batch->{'matcher_id'});
443 $template->param('current_matcher_code' => $matcher->code());
444 $template->param('current_matcher_description' => $matcher->description());
447 add_matcher_list
($template,$batch->{'matcher_id'});
450 sub add_matcher_list
{
451 my ($template,$current_matcher_id) = @_;
452 my @matchers = C4
::Matcher
::GetMatcherList
();
453 if (defined $current_matcher_id) {
454 for (my $i = 0; $i <= $#matchers; $i++) {
455 if ($matchers[$i]->{'matcher_id'} eq $current_matcher_id) {
456 $matchers[$i]->{'selected'} = 1;
460 $template->param(available_matchers
=> \
@matchers);
463 sub add_page_numbers
{
464 my ($template, $offset, $results_per_page, $total_results) = @_;
465 my $max_pages = POSIX
::ceil
($total_results / $results_per_page);
466 return if $max_pages < 2;
467 my $current_page = int($offset / $results_per_page) + 1;
469 for (my $i = 1; $i <= $max_pages; $i++) {
472 current_page
=> ($current_page == $i) ?
1 : 0,
473 offset
=> ($i - 1) * $results_per_page
476 $template->param(pages
=> \
@pages);