1 package C4::ImportBatch;
3 # Copyright (C) 2007 LibLime, 2012 C & P Bibliography Services
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (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 Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 use C4::AuthoritiesMarc;
29 use C4::MarcModificationTemplates;
30 use Koha::Plugins::Handler;
33 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
36 # set the version for version checking
37 $VERSION = 3.07.00.049;
44 GetImportRecordMarcXML
49 AddItemsToImportBiblio
60 GetStagedWebserviceBatches
61 GetImportBatchRangeDesc
62 GetNumberOfNonZ3950ImportBatches
65 GetItemNumbersFromImportBatch
69 GetImportBatchOverlayAction
70 SetImportBatchOverlayAction
71 GetImportBatchNoMatchAction
72 SetImportBatchNoMatchAction
73 GetImportBatchItemAction
74 SetImportBatchItemAction
77 GetImportRecordOverlayStatus
78 SetImportRecordOverlayStatus
81 GetImportRecordMatches
82 SetImportRecordMatches
86 our $logger = Koha::Logger->get( { category => 'C4.ImportBatch' } );
90 C4::ImportBatch - manage batches of imported MARC records
98 =head2 GetZ3950BatchId
100 my $batchid = GetZ3950BatchId($z3950server);
102 Retrieves the ID of the import batch for the Z39.50
103 reservoir for the given target. If necessary,
104 creates the import batch.
108 sub GetZ3950BatchId {
109 my ($z3950server) = @_;
111 my $dbh = C4::Context->dbh;
112 my $sth = $dbh->prepare("SELECT import_batch_id FROM import_batches
113 WHERE batch_type = 'z3950'
115 $sth->execute($z3950server);
116 my $rowref = $sth->fetchrow_arrayref();
118 if (defined $rowref) {
121 my $batch_id = AddImportBatch( {
122 overlay_action => 'create_new',
123 import_status => 'staged',
124 batch_type => 'z3950',
125 file_name => $z3950server,
132 =head2 GetWebserviceBatchId
134 my $batchid = GetWebserviceBatchId();
136 Retrieves the ID of the import batch for webservice.
137 If necessary, creates the import batch.
141 my $WEBSERVICE_BASE_QRY = <<EOQ;
142 SELECT import_batch_id FROM import_batches
143 WHERE batch_type = 'webservice'
144 AND import_status = 'staged'
146 sub GetWebserviceBatchId {
149 my $dbh = C4::Context->dbh;
150 my $sql = $WEBSERVICE_BASE_QRY;
152 foreach my $field (qw(matcher_id overlay_action nomatch_action item_action)) {
153 if (my $val = $params->{$field}) {
154 $sql .= " AND $field = ?";
158 my $id = $dbh->selectrow_array($sql, undef, @args);
161 $params->{batch_type} = 'webservice';
162 $params->{import_status} = 'staged';
163 return AddImportBatch($params);
166 =head2 GetImportRecordMarc
168 my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
172 sub GetImportRecordMarc {
173 my ($import_record_id) = @_;
175 my $dbh = C4::Context->dbh;
176 my ( $marc, $encoding ) = $dbh->selectrow_array(q|
177 SELECT marc, encoding
179 WHERE import_record_id = ?
180 |, undef, $import_record_id );
182 return $marc, $encoding;
185 sub GetRecordFromImportBiblio {
186 my ( $import_record_id, $embed_items ) = @_;
188 my ($marc) = GetImportRecordMarc($import_record_id);
189 my $record = MARC::Record->new_from_usmarc($marc);
191 EmbedItemsInImportBiblio( $record, $import_record_id ) if $embed_items;
196 sub EmbedItemsInImportBiblio {
197 my ( $record, $import_record_id ) = @_;
198 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber", '');
199 my $dbh = C4::Context->dbh;
200 my $import_items = $dbh->selectall_arrayref(q|
201 SELECT import_items.marcxml
203 WHERE import_record_id = ?
204 |, { Slice => {} }, $import_record_id );
206 for my $import_item ( @$import_items ) {
207 my $item_marc = MARC::Record::new_from_xml($import_item->{marcxml});
208 push @item_fields, $item_marc->field($itemtag);
210 $record->append_fields(@item_fields);
214 =head2 GetImportRecordMarcXML
216 my $marcxml = GetImportRecordMarcXML($import_record_id);
220 sub GetImportRecordMarcXML {
221 my ($import_record_id) = @_;
223 my $dbh = C4::Context->dbh;
224 my $sth = $dbh->prepare("SELECT marcxml FROM import_records WHERE import_record_id = ?");
225 $sth->execute($import_record_id);
226 my ($marcxml) = $sth->fetchrow();
232 =head2 AddImportBatch
234 my $batch_id = AddImportBatch($params_hash);
242 foreach (qw( matcher_id template_id branchcode
243 overlay_action nomatch_action item_action
244 import_status batch_type file_name comments record_type )) {
245 if (exists $params->{$_}) {
247 push @vals, $params->{$_};
250 my $dbh = C4::Context->dbh;
251 $dbh->do("INSERT INTO import_batches (".join( ',', @fields).")
252 VALUES (".join( ',', map '?', @fields).")",
255 return $dbh->{'mysql_insertid'};
258 =head2 GetImportBatch
260 my $row = GetImportBatch($batch_id);
262 Retrieve a hashref of an import_batches row.
269 my $dbh = C4::Context->dbh;
270 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches WHERE import_batch_id = ?");
271 $sth->bind_param(1, $batch_id);
273 my $result = $sth->fetchrow_hashref;
279 =head2 AddBiblioToBatch
281 my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence,
282 $marc_record, $encoding, $z3950random, $update_counts);
286 sub AddBiblioToBatch {
287 my $batch_id = shift;
288 my $record_sequence = shift;
289 my $marc_record = shift;
290 my $encoding = shift;
291 my $z3950random = shift;
292 my $update_counts = @_ ? shift : 1;
294 my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, $z3950random, C4::Context->preference('marcflavour'));
295 _add_biblio_fields($import_record_id, $marc_record);
296 _update_batch_record_counts($batch_id) if $update_counts;
297 return $import_record_id;
300 =head2 ModBiblioInBatch
302 ModBiblioInBatch($import_record_id, $marc_record);
306 sub ModBiblioInBatch {
307 my ($import_record_id, $marc_record) = @_;
309 _update_import_record_marc($import_record_id, $marc_record, C4::Context->preference('marcflavour'));
310 _update_biblio_fields($import_record_id, $marc_record);
314 =head2 AddAuthToBatch
316 my $import_record_id = AddAuthToBatch($batch_id, $record_sequence,
317 $marc_record, $encoding, $z3950random, $update_counts, [$marc_type]);
322 my $batch_id = shift;
323 my $record_sequence = shift;
324 my $marc_record = shift;
325 my $encoding = shift;
326 my $z3950random = shift;
327 my $update_counts = @_ ? shift : 1;
328 my $marc_type = shift || C4::Context->preference('marcflavour');
330 $marc_type = 'UNIMARCAUTH' if $marc_type eq 'UNIMARC';
332 my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'auth', $encoding, $z3950random, $marc_type);
333 _add_auth_fields($import_record_id, $marc_record);
334 _update_batch_record_counts($batch_id) if $update_counts;
335 return $import_record_id;
338 =head2 ModAuthInBatch
340 ModAuthInBatch($import_record_id, $marc_record);
345 my ($import_record_id, $marc_record) = @_;
347 my $marcflavour = C4::Context->preference('marcflavour');
348 _update_import_record_marc($import_record_id, $marc_record, $marcflavour eq 'UNIMARC' ? 'UNIMARCAUTH' : 'USMARC');
352 =head2 BatchStageMarcRecords
354 ( $batch_id, $num_records, $num_items, @invalid_records ) =
355 BatchStageMarcRecords(
356 $encoding, $marc_records,
357 $file_name, $to_marc_plugin,
358 $marc_modification_template, $comments,
359 $branch_code, $parse_items,
360 $leave_as_staging, $progress_interval,
366 sub BatchStageMarcRecords {
367 my $record_type = shift;
368 my $encoding = shift;
369 my $marc_records = shift;
370 my $file_name = shift;
371 my $to_marc_plugin = shift;
372 my $marc_modification_template = shift;
373 my $comments = shift;
374 my $branch_code = shift;
375 my $parse_items = shift;
376 my $leave_as_staging = shift;
378 # optional callback to monitor status
380 my $progress_interval = 0;
381 my $progress_callback = undef;
383 $progress_interval = shift;
384 $progress_callback = shift;
385 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
386 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
389 my $batch_id = AddImportBatch( {
390 overlay_action => 'create_new',
391 import_status => 'staging',
392 batch_type => 'batch',
393 file_name => $file_name,
394 comments => $comments,
395 record_type => $record_type,
398 SetImportBatchItemAction($batch_id, 'always_add');
400 SetImportBatchItemAction($batch_id, 'ignore');
403 $marc_records = Koha::Plugins::Handler->run(
405 class => $to_marc_plugin,
407 params => { data => $marc_records }
409 ) if $to_marc_plugin;
411 my $marc_type = C4::Context->preference('marcflavour');
412 $marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth');
413 my @invalid_records = ();
416 # FIXME - for now, we're dealing only with bibs
418 foreach my $marc_blob (split(/\x1D/, $marc_records)) {
419 $marc_blob =~ s/^\s+//g;
420 $marc_blob =~ s/\s+$//g;
421 next unless $marc_blob;
423 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
424 &$progress_callback($rec_num);
426 my ($marc_record, $charset_guessed, $char_errors) =
427 MarcToUTF8Record($marc_blob, $marc_type, $encoding);
429 $encoding = $charset_guessed unless $encoding;
431 ModifyRecordWithTemplate( $marc_modification_template, $marc_record ) if ( $marc_modification_template );
433 my $import_record_id;
434 if (scalar($marc_record->fields()) == 0) {
435 push @invalid_records, $marc_blob;
438 # Normalize the record so it doesn't have separated diacritics
439 SetUTF8Flag($marc_record);
442 if ($record_type eq 'biblio') {
443 $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0);
445 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0);
446 $num_items += scalar(@import_items_ids);
448 } elsif ($record_type eq 'auth') {
449 $import_record_id = AddAuthToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0, $marc_type);
453 unless ($leave_as_staging) {
454 SetImportBatchStatus($batch_id, 'staged');
456 # FIXME branch_code, number of bibs, number of items
457 _update_batch_record_counts($batch_id);
458 return ($batch_id, $num_valid, $num_items, @invalid_records);
461 =head2 AddItemsToImportBiblio
463 my @import_items_ids = AddItemsToImportBiblio($batch_id,
464 $import_record_id, $marc_record, $update_counts);
468 sub AddItemsToImportBiblio {
469 my $batch_id = shift;
470 my $import_record_id = shift;
471 my $marc_record = shift;
472 my $update_counts = @_ ? shift : 0;
474 my @import_items_ids = ();
476 my $dbh = C4::Context->dbh;
477 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
478 foreach my $item_field ($marc_record->field($item_tag)) {
479 my $item_marc = MARC::Record->new();
480 $item_marc->leader("00000 a "); # must set Leader/09 to 'a'
481 $item_marc->append_fields($item_field);
482 $marc_record->delete_field($item_field);
483 my $sth = $dbh->prepare_cached("INSERT INTO import_items (import_record_id, status, marcxml)
485 $sth->bind_param(1, $import_record_id);
486 $sth->bind_param(2, 'staged');
487 $sth->bind_param(3, $item_marc->as_xml());
489 push @import_items_ids, $dbh->{'mysql_insertid'};
493 if ($#import_items_ids > -1) {
494 _update_batch_record_counts($batch_id) if $update_counts;
495 _update_import_record_marc($import_record_id, $marc_record, C4::Context->preference('marcflavour'));
497 return @import_items_ids;
500 =head2 BatchFindDuplicates
502 my $num_with_matches = BatchFindDuplicates($batch_id, $matcher,
503 $max_matches, $progress_interval, $progress_callback);
505 Goes through the records loaded in the batch and attempts to
506 find duplicates for each one. Sets the matching status
507 of each record to "no_match" or "auto_match" as appropriate.
509 The $max_matches parameter is optional; if it is not supplied,
512 The $progress_interval and $progress_callback parameters are
513 optional; if both are supplied, the sub referred to by
514 $progress_callback will be invoked every $progress_interval
515 records using the number of records processed as the
520 sub BatchFindDuplicates {
521 my $batch_id = shift;
523 my $max_matches = @_ ? shift : 10;
525 # optional callback to monitor status
527 my $progress_interval = 0;
528 my $progress_callback = undef;
530 $progress_interval = shift;
531 $progress_callback = shift;
532 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
533 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
536 my $dbh = C4::Context->dbh;
538 my $sth = $dbh->prepare("SELECT import_record_id, record_type, marc
540 WHERE import_batch_id = ?");
541 $sth->execute($batch_id);
542 my $num_with_matches = 0;
544 while (my $rowref = $sth->fetchrow_hashref) {
546 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
547 &$progress_callback($rec_num);
549 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
551 if (defined $matcher) {
552 @matches = $matcher->get_matches($marc_record, $max_matches);
554 if (scalar(@matches) > 0) {
556 SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
557 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
559 SetImportRecordMatches($rowref->{'import_record_id'}, ());
560 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
564 return $num_with_matches;
567 =head2 BatchCommitRecords
569 my ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored) =
570 BatchCommitRecords($batch_id, $framework,
571 $progress_interval, $progress_callback);
575 sub BatchCommitRecords {
576 my $batch_id = shift;
577 my $framework = shift;
579 # optional callback to monitor status
581 my $progress_interval = 0;
582 my $progress_callback = undef;
584 $progress_interval = shift;
585 $progress_callback = shift;
586 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
587 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
593 my $num_items_added = 0;
594 my $num_items_replaced = 0;
595 my $num_items_errored = 0;
597 # commit (i.e., save, all records in the batch)
598 SetImportBatchStatus('importing');
599 my $overlay_action = GetImportBatchOverlayAction($batch_id);
600 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
601 my $item_action = GetImportBatchItemAction($batch_id);
604 my $dbh = C4::Context->dbh;
605 my $sth = $dbh->prepare("SELECT import_records.import_record_id, record_type, status, overlay_status, marc, encoding
607 LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id)
608 LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
609 WHERE import_batch_id = ?");
610 $sth->execute($batch_id);
611 my $marcflavour = C4::Context->preference('marcflavour');
613 while (my $rowref = $sth->fetchrow_hashref) {
614 $record_type = $rowref->{'record_type'};
616 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
617 &$progress_callback($rec_num);
619 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
625 if ($marcflavour eq 'UNIMARC' && $record_type eq 'auth') {
626 $marc_type = 'UNIMARCAUTH';
627 } elsif ($marcflavour eq 'UNIMARC') {
628 $marc_type = 'UNIMARC';
630 $marc_type = 'USMARC';
632 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
634 if ($record_type eq 'biblio') {
635 # remove any item tags - rely on BatchCommitItems
636 ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
637 foreach my $item_field ($marc_record->field($item_tag)) {
638 $marc_record->delete_field($item_field);
642 my ($record_result, $item_result, $record_match) =
643 _get_commit_action($overlay_action, $nomatch_action, $item_action,
644 $rowref->{'overlay_status'}, $rowref->{'import_record_id'}, $record_type);
648 if ($record_result eq 'create_new') {
650 if ($record_type eq 'biblio') {
651 my $biblioitemnumber;
652 ($recordid, $biblioitemnumber) = AddBiblio($marc_record, $framework);
653 $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?";
654 if ($item_result eq 'create_new' || $item_result eq 'replace') {
655 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
656 $num_items_added += $bib_items_added;
657 $num_items_replaced += $bib_items_replaced;
658 $num_items_errored += $bib_items_errored;
661 $recordid = AddAuthority($marc_record, undef, GuessAuthTypeCode($marc_record));
662 $query = "UPDATE import_auths SET matched_authid = ? WHERE import_record_id = ?";
664 my $sth = $dbh->prepare_cached($query);
665 $sth->execute($recordid, $rowref->{'import_record_id'});
667 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
668 } elsif ($record_result eq 'replace') {
670 $recordid = $record_match;
672 if ($record_type eq 'biblio') {
673 my $oldbiblio = GetBiblio($recordid);
674 $oldxml = GetXmlBiblio($recordid);
676 # remove item fields so that they don't get
677 # added again if record is reverted
678 # FIXME: GetXmlBiblio output should not contain item info any more! So the next foreach should not be needed. Does not hurt either; may remove old 952s that should not have been there anymore.
679 my $old_marc = MARC::Record->new_from_xml(StripNonXmlChars($oldxml), 'UTF-8', $rowref->{'encoding'}, $marc_type);
680 foreach my $item_field ($old_marc->field($item_tag)) {
681 $old_marc->delete_field($item_field);
683 $oldxml = $old_marc->as_xml($marc_type);
685 ModBiblio($marc_record, $recordid, $oldbiblio->{'frameworkcode'});
686 $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?";
688 if ($item_result eq 'create_new' || $item_result eq 'replace') {
689 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
690 $num_items_added += $bib_items_added;
691 $num_items_replaced += $bib_items_replaced;
692 $num_items_errored += $bib_items_errored;
695 $oldxml = GetAuthorityXML($recordid);
697 ModAuthority($recordid, $marc_record, GuessAuthTypeCode($marc_record));
698 $query = "UPDATE import_auths SET matched_authid = ? WHERE import_record_id = ?";
700 my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
701 $sth->execute($oldxml, $rowref->{'import_record_id'});
703 my $sth2 = $dbh->prepare_cached($query);
704 $sth2->execute($recordid, $rowref->{'import_record_id'});
706 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
707 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
708 } elsif ($record_result eq 'ignore') {
709 $recordid = $record_match;
711 $recordid = $record_match;
712 if ($record_type eq 'biblio' and defined $recordid and ( $item_result eq 'create_new' || $item_result eq 'replace' ) ) {
713 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
714 $num_items_added += $bib_items_added;
715 $num_items_replaced += $bib_items_replaced;
716 $num_items_errored += $bib_items_errored;
717 # still need to record the matched biblionumber so that the
718 # items can be reverted
719 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
720 $sth2->execute($recordid, $rowref->{'import_record_id'});
721 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
723 SetImportRecordStatus($rowref->{'import_record_id'}, 'ignored');
727 SetImportBatchStatus($batch_id, 'imported');
728 return ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored);
731 =head2 BatchCommitItems
733 ($num_items_added, $num_items_errored) =
734 BatchCommitItems($import_record_id, $biblionumber);
738 sub BatchCommitItems {
739 my ( $import_record_id, $biblionumber, $action ) = @_;
741 my $dbh = C4::Context->dbh;
743 my $num_items_added = 0;
744 my $num_items_errored = 0;
745 my $num_items_replaced = 0;
747 my $sth = $dbh->prepare( "
748 SELECT import_items_id, import_items.marcxml, encoding
750 JOIN import_records USING (import_record_id)
751 WHERE import_record_id = ?
752 ORDER BY import_items_id
754 $sth->bind_param( 1, $import_record_id );
757 while ( my $row = $sth->fetchrow_hashref() ) {
758 my $item_marc = MARC::Record->new_from_xml( StripNonXmlChars( $row->{'marcxml'} ), 'UTF-8', $row->{'encoding'} );
760 # Delete date_due subfield as to not accidentally delete item checkout due dates
761 my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.onloan', GetFrameworkCode($biblionumber) );
762 $item_marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
764 my $item = TransformMarcToKoha( $dbh, $item_marc );
766 my $duplicate_barcode = exists( $item->{'barcode'} ) && GetItemnumberFromBarcode( $item->{'barcode'} );
767 my $duplicate_itemnumber = exists( $item->{'itemnumber'} );
769 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
770 if ( $action eq "replace" && $duplicate_itemnumber ) {
771 # Duplicate itemnumbers have precedence, that way we can update barcodes by overlaying
772 ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber} );
773 $updsth->bind_param( 1, 'imported' );
774 $updsth->bind_param( 2, $item->{itemnumber} );
775 $updsth->bind_param( 3, $row->{'import_items_id'} );
778 $num_items_replaced++;
779 } elsif ( $action eq "replace" && $duplicate_barcode ) {
780 my $itemnumber = GetItemnumberFromBarcode( $item->{'barcode'} );
781 ModItemFromMarc( $item_marc, $biblionumber, $itemnumber );
782 $updsth->bind_param( 1, 'imported' );
783 $updsth->bind_param( 2, $item->{itemnumber} );
784 $updsth->bind_param( 3, $row->{'import_items_id'} );
787 $num_items_replaced++;
788 } elsif ($duplicate_barcode) {
789 $updsth->bind_param( 1, 'error' );
790 $updsth->bind_param( 2, 'duplicate item barcode' );
791 $updsth->bind_param( 3, $row->{'import_items_id'} );
793 $num_items_errored++;
795 my ( $item_biblionumber, $biblioitemnumber, $itemnumber ) = AddItemFromMarc( $item_marc, $biblionumber );
797 $updsth->bind_param( 1, 'imported' );
798 $updsth->bind_param( 2, $itemnumber );
799 $updsth->bind_param( 3, $row->{'import_items_id'} );
807 return ( $num_items_added, $num_items_replaced, $num_items_errored );
810 =head2 BatchRevertRecords
812 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted,
813 $num_ignored) = BatchRevertRecords($batch_id);
817 sub BatchRevertRecords {
818 my $batch_id = shift;
820 $logger->trace("C4::ImportBatch::BatchRevertRecords( $batch_id )");
825 my $num_reverted = 0;
827 my $num_items_deleted = 0;
828 # commit (i.e., save, all records in the batch)
829 SetImportBatchStatus('reverting');
830 my $overlay_action = GetImportBatchOverlayAction($batch_id);
831 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
832 my $dbh = C4::Context->dbh;
833 my $sth = $dbh->prepare("SELECT import_records.import_record_id, record_type, status, overlay_status, marcxml_old, encoding, matched_biblionumber, matched_authid
835 LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id)
836 LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
837 WHERE import_batch_id = ?");
838 $sth->execute($batch_id);
840 my $marcflavour = C4::Context->preference('marcflavour');
841 while (my $rowref = $sth->fetchrow_hashref) {
842 $record_type = $rowref->{'record_type'};
843 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
847 if ($marcflavour eq 'UNIMARC' && $record_type eq 'auth') {
848 $marc_type = 'UNIMARCAUTH';
849 } elsif ($marcflavour eq 'UNIMARC') {
850 $marc_type = 'UNIMARC';
852 $marc_type = 'USMARC';
855 my $record_result = _get_revert_action($overlay_action, $rowref->{'overlay_status'}, $rowref->{'status'});
857 if ($record_result eq 'delete') {
859 if ($record_type eq 'biblio') {
860 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
861 $error = DelBiblio($rowref->{'matched_biblionumber'});
863 my $deletedauthid = DelAuthority($rowref->{'matched_authid'});
865 if (defined $error) {
869 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
871 } elsif ($record_result eq 'restore') {
873 my $old_record = MARC::Record->new_from_xml(StripNonXmlChars($rowref->{'marcxml_old'}), 'UTF-8', $rowref->{'encoding'}, $marc_type);
874 if ($record_type eq 'biblio') {
875 my $biblionumber = $rowref->{'matched_biblionumber'};
876 my $oldbiblio = GetBiblio($biblionumber);
878 $logger->info("C4::ImportBatch::BatchRevertRecords: Biblio record $biblionumber does not exist, restoration of this record was skipped") unless $oldbiblio;
879 next unless $oldbiblio; # Record has since been deleted. Deleted records should stay deleted.
881 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
882 ModBiblio($old_record, $biblionumber, $oldbiblio->{'frameworkcode'});
884 my $authid = $rowref->{'matched_authid'};
885 ModAuthority($authid, $old_record, GuessAuthTypeCode($old_record));
887 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
888 } elsif ($record_result eq 'ignore') {
889 if ($record_type eq 'biblio') {
890 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
892 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
895 if ($record_type eq 'biblio') {
896 # remove matched_biblionumber only if there is no 'imported' item left
897 $query = "UPDATE import_biblios SET matched_biblionumber = NULL WHERE import_record_id = ?";
898 $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')";
900 $query = "UPDATE import_auths SET matched_authid = NULL WHERE import_record_id = ?";
902 my $sth2 = $dbh->prepare_cached($query);
903 $sth2->execute($rowref->{'import_record_id'});
907 SetImportBatchStatus($batch_id, 'reverted');
908 return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored);
911 =head2 BatchRevertItems
913 my $num_items_deleted = BatchRevertItems($import_record_id, $biblionumber);
917 sub BatchRevertItems {
918 my ($import_record_id, $biblionumber) = @_;
920 my $dbh = C4::Context->dbh;
921 my $num_items_deleted = 0;
923 my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
925 JOIN items USING (itemnumber)
926 WHERE import_record_id = ?");
927 $sth->bind_param(1, $import_record_id);
929 while (my $row = $sth->fetchrow_hashref()) {
930 my $error = DelItemCheck($dbh, $biblionumber, $row->{'itemnumber'});
932 my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?");
933 $updsth->bind_param(1, 'reverted');
934 $updsth->bind_param(2, $row->{'import_items_id'});
937 $num_items_deleted++;
944 return $num_items_deleted;
949 CleanBatch($batch_id)
951 Deletes all staged records from the import batch
952 and sets the status of the batch to 'cleaned'. Note
953 that deleting a stage record does *not* affect
954 any record that has been committed to the database.
959 my $batch_id = shift;
960 return unless defined $batch_id;
962 C4::Context->dbh->do('DELETE FROM import_records WHERE import_batch_id = ?', {}, $batch_id);
963 SetImportBatchStatus($batch_id, 'cleaned');
966 =head2 GetAllImportBatches
968 my $results = GetAllImportBatches();
970 Returns a references to an array of hash references corresponding
971 to all import_batches rows (of batch_type 'batch'), sorted in
972 ascending order by import_batch_id.
976 sub GetAllImportBatches {
977 my $dbh = C4::Context->dbh;
978 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
979 WHERE batch_type IN ('batch', 'webservice')
980 ORDER BY import_batch_id ASC");
984 while (my $row = $sth->fetchrow_hashref) {
985 push @$results, $row;
991 =head2 GetStagedWebserviceBatches
993 my $batch_ids = GetStagedWebserviceBatches();
995 Returns a references to an array of batch id's
996 of batch_type 'webservice' that are not imported
1000 my $PENDING_WEBSERVICE_BATCHES_QRY = <<EOQ;
1001 SELECT import_batch_id FROM import_batches
1002 WHERE batch_type = 'webservice'
1003 AND import_status = 'staged'
1005 sub GetStagedWebserviceBatches {
1006 my $dbh = C4::Context->dbh;
1007 return $dbh->selectcol_arrayref($PENDING_WEBSERVICE_BATCHES_QRY);
1010 =head2 GetImportBatchRangeDesc
1012 my $results = GetImportBatchRangeDesc($offset, $results_per_group);
1014 Returns a reference to an array of hash references corresponding to
1015 import_batches rows (sorted in descending order by import_batch_id)
1016 start at the given offset.
1020 sub GetImportBatchRangeDesc {
1021 my ($offset, $results_per_group) = @_;
1023 my $dbh = C4::Context->dbh;
1024 my $query = "SELECT * FROM import_batches
1025 WHERE batch_type IN ('batch', 'webservice')
1026 ORDER BY import_batch_id DESC";
1028 if ($results_per_group){
1029 $query .= " LIMIT ?";
1030 push(@params, $results_per_group);
1033 $query .= " OFFSET ?";
1034 push(@params, $offset);
1036 my $sth = $dbh->prepare_cached($query);
1037 $sth->execute(@params);
1038 my $results = $sth->fetchall_arrayref({});
1043 =head2 GetItemNumbersFromImportBatch
1045 my @itemsnos = GetItemNumbersFromImportBatch($batch_id);
1049 sub GetItemNumbersFromImportBatch {
1050 my ($batch_id) = @_;
1051 my $dbh = C4::Context->dbh;
1052 my $sth = $dbh->prepare("SELECT itemnumber FROM import_batches,import_records,import_items WHERE import_batches.import_batch_id=import_records.import_batch_id AND import_records.import_record_id=import_items.import_record_id AND import_batches.import_batch_id=?");
1053 $sth->execute($batch_id);
1055 while ( my ($itm) = $sth->fetchrow_array ) {
1061 =head2 GetNumberOfImportBatches
1063 my $count = GetNumberOfImportBatches();
1067 sub GetNumberOfNonZ3950ImportBatches {
1068 my $dbh = C4::Context->dbh;
1069 my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type != 'z3950'");
1071 my ($count) = $sth->fetchrow_array();
1076 =head2 GetImportBiblios
1078 my $results = GetImportBiblios($importid);
1082 sub GetImportBiblios {
1083 my ($import_record_id) = @_;
1085 my $dbh = C4::Context->dbh;
1086 my $query = "SELECT * FROM import_biblios WHERE import_record_id = ?";
1087 return $dbh->selectall_arrayref(
1095 =head2 GetImportRecordsRange
1097 my $results = GetImportRecordsRange($batch_id, $offset, $results_per_group);
1099 Returns a reference to an array of hash references corresponding to
1100 import_biblios/import_auths/import_records rows for a given batch
1101 starting at the given offset.
1105 sub GetImportRecordsRange {
1106 my ( $batch_id, $offset, $results_per_group, $status, $parameters ) = @_;
1108 my $dbh = C4::Context->dbh;
1110 my $order_by = $parameters->{order_by} || 'import_record_id';
1111 ( $order_by ) = grep( /^$order_by$/, qw( import_record_id title status overlay_status ) ) ? $order_by : 'import_record_id';
1113 my $order_by_direction =
1114 uc( $parameters->{order_by_direction} ) eq 'DESC' ? 'DESC' : 'ASC';
1116 $order_by .= " $order_by_direction, authorized_heading" if $order_by eq 'title';
1118 my $query = "SELECT title, author, isbn, issn, authorized_heading, import_records.import_record_id,
1119 record_sequence, status, overlay_status,
1120 matched_biblionumber, matched_authid, record_type
1122 LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id)
1123 LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
1124 WHERE import_batch_id = ?";
1126 push(@params, $batch_id);
1128 $query .= " AND status=?";
1129 push(@params,$status);
1132 $query.=" ORDER BY $order_by $order_by_direction";
1134 if($results_per_group){
1135 $query .= " LIMIT ?";
1136 push(@params, $results_per_group);
1139 $query .= " OFFSET ?";
1140 push(@params, $offset);
1142 my $sth = $dbh->prepare_cached($query);
1143 $sth->execute(@params);
1144 my $results = $sth->fetchall_arrayref({});
1150 =head2 GetBestRecordMatch
1152 my $record_id = GetBestRecordMatch($import_record_id);
1156 sub GetBestRecordMatch {
1157 my ($import_record_id) = @_;
1159 my $dbh = C4::Context->dbh;
1160 my $sth = $dbh->prepare("SELECT candidate_match_id
1161 FROM import_record_matches
1162 JOIN import_records ON ( import_record_matches.import_record_id = import_records.import_record_id )
1163 LEFT JOIN biblio ON ( candidate_match_id = biblio.biblionumber )
1164 LEFT JOIN auth_header ON ( candidate_match_id = auth_header.authid )
1165 WHERE import_record_matches.import_record_id = ? AND
1166 ( (import_records.record_type = 'biblio' AND biblio.biblionumber IS NOT NULL) OR
1167 (import_records.record_type = 'auth' AND auth_header.authid IS NOT NULL) )
1168 ORDER BY score DESC, candidate_match_id DESC");
1169 $sth->execute($import_record_id);
1170 my ($record_id) = $sth->fetchrow_array();
1175 =head2 GetImportBatchStatus
1177 my $status = GetImportBatchStatus($batch_id);
1181 sub GetImportBatchStatus {
1182 my ($batch_id) = @_;
1184 my $dbh = C4::Context->dbh;
1185 my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE import_batch_id = ?");
1186 $sth->execute($batch_id);
1187 my ($status) = $sth->fetchrow_array();
1193 =head2 SetImportBatchStatus
1195 SetImportBatchStatus($batch_id, $new_status);
1199 sub SetImportBatchStatus {
1200 my ($batch_id, $new_status) = @_;
1202 my $dbh = C4::Context->dbh;
1203 my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
1204 $sth->execute($new_status, $batch_id);
1209 =head2 GetImportBatchOverlayAction
1211 my $overlay_action = GetImportBatchOverlayAction($batch_id);
1215 sub GetImportBatchOverlayAction {
1216 my ($batch_id) = @_;
1218 my $dbh = C4::Context->dbh;
1219 my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
1220 $sth->execute($batch_id);
1221 my ($overlay_action) = $sth->fetchrow_array();
1223 return $overlay_action;
1228 =head2 SetImportBatchOverlayAction
1230 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
1234 sub SetImportBatchOverlayAction {
1235 my ($batch_id, $new_overlay_action) = @_;
1237 my $dbh = C4::Context->dbh;
1238 my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
1239 $sth->execute($new_overlay_action, $batch_id);
1244 =head2 GetImportBatchNoMatchAction
1246 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
1250 sub GetImportBatchNoMatchAction {
1251 my ($batch_id) = @_;
1253 my $dbh = C4::Context->dbh;
1254 my $sth = $dbh->prepare("SELECT nomatch_action FROM import_batches WHERE import_batch_id = ?");
1255 $sth->execute($batch_id);
1256 my ($nomatch_action) = $sth->fetchrow_array();
1258 return $nomatch_action;
1263 =head2 SetImportBatchNoMatchAction
1265 SetImportBatchNoMatchAction($batch_id, $new_nomatch_action);
1269 sub SetImportBatchNoMatchAction {
1270 my ($batch_id, $new_nomatch_action) = @_;
1272 my $dbh = C4::Context->dbh;
1273 my $sth = $dbh->prepare("UPDATE import_batches SET nomatch_action = ? WHERE import_batch_id = ?");
1274 $sth->execute($new_nomatch_action, $batch_id);
1279 =head2 GetImportBatchItemAction
1281 my $item_action = GetImportBatchItemAction($batch_id);
1285 sub GetImportBatchItemAction {
1286 my ($batch_id) = @_;
1288 my $dbh = C4::Context->dbh;
1289 my $sth = $dbh->prepare("SELECT item_action FROM import_batches WHERE import_batch_id = ?");
1290 $sth->execute($batch_id);
1291 my ($item_action) = $sth->fetchrow_array();
1293 return $item_action;
1298 =head2 SetImportBatchItemAction
1300 SetImportBatchItemAction($batch_id, $new_item_action);
1304 sub SetImportBatchItemAction {
1305 my ($batch_id, $new_item_action) = @_;
1307 my $dbh = C4::Context->dbh;
1308 my $sth = $dbh->prepare("UPDATE import_batches SET item_action = ? WHERE import_batch_id = ?");
1309 $sth->execute($new_item_action, $batch_id);
1314 =head2 GetImportBatchMatcher
1316 my $matcher_id = GetImportBatchMatcher($batch_id);
1320 sub GetImportBatchMatcher {
1321 my ($batch_id) = @_;
1323 my $dbh = C4::Context->dbh;
1324 my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
1325 $sth->execute($batch_id);
1326 my ($matcher_id) = $sth->fetchrow_array();
1333 =head2 SetImportBatchMatcher
1335 SetImportBatchMatcher($batch_id, $new_matcher_id);
1339 sub SetImportBatchMatcher {
1340 my ($batch_id, $new_matcher_id) = @_;
1342 my $dbh = C4::Context->dbh;
1343 my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
1344 $sth->execute($new_matcher_id, $batch_id);
1349 =head2 GetImportRecordOverlayStatus
1351 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
1355 sub GetImportRecordOverlayStatus {
1356 my ($import_record_id) = @_;
1358 my $dbh = C4::Context->dbh;
1359 my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
1360 $sth->execute($import_record_id);
1361 my ($overlay_status) = $sth->fetchrow_array();
1363 return $overlay_status;
1368 =head2 SetImportRecordOverlayStatus
1370 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
1374 sub SetImportRecordOverlayStatus {
1375 my ($import_record_id, $new_overlay_status) = @_;
1377 my $dbh = C4::Context->dbh;
1378 my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
1379 $sth->execute($new_overlay_status, $import_record_id);
1384 =head2 GetImportRecordStatus
1386 my $status = GetImportRecordStatus($import_record_id);
1390 sub GetImportRecordStatus {
1391 my ($import_record_id) = @_;
1393 my $dbh = C4::Context->dbh;
1394 my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
1395 $sth->execute($import_record_id);
1396 my ($status) = $sth->fetchrow_array();
1403 =head2 SetImportRecordStatus
1405 SetImportRecordStatus($import_record_id, $new_status);
1409 sub SetImportRecordStatus {
1410 my ($import_record_id, $new_status) = @_;
1412 my $dbh = C4::Context->dbh;
1413 my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
1414 $sth->execute($new_status, $import_record_id);
1419 =head2 GetImportRecordMatches
1421 my $results = GetImportRecordMatches($import_record_id, $best_only);
1425 sub GetImportRecordMatches {
1426 my $import_record_id = shift;
1427 my $best_only = @_ ? shift : 0;
1429 my $dbh = C4::Context->dbh;
1430 # FIXME currently biblio only
1431 my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber,
1432 candidate_match_id, score, record_type
1434 JOIN import_record_matches USING (import_record_id)
1435 LEFT JOIN biblio ON (biblionumber = candidate_match_id)
1436 WHERE import_record_id = ?
1437 ORDER BY score DESC, biblionumber DESC");
1438 $sth->bind_param(1, $import_record_id);
1441 while (my $row = $sth->fetchrow_hashref) {
1442 if ($row->{'record_type'} eq 'auth') {
1443 $row->{'authorized_heading'} = C4::AuthoritiesMarc::GetAuthorizedHeading( { authid => $row->{'candidate_match_id'} } );
1445 next if ($row->{'record_type'} eq 'biblio' && not $row->{'biblionumber'});
1446 push @$results, $row;
1456 =head2 SetImportRecordMatches
1458 SetImportRecordMatches($import_record_id, @matches);
1462 sub SetImportRecordMatches {
1463 my $import_record_id = shift;
1466 my $dbh = C4::Context->dbh;
1467 my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
1468 $delsth->execute($import_record_id);
1471 my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
1473 foreach my $match (@matches) {
1474 $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
1479 # internal functions
1481 sub _create_import_record {
1482 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random, $marc_type) = @_;
1484 my $dbh = C4::Context->dbh;
1485 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml,
1486 record_type, encoding, z3950random)
1487 VALUES (?, ?, ?, ?, ?, ?, ?)");
1488 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml($marc_type),
1489 $record_type, $encoding, $z3950random);
1490 my $import_record_id = $dbh->{'mysql_insertid'};
1492 return $import_record_id;
1495 sub _update_import_record_marc {
1496 my ($import_record_id, $marc_record, $marc_type) = @_;
1498 my $dbh = C4::Context->dbh;
1499 my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
1500 WHERE import_record_id = ?");
1501 $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml($marc_type), $import_record_id);
1505 sub _add_auth_fields {
1506 my ($import_record_id, $marc_record) = @_;
1509 if ($marc_record->field('001')) {
1510 $controlnumber = $marc_record->field('001')->data();
1512 my $authorized_heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marc_record });
1513 my $dbh = C4::Context->dbh;
1514 my $sth = $dbh->prepare("INSERT INTO import_auths (import_record_id, control_number, authorized_heading) VALUES (?, ?, ?)");
1515 $sth->execute($import_record_id, $controlnumber, $authorized_heading);
1519 sub _add_biblio_fields {
1520 my ($import_record_id, $marc_record) = @_;
1522 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1523 my $dbh = C4::Context->dbh;
1524 # FIXME no controlnumber, originalsource
1525 $isbn = C4::Koha::GetNormalizedISBN($isbn);
1526 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
1527 $sth->execute($import_record_id, $title, $author, $isbn, $issn);
1532 sub _update_biblio_fields {
1533 my ($import_record_id, $marc_record) = @_;
1535 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1536 my $dbh = C4::Context->dbh;
1537 # FIXME no controlnumber, originalsource
1538 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1542 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
1543 WHERE import_record_id = ?");
1544 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
1548 sub _parse_biblio_fields {
1549 my ($marc_record) = @_;
1551 my $dbh = C4::Context->dbh;
1552 my $bibliofields = TransformMarcToKoha($dbh, $marc_record, '');
1553 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
1557 sub _update_batch_record_counts {
1558 my ($batch_id) = @_;
1560 my $dbh = C4::Context->dbh;
1561 my $sth = $dbh->prepare_cached("UPDATE import_batches SET
1565 WHERE import_batch_id = import_batches.import_batch_id),
1569 JOIN import_items USING (import_record_id)
1570 WHERE import_batch_id = import_batches.import_batch_id
1571 AND record_type = 'biblio')
1572 WHERE import_batch_id = ?");
1573 $sth->bind_param(1, $batch_id);
1578 sub _get_commit_action {
1579 my ($overlay_action, $nomatch_action, $item_action, $overlay_status, $import_record_id, $record_type) = @_;
1581 if ($record_type eq 'biblio') {
1582 my ($bib_result, $bib_match, $item_result);
1584 if ($overlay_status ne 'no_match') {
1585 $bib_match = GetBestRecordMatch($import_record_id);
1586 if ($overlay_action eq 'replace') {
1587 $bib_result = defined($bib_match) ? 'replace' : 'create_new';
1588 } elsif ($overlay_action eq 'create_new') {
1589 $bib_result = 'create_new';
1590 } elsif ($overlay_action eq 'ignore') {
1591 $bib_result = 'ignore';
1593 if($item_action eq 'always_add' or $item_action eq 'add_only_for_matches'){
1594 $item_result = 'create_new';
1596 elsif($item_action eq 'replace'){
1597 $item_result = 'replace';
1600 $item_result = 'ignore';
1603 $bib_result = $nomatch_action;
1604 $item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_new') ? 'create_new' : 'ignore';
1606 return ($bib_result, $item_result, $bib_match);
1607 } else { # must be auths
1608 my ($auth_result, $auth_match);
1610 if ($overlay_status ne 'no_match') {
1611 $auth_match = GetBestRecordMatch($import_record_id);
1612 if ($overlay_action eq 'replace') {
1613 $auth_result = defined($auth_match) ? 'replace' : 'create_new';
1614 } elsif ($overlay_action eq 'create_new') {
1615 $auth_result = 'create_new';
1616 } elsif ($overlay_action eq 'ignore') {
1617 $auth_result = 'ignore';
1620 $auth_result = $nomatch_action;
1623 return ($auth_result, undef, $auth_match);
1628 sub _get_revert_action {
1629 my ($overlay_action, $overlay_status, $status) = @_;
1633 if ($status eq 'ignored') {
1634 $bib_result = 'ignore';
1636 if ($overlay_action eq 'create_new') {
1637 $bib_result = 'delete';
1639 $bib_result = ($overlay_status eq 'match_applied') ? 'restore' : 'delete';
1650 Koha Development Team <http://koha-community.org/>
1652 Galen Charlton <galen.charlton@liblime.com>