Lines 1-6
Link Here
|
1 |
package C4::ImportBatch; |
1 |
package C4::ImportBatch; |
2 |
|
2 |
|
3 |
# Copyright (C) 2007 LibLime |
3 |
# Copyright (C) 2007 LibLime, 2012 C & P Bibliography Services |
4 |
# |
4 |
# |
5 |
# This file is part of Koha. |
5 |
# This file is part of Koha. |
6 |
# |
6 |
# |
Lines 40-53
BEGIN {
Link Here
|
40 |
GetImportRecordMarcXML |
40 |
GetImportRecordMarcXML |
41 |
AddImportBatch |
41 |
AddImportBatch |
42 |
GetImportBatch |
42 |
GetImportBatch |
|
|
43 |
AddAuthorityToBatch |
43 |
AddBiblioToBatch |
44 |
AddBiblioToBatch |
44 |
AddItemsToImportBiblio |
45 |
AddItemsToImportBiblio |
|
|
46 |
ModAuthorityInBatch |
45 |
ModBiblioInBatch |
47 |
ModBiblioInBatch |
46 |
|
48 |
|
47 |
BatchStageMarcRecords |
49 |
BatchStageMarcRecords |
48 |
BatchFindBibDuplicates |
50 |
BatchFindDuplicates |
49 |
BatchCommitBibRecords |
51 |
BatchCommitRecords |
50 |
BatchRevertBibRecords |
52 |
BatchRevertRecords |
51 |
CleanBatch |
53 |
CleanBatch |
52 |
|
54 |
|
53 |
GetAllImportBatches |
55 |
GetAllImportBatches |
Lines 272-281
sub ModBiblioInBatch {
Link Here
|
272 |
|
274 |
|
273 |
} |
275 |
} |
274 |
|
276 |
|
|
|
277 |
=head2 AddAuthToBatch |
278 |
|
279 |
my $import_record_id = AddAuthToBatch($batch_id, $record_sequence, |
280 |
$marc_record, $encoding, $z3950random, $update_counts); |
281 |
|
282 |
=cut |
283 |
|
284 |
sub AddAuthToBatch { |
285 |
my $batch_id = shift; |
286 |
my $record_sequence = shift; |
287 |
my $marc_record = shift; |
288 |
my $encoding = shift; |
289 |
my $z3950random = shift; |
290 |
my $update_counts = @_ ? shift : 1; |
291 |
|
292 |
my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'auth', $encoding, $z3950random); |
293 |
_add_auth_fields($import_record_id, $marc_record); |
294 |
_update_batch_record_counts($batch_id) if $update_counts; |
295 |
return $import_record_id; |
296 |
} |
297 |
|
298 |
=head2 ModAuthInBatch |
299 |
|
300 |
ModAuthInBatch($import_record_id, $marc_record); |
301 |
|
302 |
=cut |
303 |
|
304 |
sub ModAuthInBatch { |
305 |
my ($import_record_id, $marc_record) = @_; |
306 |
|
307 |
_update_import_record_marc($import_record_id, $marc_record); |
308 |
|
309 |
} |
310 |
|
275 |
=head2 BatchStageMarcRecords |
311 |
=head2 BatchStageMarcRecords |
276 |
|
312 |
|
277 |
($batch_id, $num_records, $num_items, @invalid_records) = |
313 |
($batch_id, $num_records, $num_items, @invalid_records) = |
278 |
BatchStageMarcRecords($encoding, $marc_records, $file_name, |
314 |
BatchStageMarcRecords($record_type, $encoding, $marc_records, $file_name, |
279 |
$comments, $branch_code, $parse_items, |
315 |
$comments, $branch_code, $parse_items, |
280 |
$leave_as_staging, |
316 |
$leave_as_staging, |
281 |
$progress_interval, $progress_callback); |
317 |
$progress_interval, $progress_callback); |
Lines 283-288
sub ModBiblioInBatch {
Link Here
|
283 |
=cut |
319 |
=cut |
284 |
|
320 |
|
285 |
sub BatchStageMarcRecords { |
321 |
sub BatchStageMarcRecords { |
|
|
322 |
my $record_type = shift; |
286 |
my $encoding = shift; |
323 |
my $encoding = shift; |
287 |
my $marc_records = shift; |
324 |
my $marc_records = shift; |
288 |
my $file_name = shift; |
325 |
my $file_name = shift; |
Lines 338-347
sub BatchStageMarcRecords {
Link Here
|
338 |
push @invalid_records, $marc_blob; |
375 |
push @invalid_records, $marc_blob; |
339 |
} else { |
376 |
} else { |
340 |
$num_valid++; |
377 |
$num_valid++; |
341 |
$import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0); |
378 |
if ($record_type eq 'biblio') { |
342 |
if ($parse_items) { |
379 |
$import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0); |
343 |
my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0); |
380 |
if ($parse_items) { |
344 |
$num_items += scalar(@import_items_ids); |
381 |
my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0); |
|
|
382 |
$num_items += scalar(@import_items_ids); |
383 |
} |
384 |
} elsif ($record_type eq 'auth') { |
385 |
$import_record_id = AddAuthToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0); |
345 |
} |
386 |
} |
346 |
} |
387 |
} |
347 |
} |
388 |
} |
Lines 392-400
sub AddItemsToImportBiblio {
Link Here
|
392 |
return @import_items_ids; |
433 |
return @import_items_ids; |
393 |
} |
434 |
} |
394 |
|
435 |
|
395 |
=head2 BatchFindBibDuplicates |
436 |
=head2 BatchFindDuplicates |
396 |
|
437 |
|
397 |
my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, |
438 |
my $num_with_matches = BatchFindDuplicates($batch_id, $matcher, |
398 |
$max_matches, $progress_interval, $progress_callback); |
439 |
$max_matches, $progress_interval, $progress_callback); |
399 |
|
440 |
|
400 |
Goes through the records loaded in the batch and attempts to |
441 |
Goes through the records loaded in the batch and attempts to |
Lines 412-418
singular argument.
Link Here
|
412 |
|
453 |
|
413 |
=cut |
454 |
=cut |
414 |
|
455 |
|
415 |
sub BatchFindBibDuplicates { |
456 |
sub BatchFindDuplicates { |
416 |
my $batch_id = shift; |
457 |
my $batch_id = shift; |
417 |
my $matcher = shift; |
458 |
my $matcher = shift; |
418 |
my $max_matches = @_ ? shift : 10; |
459 |
my $max_matches = @_ ? shift : 10; |
Lines 430-438
sub BatchFindBibDuplicates {
Link Here
|
430 |
|
471 |
|
431 |
my $dbh = C4::Context->dbh; |
472 |
my $dbh = C4::Context->dbh; |
432 |
|
473 |
|
433 |
my $sth = $dbh->prepare("SELECT import_record_id, marc |
474 |
my $sth = $dbh->prepare("SELECT import_records.import_record_id, record_type, marc |
434 |
FROM import_records |
475 |
FROM import_records |
435 |
JOIN import_biblios USING (import_record_id) |
476 |
LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id) |
|
|
477 |
LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id) |
436 |
WHERE import_batch_id = ?"); |
478 |
WHERE import_batch_id = ?"); |
437 |
$sth->execute($batch_id); |
479 |
$sth->execute($batch_id); |
438 |
my $num_with_matches = 0; |
480 |
my $num_with_matches = 0; |
Lines 460-474
sub BatchFindBibDuplicates {
Link Here
|
460 |
return $num_with_matches; |
502 |
return $num_with_matches; |
461 |
} |
503 |
} |
462 |
|
504 |
|
463 |
=head2 BatchCommitBibRecords |
505 |
=head2 BatchCommitRecords |
464 |
|
506 |
|
465 |
my ($num_added, $num_updated, $num_items_added, $num_items_errored, |
507 |
my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) = |
466 |
$num_ignored) = BatchCommitBibRecords($batch_id, $framework, |
508 |
BatchCommitRecords($batch_id, $framework, |
467 |
$progress_interval, $progress_callback); |
509 |
$progress_interval, $progress_callback); |
468 |
|
510 |
|
469 |
=cut |
511 |
=cut |
470 |
|
512 |
|
471 |
sub BatchCommitBibRecords { |
513 |
sub BatchCommitRecords { |
472 |
my $batch_id = shift; |
514 |
my $batch_id = shift; |
473 |
my $framework = shift; |
515 |
my $framework = shift; |
474 |
|
516 |
|
Lines 483-507
sub BatchCommitBibRecords {
Link Here
|
483 |
$progress_interval = 0 unless 'CODE' eq ref $progress_callback; |
525 |
$progress_interval = 0 unless 'CODE' eq ref $progress_callback; |
484 |
} |
526 |
} |
485 |
|
527 |
|
|
|
528 |
my $record_type; |
486 |
my $num_added = 0; |
529 |
my $num_added = 0; |
487 |
my $num_updated = 0; |
530 |
my $num_updated = 0; |
488 |
my $num_items_added = 0; |
531 |
my $num_items_added = 0; |
489 |
my $num_items_errored = 0; |
532 |
my $num_items_errored = 0; |
490 |
my $num_ignored = 0; |
533 |
my $num_ignored = 0; |
491 |
# commit (i.e., save, all records in the batch) |
534 |
# commit (i.e., save, all records in the batch) |
492 |
# FIXME biblio only at the moment |
|
|
493 |
SetImportBatchStatus('importing'); |
535 |
SetImportBatchStatus('importing'); |
494 |
my $overlay_action = GetImportBatchOverlayAction($batch_id); |
536 |
my $overlay_action = GetImportBatchOverlayAction($batch_id); |
495 |
my $nomatch_action = GetImportBatchNoMatchAction($batch_id); |
537 |
my $nomatch_action = GetImportBatchNoMatchAction($batch_id); |
496 |
my $item_action = GetImportBatchItemAction($batch_id); |
538 |
my $item_action = GetImportBatchItemAction($batch_id); |
|
|
539 |
my $item_tag; |
540 |
my $item_subfield; |
497 |
my $dbh = C4::Context->dbh; |
541 |
my $dbh = C4::Context->dbh; |
498 |
my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marc, encoding |
542 |
my $sth = $dbh->prepare("SELECT import_records.import_record_id, record_type, status, overlay_status, marc, encoding |
499 |
FROM import_records |
543 |
FROM import_records |
500 |
JOIN import_biblios USING (import_record_id) |
544 |
LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id) |
|
|
545 |
LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id) |
501 |
WHERE import_batch_id = ?"); |
546 |
WHERE import_batch_id = ?"); |
502 |
$sth->execute($batch_id); |
547 |
$sth->execute($batch_id); |
503 |
my $rec_num = 0; |
548 |
my $rec_num = 0; |
504 |
while (my $rowref = $sth->fetchrow_hashref) { |
549 |
while (my $rowref = $sth->fetchrow_hashref) { |
|
|
550 |
$record_type = $rowref->{'record_type'}; |
505 |
$rec_num++; |
551 |
$rec_num++; |
506 |
if ($progress_interval and (0 == ($rec_num % $progress_interval))) { |
552 |
if ($progress_interval and (0 == ($rec_num % $progress_interval))) { |
507 |
&$progress_callback($rec_num); |
553 |
&$progress_callback($rec_num); |
Lines 513-579
sub BatchCommitBibRecords {
Link Here
|
513 |
|
559 |
|
514 |
my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'}); |
560 |
my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'}); |
515 |
|
561 |
|
516 |
# remove any item tags - rely on BatchCommitItems |
562 |
if ($record_type eq 'biblio') { |
517 |
my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",''); |
563 |
# remove any item tags - rely on BatchCommitItems |
518 |
foreach my $item_field ($marc_record->field($item_tag)) { |
564 |
($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",''); |
519 |
$marc_record->delete_field($item_field); |
565 |
foreach my $item_field ($marc_record->field($item_tag)) { |
|
|
566 |
$marc_record->delete_field($item_field); |
567 |
} |
520 |
} |
568 |
} |
521 |
|
569 |
|
522 |
# decide what what to do with the bib and item records |
570 |
my ($record_result, $item_result, $record_match) = |
523 |
my ($bib_result, $item_result, $bib_match) = |
|
|
524 |
_get_commit_action($overlay_action, $nomatch_action, $item_action, |
571 |
_get_commit_action($overlay_action, $nomatch_action, $item_action, |
525 |
$rowref->{'overlay_status'}, $rowref->{'import_record_id'}); |
572 |
$rowref->{'overlay_status'}, $rowref->{'import_record_id'}, $record_type); |
526 |
|
573 |
|
527 |
if ($bib_result eq 'create_new') { |
574 |
my $recordid; |
|
|
575 |
my $query; |
576 |
if ($record_result eq 'create_new') { |
528 |
$num_added++; |
577 |
$num_added++; |
529 |
my ($biblionumber, $biblioitemnumber) = AddBiblio($marc_record, $framework); |
578 |
if ($record_type eq 'biblio') { |
530 |
my $sth = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"); |
579 |
my $biblioitemnumber; |
531 |
$sth->execute($biblionumber, $rowref->{'import_record_id'}); |
580 |
($recordid, $biblioitemnumber) = AddBiblio($marc_record, $framework); |
532 |
$sth->finish(); |
581 |
$query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; |
533 |
if ($item_result eq 'create_new') { |
582 |
if ($item_result eq 'create_new') { |
534 |
my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber); |
583 |
my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid); |
535 |
$num_items_added += $bib_items_added; |
584 |
$num_items_added += $bib_items_added; |
536 |
$num_items_errored += $bib_items_errored; |
585 |
$num_items_errored += $bib_items_errored; |
|
|
586 |
} |
587 |
} else { |
588 |
my $authid = AddAuthority($marc_record, undef, GuessAuthTypeCode($marc_record)); |
589 |
$query = "UPDATE import_auths SET matched_authid = ? WHERE import_record_id = ?"; |
537 |
} |
590 |
} |
|
|
591 |
my $sth = $dbh->prepare_cached($query); |
592 |
$sth->execute($recordid, $rowref->{'import_record_id'}); |
593 |
$sth->finish(); |
538 |
SetImportRecordStatus($rowref->{'import_record_id'}, 'imported'); |
594 |
SetImportRecordStatus($rowref->{'import_record_id'}, 'imported'); |
539 |
} elsif ($bib_result eq 'replace') { |
595 |
} elsif ($record_result eq 'replace') { |
540 |
$num_updated++; |
596 |
$num_updated++; |
541 |
my $biblionumber = $bib_match; |
597 |
$recordid = $record_match; |
542 |
my ($count, $oldbiblio) = GetBiblio($biblionumber); |
598 |
my $oldxml; |
543 |
my $oldxml = GetXmlBiblio($biblionumber); |
599 |
if ($record_type eq 'biblio') { |
544 |
|
600 |
my ($count, $oldbiblio) = GetBiblio($recordid); |
545 |
# remove item fields so that they don't get |
601 |
$oldxml = GetXmlBiblio($recordid); |
546 |
# added again if record is reverted |
602 |
|
547 |
my $old_marc = MARC::Record->new_from_xml(StripNonXmlChars($oldxml), 'UTF-8', $rowref->{'encoding'}); |
603 |
# remove item fields so that they don't get |
548 |
foreach my $item_field ($old_marc->field($item_tag)) { |
604 |
# added again if record is reverted |
549 |
$old_marc->delete_field($item_field); |
605 |
my $old_marc = MARC::Record->new_from_xml(StripNonXmlChars($oldxml), 'UTF-8', $rowref->{'encoding'}); |
550 |
} |
606 |
foreach my $item_field ($old_marc->field($item_tag)) { |
|
|
607 |
$old_marc->delete_field($item_field); |
608 |
} |
609 |
$oldxml = $old_marc->as_xml(); |
610 |
|
611 |
ModBiblio($marc_record, $recordid, $oldbiblio->{'frameworkcode'}); |
612 |
$query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; |
613 |
|
614 |
if ($item_result eq 'create_new') { |
615 |
my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid); |
616 |
$num_items_added += $bib_items_added; |
617 |
$num_items_errored += $bib_items_errored; |
618 |
} |
619 |
} else { |
620 |
my $oldxml = GetAuthorityXML($recordid); |
551 |
|
621 |
|
552 |
ModBiblio($marc_record, $biblionumber, $oldbiblio->{'frameworkcode'}); |
622 |
ModAuthority($recordid, $marc_record, GuessAuthTypeCode($marc_record)); |
|
|
623 |
$query = "UPDATE import_auths SET matched_authid = ? WHERE import_record_id = ?"; |
624 |
} |
553 |
my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?"); |
625 |
my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?"); |
554 |
$sth->execute($old_marc->as_xml(), $rowref->{'import_record_id'}); |
626 |
$sth->execute($oldxml, $rowref->{'import_record_id'}); |
555 |
$sth->finish(); |
627 |
$sth->finish(); |
556 |
my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"); |
628 |
my $sth2 = $dbh->prepare_cached($query); |
557 |
$sth2->execute($biblionumber, $rowref->{'import_record_id'}); |
629 |
$sth2->execute($recordid, $rowref->{'import_record_id'}); |
558 |
$sth2->finish(); |
630 |
$sth2->finish(); |
559 |
if ($item_result eq 'create_new') { |
|
|
560 |
my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber); |
561 |
$num_items_added += $bib_items_added; |
562 |
$num_items_errored += $bib_items_errored; |
563 |
} |
564 |
SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied'); |
631 |
SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied'); |
565 |
SetImportRecordStatus($rowref->{'import_record_id'}, 'imported'); |
632 |
SetImportRecordStatus($rowref->{'import_record_id'}, 'imported'); |
566 |
} elsif ($bib_result eq 'ignore') { |
633 |
} elsif ($record_result eq 'ignore') { |
567 |
$num_ignored++; |
634 |
$num_ignored++; |
568 |
my $biblionumber = $bib_match; |
635 |
if ($record_type eq 'biblio' and defined $recordid and $item_result eq 'create_new') { |
569 |
if (defined $biblionumber and $item_result eq 'create_new') { |
636 |
my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid); |
570 |
my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber); |
|
|
571 |
$num_items_added += $bib_items_added; |
637 |
$num_items_added += $bib_items_added; |
572 |
$num_items_errored += $bib_items_errored; |
638 |
$num_items_errored += $bib_items_errored; |
573 |
# still need to record the matched biblionumber so that the |
639 |
# still need to record the matched biblionumber so that the |
574 |
# items can be reverted |
640 |
# items can be reverted |
575 |
my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"); |
641 |
my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"); |
576 |
$sth2->execute($biblionumber, $rowref->{'import_record_id'}); |
642 |
$sth2->execute($recordid, $rowref->{'import_record_id'}); |
577 |
SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied'); |
643 |
SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied'); |
578 |
} |
644 |
} |
579 |
SetImportRecordStatus($rowref->{'import_record_id'}, 'ignored'); |
645 |
SetImportRecordStatus($rowref->{'import_record_id'}, 'ignored'); |
Lines 632-694
sub BatchCommitItems {
Link Here
|
632 |
return ($num_items_added, $num_items_errored); |
698 |
return ($num_items_added, $num_items_errored); |
633 |
} |
699 |
} |
634 |
|
700 |
|
635 |
=head2 BatchRevertBibRecords |
701 |
=head2 BatchRevertRecords |
636 |
|
702 |
|
637 |
my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, |
703 |
my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, |
638 |
$num_ignored) = BatchRevertBibRecords($batch_id); |
704 |
$num_ignored) = BatchRevertRecords($batch_id); |
639 |
|
705 |
|
640 |
=cut |
706 |
=cut |
641 |
|
707 |
|
642 |
sub BatchRevertBibRecords { |
708 |
sub BatchRevertRecords { |
643 |
my $batch_id = shift; |
709 |
my $batch_id = shift; |
644 |
|
710 |
|
|
|
711 |
my $record_type; |
645 |
my $num_deleted = 0; |
712 |
my $num_deleted = 0; |
646 |
my $num_errors = 0; |
713 |
my $num_errors = 0; |
647 |
my $num_reverted = 0; |
714 |
my $num_reverted = 0; |
648 |
my $num_items_deleted = 0; |
|
|
649 |
my $num_ignored = 0; |
715 |
my $num_ignored = 0; |
|
|
716 |
my $num_items_deleted = 0; |
650 |
# commit (i.e., save, all records in the batch) |
717 |
# commit (i.e., save, all records in the batch) |
651 |
# FIXME biblio only at the moment |
|
|
652 |
SetImportBatchStatus('reverting'); |
718 |
SetImportBatchStatus('reverting'); |
653 |
my $overlay_action = GetImportBatchOverlayAction($batch_id); |
719 |
my $overlay_action = GetImportBatchOverlayAction($batch_id); |
654 |
my $nomatch_action = GetImportBatchNoMatchAction($batch_id); |
720 |
my $nomatch_action = GetImportBatchNoMatchAction($batch_id); |
655 |
my $dbh = C4::Context->dbh; |
721 |
my $dbh = C4::Context->dbh; |
656 |
my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marcxml_old, encoding, matched_biblionumber |
722 |
my $sth = $dbh->prepare("SELECT import_records.import_record_id, record_type, status, overlay_status, marcxml_old, encoding, matched_biblionumber, matched_authid |
657 |
FROM import_records |
723 |
FROM import_records |
658 |
JOIN import_biblios USING (import_record_id) |
724 |
LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id) |
|
|
725 |
LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id) |
659 |
WHERE import_batch_id = ?"); |
726 |
WHERE import_batch_id = ?"); |
660 |
$sth->execute($batch_id); |
727 |
$sth->execute($batch_id); |
661 |
while (my $rowref = $sth->fetchrow_hashref) { |
728 |
while (my $rowref = $sth->fetchrow_hashref) { |
|
|
729 |
$record_type = $rowref->{'record_type'}; |
662 |
if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') { |
730 |
if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') { |
663 |
$num_ignored++; |
731 |
$num_ignored++; |
664 |
next; |
732 |
next; |
665 |
} |
733 |
} |
666 |
|
734 |
|
667 |
my $bib_result = _get_revert_action($overlay_action, $rowref->{'overlay_status'}, $rowref->{'status'}); |
735 |
my $record_result = _get_revert_action($overlay_action, $rowref->{'overlay_status'}, $rowref->{'status'}); |
668 |
|
736 |
|
669 |
if ($bib_result eq 'delete') { |
737 |
if ($record_result eq 'delete') { |
670 |
$num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'}); |
738 |
my $error = undef; |
671 |
my $error = DelBiblio($rowref->{'matched_biblionumber'}); |
739 |
if ($record_type eq 'biblio') { |
|
|
740 |
$num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'}); |
741 |
$error = DelBiblio($rowref->{'matched_biblionumber'}); |
742 |
} else { |
743 |
my $deletedauthid = DelAuthority($rowref->{'matched_authid'}); |
744 |
} |
672 |
if (defined $error) { |
745 |
if (defined $error) { |
673 |
$num_errors++; |
746 |
$num_errors++; |
674 |
} else { |
747 |
} else { |
675 |
$num_deleted++; |
748 |
$num_deleted++; |
676 |
SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted'); |
749 |
SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted'); |
677 |
} |
750 |
} |
678 |
} elsif ($bib_result eq 'restore') { |
751 |
} elsif ($record_result eq 'restore') { |
679 |
$num_reverted++; |
752 |
$num_reverted++; |
680 |
my $old_record = MARC::Record->new_from_xml(StripNonXmlChars($rowref->{'marcxml_old'}), 'UTF-8', $rowref->{'encoding'}); |
753 |
my $old_record = MARC::Record->new_from_xml(StripNonXmlChars($rowref->{'marcxml_old'}), 'UTF-8', $rowref->{'encoding'}); |
681 |
my $biblionumber = $rowref->{'matched_biblionumber'}; |
754 |
if ($record_type eq 'biblio') { |
682 |
my ($count, $oldbiblio) = GetBiblio($biblionumber); |
755 |
my $biblionumber = $rowref->{'matched_biblionumber'}; |
683 |
$num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'}); |
756 |
my ($count, $oldbiblio) = GetBiblio($biblionumber); |
684 |
ModBiblio($old_record, $biblionumber, $oldbiblio->{'frameworkcode'}); |
757 |
$num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'}); |
|
|
758 |
ModBiblio($old_record, $biblionumber, $oldbiblio->{'frameworkcode'}); |
759 |
} else { |
760 |
my $authid = $rowref->{'matched_authid'}; |
761 |
ModAuthority($authid, $old_record, GuessAuthTypeCode($old_record)); |
762 |
} |
685 |
SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted'); |
763 |
SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted'); |
686 |
} elsif ($bib_result eq 'ignore') { |
764 |
} elsif ($record_result eq 'ignore') { |
687 |
$num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'}); |
765 |
if ($record_type eq 'biblio') { |
|
|
766 |
$num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'}); |
767 |
} |
688 |
SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted'); |
768 |
SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted'); |
689 |
} |
769 |
} |
690 |
# remove matched_biblionumber only if there is no 'imported' item left |
770 |
my $query; |
691 |
my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = NULL WHERE import_record_id = ? AND NOT EXISTS (SELECT * FROM import_items WHERE import_items.import_record_id=import_biblios.import_record_id and status='imported')" ); |
771 |
if ($record_type eq 'biblio') { |
|
|
772 |
# remove matched_biblionumber only if there is no 'imported' item left |
773 |
$query = "UPDATE import_biblios SET matched_biblionumber = NULL WHERE import_record_id = ?"; |
774 |
$query = "UPDATE import_biblios SET matched_biblionumber = NULL WHERE import_record_id = ? AND NOT EXISTS (SELECT * FROM import_items WHERE import_items.import_record_id=import_biblios.import_record_id and status='imported')"; |
775 |
} else { |
776 |
$query = "UPDATE import_auths SET matched_authid = NULL WHERE import_record_id = ?"; |
777 |
} |
778 |
my $sth2 = $dbh->prepare_cached($query); |
692 |
$sth2->execute($rowref->{'import_record_id'}); |
779 |
$sth2->execute($rowref->{'import_record_id'}); |
693 |
} |
780 |
} |
694 |
|
781 |
|
Lines 1263-1268
sub _add_biblio_fields {
Link Here
|
1263 |
|
1350 |
|
1264 |
} |
1351 |
} |
1265 |
|
1352 |
|
|
|
1353 |
sub _add_auth_fields { |
1354 |
my ($import_record_id, $marc_record) = @_; |
1355 |
|
1356 |
my $dbh = C4::Context->dbh; |
1357 |
# FIXME no controlnumber |
1358 |
my $sth = $dbh->prepare("INSERT INTO import_auths (import_record_id) VALUES (?)"); |
1359 |
$sth->execute($import_record_id); |
1360 |
$sth->finish(); |
1361 |
|
1362 |
} |
1363 |
|
1364 |
|
1266 |
sub _update_biblio_fields { |
1365 |
sub _update_biblio_fields { |
1267 |
my ($import_record_id, $marc_record) = @_; |
1366 |
my ($import_record_id, $marc_record) = @_; |
1268 |
|
1367 |
|
Lines 1311-1336
sub _update_batch_record_counts {
Link Here
|
1311 |
} |
1410 |
} |
1312 |
|
1411 |
|
1313 |
sub _get_commit_action { |
1412 |
sub _get_commit_action { |
1314 |
my ($overlay_action, $nomatch_action, $item_action, $overlay_status, $import_record_id) = @_; |
1413 |
my ($overlay_action, $nomatch_action, $item_action, $overlay_status, $import_record_id, $record_type) = @_; |
1315 |
|
1414 |
|
1316 |
my ($bib_result, $bib_match, $item_result); |
1415 |
if ($record_type eq 'biblio') { |
1317 |
|
1416 |
my ($bib_result, $bib_match, $item_result); |
1318 |
if ($overlay_status ne 'no_match') { |
1417 |
|
1319 |
$bib_match = GetBestRecordMatch($import_record_id); |
1418 |
if ($overlay_status ne 'no_match') { |
1320 |
if ($overlay_action eq 'replace') { |
1419 |
$bib_match = GetBestRecordMatch($import_record_id); |
1321 |
$bib_result = defined($bib_match) ? 'replace' : 'create_new'; |
1420 |
if ($overlay_action eq 'replace') { |
1322 |
} elsif ($overlay_action eq 'create_new') { |
1421 |
$bib_result = defined($bib_match) ? 'replace' : 'create_new'; |
1323 |
$bib_result = 'create_new'; |
1422 |
} elsif ($overlay_action eq 'create_new') { |
1324 |
} elsif ($overlay_action eq 'ignore') { |
1423 |
$bib_result = 'create_new'; |
1325 |
$bib_result = 'ignore'; |
1424 |
} elsif ($overlay_action eq 'ignore') { |
1326 |
} |
1425 |
$bib_result = 'ignore'; |
1327 |
$item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_matches') ? 'create_new' : 'ignore'; |
1426 |
} |
1328 |
} else { |
1427 |
$item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_matches') ? 'create_new' : 'ignore'; |
1329 |
$bib_result = $nomatch_action; |
1428 |
} else { |
1330 |
$item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_new') ? 'create_new' : 'ignore'; |
1429 |
$bib_result = $nomatch_action; |
1331 |
} |
1430 |
$item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_new') ? 'create_new' : 'ignore'; |
|
|
1431 |
} |
1432 |
return ($bib_result, $item_result, $bib_match); |
1433 |
} else { # must be auths |
1434 |
my ($auth_result, $auth_match); |
1435 |
|
1436 |
if ($overlay_status ne 'no_match') { |
1437 |
$auth_match = GetBestRecordMatch($import_record_id); |
1438 |
if ($overlay_action eq 'replace') { |
1439 |
$auth_result = defined($auth_match) ? 'replace' : 'create_new'; |
1440 |
} elsif ($overlay_action eq 'create_new') { |
1441 |
$auth_result = 'create_new'; |
1442 |
} elsif ($overlay_action eq 'ignore') { |
1443 |
$auth_result = 'ignore'; |
1444 |
} |
1445 |
} else { |
1446 |
$auth_result = $nomatch_action; |
1447 |
} |
1332 |
|
1448 |
|
1333 |
return ($bib_result, $item_result, $bib_match); |
1449 |
return ($auth_result, undef, $auth_match); |
|
|
1450 |
|
1451 |
} |
1334 |
} |
1452 |
} |
1335 |
|
1453 |
|
1336 |
sub _get_revert_action { |
1454 |
sub _get_revert_action { |