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 qw( AddAuthority GuessAuthTypeCode GetAuthorityXML ModAuthority DelAuthority );
36 use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate );
38 use Koha::Plugins::Handler;
41 our (@ISA, @EXPORT_OK);
53 AddItemsToImportBiblio
65 GetStagedWebserviceBatches
66 GetImportBatchRangeDesc
67 GetNumberOfNonZ3950ImportBatches
70 GetItemNumbersFromImportBatch
74 GetImportBatchOverlayAction
75 SetImportBatchOverlayAction
76 GetImportBatchNoMatchAction
77 SetImportBatchNoMatchAction
78 GetImportBatchItemAction
79 SetImportBatchItemAction
82 GetImportRecordOverlayStatus
83 SetImportRecordOverlayStatus
86 SetMatchedBiblionumber
87 GetImportRecordMatches
88 SetImportRecordMatches
90 RecordsFromMARCXMLFile
91 RecordsFromISO2709File
98 C4::ImportBatch - manage batches of imported MARC records
106 =head2 GetZ3950BatchId
108 my $batchid = GetZ3950BatchId($z3950server);
110 Retrieves the ID of the import batch for the Z39.50
111 reservoir for the given target. If necessary,
112 creates the import batch.
116 sub GetZ3950BatchId {
117 my ($z3950server) = @_;
119 my $dbh = C4::Context->dbh;
120 my $sth = $dbh->prepare("SELECT import_batch_id FROM import_batches
121 WHERE batch_type = 'z3950'
123 $sth->execute($z3950server);
124 my $rowref = $sth->fetchrow_arrayref();
126 if (defined $rowref) {
129 my $batch_id = AddImportBatch( {
130 overlay_action => 'create_new',
131 import_status => 'staged',
132 batch_type => 'z3950',
133 file_name => $z3950server,
140 =head2 GetWebserviceBatchId
142 my $batchid = GetWebserviceBatchId();
144 Retrieves the ID of the import batch for webservice.
145 If necessary, creates the import batch.
149 my $WEBSERVICE_BASE_QRY = <<EOQ;
150 SELECT import_batch_id FROM import_batches
151 WHERE batch_type = 'webservice'
152 AND import_status = 'staged'
154 sub GetWebserviceBatchId {
157 my $dbh = C4::Context->dbh;
158 my $sql = $WEBSERVICE_BASE_QRY;
160 foreach my $field (qw(matcher_id overlay_action nomatch_action item_action)) {
161 if (my $val = $params->{$field}) {
162 $sql .= " AND $field = ?";
166 my $id = $dbh->selectrow_array($sql, undef, @args);
169 $params->{batch_type} = 'webservice';
170 $params->{import_status} = 'staged';
171 return AddImportBatch($params);
174 =head2 GetImportRecordMarc
176 my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
180 sub GetImportRecordMarc {
181 my ($import_record_id) = @_;
183 my $dbh = C4::Context->dbh;
184 my ( $marc, $encoding ) = $dbh->selectrow_array(q|
185 SELECT marc, encoding
187 WHERE import_record_id = ?
188 |, undef, $import_record_id );
190 return $marc, $encoding;
193 sub EmbedItemsInImportBiblio {
194 my ( $record, $import_record_id ) = @_;
195 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
196 my $dbh = C4::Context->dbh;
197 my $import_items = $dbh->selectall_arrayref(q|
198 SELECT import_items.marcxml
200 WHERE import_record_id = ?
201 |, { Slice => {} }, $import_record_id );
203 for my $import_item ( @$import_items ) {
204 my $item_marc = MARC::Record::new_from_xml($import_item->{marcxml}, 'UTF-8');
205 push @item_fields, $item_marc->field($itemtag);
207 $record->append_fields(@item_fields);
211 =head2 AddImportBatch
213 my $batch_id = AddImportBatch($params_hash);
221 foreach (qw( matcher_id template_id branchcode
222 overlay_action nomatch_action item_action
223 import_status batch_type file_name comments record_type )) {
224 if (exists $params->{$_}) {
226 push @vals, $params->{$_};
229 my $dbh = C4::Context->dbh;
230 $dbh->do("INSERT INTO import_batches (".join( ',', @fields).")
231 VALUES (".join( ',', map '?', @fields).")",
234 return $dbh->{'mysql_insertid'};
237 =head2 GetImportBatch
239 my $row = GetImportBatch($batch_id);
241 Retrieve a hashref of an import_batches row.
248 my $dbh = C4::Context->dbh;
249 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 = ?");
250 $sth->bind_param(1, $batch_id);
252 my $result = $sth->fetchrow_hashref;
258 =head2 AddBiblioToBatch
260 my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence,
261 $marc_record, $encoding, $update_counts);
265 sub AddBiblioToBatch {
266 my $batch_id = shift;
267 my $record_sequence = shift;
268 my $marc_record = shift;
269 my $encoding = shift;
270 my $update_counts = @_ ? shift : 1;
272 my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, C4::Context->preference('marcflavour'));
273 _add_biblio_fields($import_record_id, $marc_record);
274 _update_batch_record_counts($batch_id) if $update_counts;
275 return $import_record_id;
278 =head2 ModBiblioInBatch
280 ModBiblioInBatch($import_record_id, $marc_record);
284 sub ModBiblioInBatch {
285 my ($import_record_id, $marc_record) = @_;
287 _update_import_record_marc($import_record_id, $marc_record, C4::Context->preference('marcflavour'));
288 _update_biblio_fields($import_record_id, $marc_record);
292 =head2 AddAuthToBatch
294 my $import_record_id = AddAuthToBatch($batch_id, $record_sequence,
295 $marc_record, $encoding, $update_counts, [$marc_type]);
300 my $batch_id = shift;
301 my $record_sequence = shift;
302 my $marc_record = shift;
303 my $encoding = shift;
304 my $update_counts = @_ ? shift : 1;
305 my $marc_type = shift || C4::Context->preference('marcflavour');
307 $marc_type = 'UNIMARCAUTH' if $marc_type eq 'UNIMARC';
309 my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'auth', $encoding, $marc_type);
310 _add_auth_fields($import_record_id, $marc_record);
311 _update_batch_record_counts($batch_id) if $update_counts;
312 return $import_record_id;
315 =head2 ModAuthInBatch
317 ModAuthInBatch($import_record_id, $marc_record);
322 my ($import_record_id, $marc_record) = @_;
324 my $marcflavour = C4::Context->preference('marcflavour');
325 _update_import_record_marc($import_record_id, $marc_record, $marcflavour eq 'UNIMARC' ? 'UNIMARCAUTH' : 'USMARC');
329 =head2 BatchStageMarcRecords
331 ( $batch_id, $num_records, $num_items, @invalid_records ) =
332 BatchStageMarcRecords(
333 $record_type, $encoding,
334 $marc_records, $file_name,
335 $marc_modification_template, $comments,
336 $branch_code, $parse_items,
337 $leave_as_staging, $progress_interval,
343 sub BatchStageMarcRecords {
344 my $record_type = shift;
345 my $encoding = shift;
346 my $marc_records = shift;
347 my $file_name = shift;
348 my $marc_modification_template = shift;
349 my $comments = shift;
350 my $branch_code = shift;
351 my $parse_items = shift;
352 my $leave_as_staging = shift;
354 # optional callback to monitor status
356 my $progress_interval = 0;
357 my $progress_callback = undef;
359 $progress_interval = shift;
360 $progress_callback = shift;
361 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
362 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
365 my $batch_id = AddImportBatch( {
366 overlay_action => 'create_new',
367 import_status => 'staging',
368 batch_type => 'batch',
369 file_name => $file_name,
370 comments => $comments,
371 record_type => $record_type,
374 SetImportBatchItemAction($batch_id, 'always_add');
376 SetImportBatchItemAction($batch_id, 'ignore');
380 my $marc_type = C4::Context->preference('marcflavour');
381 $marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth');
382 my @invalid_records = ();
385 # FIXME - for now, we're dealing only with bibs
387 foreach my $marc_record (@$marc_records) {
389 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
390 &$progress_callback($rec_num);
393 ModifyRecordWithTemplate( $marc_modification_template, $marc_record ) if ( $marc_modification_template );
395 my $import_record_id;
396 if (scalar($marc_record->fields()) == 0) {
397 push @invalid_records, $marc_record;
400 # Normalize the record so it doesn't have separated diacritics
401 SetUTF8Flag($marc_record);
404 if ($record_type eq 'biblio') {
405 $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $encoding, 0);
407 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0);
408 $num_items += scalar(@import_items_ids);
410 } elsif ($record_type eq 'auth') {
411 $import_record_id = AddAuthToBatch($batch_id, $rec_num, $marc_record, $encoding, 0, $marc_type);
415 unless ($leave_as_staging) {
416 SetImportBatchStatus($batch_id, 'staged');
418 # FIXME branch_code, number of bibs, number of items
419 _update_batch_record_counts($batch_id);
420 return ($batch_id, $num_valid, $num_items, @invalid_records);
423 =head2 AddItemsToImportBiblio
425 my @import_items_ids = AddItemsToImportBiblio($batch_id,
426 $import_record_id, $marc_record, $update_counts);
430 sub AddItemsToImportBiblio {
431 my $batch_id = shift;
432 my $import_record_id = shift;
433 my $marc_record = shift;
434 my $update_counts = @_ ? shift : 0;
436 my @import_items_ids = ();
438 my $dbh = C4::Context->dbh;
439 my ($item_tag,$item_subfield) = &GetMarcFromKohaField( "items.itemnumber" );
440 foreach my $item_field ($marc_record->field($item_tag)) {
441 my $item_marc = MARC::Record->new();
442 $item_marc->leader("00000 a "); # must set Leader/09 to 'a'
443 $item_marc->append_fields($item_field);
444 $marc_record->delete_field($item_field);
445 my $sth = $dbh->prepare_cached("INSERT INTO import_items (import_record_id, status, marcxml)
447 $sth->bind_param(1, $import_record_id);
448 $sth->bind_param(2, 'staged');
449 $sth->bind_param(3, $item_marc->as_xml("USMARC"));
451 push @import_items_ids, $dbh->{'mysql_insertid'};
455 if ($#import_items_ids > -1) {
456 _update_batch_record_counts($batch_id) if $update_counts;
458 return @import_items_ids;
461 =head2 BatchFindDuplicates
463 my $num_with_matches = BatchFindDuplicates($batch_id, $matcher,
464 $max_matches, $progress_interval, $progress_callback);
466 Goes through the records loaded in the batch and attempts to
467 find duplicates for each one. Sets the matching status
468 of each record to "no_match" or "auto_match" as appropriate.
470 The $max_matches parameter is optional; if it is not supplied,
473 The $progress_interval and $progress_callback parameters are
474 optional; if both are supplied, the sub referred to by
475 $progress_callback will be invoked every $progress_interval
476 records using the number of records processed as the
481 sub BatchFindDuplicates {
482 my $batch_id = shift;
484 my $max_matches = @_ ? shift : 10;
486 # optional callback to monitor status
488 my $progress_interval = 0;
489 my $progress_callback = undef;
491 $progress_interval = shift;
492 $progress_callback = shift;
493 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
494 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
497 my $dbh = C4::Context->dbh;
499 my $sth = $dbh->prepare("SELECT import_record_id, record_type, marc
501 WHERE import_batch_id = ?");
502 $sth->execute($batch_id);
503 my $num_with_matches = 0;
505 while (my $rowref = $sth->fetchrow_hashref) {
507 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
508 &$progress_callback($rec_num);
510 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
512 if (defined $matcher) {
513 @matches = $matcher->get_matches($marc_record, $max_matches);
515 if (scalar(@matches) > 0) {
517 SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
518 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
520 SetImportRecordMatches($rowref->{'import_record_id'}, ());
521 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
525 return $num_with_matches;
528 =head2 BatchCommitRecords
530 my ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored) =
531 BatchCommitRecords($batch_id, $framework,
532 $progress_interval, $progress_callback);
536 sub BatchCommitRecords {
537 my $batch_id = shift;
538 my $framework = shift;
540 # optional callback to monitor status
542 my $progress_interval = 0;
543 my $progress_callback = undef;
545 $progress_interval = shift;
546 $progress_callback = shift;
547 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
548 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
554 my $num_items_added = 0;
555 my $num_items_replaced = 0;
556 my $num_items_errored = 0;
558 # commit (i.e., save, all records in the batch)
559 SetImportBatchStatus($batch_id, 'importing');
560 my $overlay_action = GetImportBatchOverlayAction($batch_id);
561 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
562 my $item_action = GetImportBatchItemAction($batch_id);
565 my $dbh = C4::Context->dbh;
566 my $sth = $dbh->prepare("SELECT import_records.import_record_id, record_type, status, overlay_status, marc, encoding
568 LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id)
569 LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
570 WHERE import_batch_id = ?");
571 $sth->execute($batch_id);
572 my $marcflavour = C4::Context->preference('marcflavour');
574 my $userenv = C4::Context->userenv;
575 my $logged_in_patron = Koha::Patrons->find( $userenv->{number} );
578 while (my $rowref = $sth->fetchrow_hashref) {
579 $record_type = $rowref->{'record_type'};
581 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
582 &$progress_callback($rec_num);
584 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
590 if ($marcflavour eq 'UNIMARC' && $record_type eq 'auth') {
591 $marc_type = 'UNIMARCAUTH';
592 } elsif ($marcflavour eq 'UNIMARC') {
593 $marc_type = 'UNIMARC';
595 $marc_type = 'USMARC';
597 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
599 if ($record_type eq 'biblio') {
600 # remove any item tags - rely on BatchCommitItems
601 ($item_tag,$item_subfield) = &GetMarcFromKohaField( "items.itemnumber" );
602 foreach my $item_field ($marc_record->field($item_tag)) {
603 $marc_record->delete_field($item_field);
607 my ($record_result, $item_result, $record_match) =
608 _get_commit_action($overlay_action, $nomatch_action, $item_action,
609 $rowref->{'overlay_status'}, $rowref->{'import_record_id'}, $record_type);
613 if ($record_result eq 'create_new') {
615 if ($record_type eq 'biblio') {
616 my $biblioitemnumber;
617 ($recordid, $biblioitemnumber) = AddBiblio($marc_record, $framework);
618 $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; # FIXME call SetMatchedBiblionumber instead
619 if ($item_result eq 'create_new' || $item_result eq 'replace') {
620 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
621 $num_items_added += $bib_items_added;
622 $num_items_replaced += $bib_items_replaced;
623 $num_items_errored += $bib_items_errored;
626 $recordid = AddAuthority($marc_record, undef, GuessAuthTypeCode($marc_record));
627 $query = "UPDATE import_auths SET matched_authid = ? WHERE import_record_id = ?";
629 my $sth = $dbh->prepare_cached($query);
630 $sth->execute($recordid, $rowref->{'import_record_id'});
632 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
633 } elsif ($record_result eq 'replace') {
635 $recordid = $record_match;
637 if ($record_type eq 'biblio') {
638 my $oldbiblio = Koha::Biblios->find( $recordid );
639 $oldxml = GetXmlBiblio($recordid);
641 # remove item fields so that they don't get
642 # added again if record is reverted
643 # 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.
644 my $old_marc = MARC::Record->new_from_xml(StripNonXmlChars($oldxml), 'UTF-8', $rowref->{'encoding'}, $marc_type);
645 foreach my $item_field ($old_marc->field($item_tag)) {
646 $old_marc->delete_field($item_field);
648 $oldxml = $old_marc->as_xml($marc_type);
650 ModBiblio($marc_record, $recordid, $oldbiblio->frameworkcode, {
652 source => 'batchimport',
653 categorycode => $logged_in_patron->categorycode,
654 userid => $logged_in_patron->userid
657 $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; # FIXME call SetMatchedBiblionumber instead
659 if ($item_result eq 'create_new' || $item_result eq 'replace') {
660 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
661 $num_items_added += $bib_items_added;
662 $num_items_replaced += $bib_items_replaced;
663 $num_items_errored += $bib_items_errored;
666 $oldxml = GetAuthorityXML($recordid);
668 ModAuthority($recordid, $marc_record, GuessAuthTypeCode($marc_record));
669 $query = "UPDATE import_auths SET matched_authid = ? WHERE import_record_id = ?";
671 my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
672 $sth->execute($oldxml, $rowref->{'import_record_id'});
674 my $sth2 = $dbh->prepare_cached($query);
675 $sth2->execute($recordid, $rowref->{'import_record_id'});
677 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
678 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
679 } elsif ($record_result eq 'ignore') {
680 $recordid = $record_match;
682 $recordid = $record_match;
683 if ($record_type eq 'biblio' and defined $recordid and ( $item_result eq 'create_new' || $item_result eq 'replace' ) ) {
684 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
685 $num_items_added += $bib_items_added;
686 $num_items_replaced += $bib_items_replaced;
687 $num_items_errored += $bib_items_errored;
688 # still need to record the matched biblionumber so that the
689 # items can be reverted
690 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"); # FIXME call SetMatchedBiblionumber instead
691 $sth2->execute($recordid, $rowref->{'import_record_id'});
692 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
694 SetImportRecordStatus($rowref->{'import_record_id'}, 'ignored');
698 SetImportBatchStatus($batch_id, 'imported');
699 return ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored);
702 =head2 BatchCommitItems
704 ($num_items_added, $num_items_errored) =
705 BatchCommitItems($import_record_id, $biblionumber);
709 sub BatchCommitItems {
710 my ( $import_record_id, $biblionumber, $action ) = @_;
712 my $dbh = C4::Context->dbh;
714 my $num_items_added = 0;
715 my $num_items_errored = 0;
716 my $num_items_replaced = 0;
718 my $sth = $dbh->prepare( "
719 SELECT import_items_id, import_items.marcxml, encoding
721 JOIN import_records USING (import_record_id)
722 WHERE import_record_id = ?
723 ORDER BY import_items_id
725 $sth->bind_param( 1, $import_record_id );
728 while ( my $row = $sth->fetchrow_hashref() ) {
729 my $item_marc = MARC::Record->new_from_xml( StripNonXmlChars( $row->{'marcxml'} ), 'UTF-8', $row->{'encoding'} );
731 # Delete date_due subfield as to not accidentally delete item checkout due dates
732 my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.onloan' );
733 $item_marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
735 my $item = TransformMarcToKoha( $item_marc );
737 my $duplicate_barcode = exists( $item->{'barcode'} ) && Koha::Items->find({ barcode => $item->{'barcode'} });
738 my $duplicate_itemnumber = exists( $item->{'itemnumber'} );
740 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ?, import_error = ? WHERE import_items_id = ?");
741 if ( $action eq "replace" && $duplicate_itemnumber ) {
742 # Duplicate itemnumbers have precedence, that way we can update barcodes by overlaying
743 ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber} );
744 $updsth->bind_param( 1, 'imported' );
745 $updsth->bind_param( 2, $item->{itemnumber} );
746 $updsth->bind_param( 3, undef );
747 $updsth->bind_param( 4, $row->{'import_items_id'} );
750 $num_items_replaced++;
751 } elsif ( $action eq "replace" && $duplicate_barcode ) {
752 my $itemnumber = $duplicate_barcode->itemnumber;
753 ModItemFromMarc( $item_marc, $biblionumber, $itemnumber );
754 $updsth->bind_param( 1, 'imported' );
755 $updsth->bind_param( 2, $item->{itemnumber} );
756 $updsth->bind_param( 3, undef );
757 $updsth->bind_param( 4, $row->{'import_items_id'} );
760 $num_items_replaced++;
761 } elsif ($duplicate_barcode) {
762 $updsth->bind_param( 1, 'error' );
763 $updsth->bind_param( 2, undef );
764 $updsth->bind_param( 3, 'duplicate item barcode' );
765 $updsth->bind_param( 4, $row->{'import_items_id'} );
767 $num_items_errored++;
769 # Remove the itemnumber if it exists, we want to create a new item
770 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
771 $item_marc->field($itemtag)->delete_subfield( code => $itemsubfield );
773 my ( $item_biblionumber, $biblioitemnumber, $itemnumber ) = AddItemFromMarc( $item_marc, $biblionumber );
775 $updsth->bind_param( 1, 'imported' );
776 $updsth->bind_param( 2, $itemnumber );
777 $updsth->bind_param( 3, undef );
778 $updsth->bind_param( 4, $row->{'import_items_id'} );
786 return ( $num_items_added, $num_items_replaced, $num_items_errored );
789 =head2 BatchRevertRecords
791 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted,
792 $num_ignored) = BatchRevertRecords($batch_id);
796 sub BatchRevertRecords {
797 my $batch_id = shift;
799 my $logger = Koha::Logger->get( { category => 'C4.ImportBatch' } );
801 $logger->trace("C4::ImportBatch::BatchRevertRecords( $batch_id )");
806 my $num_reverted = 0;
808 my $num_items_deleted = 0;
809 # commit (i.e., save, all records in the batch)
810 SetImportBatchStatus($batch_id, 'reverting');
811 my $overlay_action = GetImportBatchOverlayAction($batch_id);
812 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
813 my $dbh = C4::Context->dbh;
814 my $sth = $dbh->prepare("SELECT import_records.import_record_id, record_type, status, overlay_status, marcxml_old, encoding, matched_biblionumber, matched_authid
816 LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id)
817 LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
818 WHERE import_batch_id = ?");
819 $sth->execute($batch_id);
821 my $marcflavour = C4::Context->preference('marcflavour');
822 while (my $rowref = $sth->fetchrow_hashref) {
823 $record_type = $rowref->{'record_type'};
824 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
828 if ($marcflavour eq 'UNIMARC' && $record_type eq 'auth') {
829 $marc_type = 'UNIMARCAUTH';
830 } elsif ($marcflavour eq 'UNIMARC') {
831 $marc_type = 'UNIMARC';
833 $marc_type = 'USMARC';
836 my $record_result = _get_revert_action($overlay_action, $rowref->{'overlay_status'}, $rowref->{'status'});
838 if ($record_result eq 'delete') {
840 if ($record_type eq 'biblio') {
841 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
842 $error = DelBiblio($rowref->{'matched_biblionumber'});
844 DelAuthority({ authid => $rowref->{'matched_authid'} });
846 if (defined $error) {
850 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
852 } elsif ($record_result eq 'restore') {
854 my $old_record = MARC::Record->new_from_xml(StripNonXmlChars($rowref->{'marcxml_old'}), 'UTF-8', $rowref->{'encoding'}, $marc_type);
855 if ($record_type eq 'biblio') {
856 my $biblionumber = $rowref->{'matched_biblionumber'};
857 my $oldbiblio = Koha::Biblios->find( $biblionumber );
859 $logger->info("C4::ImportBatch::BatchRevertRecords: Biblio record $biblionumber does not exist, restoration of this record was skipped") unless $oldbiblio;
860 next unless $oldbiblio; # Record has since been deleted. Deleted records should stay deleted.
862 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
863 ModBiblio($old_record, $biblionumber, $oldbiblio->frameworkcode);
865 my $authid = $rowref->{'matched_authid'};
866 ModAuthority($authid, $old_record, GuessAuthTypeCode($old_record));
868 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
869 } elsif ($record_result eq 'ignore') {
870 if ($record_type eq 'biblio') {
871 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
873 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
876 if ($record_type eq 'biblio') {
877 # remove matched_biblionumber only if there is no 'imported' item left
878 $query = "UPDATE import_biblios SET matched_biblionumber = NULL WHERE import_record_id = ?"; # FIXME Remove me
879 $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')";
881 $query = "UPDATE import_auths SET matched_authid = NULL WHERE import_record_id = ?";
883 my $sth2 = $dbh->prepare_cached($query);
884 $sth2->execute($rowref->{'import_record_id'});
888 SetImportBatchStatus($batch_id, 'reverted');
889 return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored);
892 =head2 BatchRevertItems
894 my $num_items_deleted = BatchRevertItems($import_record_id, $biblionumber);
898 sub BatchRevertItems {
899 my ($import_record_id, $biblionumber) = @_;
901 my $dbh = C4::Context->dbh;
902 my $num_items_deleted = 0;
904 my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
906 JOIN items USING (itemnumber)
907 WHERE import_record_id = ?");
908 $sth->bind_param(1, $import_record_id);
910 while (my $row = $sth->fetchrow_hashref()) {
911 my $item = Koha::Items->find($row->{itemnumber});
912 my $error = $item->safe_delete;
914 my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?");
915 $updsth->bind_param(1, 'reverted');
916 $updsth->bind_param(2, $row->{'import_items_id'});
919 $num_items_deleted++;
926 return $num_items_deleted;
931 CleanBatch($batch_id)
933 Deletes all staged records from the import batch
934 and sets the status of the batch to 'cleaned'. Note
935 that deleting a stage record does *not* affect
936 any record that has been committed to the database.
941 my $batch_id = shift;
942 return unless defined $batch_id;
944 C4::Context->dbh->do('DELETE FROM import_records WHERE import_batch_id = ?', {}, $batch_id);
945 SetImportBatchStatus($batch_id, 'cleaned');
950 DeleteBatch($batch_id)
952 Deletes the record from the database. This can only be done
953 once the batch has been cleaned.
958 my $batch_id = shift;
959 return unless defined $batch_id;
961 my $dbh = C4::Context->dbh;
962 my $sth = $dbh->prepare('DELETE FROM import_batches WHERE import_batch_id = ?');
963 $sth->execute( $batch_id );
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 b.*, p.name as profile FROM import_batches b
1025 LEFT JOIN import_batch_profiles p
1026 ON b.profile_id = p.id
1027 WHERE b.batch_type IN ('batch', 'webservice')
1028 ORDER BY b.import_batch_id DESC";
1030 if ($results_per_group){
1031 $query .= " LIMIT ?";
1032 push(@params, $results_per_group);
1035 $query .= " OFFSET ?";
1036 push(@params, $offset);
1038 my $sth = $dbh->prepare_cached($query);
1039 $sth->execute(@params);
1040 my $results = $sth->fetchall_arrayref({});
1045 =head2 GetItemNumbersFromImportBatch
1047 my @itemsnos = GetItemNumbersFromImportBatch($batch_id);
1051 sub GetItemNumbersFromImportBatch {
1052 my ($batch_id) = @_;
1053 my $dbh = C4::Context->dbh;
1055 SELECT itemnumber FROM import_items
1056 INNER JOIN items USING (itemnumber)
1057 INNER JOIN import_records USING (import_record_id)
1058 WHERE import_batch_id = ?|;
1059 my $sth = $dbh->prepare( $sql );
1060 $sth->execute($batch_id);
1062 while ( my ($itm) = $sth->fetchrow_array ) {
1068 =head2 GetNumberOfImportBatches
1070 my $count = GetNumberOfImportBatches();
1074 sub GetNumberOfNonZ3950ImportBatches {
1075 my $dbh = C4::Context->dbh;
1076 my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type != 'z3950'");
1078 my ($count) = $sth->fetchrow_array();
1083 =head2 GetImportBiblios
1085 my $results = GetImportBiblios($importid);
1089 sub GetImportBiblios {
1090 my ($import_record_id) = @_;
1092 my $dbh = C4::Context->dbh;
1093 my $query = "SELECT * FROM import_biblios WHERE import_record_id = ?";
1094 return $dbh->selectall_arrayref(
1102 =head2 GetImportRecordsRange
1104 my $results = GetImportRecordsRange($batch_id, $offset, $results_per_group);
1106 Returns a reference to an array of hash references corresponding to
1107 import_biblios/import_auths/import_records rows for a given batch
1108 starting at the given offset.
1112 sub GetImportRecordsRange {
1113 my ( $batch_id, $offset, $results_per_group, $status, $parameters ) = @_;
1115 my $dbh = C4::Context->dbh;
1117 my $order_by = $parameters->{order_by} || 'import_record_id';
1118 ( $order_by ) = grep( { $_ eq $order_by } qw( import_record_id title status overlay_status ) ) ? $order_by : 'import_record_id';
1120 my $order_by_direction =
1121 uc( $parameters->{order_by_direction} // 'ASC' ) eq 'DESC' ? 'DESC' : 'ASC';
1123 $order_by .= " $order_by_direction, authorized_heading" if $order_by eq 'title';
1125 my $query = "SELECT title, author, isbn, issn, authorized_heading, import_records.import_record_id,
1126 record_sequence, status, overlay_status,
1127 matched_biblionumber, matched_authid, record_type
1129 LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id)
1130 LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
1131 WHERE import_batch_id = ?";
1133 push(@params, $batch_id);
1135 $query .= " AND status=?";
1136 push(@params,$status);
1139 $query.=" ORDER BY $order_by $order_by_direction";
1141 if($results_per_group){
1142 $query .= " LIMIT ?";
1143 push(@params, $results_per_group);
1146 $query .= " OFFSET ?";
1147 push(@params, $offset);
1149 my $sth = $dbh->prepare_cached($query);
1150 $sth->execute(@params);
1151 my $results = $sth->fetchall_arrayref({});
1157 =head2 GetBestRecordMatch
1159 my $record_id = GetBestRecordMatch($import_record_id);
1163 sub GetBestRecordMatch {
1164 my ($import_record_id) = @_;
1166 my $dbh = C4::Context->dbh;
1167 my $sth = $dbh->prepare("SELECT candidate_match_id
1168 FROM import_record_matches
1169 JOIN import_records ON ( import_record_matches.import_record_id = import_records.import_record_id )
1170 LEFT JOIN biblio ON ( candidate_match_id = biblio.biblionumber )
1171 LEFT JOIN auth_header ON ( candidate_match_id = auth_header.authid )
1172 WHERE import_record_matches.import_record_id = ? AND
1173 ( (import_records.record_type = 'biblio' AND biblio.biblionumber IS NOT NULL) OR
1174 (import_records.record_type = 'auth' AND auth_header.authid IS NOT NULL) )
1175 ORDER BY score DESC, candidate_match_id DESC");
1176 $sth->execute($import_record_id);
1177 my ($record_id) = $sth->fetchrow_array();
1182 =head2 GetImportBatchStatus
1184 my $status = GetImportBatchStatus($batch_id);
1188 sub GetImportBatchStatus {
1189 my ($batch_id) = @_;
1191 my $dbh = C4::Context->dbh;
1192 my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE import_batch_id = ?");
1193 $sth->execute($batch_id);
1194 my ($status) = $sth->fetchrow_array();
1200 =head2 SetImportBatchStatus
1202 SetImportBatchStatus($batch_id, $new_status);
1206 sub SetImportBatchStatus {
1207 my ($batch_id, $new_status) = @_;
1209 my $dbh = C4::Context->dbh;
1210 my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
1211 $sth->execute($new_status, $batch_id);
1216 =head2 SetMatchedBiblionumber
1218 SetMatchedBiblionumber($import_record_id, $biblionumber);
1222 sub SetMatchedBiblionumber {
1223 my ($import_record_id, $biblionumber) = @_;
1225 my $dbh = C4::Context->dbh;
1227 q|UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?|,
1228 undef, $biblionumber, $import_record_id
1232 =head2 GetImportBatchOverlayAction
1234 my $overlay_action = GetImportBatchOverlayAction($batch_id);
1238 sub GetImportBatchOverlayAction {
1239 my ($batch_id) = @_;
1241 my $dbh = C4::Context->dbh;
1242 my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
1243 $sth->execute($batch_id);
1244 my ($overlay_action) = $sth->fetchrow_array();
1246 return $overlay_action;
1251 =head2 SetImportBatchOverlayAction
1253 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
1257 sub SetImportBatchOverlayAction {
1258 my ($batch_id, $new_overlay_action) = @_;
1260 my $dbh = C4::Context->dbh;
1261 my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
1262 $sth->execute($new_overlay_action, $batch_id);
1267 =head2 GetImportBatchNoMatchAction
1269 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
1273 sub GetImportBatchNoMatchAction {
1274 my ($batch_id) = @_;
1276 my $dbh = C4::Context->dbh;
1277 my $sth = $dbh->prepare("SELECT nomatch_action FROM import_batches WHERE import_batch_id = ?");
1278 $sth->execute($batch_id);
1279 my ($nomatch_action) = $sth->fetchrow_array();
1281 return $nomatch_action;
1286 =head2 SetImportBatchNoMatchAction
1288 SetImportBatchNoMatchAction($batch_id, $new_nomatch_action);
1292 sub SetImportBatchNoMatchAction {
1293 my ($batch_id, $new_nomatch_action) = @_;
1295 my $dbh = C4::Context->dbh;
1296 my $sth = $dbh->prepare("UPDATE import_batches SET nomatch_action = ? WHERE import_batch_id = ?");
1297 $sth->execute($new_nomatch_action, $batch_id);
1302 =head2 GetImportBatchItemAction
1304 my $item_action = GetImportBatchItemAction($batch_id);
1308 sub GetImportBatchItemAction {
1309 my ($batch_id) = @_;
1311 my $dbh = C4::Context->dbh;
1312 my $sth = $dbh->prepare("SELECT item_action FROM import_batches WHERE import_batch_id = ?");
1313 $sth->execute($batch_id);
1314 my ($item_action) = $sth->fetchrow_array();
1316 return $item_action;
1321 =head2 SetImportBatchItemAction
1323 SetImportBatchItemAction($batch_id, $new_item_action);
1327 sub SetImportBatchItemAction {
1328 my ($batch_id, $new_item_action) = @_;
1330 my $dbh = C4::Context->dbh;
1331 my $sth = $dbh->prepare("UPDATE import_batches SET item_action = ? WHERE import_batch_id = ?");
1332 $sth->execute($new_item_action, $batch_id);
1337 =head2 GetImportBatchMatcher
1339 my $matcher_id = GetImportBatchMatcher($batch_id);
1343 sub GetImportBatchMatcher {
1344 my ($batch_id) = @_;
1346 my $dbh = C4::Context->dbh;
1347 my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
1348 $sth->execute($batch_id);
1349 my ($matcher_id) = $sth->fetchrow_array();
1356 =head2 SetImportBatchMatcher
1358 SetImportBatchMatcher($batch_id, $new_matcher_id);
1362 sub SetImportBatchMatcher {
1363 my ($batch_id, $new_matcher_id) = @_;
1365 my $dbh = C4::Context->dbh;
1366 my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
1367 $sth->execute($new_matcher_id, $batch_id);
1372 =head2 GetImportRecordOverlayStatus
1374 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
1378 sub GetImportRecordOverlayStatus {
1379 my ($import_record_id) = @_;
1381 my $dbh = C4::Context->dbh;
1382 my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
1383 $sth->execute($import_record_id);
1384 my ($overlay_status) = $sth->fetchrow_array();
1386 return $overlay_status;
1391 =head2 SetImportRecordOverlayStatus
1393 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
1397 sub SetImportRecordOverlayStatus {
1398 my ($import_record_id, $new_overlay_status) = @_;
1400 my $dbh = C4::Context->dbh;
1401 my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
1402 $sth->execute($new_overlay_status, $import_record_id);
1407 =head2 GetImportRecordStatus
1409 my $status = GetImportRecordStatus($import_record_id);
1413 sub GetImportRecordStatus {
1414 my ($import_record_id) = @_;
1416 my $dbh = C4::Context->dbh;
1417 my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
1418 $sth->execute($import_record_id);
1419 my ($status) = $sth->fetchrow_array();
1426 =head2 SetImportRecordStatus
1428 SetImportRecordStatus($import_record_id, $new_status);
1432 sub SetImportRecordStatus {
1433 my ($import_record_id, $new_status) = @_;
1435 my $dbh = C4::Context->dbh;
1436 my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
1437 $sth->execute($new_status, $import_record_id);
1442 =head2 GetImportRecordMatches
1444 my $results = GetImportRecordMatches($import_record_id, $best_only);
1448 sub GetImportRecordMatches {
1449 my $import_record_id = shift;
1450 my $best_only = @_ ? shift : 0;
1452 my $dbh = C4::Context->dbh;
1453 # FIXME currently biblio only
1454 my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber,
1455 candidate_match_id, score, record_type
1457 JOIN import_record_matches USING (import_record_id)
1458 LEFT JOIN biblio ON (biblionumber = candidate_match_id)
1459 WHERE import_record_id = ?
1460 ORDER BY score DESC, biblionumber DESC");
1461 $sth->bind_param(1, $import_record_id);
1464 while (my $row = $sth->fetchrow_hashref) {
1465 if ($row->{'record_type'} eq 'auth') {
1466 $row->{'authorized_heading'} = C4::AuthoritiesMarc::GetAuthorizedHeading( { authid => $row->{'candidate_match_id'} } );
1468 next if ($row->{'record_type'} eq 'biblio' && not $row->{'biblionumber'});
1469 push @$results, $row;
1478 =head2 SetImportRecordMatches
1480 SetImportRecordMatches($import_record_id, @matches);
1484 sub SetImportRecordMatches {
1485 my $import_record_id = shift;
1488 my $dbh = C4::Context->dbh;
1489 my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
1490 $delsth->execute($import_record_id);
1493 my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
1495 foreach my $match (@matches) {
1496 $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
1500 =head2 RecordsFromISO2709File
1502 my ($errors, $records) = C4::ImportBatch::RecordsFromISO2709File($input_file, $record_type, $encoding);
1504 Reads ISO2709 binary porridge from the given file and creates MARC::Record-objects out of it.
1506 @PARAM1, String, absolute path to the ISO2709 file.
1507 @PARAM2, String, see stage_file.pl
1508 @PARAM3, String, should be utf8
1510 Returns two array refs.
1514 sub RecordsFromISO2709File {
1515 my ($input_file, $record_type, $encoding) = @_;
1518 my $marc_type = C4::Context->preference('marcflavour');
1519 $marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth');
1521 open my $fh, '<', $input_file or die "$0: cannot open input file $input_file: $!\n";
1527 next unless $_; # skip if record has only whitespace, as might occur
1528 # if file includes newlines between each MARC record
1529 my ($marc_record, $charset_guessed, $char_errors) = MarcToUTF8Record($_, $marc_type, $encoding);
1530 push @marc_records, $marc_record;
1531 if ($charset_guessed ne $encoding) {
1533 "Unexpected charset $charset_guessed, expecting $encoding";
1537 return ( \@errors, \@marc_records );
1540 =head2 RecordsFromMARCXMLFile
1542 my ($errors, $records) = C4::ImportBatch::RecordsFromMARCXMLFile($input_file, $encoding);
1544 Creates MARC::Record-objects out of the given MARCXML-file.
1546 @PARAM1, String, absolute path to the ISO2709 file.
1547 @PARAM2, String, should be utf8
1549 Returns two array refs.
1553 sub RecordsFromMARCXMLFile {
1554 my ( $filename, $encoding ) = @_;
1555 my $batch = MARC::File::XML->in( $filename );
1556 my ( @marcRecords, @errors, $record );
1558 eval { $record = $batch->next( $encoding ); };
1562 push @marcRecords, $record if $record;
1564 return (\@errors, \@marcRecords);
1567 =head2 RecordsFromMarcPlugin
1569 Converts text of input_file into array of MARC records with to_marc plugin
1573 sub RecordsFromMarcPlugin {
1574 my ($input_file, $plugin_class, $encoding) = @_;
1575 my ( $text, @return );
1576 return \@return if !$input_file || !$plugin_class;
1579 open my $fh, '<', $input_file or die "$0: cannot open input file $input_file: $!\n";
1589 # Convert to large MARC blob with plugin
1590 $text = Koha::Plugins::Handler->run({
1591 class => $plugin_class,
1592 method => 'to_marc',
1593 params => { data => $text },
1596 # Convert to array of MARC records
1598 my $marc_type = C4::Context->preference('marcflavour');
1599 foreach my $blob ( split(/\x1D/, $text) ) {
1600 next if $blob =~ /^\s*$/;
1601 my ($marcrecord) = MarcToUTF8Record($blob, $marc_type, $encoding);
1602 push @return, $marcrecord;
1608 # internal functions
1610 sub _create_import_record {
1611 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $marc_type) = @_;
1613 my $dbh = C4::Context->dbh;
1614 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml, marcxml_old,
1615 record_type, encoding)
1616 VALUES (?, ?, ?, ?, ?, ?, ?)");
1617 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml($marc_type), '',
1618 $record_type, $encoding);
1619 my $import_record_id = $dbh->{'mysql_insertid'};
1621 return $import_record_id;
1624 sub _add_auth_fields {
1625 my ($import_record_id, $marc_record) = @_;
1628 if ($marc_record->field('001')) {
1629 $controlnumber = $marc_record->field('001')->data();
1631 my $authorized_heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marc_record });
1632 my $dbh = C4::Context->dbh;
1633 my $sth = $dbh->prepare("INSERT INTO import_auths (import_record_id, control_number, authorized_heading) VALUES (?, ?, ?)");
1634 $sth->execute($import_record_id, $controlnumber, $authorized_heading);
1638 sub _add_biblio_fields {
1639 my ($import_record_id, $marc_record) = @_;
1641 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1642 my $dbh = C4::Context->dbh;
1643 # FIXME no controlnumber, originalsource
1644 $isbn = C4::Koha::GetNormalizedISBN($isbn);
1645 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
1646 $sth->execute($import_record_id, $title, $author, $isbn, $issn) or die $sth->errstr;
1651 sub _update_biblio_fields {
1652 my ($import_record_id, $marc_record) = @_;
1654 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1655 my $dbh = C4::Context->dbh;
1656 # FIXME no controlnumber, originalsource
1657 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1661 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
1662 WHERE import_record_id = ?");
1663 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
1667 sub _parse_biblio_fields {
1668 my ($marc_record) = @_;
1670 my $dbh = C4::Context->dbh;
1671 my $bibliofields = TransformMarcToKoha($marc_record, '');
1672 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
1676 sub _update_batch_record_counts {
1677 my ($batch_id) = @_;
1679 my $dbh = C4::Context->dbh;
1680 my $sth = $dbh->prepare_cached("UPDATE import_batches SET
1684 WHERE import_batch_id = import_batches.import_batch_id),
1688 JOIN import_items USING (import_record_id)
1689 WHERE import_batch_id = import_batches.import_batch_id
1690 AND record_type = 'biblio')
1691 WHERE import_batch_id = ?");
1692 $sth->bind_param(1, $batch_id);
1697 sub _get_commit_action {
1698 my ($overlay_action, $nomatch_action, $item_action, $overlay_status, $import_record_id, $record_type) = @_;
1700 if ($record_type eq 'biblio') {
1701 my ($bib_result, $bib_match, $item_result);
1703 if ($overlay_status ne 'no_match') {
1704 $bib_match = GetBestRecordMatch($import_record_id);
1705 if ($overlay_action eq 'replace') {
1706 $bib_result = defined($bib_match) ? 'replace' : 'create_new';
1707 } elsif ($overlay_action eq 'create_new') {
1708 $bib_result = 'create_new';
1709 } elsif ($overlay_action eq 'ignore') {
1710 $bib_result = 'ignore';
1712 if($item_action eq 'always_add' or $item_action eq 'add_only_for_matches'){
1713 $item_result = 'create_new';
1715 elsif($item_action eq 'replace'){
1716 $item_result = 'replace';
1719 $item_result = 'ignore';
1722 $bib_result = $nomatch_action;
1723 $item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_new') ? 'create_new' : 'ignore';
1725 return ($bib_result, $item_result, $bib_match);
1726 } else { # must be auths
1727 my ($auth_result, $auth_match);
1729 if ($overlay_status ne 'no_match') {
1730 $auth_match = GetBestRecordMatch($import_record_id);
1731 if ($overlay_action eq 'replace') {
1732 $auth_result = defined($auth_match) ? 'replace' : 'create_new';
1733 } elsif ($overlay_action eq 'create_new') {
1734 $auth_result = 'create_new';
1735 } elsif ($overlay_action eq 'ignore') {
1736 $auth_result = 'ignore';
1739 $auth_result = $nomatch_action;
1742 return ($auth_result, undef, $auth_match);
1747 sub _get_revert_action {
1748 my ($overlay_action, $overlay_status, $status) = @_;
1752 if ($status eq 'ignored') {
1753 $bib_result = 'ignore';
1755 if ($overlay_action eq 'create_new') {
1756 $bib_result = 'delete';
1758 $bib_result = ($overlay_status eq 'match_applied') ? 'restore' : 'delete';
1769 Koha Development Team <http://koha-community.org/>
1771 Galen Charlton <galen.charlton@liblime.com>