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>.
24 use C4::Koha qw( GetNormalizedISBN );
33 use C4::Items qw( AddItemFromMarc ModItemFromMarc );
34 use C4::Charset qw( MarcToUTF8Record SetUTF8Flag StripNonXmlChars );
35 use C4::AuthoritiesMarc;
36 use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate );
38 use Koha::Plugins::Handler;
41 our (@ISA, @EXPORT_OK);
49 GetImportRecordMarcXML
50 GetRecordFromImportBiblio
55 AddItemsToImportBiblio
67 GetStagedWebserviceBatches
68 GetImportBatchRangeDesc
69 GetNumberOfNonZ3950ImportBatches
72 GetItemNumbersFromImportBatch
76 GetImportBatchOverlayAction
77 SetImportBatchOverlayAction
78 GetImportBatchNoMatchAction
79 SetImportBatchNoMatchAction
80 GetImportBatchItemAction
81 SetImportBatchItemAction
84 GetImportRecordOverlayStatus
85 SetImportRecordOverlayStatus
88 SetMatchedBiblionumber
89 GetImportRecordMatches
90 SetImportRecordMatches
92 RecordsFromMARCXMLFile
93 RecordsFromISO2709File
100 C4::ImportBatch - manage batches of imported MARC records
108 =head2 GetZ3950BatchId
110 my $batchid = GetZ3950BatchId($z3950server);
112 Retrieves the ID of the import batch for the Z39.50
113 reservoir for the given target. If necessary,
114 creates the import batch.
118 sub GetZ3950BatchId {
119 my ($z3950server) = @_;
121 my $dbh = C4::Context->dbh;
122 my $sth = $dbh->prepare("SELECT import_batch_id FROM import_batches
123 WHERE batch_type = 'z3950'
125 $sth->execute($z3950server);
126 my $rowref = $sth->fetchrow_arrayref();
128 if (defined $rowref) {
131 my $batch_id = AddImportBatch( {
132 overlay_action => 'create_new',
133 import_status => 'staged',
134 batch_type => 'z3950',
135 file_name => $z3950server,
142 =head2 GetWebserviceBatchId
144 my $batchid = GetWebserviceBatchId();
146 Retrieves the ID of the import batch for webservice.
147 If necessary, creates the import batch.
151 my $WEBSERVICE_BASE_QRY = <<EOQ;
152 SELECT import_batch_id FROM import_batches
153 WHERE batch_type = 'webservice'
154 AND import_status = 'staged'
156 sub GetWebserviceBatchId {
159 my $dbh = C4::Context->dbh;
160 my $sql = $WEBSERVICE_BASE_QRY;
162 foreach my $field (qw(matcher_id overlay_action nomatch_action item_action)) {
163 if (my $val = $params->{$field}) {
164 $sql .= " AND $field = ?";
168 my $id = $dbh->selectrow_array($sql, undef, @args);
171 $params->{batch_type} = 'webservice';
172 $params->{import_status} = 'staged';
173 return AddImportBatch($params);
176 =head2 GetImportRecordMarc
178 my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
182 sub GetImportRecordMarc {
183 my ($import_record_id) = @_;
185 my $dbh = C4::Context->dbh;
186 my ( $marc, $encoding ) = $dbh->selectrow_array(q|
187 SELECT marc, encoding
189 WHERE import_record_id = ?
190 |, undef, $import_record_id );
192 return $marc, $encoding;
195 sub GetRecordFromImportBiblio {
196 my ( $import_record_id, $embed_items ) = @_;
198 my ($marc) = GetImportRecordMarc($import_record_id);
199 my $record = MARC::Record->new_from_usmarc($marc);
201 EmbedItemsInImportBiblio( $record, $import_record_id ) if $embed_items;
206 sub EmbedItemsInImportBiblio {
207 my ( $record, $import_record_id ) = @_;
208 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
209 my $dbh = C4::Context->dbh;
210 my $import_items = $dbh->selectall_arrayref(q|
211 SELECT import_items.marcxml
213 WHERE import_record_id = ?
214 |, { Slice => {} }, $import_record_id );
216 for my $import_item ( @$import_items ) {
217 my $item_marc = MARC::Record::new_from_xml($import_item->{marcxml}, 'UTF-8');
218 push @item_fields, $item_marc->field($itemtag);
220 $record->append_fields(@item_fields);
224 =head2 GetImportRecordMarcXML
226 my $marcxml = GetImportRecordMarcXML($import_record_id);
230 sub GetImportRecordMarcXML {
231 my ($import_record_id) = @_;
233 my $dbh = C4::Context->dbh;
234 my $sth = $dbh->prepare("SELECT marcxml FROM import_records WHERE import_record_id = ?");
235 $sth->execute($import_record_id);
236 my ($marcxml) = $sth->fetchrow();
242 =head2 AddImportBatch
244 my $batch_id = AddImportBatch($params_hash);
252 foreach (qw( matcher_id template_id branchcode
253 overlay_action nomatch_action item_action
254 import_status batch_type file_name comments record_type )) {
255 if (exists $params->{$_}) {
257 push @vals, $params->{$_};
260 my $dbh = C4::Context->dbh;
261 $dbh->do("INSERT INTO import_batches (".join( ',', @fields).")
262 VALUES (".join( ',', map '?', @fields).")",
265 return $dbh->{'mysql_insertid'};
268 =head2 GetImportBatch
270 my $row = GetImportBatch($batch_id);
272 Retrieve a hashref of an import_batches row.
279 my $dbh = C4::Context->dbh;
280 my $sth = $dbh->prepare_cached("SELECT b.*, p.name as profile FROM import_batches b LEFT JOIN import_batch_profiles p ON p.id = b.profile_id WHERE import_batch_id = ?");
281 $sth->bind_param(1, $batch_id);
283 my $result = $sth->fetchrow_hashref;
289 =head2 AddBiblioToBatch
291 my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence,
292 $marc_record, $encoding, $update_counts);
296 sub AddBiblioToBatch {
297 my $batch_id = shift;
298 my $record_sequence = shift;
299 my $marc_record = shift;
300 my $encoding = shift;
301 my $update_counts = @_ ? shift : 1;
303 my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, C4::Context->preference('marcflavour'));
304 _add_biblio_fields($import_record_id, $marc_record);
305 _update_batch_record_counts($batch_id) if $update_counts;
306 return $import_record_id;
309 =head2 ModBiblioInBatch
311 ModBiblioInBatch($import_record_id, $marc_record);
315 sub ModBiblioInBatch {
316 my ($import_record_id, $marc_record) = @_;
318 _update_import_record_marc($import_record_id, $marc_record, C4::Context->preference('marcflavour'));
319 _update_biblio_fields($import_record_id, $marc_record);
323 =head2 AddAuthToBatch
325 my $import_record_id = AddAuthToBatch($batch_id, $record_sequence,
326 $marc_record, $encoding, $update_counts, [$marc_type]);
331 my $batch_id = shift;
332 my $record_sequence = shift;
333 my $marc_record = shift;
334 my $encoding = shift;
335 my $update_counts = @_ ? shift : 1;
336 my $marc_type = shift || C4::Context->preference('marcflavour');
338 $marc_type = 'UNIMARCAUTH' if $marc_type eq 'UNIMARC';
340 my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'auth', $encoding, $marc_type);
341 _add_auth_fields($import_record_id, $marc_record);
342 _update_batch_record_counts($batch_id) if $update_counts;
343 return $import_record_id;
346 =head2 ModAuthInBatch
348 ModAuthInBatch($import_record_id, $marc_record);
353 my ($import_record_id, $marc_record) = @_;
355 my $marcflavour = C4::Context->preference('marcflavour');
356 _update_import_record_marc($import_record_id, $marc_record, $marcflavour eq 'UNIMARC' ? 'UNIMARCAUTH' : 'USMARC');
360 =head2 BatchStageMarcRecords
362 ( $batch_id, $num_records, $num_items, @invalid_records ) =
363 BatchStageMarcRecords(
364 $record_type, $encoding,
365 $marc_records, $file_name,
366 $marc_modification_template, $comments,
367 $branch_code, $parse_items,
368 $leave_as_staging, $progress_interval,
374 sub BatchStageMarcRecords {
375 my $record_type = shift;
376 my $encoding = shift;
377 my $marc_records = shift;
378 my $file_name = shift;
379 my $marc_modification_template = shift;
380 my $comments = shift;
381 my $branch_code = shift;
382 my $parse_items = shift;
383 my $leave_as_staging = shift;
385 # optional callback to monitor status
387 my $progress_interval = 0;
388 my $progress_callback = undef;
390 $progress_interval = shift;
391 $progress_callback = shift;
392 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
393 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
396 my $batch_id = AddImportBatch( {
397 overlay_action => 'create_new',
398 import_status => 'staging',
399 batch_type => 'batch',
400 file_name => $file_name,
401 comments => $comments,
402 record_type => $record_type,
405 SetImportBatchItemAction($batch_id, 'always_add');
407 SetImportBatchItemAction($batch_id, 'ignore');
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_record (@$marc_records) {
420 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
421 &$progress_callback($rec_num);
424 ModifyRecordWithTemplate( $marc_modification_template, $marc_record ) if ( $marc_modification_template );
426 my $import_record_id;
427 if (scalar($marc_record->fields()) == 0) {
428 push @invalid_records, $marc_record;
431 # Normalize the record so it doesn't have separated diacritics
432 SetUTF8Flag($marc_record);
435 if ($record_type eq 'biblio') {
436 $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0);
438 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0);
439 $num_items += scalar(@import_items_ids);
441 } elsif ($record_type eq 'auth') {
442 $import_record_id = AddAuthToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0, $marc_type);
446 unless ($leave_as_staging) {
447 SetImportBatchStatus($batch_id, 'staged');
449 # FIXME branch_code, number of bibs, number of items
450 _update_batch_record_counts($batch_id);
451 return ($batch_id, $num_valid, $num_items, @invalid_records);
454 =head2 AddItemsToImportBiblio
456 my @import_items_ids = AddItemsToImportBiblio($batch_id,
457 $import_record_id, $marc_record, $update_counts);
461 sub AddItemsToImportBiblio {
462 my $batch_id = shift;
463 my $import_record_id = shift;
464 my $marc_record = shift;
465 my $update_counts = @_ ? shift : 0;
467 my @import_items_ids = ();
469 my $dbh = C4::Context->dbh;
470 my ($item_tag,$item_subfield) = &GetMarcFromKohaField( "items.itemnumber" );
471 foreach my $item_field ($marc_record->field($item_tag)) {
472 my $item_marc = MARC::Record->new();
473 $item_marc->leader("00000 a "); # must set Leader/09 to 'a'
474 $item_marc->append_fields($item_field);
475 $marc_record->delete_field($item_field);
476 my $sth = $dbh->prepare_cached("INSERT INTO import_items (import_record_id, status, marcxml)
478 $sth->bind_param(1, $import_record_id);
479 $sth->bind_param(2, 'staged');
480 $sth->bind_param(3, $item_marc->as_xml("USMARC"));
482 push @import_items_ids, $dbh->{'mysql_insertid'};
486 if ($#import_items_ids > -1) {
487 _update_batch_record_counts($batch_id) if $update_counts;
488 _update_import_record_marc($import_record_id, $marc_record, C4::Context->preference('marcflavour'));
490 return @import_items_ids;
493 =head2 BatchFindDuplicates
495 my $num_with_matches = BatchFindDuplicates($batch_id, $matcher,
496 $max_matches, $progress_interval, $progress_callback);
498 Goes through the records loaded in the batch and attempts to
499 find duplicates for each one. Sets the matching status
500 of each record to "no_match" or "auto_match" as appropriate.
502 The $max_matches parameter is optional; if it is not supplied,
505 The $progress_interval and $progress_callback parameters are
506 optional; if both are supplied, the sub referred to by
507 $progress_callback will be invoked every $progress_interval
508 records using the number of records processed as the
513 sub BatchFindDuplicates {
514 my $batch_id = shift;
516 my $max_matches = @_ ? shift : 10;
518 # optional callback to monitor status
520 my $progress_interval = 0;
521 my $progress_callback = undef;
523 $progress_interval = shift;
524 $progress_callback = shift;
525 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
526 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
529 my $dbh = C4::Context->dbh;
531 my $sth = $dbh->prepare("SELECT import_record_id, record_type, marc
533 WHERE import_batch_id = ?");
534 $sth->execute($batch_id);
535 my $num_with_matches = 0;
537 while (my $rowref = $sth->fetchrow_hashref) {
539 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
540 &$progress_callback($rec_num);
542 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
544 if (defined $matcher) {
545 @matches = $matcher->get_matches($marc_record, $max_matches);
547 if (scalar(@matches) > 0) {
549 SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
550 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
552 SetImportRecordMatches($rowref->{'import_record_id'}, ());
553 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
557 return $num_with_matches;
560 =head2 BatchCommitRecords
562 my ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored) =
563 BatchCommitRecords($batch_id, $framework,
564 $progress_interval, $progress_callback);
568 sub BatchCommitRecords {
569 my $batch_id = shift;
570 my $framework = shift;
572 # optional callback to monitor status
574 my $progress_interval = 0;
575 my $progress_callback = undef;
577 $progress_interval = shift;
578 $progress_callback = shift;
579 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
580 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
586 my $num_items_added = 0;
587 my $num_items_replaced = 0;
588 my $num_items_errored = 0;
590 # commit (i.e., save, all records in the batch)
591 SetImportBatchStatus($batch_id, 'importing');
592 my $overlay_action = GetImportBatchOverlayAction($batch_id);
593 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
594 my $item_action = GetImportBatchItemAction($batch_id);
597 my $dbh = C4::Context->dbh;
598 my $sth = $dbh->prepare("SELECT import_records.import_record_id, record_type, status, overlay_status, marc, encoding
600 LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id)
601 LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
602 WHERE import_batch_id = ?");
603 $sth->execute($batch_id);
604 my $marcflavour = C4::Context->preference('marcflavour');
606 while (my $rowref = $sth->fetchrow_hashref) {
607 $record_type = $rowref->{'record_type'};
609 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
610 &$progress_callback($rec_num);
612 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
618 if ($marcflavour eq 'UNIMARC' && $record_type eq 'auth') {
619 $marc_type = 'UNIMARCAUTH';
620 } elsif ($marcflavour eq 'UNIMARC') {
621 $marc_type = 'UNIMARC';
623 $marc_type = 'USMARC';
625 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
627 if ($record_type eq 'biblio') {
628 # remove any item tags - rely on BatchCommitItems
629 ($item_tag,$item_subfield) = &GetMarcFromKohaField( "items.itemnumber" );
630 foreach my $item_field ($marc_record->field($item_tag)) {
631 $marc_record->delete_field($item_field);
635 my ($record_result, $item_result, $record_match) =
636 _get_commit_action($overlay_action, $nomatch_action, $item_action,
637 $rowref->{'overlay_status'}, $rowref->{'import_record_id'}, $record_type);
641 if ($record_result eq 'create_new') {
643 if ($record_type eq 'biblio') {
644 my $biblioitemnumber;
645 ($recordid, $biblioitemnumber) = AddBiblio($marc_record, $framework);
646 $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; # FIXME call SetMatchedBiblionumber instead
647 if ($item_result eq 'create_new' || $item_result eq 'replace') {
648 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
649 $num_items_added += $bib_items_added;
650 $num_items_replaced += $bib_items_replaced;
651 $num_items_errored += $bib_items_errored;
654 $recordid = AddAuthority($marc_record, undef, GuessAuthTypeCode($marc_record));
655 $query = "UPDATE import_auths SET matched_authid = ? WHERE import_record_id = ?";
657 my $sth = $dbh->prepare_cached($query);
658 $sth->execute($recordid, $rowref->{'import_record_id'});
660 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
661 } elsif ($record_result eq 'replace') {
663 $recordid = $record_match;
665 if ($record_type eq 'biblio') {
666 my $oldbiblio = Koha::Biblios->find( $recordid );
667 $oldxml = GetXmlBiblio($recordid);
669 # remove item fields so that they don't get
670 # added again if record is reverted
671 # 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.
672 my $old_marc = MARC::Record->new_from_xml(StripNonXmlChars($oldxml), 'UTF-8', $rowref->{'encoding'}, $marc_type);
673 foreach my $item_field ($old_marc->field($item_tag)) {
674 $old_marc->delete_field($item_field);
676 $oldxml = $old_marc->as_xml($marc_type);
678 ModBiblio($marc_record, $recordid, $oldbiblio->frameworkcode);
679 $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; # FIXME call SetMatchedBiblionumber instead
681 if ($item_result eq 'create_new' || $item_result eq 'replace') {
682 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
683 $num_items_added += $bib_items_added;
684 $num_items_replaced += $bib_items_replaced;
685 $num_items_errored += $bib_items_errored;
688 $oldxml = GetAuthorityXML($recordid);
690 ModAuthority($recordid, $marc_record, GuessAuthTypeCode($marc_record));
691 $query = "UPDATE import_auths SET matched_authid = ? WHERE import_record_id = ?";
693 my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
694 $sth->execute($oldxml, $rowref->{'import_record_id'});
696 my $sth2 = $dbh->prepare_cached($query);
697 $sth2->execute($recordid, $rowref->{'import_record_id'});
699 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
700 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
701 } elsif ($record_result eq 'ignore') {
702 $recordid = $record_match;
704 $recordid = $record_match;
705 if ($record_type eq 'biblio' and defined $recordid and ( $item_result eq 'create_new' || $item_result eq 'replace' ) ) {
706 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
707 $num_items_added += $bib_items_added;
708 $num_items_replaced += $bib_items_replaced;
709 $num_items_errored += $bib_items_errored;
710 # still need to record the matched biblionumber so that the
711 # items can be reverted
712 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"); # FIXME call SetMatchedBiblionumber instead
713 $sth2->execute($recordid, $rowref->{'import_record_id'});
714 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
716 SetImportRecordStatus($rowref->{'import_record_id'}, 'ignored');
720 SetImportBatchStatus($batch_id, 'imported');
721 return ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored);
724 =head2 BatchCommitItems
726 ($num_items_added, $num_items_errored) =
727 BatchCommitItems($import_record_id, $biblionumber);
731 sub BatchCommitItems {
732 my ( $import_record_id, $biblionumber, $action ) = @_;
734 my $dbh = C4::Context->dbh;
736 my $num_items_added = 0;
737 my $num_items_errored = 0;
738 my $num_items_replaced = 0;
740 my $sth = $dbh->prepare( "
741 SELECT import_items_id, import_items.marcxml, encoding
743 JOIN import_records USING (import_record_id)
744 WHERE import_record_id = ?
745 ORDER BY import_items_id
747 $sth->bind_param( 1, $import_record_id );
750 while ( my $row = $sth->fetchrow_hashref() ) {
751 my $item_marc = MARC::Record->new_from_xml( StripNonXmlChars( $row->{'marcxml'} ), 'UTF-8', $row->{'encoding'} );
753 # Delete date_due subfield as to not accidentally delete item checkout due dates
754 my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.onloan' );
755 $item_marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
757 my $item = TransformMarcToKoha( $item_marc );
759 my $duplicate_barcode = exists( $item->{'barcode'} ) && Koha::Items->find({ barcode => $item->{'barcode'} });
760 my $duplicate_itemnumber = exists( $item->{'itemnumber'} );
762 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
763 if ( $action eq "replace" && $duplicate_itemnumber ) {
764 # Duplicate itemnumbers have precedence, that way we can update barcodes by overlaying
765 ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber} );
766 $updsth->bind_param( 1, 'imported' );
767 $updsth->bind_param( 2, $item->{itemnumber} );
768 $updsth->bind_param( 3, $row->{'import_items_id'} );
771 $num_items_replaced++;
772 } elsif ( $action eq "replace" && $duplicate_barcode ) {
773 my $itemnumber = $duplicate_barcode->itemnumber;
774 ModItemFromMarc( $item_marc, $biblionumber, $itemnumber );
775 $updsth->bind_param( 1, 'imported' );
776 $updsth->bind_param( 2, $item->{itemnumber} );
777 $updsth->bind_param( 3, $row->{'import_items_id'} );
780 $num_items_replaced++;
781 } elsif ($duplicate_barcode) {
782 $updsth->bind_param( 1, 'error' );
783 $updsth->bind_param( 2, 'duplicate item barcode' );
784 $updsth->bind_param( 3, $row->{'import_items_id'} );
786 $num_items_errored++;
788 # Remove the itemnumber if it exists, we want to create a new item
789 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
790 $item_marc->field($itemtag)->delete_subfield( code => $itemsubfield );
792 my ( $item_biblionumber, $biblioitemnumber, $itemnumber ) = AddItemFromMarc( $item_marc, $biblionumber );
794 $updsth->bind_param( 1, 'imported' );
795 $updsth->bind_param( 2, $itemnumber );
796 $updsth->bind_param( 3, $row->{'import_items_id'} );
804 return ( $num_items_added, $num_items_replaced, $num_items_errored );
807 =head2 BatchRevertRecords
809 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted,
810 $num_ignored) = BatchRevertRecords($batch_id);
814 sub BatchRevertRecords {
815 my $batch_id = shift;
817 my $logger = Koha::Logger->get( { category => 'C4.ImportBatch' } );
819 $logger->trace("C4::ImportBatch::BatchRevertRecords( $batch_id )");
824 my $num_reverted = 0;
826 my $num_items_deleted = 0;
827 # commit (i.e., save, all records in the batch)
828 SetImportBatchStatus($batch_id, 'reverting');
829 my $overlay_action = GetImportBatchOverlayAction($batch_id);
830 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
831 my $dbh = C4::Context->dbh;
832 my $sth = $dbh->prepare("SELECT import_records.import_record_id, record_type, status, overlay_status, marcxml_old, encoding, matched_biblionumber, matched_authid
834 LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id)
835 LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
836 WHERE import_batch_id = ?");
837 $sth->execute($batch_id);
839 my $marcflavour = C4::Context->preference('marcflavour');
840 while (my $rowref = $sth->fetchrow_hashref) {
841 $record_type = $rowref->{'record_type'};
842 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
846 if ($marcflavour eq 'UNIMARC' && $record_type eq 'auth') {
847 $marc_type = 'UNIMARCAUTH';
848 } elsif ($marcflavour eq 'UNIMARC') {
849 $marc_type = 'UNIMARC';
851 $marc_type = 'USMARC';
854 my $record_result = _get_revert_action($overlay_action, $rowref->{'overlay_status'}, $rowref->{'status'});
856 if ($record_result eq 'delete') {
858 if ($record_type eq 'biblio') {
859 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
860 $error = DelBiblio($rowref->{'matched_biblionumber'});
862 DelAuthority({ authid => $rowref->{'matched_authid'} });
864 if (defined $error) {
868 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
870 } elsif ($record_result eq 'restore') {
872 my $old_record = MARC::Record->new_from_xml(StripNonXmlChars($rowref->{'marcxml_old'}), 'UTF-8', $rowref->{'encoding'}, $marc_type);
873 if ($record_type eq 'biblio') {
874 my $biblionumber = $rowref->{'matched_biblionumber'};
875 my $oldbiblio = Koha::Biblios->find( $biblionumber );
877 $logger->info("C4::ImportBatch::BatchRevertRecords: Biblio record $biblionumber does not exist, restoration of this record was skipped") unless $oldbiblio;
878 next unless $oldbiblio; # Record has since been deleted. Deleted records should stay deleted.
880 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
881 ModBiblio($old_record, $biblionumber, $oldbiblio->frameworkcode);
883 my $authid = $rowref->{'matched_authid'};
884 ModAuthority($authid, $old_record, GuessAuthTypeCode($old_record));
886 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
887 } elsif ($record_result eq 'ignore') {
888 if ($record_type eq 'biblio') {
889 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
891 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
894 if ($record_type eq 'biblio') {
895 # remove matched_biblionumber only if there is no 'imported' item left
896 $query = "UPDATE import_biblios SET matched_biblionumber = NULL WHERE import_record_id = ?"; # FIXME Remove me
897 $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')";
899 $query = "UPDATE import_auths SET matched_authid = NULL WHERE import_record_id = ?";
901 my $sth2 = $dbh->prepare_cached($query);
902 $sth2->execute($rowref->{'import_record_id'});
906 SetImportBatchStatus($batch_id, 'reverted');
907 return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored);
910 =head2 BatchRevertItems
912 my $num_items_deleted = BatchRevertItems($import_record_id, $biblionumber);
916 sub BatchRevertItems {
917 my ($import_record_id, $biblionumber) = @_;
919 my $dbh = C4::Context->dbh;
920 my $num_items_deleted = 0;
922 my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
924 JOIN items USING (itemnumber)
925 WHERE import_record_id = ?");
926 $sth->bind_param(1, $import_record_id);
928 while (my $row = $sth->fetchrow_hashref()) {
929 my $item = Koha::Items->find($row->{itemnumber});
930 my $error = $item->safe_delete;
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');
968 DeleteBatch($batch_id)
970 Deletes the record from the database. This can only be done
971 once the batch has been cleaned.
976 my $batch_id = shift;
977 return unless defined $batch_id;
979 my $dbh = C4::Context->dbh;
980 my $sth = $dbh->prepare('DELETE FROM import_batches WHERE import_batch_id = ?');
981 $sth->execute( $batch_id );
984 =head2 GetAllImportBatches
986 my $results = GetAllImportBatches();
988 Returns a references to an array of hash references corresponding
989 to all import_batches rows (of batch_type 'batch'), sorted in
990 ascending order by import_batch_id.
994 sub GetAllImportBatches {
995 my $dbh = C4::Context->dbh;
996 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
997 WHERE batch_type IN ('batch', 'webservice')
998 ORDER BY import_batch_id ASC");
1002 while (my $row = $sth->fetchrow_hashref) {
1003 push @$results, $row;
1009 =head2 GetStagedWebserviceBatches
1011 my $batch_ids = GetStagedWebserviceBatches();
1013 Returns a references to an array of batch id's
1014 of batch_type 'webservice' that are not imported
1018 my $PENDING_WEBSERVICE_BATCHES_QRY = <<EOQ;
1019 SELECT import_batch_id FROM import_batches
1020 WHERE batch_type = 'webservice'
1021 AND import_status = 'staged'
1023 sub GetStagedWebserviceBatches {
1024 my $dbh = C4::Context->dbh;
1025 return $dbh->selectcol_arrayref($PENDING_WEBSERVICE_BATCHES_QRY);
1028 =head2 GetImportBatchRangeDesc
1030 my $results = GetImportBatchRangeDesc($offset, $results_per_group);
1032 Returns a reference to an array of hash references corresponding to
1033 import_batches rows (sorted in descending order by import_batch_id)
1034 start at the given offset.
1038 sub GetImportBatchRangeDesc {
1039 my ($offset, $results_per_group) = @_;
1041 my $dbh = C4::Context->dbh;
1042 my $query = "SELECT b.*, p.name as profile FROM import_batches b
1043 LEFT JOIN import_batch_profiles p
1044 ON b.profile_id = p.id
1045 WHERE b.batch_type IN ('batch', 'webservice')
1046 ORDER BY b.import_batch_id DESC";
1048 if ($results_per_group){
1049 $query .= " LIMIT ?";
1050 push(@params, $results_per_group);
1053 $query .= " OFFSET ?";
1054 push(@params, $offset);
1056 my $sth = $dbh->prepare_cached($query);
1057 $sth->execute(@params);
1058 my $results = $sth->fetchall_arrayref({});
1063 =head2 GetItemNumbersFromImportBatch
1065 my @itemsnos = GetItemNumbersFromImportBatch($batch_id);
1069 sub GetItemNumbersFromImportBatch {
1070 my ($batch_id) = @_;
1071 my $dbh = C4::Context->dbh;
1073 SELECT itemnumber FROM import_items
1074 INNER JOIN items USING (itemnumber)
1075 INNER JOIN import_records USING (import_record_id)
1076 WHERE import_batch_id = ?|;
1077 my $sth = $dbh->prepare( $sql );
1078 $sth->execute($batch_id);
1080 while ( my ($itm) = $sth->fetchrow_array ) {
1086 =head2 GetNumberOfImportBatches
1088 my $count = GetNumberOfImportBatches();
1092 sub GetNumberOfNonZ3950ImportBatches {
1093 my $dbh = C4::Context->dbh;
1094 my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type != 'z3950'");
1096 my ($count) = $sth->fetchrow_array();
1101 =head2 GetImportBiblios
1103 my $results = GetImportBiblios($importid);
1107 sub GetImportBiblios {
1108 my ($import_record_id) = @_;
1110 my $dbh = C4::Context->dbh;
1111 my $query = "SELECT * FROM import_biblios WHERE import_record_id = ?";
1112 return $dbh->selectall_arrayref(
1120 =head2 GetImportRecordsRange
1122 my $results = GetImportRecordsRange($batch_id, $offset, $results_per_group);
1124 Returns a reference to an array of hash references corresponding to
1125 import_biblios/import_auths/import_records rows for a given batch
1126 starting at the given offset.
1130 sub GetImportRecordsRange {
1131 my ( $batch_id, $offset, $results_per_group, $status, $parameters ) = @_;
1133 my $dbh = C4::Context->dbh;
1135 my $order_by = $parameters->{order_by} || 'import_record_id';
1136 ( $order_by ) = grep( { $_ eq $order_by } qw( import_record_id title status overlay_status ) ) ? $order_by : 'import_record_id';
1138 my $order_by_direction =
1139 uc( $parameters->{order_by_direction} // 'ASC' ) eq 'DESC' ? 'DESC' : 'ASC';
1141 $order_by .= " $order_by_direction, authorized_heading" if $order_by eq 'title';
1143 my $query = "SELECT title, author, isbn, issn, authorized_heading, import_records.import_record_id,
1144 record_sequence, status, overlay_status,
1145 matched_biblionumber, matched_authid, record_type
1147 LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id)
1148 LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
1149 WHERE import_batch_id = ?";
1151 push(@params, $batch_id);
1153 $query .= " AND status=?";
1154 push(@params,$status);
1157 $query.=" ORDER BY $order_by $order_by_direction";
1159 if($results_per_group){
1160 $query .= " LIMIT ?";
1161 push(@params, $results_per_group);
1164 $query .= " OFFSET ?";
1165 push(@params, $offset);
1167 my $sth = $dbh->prepare_cached($query);
1168 $sth->execute(@params);
1169 my $results = $sth->fetchall_arrayref({});
1175 =head2 GetBestRecordMatch
1177 my $record_id = GetBestRecordMatch($import_record_id);
1181 sub GetBestRecordMatch {
1182 my ($import_record_id) = @_;
1184 my $dbh = C4::Context->dbh;
1185 my $sth = $dbh->prepare("SELECT candidate_match_id
1186 FROM import_record_matches
1187 JOIN import_records ON ( import_record_matches.import_record_id = import_records.import_record_id )
1188 LEFT JOIN biblio ON ( candidate_match_id = biblio.biblionumber )
1189 LEFT JOIN auth_header ON ( candidate_match_id = auth_header.authid )
1190 WHERE import_record_matches.import_record_id = ? AND
1191 ( (import_records.record_type = 'biblio' AND biblio.biblionumber IS NOT NULL) OR
1192 (import_records.record_type = 'auth' AND auth_header.authid IS NOT NULL) )
1193 ORDER BY score DESC, candidate_match_id DESC");
1194 $sth->execute($import_record_id);
1195 my ($record_id) = $sth->fetchrow_array();
1200 =head2 GetImportBatchStatus
1202 my $status = GetImportBatchStatus($batch_id);
1206 sub GetImportBatchStatus {
1207 my ($batch_id) = @_;
1209 my $dbh = C4::Context->dbh;
1210 my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE import_batch_id = ?");
1211 $sth->execute($batch_id);
1212 my ($status) = $sth->fetchrow_array();
1218 =head2 SetImportBatchStatus
1220 SetImportBatchStatus($batch_id, $new_status);
1224 sub SetImportBatchStatus {
1225 my ($batch_id, $new_status) = @_;
1227 my $dbh = C4::Context->dbh;
1228 my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
1229 $sth->execute($new_status, $batch_id);
1234 =head2 SetMatchedBiblionumber
1236 SetMatchedBiblionumber($import_record_id, $biblionumber);
1240 sub SetMatchedBiblionumber {
1241 my ($import_record_id, $biblionumber) = @_;
1243 my $dbh = C4::Context->dbh;
1245 q|UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?|,
1246 undef, $biblionumber, $import_record_id
1250 =head2 GetImportBatchOverlayAction
1252 my $overlay_action = GetImportBatchOverlayAction($batch_id);
1256 sub GetImportBatchOverlayAction {
1257 my ($batch_id) = @_;
1259 my $dbh = C4::Context->dbh;
1260 my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
1261 $sth->execute($batch_id);
1262 my ($overlay_action) = $sth->fetchrow_array();
1264 return $overlay_action;
1269 =head2 SetImportBatchOverlayAction
1271 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
1275 sub SetImportBatchOverlayAction {
1276 my ($batch_id, $new_overlay_action) = @_;
1278 my $dbh = C4::Context->dbh;
1279 my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
1280 $sth->execute($new_overlay_action, $batch_id);
1285 =head2 GetImportBatchNoMatchAction
1287 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
1291 sub GetImportBatchNoMatchAction {
1292 my ($batch_id) = @_;
1294 my $dbh = C4::Context->dbh;
1295 my $sth = $dbh->prepare("SELECT nomatch_action FROM import_batches WHERE import_batch_id = ?");
1296 $sth->execute($batch_id);
1297 my ($nomatch_action) = $sth->fetchrow_array();
1299 return $nomatch_action;
1304 =head2 SetImportBatchNoMatchAction
1306 SetImportBatchNoMatchAction($batch_id, $new_nomatch_action);
1310 sub SetImportBatchNoMatchAction {
1311 my ($batch_id, $new_nomatch_action) = @_;
1313 my $dbh = C4::Context->dbh;
1314 my $sth = $dbh->prepare("UPDATE import_batches SET nomatch_action = ? WHERE import_batch_id = ?");
1315 $sth->execute($new_nomatch_action, $batch_id);
1320 =head2 GetImportBatchItemAction
1322 my $item_action = GetImportBatchItemAction($batch_id);
1326 sub GetImportBatchItemAction {
1327 my ($batch_id) = @_;
1329 my $dbh = C4::Context->dbh;
1330 my $sth = $dbh->prepare("SELECT item_action FROM import_batches WHERE import_batch_id = ?");
1331 $sth->execute($batch_id);
1332 my ($item_action) = $sth->fetchrow_array();
1334 return $item_action;
1339 =head2 SetImportBatchItemAction
1341 SetImportBatchItemAction($batch_id, $new_item_action);
1345 sub SetImportBatchItemAction {
1346 my ($batch_id, $new_item_action) = @_;
1348 my $dbh = C4::Context->dbh;
1349 my $sth = $dbh->prepare("UPDATE import_batches SET item_action = ? WHERE import_batch_id = ?");
1350 $sth->execute($new_item_action, $batch_id);
1355 =head2 GetImportBatchMatcher
1357 my $matcher_id = GetImportBatchMatcher($batch_id);
1361 sub GetImportBatchMatcher {
1362 my ($batch_id) = @_;
1364 my $dbh = C4::Context->dbh;
1365 my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
1366 $sth->execute($batch_id);
1367 my ($matcher_id) = $sth->fetchrow_array();
1374 =head2 SetImportBatchMatcher
1376 SetImportBatchMatcher($batch_id, $new_matcher_id);
1380 sub SetImportBatchMatcher {
1381 my ($batch_id, $new_matcher_id) = @_;
1383 my $dbh = C4::Context->dbh;
1384 my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
1385 $sth->execute($new_matcher_id, $batch_id);
1390 =head2 GetImportRecordOverlayStatus
1392 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
1396 sub GetImportRecordOverlayStatus {
1397 my ($import_record_id) = @_;
1399 my $dbh = C4::Context->dbh;
1400 my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
1401 $sth->execute($import_record_id);
1402 my ($overlay_status) = $sth->fetchrow_array();
1404 return $overlay_status;
1409 =head2 SetImportRecordOverlayStatus
1411 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
1415 sub SetImportRecordOverlayStatus {
1416 my ($import_record_id, $new_overlay_status) = @_;
1418 my $dbh = C4::Context->dbh;
1419 my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
1420 $sth->execute($new_overlay_status, $import_record_id);
1425 =head2 GetImportRecordStatus
1427 my $status = GetImportRecordStatus($import_record_id);
1431 sub GetImportRecordStatus {
1432 my ($import_record_id) = @_;
1434 my $dbh = C4::Context->dbh;
1435 my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
1436 $sth->execute($import_record_id);
1437 my ($status) = $sth->fetchrow_array();
1444 =head2 SetImportRecordStatus
1446 SetImportRecordStatus($import_record_id, $new_status);
1450 sub SetImportRecordStatus {
1451 my ($import_record_id, $new_status) = @_;
1453 my $dbh = C4::Context->dbh;
1454 my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
1455 $sth->execute($new_status, $import_record_id);
1460 =head2 GetImportRecordMatches
1462 my $results = GetImportRecordMatches($import_record_id, $best_only);
1466 sub GetImportRecordMatches {
1467 my $import_record_id = shift;
1468 my $best_only = @_ ? shift : 0;
1470 my $dbh = C4::Context->dbh;
1471 # FIXME currently biblio only
1472 my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber,
1473 candidate_match_id, score, record_type
1475 JOIN import_record_matches USING (import_record_id)
1476 LEFT JOIN biblio ON (biblionumber = candidate_match_id)
1477 WHERE import_record_id = ?
1478 ORDER BY score DESC, biblionumber DESC");
1479 $sth->bind_param(1, $import_record_id);
1482 while (my $row = $sth->fetchrow_hashref) {
1483 if ($row->{'record_type'} eq 'auth') {
1484 $row->{'authorized_heading'} = C4::AuthoritiesMarc::GetAuthorizedHeading( { authid => $row->{'candidate_match_id'} } );
1486 next if ($row->{'record_type'} eq 'biblio' && not $row->{'biblionumber'});
1487 push @$results, $row;
1496 =head2 SetImportRecordMatches
1498 SetImportRecordMatches($import_record_id, @matches);
1502 sub SetImportRecordMatches {
1503 my $import_record_id = shift;
1506 my $dbh = C4::Context->dbh;
1507 my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
1508 $delsth->execute($import_record_id);
1511 my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
1513 foreach my $match (@matches) {
1514 $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
1518 =head2 RecordsFromISO2709File
1520 my ($errors, $records) = C4::ImportBatch::RecordsFromISO2709File($input_file, $record_type, $encoding);
1522 Reads ISO2709 binary porridge from the given file and creates MARC::Record-objects out of it.
1524 @PARAM1, String, absolute path to the ISO2709 file.
1525 @PARAM2, String, see stage_file.pl
1526 @PARAM3, String, should be utf8
1528 Returns two array refs.
1532 sub RecordsFromISO2709File {
1533 my ($input_file, $record_type, $encoding) = @_;
1536 my $marc_type = C4::Context->preference('marcflavour');
1537 $marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth');
1539 open my $fh, '<', $input_file or die "$0: cannot open input file $input_file: $!\n";
1545 next unless $_; # skip if record has only whitespace, as might occur
1546 # if file includes newlines between each MARC record
1547 my ($marc_record, $charset_guessed, $char_errors) = MarcToUTF8Record($_, $marc_type, $encoding);
1548 push @marc_records, $marc_record;
1549 if ($charset_guessed ne $encoding) {
1551 "Unexpected charset $charset_guessed, expecting $encoding";
1555 return ( \@errors, \@marc_records );
1558 =head2 RecordsFromMARCXMLFile
1560 my ($errors, $records) = C4::ImportBatch::RecordsFromMARCXMLFile($input_file, $encoding);
1562 Creates MARC::Record-objects out of the given MARCXML-file.
1564 @PARAM1, String, absolute path to the ISO2709 file.
1565 @PARAM2, String, should be utf8
1567 Returns two array refs.
1571 sub RecordsFromMARCXMLFile {
1572 my ( $filename, $encoding ) = @_;
1573 my $batch = MARC::File::XML->in( $filename );
1574 my ( @marcRecords, @errors, $record );
1576 eval { $record = $batch->next( $encoding ); };
1580 push @marcRecords, $record if $record;
1582 return (\@errors, \@marcRecords);
1585 =head2 RecordsFromMarcPlugin
1587 Converts text of input_file into array of MARC records with to_marc plugin
1591 sub RecordsFromMarcPlugin {
1592 my ($input_file, $plugin_class, $encoding) = @_;
1593 my ( $text, @return );
1594 return \@return if !$input_file || !$plugin_class;
1597 open my $fh, '<', $input_file or die "$0: cannot open input file $input_file: $!\n";
1607 # Convert to large MARC blob with plugin
1608 $text = Koha::Plugins::Handler->run({
1609 class => $plugin_class,
1610 method => 'to_marc',
1611 params => { data => $text },
1614 # Convert to array of MARC records
1616 my $marc_type = C4::Context->preference('marcflavour');
1617 foreach my $blob ( split(/\x1D/, $text) ) {
1618 next if $blob =~ /^\s*$/;
1619 my ($marcrecord) = MarcToUTF8Record($blob, $marc_type, $encoding);
1620 push @return, $marcrecord;
1626 # internal functions
1628 sub _create_import_record {
1629 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $marc_type) = @_;
1631 my $dbh = C4::Context->dbh;
1632 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml, marcxml_old,
1633 record_type, encoding)
1634 VALUES (?, ?, ?, ?, ?, ?, ?)");
1635 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml($marc_type), '',
1636 $record_type, $encoding);
1637 my $import_record_id = $dbh->{'mysql_insertid'};
1639 return $import_record_id;
1642 sub _update_import_record_marc {
1643 my ($import_record_id, $marc_record, $marc_type) = @_;
1645 my $dbh = C4::Context->dbh;
1646 my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
1647 WHERE import_record_id = ?");
1648 $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml($marc_type), $import_record_id);
1652 sub _add_auth_fields {
1653 my ($import_record_id, $marc_record) = @_;
1656 if ($marc_record->field('001')) {
1657 $controlnumber = $marc_record->field('001')->data();
1659 my $authorized_heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marc_record });
1660 my $dbh = C4::Context->dbh;
1661 my $sth = $dbh->prepare("INSERT INTO import_auths (import_record_id, control_number, authorized_heading) VALUES (?, ?, ?)");
1662 $sth->execute($import_record_id, $controlnumber, $authorized_heading);
1666 sub _add_biblio_fields {
1667 my ($import_record_id, $marc_record) = @_;
1669 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1670 my $dbh = C4::Context->dbh;
1671 # FIXME no controlnumber, originalsource
1672 $isbn = C4::Koha::GetNormalizedISBN($isbn);
1673 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
1674 $sth->execute($import_record_id, $title, $author, $isbn, $issn) or die $sth->errstr;
1679 sub _update_biblio_fields {
1680 my ($import_record_id, $marc_record) = @_;
1682 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1683 my $dbh = C4::Context->dbh;
1684 # FIXME no controlnumber, originalsource
1685 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1689 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
1690 WHERE import_record_id = ?");
1691 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
1695 sub _parse_biblio_fields {
1696 my ($marc_record) = @_;
1698 my $dbh = C4::Context->dbh;
1699 my $bibliofields = TransformMarcToKoha($marc_record, '');
1700 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
1704 sub _update_batch_record_counts {
1705 my ($batch_id) = @_;
1707 my $dbh = C4::Context->dbh;
1708 my $sth = $dbh->prepare_cached("UPDATE import_batches SET
1712 WHERE import_batch_id = import_batches.import_batch_id),
1716 JOIN import_items USING (import_record_id)
1717 WHERE import_batch_id = import_batches.import_batch_id
1718 AND record_type = 'biblio')
1719 WHERE import_batch_id = ?");
1720 $sth->bind_param(1, $batch_id);
1725 sub _get_commit_action {
1726 my ($overlay_action, $nomatch_action, $item_action, $overlay_status, $import_record_id, $record_type) = @_;
1728 if ($record_type eq 'biblio') {
1729 my ($bib_result, $bib_match, $item_result);
1731 if ($overlay_status ne 'no_match') {
1732 $bib_match = GetBestRecordMatch($import_record_id);
1733 if ($overlay_action eq 'replace') {
1734 $bib_result = defined($bib_match) ? 'replace' : 'create_new';
1735 } elsif ($overlay_action eq 'create_new') {
1736 $bib_result = 'create_new';
1737 } elsif ($overlay_action eq 'ignore') {
1738 $bib_result = 'ignore';
1740 if($item_action eq 'always_add' or $item_action eq 'add_only_for_matches'){
1741 $item_result = 'create_new';
1743 elsif($item_action eq 'replace'){
1744 $item_result = 'replace';
1747 $item_result = 'ignore';
1750 $bib_result = $nomatch_action;
1751 $item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_new') ? 'create_new' : 'ignore';
1753 return ($bib_result, $item_result, $bib_match);
1754 } else { # must be auths
1755 my ($auth_result, $auth_match);
1757 if ($overlay_status ne 'no_match') {
1758 $auth_match = GetBestRecordMatch($import_record_id);
1759 if ($overlay_action eq 'replace') {
1760 $auth_result = defined($auth_match) ? 'replace' : 'create_new';
1761 } elsif ($overlay_action eq 'create_new') {
1762 $auth_result = 'create_new';
1763 } elsif ($overlay_action eq 'ignore') {
1764 $auth_result = 'ignore';
1767 $auth_result = $nomatch_action;
1770 return ($auth_result, undef, $auth_match);
1775 sub _get_revert_action {
1776 my ($overlay_action, $overlay_status, $status) = @_;
1780 if ($status eq 'ignored') {
1781 $bib_result = 'ignore';
1783 if ($overlay_action eq 'create_new') {
1784 $bib_result = 'delete';
1786 $bib_result = ($overlay_status eq 'match_applied') ? 'restore' : 'delete';
1797 Koha Development Team <http://koha-community.org/>
1799 Galen Charlton <galen.charlton@liblime.com>