1 package C4
::ImportBatch
;
3 # Copyright (C) 2007 LibLime, 2012 C & P Bibliography Services
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 use C4
::AuthoritiesMarc
;
29 use C4
::MarcModificationTemplates
;
30 use Koha
::Plugins
::Handler
;
33 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
36 # set the version for version checking
37 $VERSION = 3.07.00.049;
44 GetImportRecordMarcXML
49 AddItemsToImportBiblio
60 GetStagedWebserviceBatches
61 GetImportBatchRangeDesc
62 GetNumberOfNonZ3950ImportBatches
65 GetItemNumbersFromImportBatch
69 GetImportBatchOverlayAction
70 SetImportBatchOverlayAction
71 GetImportBatchNoMatchAction
72 SetImportBatchNoMatchAction
73 GetImportBatchItemAction
74 SetImportBatchItemAction
77 GetImportRecordOverlayStatus
78 SetImportRecordOverlayStatus
81 GetImportRecordMatches
82 SetImportRecordMatches
86 our $logger = Koha
::Logger
->get( { category
=> 'C4.ImportBatch' } );
90 C4::ImportBatch - manage batches of imported MARC records
98 =head2 GetZ3950BatchId
100 my $batchid = GetZ3950BatchId($z3950server);
102 Retrieves the ID of the import batch for the Z39.50
103 reservoir for the given target. If necessary,
104 creates the import batch.
108 sub GetZ3950BatchId
{
109 my ($z3950server) = @_;
111 my $dbh = C4
::Context
->dbh;
112 my $sth = $dbh->prepare("SELECT import_batch_id FROM import_batches
113 WHERE batch_type = 'z3950'
115 $sth->execute($z3950server);
116 my $rowref = $sth->fetchrow_arrayref();
118 if (defined $rowref) {
121 my $batch_id = AddImportBatch
( {
122 overlay_action
=> 'create_new',
123 import_status
=> 'staged',
124 batch_type
=> 'z3950',
125 file_name
=> $z3950server,
132 =head2 GetWebserviceBatchId
134 my $batchid = GetWebserviceBatchId();
136 Retrieves the ID of the import batch for webservice.
137 If necessary, creates the import batch.
141 my $WEBSERVICE_BASE_QRY = <<EOQ;
142 SELECT import_batch_id FROM import_batches
143 WHERE batch_type = 'webservice'
144 AND import_status = 'staged'
146 sub GetWebserviceBatchId
{
149 my $dbh = C4
::Context
->dbh;
150 my $sql = $WEBSERVICE_BASE_QRY;
152 foreach my $field (qw(matcher_id overlay_action nomatch_action item_action)) {
153 if (my $val = $params->{$field}) {
154 $sql .= " AND $field = ?";
158 my $id = $dbh->selectrow_array($sql, undef, @args);
161 $params->{batch_type
} = 'webservice';
162 $params->{import_status
} = 'staged';
163 return AddImportBatch
($params);
166 =head2 GetImportRecordMarc
168 my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
172 sub GetImportRecordMarc
{
173 my ($import_record_id) = @_;
175 my $dbh = C4
::Context
->dbh;
176 my ( $marc, $encoding ) = $dbh->selectrow_array(q
|
177 SELECT marc
, encoding
179 WHERE import_record_id
= ?
180 |, undef, $import_record_id );
182 return $marc, $encoding;
185 sub GetRecordFromImportBiblio
{
186 my ( $import_record_id, $embed_items ) = @_;
188 my ($marc) = GetImportRecordMarc
($import_record_id);
189 my $record = MARC
::Record
->new_from_usmarc($marc);
191 EmbedItemsInImportBiblio
( $record, $import_record_id ) if $embed_items;
196 sub EmbedItemsInImportBiblio
{
197 my ( $record, $import_record_id ) = @_;
198 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField
("items.itemnumber", '');
199 my $dbh = C4
::Context
->dbh;
200 my $import_items = $dbh->selectall_arrayref(q
|
201 SELECT import_items
.marcxml
203 WHERE import_record_id
= ?
204 |, { Slice
=> {} }, $import_record_id );
206 for my $import_item ( @
$import_items ) {
207 my $item_marc = MARC
::Record
::new_from_xml
($import_item->{marcxml
});
208 push @item_fields, $item_marc->field($itemtag);
210 $record->append_fields(@item_fields);
214 =head2 GetImportRecordMarcXML
216 my $marcxml = GetImportRecordMarcXML($import_record_id);
220 sub GetImportRecordMarcXML
{
221 my ($import_record_id) = @_;
223 my $dbh = C4
::Context
->dbh;
224 my $sth = $dbh->prepare("SELECT marcxml FROM import_records WHERE import_record_id = ?");
225 $sth->execute($import_record_id);
226 my ($marcxml) = $sth->fetchrow();
232 =head2 AddImportBatch
234 my $batch_id = AddImportBatch($params_hash);
242 foreach (qw( matcher_id template_id branchcode
243 overlay_action nomatch_action item_action
244 import_status batch_type file_name comments record_type )) {
245 if (exists $params->{$_}) {
247 push @vals, $params->{$_};
250 my $dbh = C4
::Context
->dbh;
251 $dbh->do("INSERT INTO import_batches (".join( ',', @fields).")
252 VALUES (".join( ',', map '?', @fields).")",
255 return $dbh->{'mysql_insertid'};
258 =head2 GetImportBatch
260 my $row = GetImportBatch($batch_id);
262 Retrieve a hashref of an import_batches row.
269 my $dbh = C4
::Context
->dbh;
270 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches WHERE import_batch_id = ?");
271 $sth->bind_param(1, $batch_id);
273 my $result = $sth->fetchrow_hashref;
279 =head2 AddBiblioToBatch
281 my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence,
282 $marc_record, $encoding, $z3950random, $update_counts);
286 sub AddBiblioToBatch
{
287 my $batch_id = shift;
288 my $record_sequence = shift;
289 my $marc_record = shift;
290 my $encoding = shift;
291 my $z3950random = shift;
292 my $update_counts = @_ ?
shift : 1;
294 my $import_record_id = _create_import_record
($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, $z3950random, C4
::Context
->preference('marcflavour'));
295 _add_biblio_fields
($import_record_id, $marc_record);
296 _update_batch_record_counts
($batch_id) if $update_counts;
297 return $import_record_id;
300 =head2 ModBiblioInBatch
302 ModBiblioInBatch($import_record_id, $marc_record);
306 sub ModBiblioInBatch
{
307 my ($import_record_id, $marc_record) = @_;
309 _update_import_record_marc
($import_record_id, $marc_record, C4
::Context
->preference('marcflavour'));
310 _update_biblio_fields
($import_record_id, $marc_record);
314 =head2 AddAuthToBatch
316 my $import_record_id = AddAuthToBatch($batch_id, $record_sequence,
317 $marc_record, $encoding, $z3950random, $update_counts, [$marc_type]);
322 my $batch_id = shift;
323 my $record_sequence = shift;
324 my $marc_record = shift;
325 my $encoding = shift;
326 my $z3950random = shift;
327 my $update_counts = @_ ?
shift : 1;
328 my $marc_type = shift || C4
::Context
->preference('marcflavour');
330 $marc_type = 'UNIMARCAUTH' if $marc_type eq 'UNIMARC';
332 my $import_record_id = _create_import_record
($batch_id, $record_sequence, $marc_record, 'auth', $encoding, $z3950random, $marc_type);
333 _add_auth_fields
($import_record_id, $marc_record);
334 _update_batch_record_counts
($batch_id) if $update_counts;
335 return $import_record_id;
338 =head2 ModAuthInBatch
340 ModAuthInBatch($import_record_id, $marc_record);
345 my ($import_record_id, $marc_record) = @_;
347 my $marcflavour = C4
::Context
->preference('marcflavour');
348 _update_import_record_marc
($import_record_id, $marc_record, $marcflavour eq 'UNIMARC' ?
'UNIMARCAUTH' : 'USMARC');
352 =head2 BatchStageMarcRecords
354 ( $batch_id, $num_records, $num_items, @invalid_records ) =
355 BatchStageMarcRecords(
356 $encoding, $marc_records,
357 $file_name, $to_marc_plugin,
358 $marc_modification_template, $comments,
359 $branch_code, $parse_items,
360 $leave_as_staging, $progress_interval,
366 sub BatchStageMarcRecords
{
367 my $record_type = shift;
368 my $encoding = shift;
369 my $marc_records = shift;
370 my $file_name = shift;
371 my $to_marc_plugin = shift;
372 my $marc_modification_template = shift;
373 my $comments = shift;
374 my $branch_code = shift;
375 my $parse_items = shift;
376 my $leave_as_staging = shift;
378 # optional callback to monitor status
380 my $progress_interval = 0;
381 my $progress_callback = undef;
383 $progress_interval = shift;
384 $progress_callback = shift;
385 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
386 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
389 my $batch_id = AddImportBatch
( {
390 overlay_action
=> 'create_new',
391 import_status
=> 'staging',
392 batch_type
=> 'batch',
393 file_name
=> $file_name,
394 comments
=> $comments,
395 record_type
=> $record_type,
398 SetImportBatchItemAction
($batch_id, 'always_add');
400 SetImportBatchItemAction
($batch_id, 'ignore');
403 $marc_records = Koha
::Plugins
::Handler
->run(
405 class => $to_marc_plugin,
407 params
=> { data
=> $marc_records }
409 ) if $to_marc_plugin;
411 my $marc_type = C4
::Context
->preference('marcflavour');
412 $marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth');
413 my @invalid_records = ();
416 # FIXME - for now, we're dealing only with bibs
418 foreach my $marc_blob (split(/\x1D/, $marc_records)) {
419 $marc_blob =~ s/^\s+//g;
420 $marc_blob =~ s/\s+$//g;
421 next unless $marc_blob;
423 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
424 &$progress_callback($rec_num);
426 my ($marc_record, $charset_guessed, $char_errors) =
427 MarcToUTF8Record
($marc_blob, $marc_type, $encoding);
429 $encoding = $charset_guessed unless $encoding;
431 ModifyRecordWithTemplate
( $marc_modification_template, $marc_record ) if ( $marc_modification_template );
433 my $import_record_id;
434 if (scalar($marc_record->fields()) == 0) {
435 push @invalid_records, $marc_blob;
438 # Normalize the record so it doesn't have separated diacritics
439 SetUTF8Flag
($marc_record);
442 if ($record_type eq 'biblio') {
443 $import_record_id = AddBiblioToBatch
($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0);
445 my @import_items_ids = AddItemsToImportBiblio
($batch_id, $import_record_id, $marc_record, 0);
446 $num_items += scalar(@import_items_ids);
448 } elsif ($record_type eq 'auth') {
449 $import_record_id = AddAuthToBatch
($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0, $marc_type);
453 unless ($leave_as_staging) {
454 SetImportBatchStatus
($batch_id, 'staged');
456 # FIXME branch_code, number of bibs, number of items
457 _update_batch_record_counts
($batch_id);
458 return ($batch_id, $num_valid, $num_items, @invalid_records);
461 =head2 AddItemsToImportBiblio
463 my @import_items_ids = AddItemsToImportBiblio($batch_id,
464 $import_record_id, $marc_record, $update_counts);
468 sub AddItemsToImportBiblio
{
469 my $batch_id = shift;
470 my $import_record_id = shift;
471 my $marc_record = shift;
472 my $update_counts = @_ ?
shift : 0;
474 my @import_items_ids = ();
476 my $dbh = C4
::Context
->dbh;
477 my ($item_tag,$item_subfield) = &GetMarcFromKohaField
("items.itemnumber",'');
478 foreach my $item_field ($marc_record->field($item_tag)) {
479 my $item_marc = MARC
::Record
->new();
480 $item_marc->leader("00000 a "); # must set Leader/09 to 'a'
481 $item_marc->append_fields($item_field);
482 $marc_record->delete_field($item_field);
483 my $sth = $dbh->prepare_cached("INSERT INTO import_items (import_record_id, status, marcxml)
485 $sth->bind_param(1, $import_record_id);
486 $sth->bind_param(2, 'staged');
487 $sth->bind_param(3, $item_marc->as_xml());
489 push @import_items_ids, $dbh->{'mysql_insertid'};
493 if ($#import_items_ids > -1) {
494 _update_batch_record_counts
($batch_id) if $update_counts;
495 _update_import_record_marc
($import_record_id, $marc_record, C4
::Context
->preference('marcflavour'));
497 return @import_items_ids;
500 =head2 BatchFindDuplicates
502 my $num_with_matches = BatchFindDuplicates($batch_id, $matcher,
503 $max_matches, $progress_interval, $progress_callback);
505 Goes through the records loaded in the batch and attempts to
506 find duplicates for each one. Sets the matching status
507 of each record to "no_match" or "auto_match" as appropriate.
509 The $max_matches parameter is optional; if it is not supplied,
512 The $progress_interval and $progress_callback parameters are
513 optional; if both are supplied, the sub referred to by
514 $progress_callback will be invoked every $progress_interval
515 records using the number of records processed as the
520 sub BatchFindDuplicates
{
521 my $batch_id = shift;
523 my $max_matches = @_ ?
shift : 10;
525 # optional callback to monitor status
527 my $progress_interval = 0;
528 my $progress_callback = undef;
530 $progress_interval = shift;
531 $progress_callback = shift;
532 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
533 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
536 my $dbh = C4
::Context
->dbh;
538 my $sth = $dbh->prepare("SELECT import_record_id, record_type, marc
540 WHERE import_batch_id = ?");
541 $sth->execute($batch_id);
542 my $num_with_matches = 0;
544 while (my $rowref = $sth->fetchrow_hashref) {
546 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
547 &$progress_callback($rec_num);
549 my $marc_record = MARC
::Record
->new_from_usmarc($rowref->{'marc'});
551 if (defined $matcher) {
552 @matches = $matcher->get_matches($marc_record, $max_matches);
554 if (scalar(@matches) > 0) {
556 SetImportRecordMatches
($rowref->{'import_record_id'}, @matches);
557 SetImportRecordOverlayStatus
($rowref->{'import_record_id'}, 'auto_match');
559 SetImportRecordMatches
($rowref->{'import_record_id'}, ());
560 SetImportRecordOverlayStatus
($rowref->{'import_record_id'}, 'no_match');
564 return $num_with_matches;
567 =head2 BatchCommitRecords
569 my ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored) =
570 BatchCommitRecords($batch_id, $framework,
571 $progress_interval, $progress_callback);
575 sub BatchCommitRecords
{
576 my $batch_id = shift;
577 my $framework = shift;
579 # optional callback to monitor status
581 my $progress_interval = 0;
582 my $progress_callback = undef;
584 $progress_interval = shift;
585 $progress_callback = shift;
586 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
587 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
593 my $num_items_added = 0;
594 my $num_items_replaced = 0;
595 my $num_items_errored = 0;
597 # commit (i.e., save, all records in the batch)
598 SetImportBatchStatus
('importing');
599 my $overlay_action = GetImportBatchOverlayAction
($batch_id);
600 my $nomatch_action = GetImportBatchNoMatchAction
($batch_id);
601 my $item_action = GetImportBatchItemAction
($batch_id);
604 my $dbh = C4
::Context
->dbh;
605 my $sth = $dbh->prepare("SELECT import_records.import_record_id, record_type, status, overlay_status, marc, encoding
607 LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id)
608 LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
609 WHERE import_batch_id = ?");
610 $sth->execute($batch_id);
611 my $marcflavour = C4
::Context
->preference('marcflavour');
613 while (my $rowref = $sth->fetchrow_hashref) {
614 $record_type = $rowref->{'record_type'};
616 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
617 &$progress_callback($rec_num);
619 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
625 if ($marcflavour eq 'UNIMARC' && $record_type eq 'auth') {
626 $marc_type = 'UNIMARCAUTH';
627 } elsif ($marcflavour eq 'UNIMARC') {
628 $marc_type = 'UNIMARC';
630 $marc_type = 'USMARC';
632 my $marc_record = MARC
::Record
->new_from_usmarc($rowref->{'marc'});
634 if ($record_type eq 'biblio') {
635 # remove any item tags - rely on BatchCommitItems
636 ($item_tag,$item_subfield) = &GetMarcFromKohaField
("items.itemnumber",'');
637 foreach my $item_field ($marc_record->field($item_tag)) {
638 $marc_record->delete_field($item_field);
642 my ($record_result, $item_result, $record_match) =
643 _get_commit_action
($overlay_action, $nomatch_action, $item_action,
644 $rowref->{'overlay_status'}, $rowref->{'import_record_id'}, $record_type);
648 if ($record_result eq 'create_new') {
650 if ($record_type eq 'biblio') {
651 my $biblioitemnumber;
652 ($recordid, $biblioitemnumber) = AddBiblio
($marc_record, $framework);
653 $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?";
654 if ($item_result eq 'create_new' || $item_result eq 'replace') {
655 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems
($rowref->{'import_record_id'}, $recordid, $item_result);
656 $num_items_added += $bib_items_added;
657 $num_items_replaced += $bib_items_replaced;
658 $num_items_errored += $bib_items_errored;
661 $recordid = AddAuthority
($marc_record, undef, GuessAuthTypeCode
($marc_record));
662 $query = "UPDATE import_auths SET matched_authid = ? WHERE import_record_id = ?";
664 my $sth = $dbh->prepare_cached($query);
665 $sth->execute($recordid, $rowref->{'import_record_id'});
667 SetImportRecordStatus
($rowref->{'import_record_id'}, 'imported');
668 } elsif ($record_result eq 'replace') {
670 $recordid = $record_match;
672 if ($record_type eq 'biblio') {
673 my $oldbiblio = GetBiblio
($recordid);
674 $oldxml = GetXmlBiblio
($recordid);
676 # remove item fields so that they don't get
677 # added again if record is reverted
678 # FIXME: GetXmlBiblio output should not contain item info any more! So the next foreach should not be needed. Does not hurt either; may remove old 952s that should not have been there anymore.
679 my $old_marc = MARC
::Record
->new_from_xml(StripNonXmlChars
($oldxml), 'UTF-8', $rowref->{'encoding'}, $marc_type);
680 foreach my $item_field ($old_marc->field($item_tag)) {
681 $old_marc->delete_field($item_field);
683 $oldxml = $old_marc->as_xml($marc_type);
685 ModBiblio
($marc_record, $recordid, $oldbiblio->{'frameworkcode'});
686 $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?";
688 if ($item_result eq 'create_new' || $item_result eq 'replace') {
689 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems
($rowref->{'import_record_id'}, $recordid, $item_result);
690 $num_items_added += $bib_items_added;
691 $num_items_replaced += $bib_items_replaced;
692 $num_items_errored += $bib_items_errored;
695 $oldxml = GetAuthorityXML
($recordid);
697 ModAuthority
($recordid, $marc_record, GuessAuthTypeCode
($marc_record));
698 $query = "UPDATE import_auths SET matched_authid = ? WHERE import_record_id = ?";
700 my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
701 $sth->execute($oldxml, $rowref->{'import_record_id'});
703 my $sth2 = $dbh->prepare_cached($query);
704 $sth2->execute($recordid, $rowref->{'import_record_id'});
706 SetImportRecordOverlayStatus
($rowref->{'import_record_id'}, 'match_applied');
707 SetImportRecordStatus
($rowref->{'import_record_id'}, 'imported');
708 } elsif ($record_result eq 'ignore') {
709 $recordid = $record_match;
711 $recordid = $record_match;
712 if ($record_type eq 'biblio' and defined $recordid and ( $item_result eq 'create_new' || $item_result eq 'replace' ) ) {
713 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems
($rowref->{'import_record_id'}, $recordid, $item_result);
714 $num_items_added += $bib_items_added;
715 $num_items_replaced += $bib_items_replaced;
716 $num_items_errored += $bib_items_errored;
717 # still need to record the matched biblionumber so that the
718 # items can be reverted
719 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
720 $sth2->execute($recordid, $rowref->{'import_record_id'});
721 SetImportRecordOverlayStatus
($rowref->{'import_record_id'}, 'match_applied');
723 SetImportRecordStatus
($rowref->{'import_record_id'}, 'ignored');
727 SetImportBatchStatus
($batch_id, 'imported');
728 return ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored);
731 =head2 BatchCommitItems
733 ($num_items_added, $num_items_errored) =
734 BatchCommitItems($import_record_id, $biblionumber);
738 sub BatchCommitItems
{
739 my ( $import_record_id, $biblionumber, $action ) = @_;
741 my $dbh = C4
::Context
->dbh;
743 my $num_items_added = 0;
744 my $num_items_errored = 0;
745 my $num_items_replaced = 0;
747 my $sth = $dbh->prepare( "
748 SELECT import_items_id, import_items.marcxml, encoding
750 JOIN import_records USING (import_record_id)
751 WHERE import_record_id = ?
752 ORDER BY import_items_id
754 $sth->bind_param( 1, $import_record_id );
757 while ( my $row = $sth->fetchrow_hashref() ) {
758 my $item_marc = MARC
::Record
->new_from_xml( StripNonXmlChars
( $row->{'marcxml'} ), 'UTF-8', $row->{'encoding'} );
760 # Delete date_due subfield as to not accidentally delete item checkout due dates
761 my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField
( 'items.onloan', GetFrameworkCode
($biblionumber) );
762 $item_marc->field($MARCfield)->delete_subfield( code
=> $MARCsubfield );
764 my $item = TransformMarcToKoha
( $dbh, $item_marc );
766 my $duplicate_barcode = exists( $item->{'barcode'} ) && GetItemnumberFromBarcode
( $item->{'barcode'} );
767 my $duplicate_itemnumber = exists( $item->{'itemnumber'} );
769 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
770 if ( $action eq "replace" && $duplicate_itemnumber ) {
771 # Duplicate itemnumbers have precedence, that way we can update barcodes by overlaying
772 ModItemFromMarc
( $item_marc, $biblionumber, $item->{itemnumber
} );
773 $updsth->bind_param( 1, 'imported' );
774 $updsth->bind_param( 2, $item->{itemnumber
} );
775 $updsth->bind_param( 3, $row->{'import_items_id'} );
778 $num_items_replaced++;
779 } elsif ( $action eq "replace" && $duplicate_barcode ) {
780 my $itemnumber = GetItemnumberFromBarcode
( $item->{'barcode'} );
781 ModItemFromMarc
( $item_marc, $biblionumber, $itemnumber );
782 $updsth->bind_param( 1, 'imported' );
783 $updsth->bind_param( 2, $item->{itemnumber
} );
784 $updsth->bind_param( 3, $row->{'import_items_id'} );
787 $num_items_replaced++;
788 } elsif ($duplicate_barcode) {
789 $updsth->bind_param( 1, 'error' );
790 $updsth->bind_param( 2, 'duplicate item barcode' );
791 $updsth->bind_param( 3, $row->{'import_items_id'} );
793 $num_items_errored++;
795 my ( $item_biblionumber, $biblioitemnumber, $itemnumber ) = AddItemFromMarc
( $item_marc, $biblionumber );
796 $updsth->bind_param( 1, 'imported' );
797 $updsth->bind_param( 2, $itemnumber );
798 $updsth->bind_param( 3, $row->{'import_items_id'} );
805 return ( $num_items_added, $num_items_replaced, $num_items_errored );
808 =head2 BatchRevertRecords
810 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted,
811 $num_ignored) = BatchRevertRecords($batch_id);
815 sub BatchRevertRecords
{
816 my $batch_id = shift;
818 $logger->trace("C4::ImportBatch::BatchRevertRecords( $batch_id )");
823 my $num_reverted = 0;
825 my $num_items_deleted = 0;
826 # commit (i.e., save, all records in the batch)
827 SetImportBatchStatus
('reverting');
828 my $overlay_action = GetImportBatchOverlayAction
($batch_id);
829 my $nomatch_action = GetImportBatchNoMatchAction
($batch_id);
830 my $dbh = C4
::Context
->dbh;
831 my $sth = $dbh->prepare("SELECT import_records.import_record_id, record_type, status, overlay_status, marcxml_old, encoding, matched_biblionumber, matched_authid
833 LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id)
834 LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
835 WHERE import_batch_id = ?");
836 $sth->execute($batch_id);
838 my $marcflavour = C4
::Context
->preference('marcflavour');
839 while (my $rowref = $sth->fetchrow_hashref) {
840 $record_type = $rowref->{'record_type'};
841 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
845 if ($marcflavour eq 'UNIMARC' && $record_type eq 'auth') {
846 $marc_type = 'UNIMARCAUTH';
847 } elsif ($marcflavour eq 'UNIMARC') {
848 $marc_type = 'UNIMARC';
850 $marc_type = 'USMARC';
853 my $record_result = _get_revert_action
($overlay_action, $rowref->{'overlay_status'}, $rowref->{'status'});
855 if ($record_result eq 'delete') {
857 if ($record_type eq 'biblio') {
858 $num_items_deleted += BatchRevertItems
($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
859 $error = DelBiblio
($rowref->{'matched_biblionumber'});
861 my $deletedauthid = DelAuthority
($rowref->{'matched_authid'});
863 if (defined $error) {
867 SetImportRecordStatus
($rowref->{'import_record_id'}, 'reverted');
869 } elsif ($record_result eq 'restore') {
871 my $old_record = MARC
::Record
->new_from_xml(StripNonXmlChars
($rowref->{'marcxml_old'}), 'UTF-8', $rowref->{'encoding'}, $marc_type);
872 if ($record_type eq 'biblio') {
873 my $biblionumber = $rowref->{'matched_biblionumber'};
874 my $oldbiblio = GetBiblio
($biblionumber);
876 $logger->info("C4::ImportBatch::BatchRevertRecords: Biblio record $biblionumber does not exist, restoration of this record was skipped") unless $oldbiblio;
877 next unless $oldbiblio; # Record has since been deleted. Deleted records should stay deleted.
879 $num_items_deleted += BatchRevertItems
($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
880 ModBiblio
($old_record, $biblionumber, $oldbiblio->{'frameworkcode'});
882 my $authid = $rowref->{'matched_authid'};
883 ModAuthority
($authid, $old_record, GuessAuthTypeCode
($old_record));
885 SetImportRecordStatus
($rowref->{'import_record_id'}, 'reverted');
886 } elsif ($record_result eq 'ignore') {
887 if ($record_type eq 'biblio') {
888 $num_items_deleted += BatchRevertItems
($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
890 SetImportRecordStatus
($rowref->{'import_record_id'}, 'reverted');
893 if ($record_type eq 'biblio') {
894 # remove matched_biblionumber only if there is no 'imported' item left
895 $query = "UPDATE import_biblios SET matched_biblionumber = NULL WHERE import_record_id = ?";
896 $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')";
898 $query = "UPDATE import_auths SET matched_authid = NULL WHERE import_record_id = ?";
900 my $sth2 = $dbh->prepare_cached($query);
901 $sth2->execute($rowref->{'import_record_id'});
905 SetImportBatchStatus
($batch_id, 'reverted');
906 return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored);
909 =head2 BatchRevertItems
911 my $num_items_deleted = BatchRevertItems($import_record_id, $biblionumber);
915 sub BatchRevertItems
{
916 my ($import_record_id, $biblionumber) = @_;
918 my $dbh = C4
::Context
->dbh;
919 my $num_items_deleted = 0;
921 my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
923 JOIN items USING (itemnumber)
924 WHERE import_record_id = ?");
925 $sth->bind_param(1, $import_record_id);
927 while (my $row = $sth->fetchrow_hashref()) {
928 my $error = DelItemCheck
($dbh, $biblionumber, $row->{'itemnumber'});
930 my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?");
931 $updsth->bind_param(1, 'reverted');
932 $updsth->bind_param(2, $row->{'import_items_id'});
935 $num_items_deleted++;
942 return $num_items_deleted;
947 CleanBatch($batch_id)
949 Deletes all staged records from the import batch
950 and sets the status of the batch to 'cleaned'. Note
951 that deleting a stage record does *not* affect
952 any record that has been committed to the database.
957 my $batch_id = shift;
958 return unless defined $batch_id;
960 C4
::Context
->dbh->do('DELETE FROM import_records WHERE import_batch_id = ?', {}, $batch_id);
961 SetImportBatchStatus
($batch_id, 'cleaned');
964 =head2 GetAllImportBatches
966 my $results = GetAllImportBatches();
968 Returns a references to an array of hash references corresponding
969 to all import_batches rows (of batch_type 'batch'), sorted in
970 ascending order by import_batch_id.
974 sub GetAllImportBatches
{
975 my $dbh = C4
::Context
->dbh;
976 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
977 WHERE batch_type IN ('batch', 'webservice')
978 ORDER BY import_batch_id ASC");
982 while (my $row = $sth->fetchrow_hashref) {
983 push @
$results, $row;
989 =head2 GetStagedWebserviceBatches
991 my $batch_ids = GetStagedWebserviceBatches();
993 Returns a references to an array of batch id's
994 of batch_type 'webservice' that are not imported
998 my $PENDING_WEBSERVICE_BATCHES_QRY = <<EOQ;
999 SELECT import_batch_id FROM import_batches
1000 WHERE batch_type = 'webservice'
1001 AND import_status = 'staged'
1003 sub GetStagedWebserviceBatches
{
1004 my $dbh = C4
::Context
->dbh;
1005 return $dbh->selectcol_arrayref($PENDING_WEBSERVICE_BATCHES_QRY);
1008 =head2 GetImportBatchRangeDesc
1010 my $results = GetImportBatchRangeDesc($offset, $results_per_group);
1012 Returns a reference to an array of hash references corresponding to
1013 import_batches rows (sorted in descending order by import_batch_id)
1014 start at the given offset.
1018 sub GetImportBatchRangeDesc
{
1019 my ($offset, $results_per_group) = @_;
1021 my $dbh = C4
::Context
->dbh;
1022 my $query = "SELECT * FROM import_batches
1023 WHERE batch_type IN ('batch', 'webservice')
1024 ORDER BY import_batch_id DESC";
1026 if ($results_per_group){
1027 $query .= " LIMIT ?";
1028 push(@params, $results_per_group);
1031 $query .= " OFFSET ?";
1032 push(@params, $offset);
1034 my $sth = $dbh->prepare_cached($query);
1035 $sth->execute(@params);
1036 my $results = $sth->fetchall_arrayref({});
1041 =head2 GetItemNumbersFromImportBatch
1043 my @itemsnos = GetItemNumbersFromImportBatch($batch_id);
1047 sub GetItemNumbersFromImportBatch
{
1048 my ($batch_id) = @_;
1049 my $dbh = C4
::Context
->dbh;
1050 my $sth = $dbh->prepare("SELECT itemnumber FROM import_batches,import_records,import_items WHERE import_batches.import_batch_id=import_records.import_batch_id AND import_records.import_record_id=import_items.import_record_id AND import_batches.import_batch_id=?");
1051 $sth->execute($batch_id);
1053 while ( my ($itm) = $sth->fetchrow_array ) {
1059 =head2 GetNumberOfImportBatches
1061 my $count = GetNumberOfImportBatches();
1065 sub GetNumberOfNonZ3950ImportBatches
{
1066 my $dbh = C4
::Context
->dbh;
1067 my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type != 'z3950'");
1069 my ($count) = $sth->fetchrow_array();
1074 =head2 GetImportBiblios
1076 my $results = GetImportBiblios($importid);
1080 sub GetImportBiblios
{
1081 my ($import_record_id) = @_;
1083 my $dbh = C4
::Context
->dbh;
1084 my $query = "SELECT * FROM import_biblios WHERE import_record_id = ?";
1085 return $dbh->selectall_arrayref(
1093 =head2 GetImportRecordsRange
1095 my $results = GetImportRecordsRange($batch_id, $offset, $results_per_group);
1097 Returns a reference to an array of hash references corresponding to
1098 import_biblios/import_auths/import_records rows for a given batch
1099 starting at the given offset.
1103 sub GetImportRecordsRange
{
1104 my ( $batch_id, $offset, $results_per_group, $status, $parameters ) = @_;
1106 my $dbh = C4
::Context
->dbh;
1108 my $order_by = $parameters->{order_by
} || 'import_record_id';
1109 ( $order_by ) = grep( /^$order_by$/, qw( import_record_id title status overlay_status ) ) ?
$order_by : 'import_record_id';
1111 my $order_by_direction =
1112 uc( $parameters->{order_by_direction
} ) eq 'DESC' ?
'DESC' : 'ASC';
1114 $order_by .= " $order_by_direction, authorized_heading" if $order_by eq 'title';
1116 my $query = "SELECT title, author, isbn, issn, authorized_heading, import_records.import_record_id,
1117 record_sequence, status, overlay_status,
1118 matched_biblionumber, matched_authid, record_type
1120 LEFT JOIN import_auths ON (import_records.import_record_id=import_auths.import_record_id)
1121 LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
1122 WHERE import_batch_id = ?";
1124 push(@params, $batch_id);
1126 $query .= " AND status=?";
1127 push(@params,$status);
1130 $query.=" ORDER BY $order_by $order_by_direction";
1132 if($results_per_group){
1133 $query .= " LIMIT ?";
1134 push(@params, $results_per_group);
1137 $query .= " OFFSET ?";
1138 push(@params, $offset);
1140 my $sth = $dbh->prepare_cached($query);
1141 $sth->execute(@params);
1142 my $results = $sth->fetchall_arrayref({});
1148 =head2 GetBestRecordMatch
1150 my $record_id = GetBestRecordMatch($import_record_id);
1154 sub GetBestRecordMatch
{
1155 my ($import_record_id) = @_;
1157 my $dbh = C4
::Context
->dbh;
1158 my $sth = $dbh->prepare("SELECT candidate_match_id
1159 FROM import_record_matches
1160 JOIN import_records ON ( import_record_matches.import_record_id = import_records.import_record_id )
1161 LEFT JOIN biblio ON ( candidate_match_id = biblio.biblionumber )
1162 LEFT JOIN auth_header ON ( candidate_match_id = auth_header.authid )
1163 WHERE import_record_matches.import_record_id = ? AND
1164 ( (import_records.record_type = 'biblio' AND biblio.biblionumber IS NOT NULL) OR
1165 (import_records.record_type = 'auth' AND auth_header.authid IS NOT NULL) )
1166 ORDER BY score DESC, candidate_match_id DESC");
1167 $sth->execute($import_record_id);
1168 my ($record_id) = $sth->fetchrow_array();
1173 =head2 GetImportBatchStatus
1175 my $status = GetImportBatchStatus($batch_id);
1179 sub GetImportBatchStatus
{
1180 my ($batch_id) = @_;
1182 my $dbh = C4
::Context
->dbh;
1183 my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE import_batch_id = ?");
1184 $sth->execute($batch_id);
1185 my ($status) = $sth->fetchrow_array();
1191 =head2 SetImportBatchStatus
1193 SetImportBatchStatus($batch_id, $new_status);
1197 sub SetImportBatchStatus
{
1198 my ($batch_id, $new_status) = @_;
1200 my $dbh = C4
::Context
->dbh;
1201 my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
1202 $sth->execute($new_status, $batch_id);
1207 =head2 GetImportBatchOverlayAction
1209 my $overlay_action = GetImportBatchOverlayAction($batch_id);
1213 sub GetImportBatchOverlayAction
{
1214 my ($batch_id) = @_;
1216 my $dbh = C4
::Context
->dbh;
1217 my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
1218 $sth->execute($batch_id);
1219 my ($overlay_action) = $sth->fetchrow_array();
1221 return $overlay_action;
1226 =head2 SetImportBatchOverlayAction
1228 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
1232 sub SetImportBatchOverlayAction
{
1233 my ($batch_id, $new_overlay_action) = @_;
1235 my $dbh = C4
::Context
->dbh;
1236 my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
1237 $sth->execute($new_overlay_action, $batch_id);
1242 =head2 GetImportBatchNoMatchAction
1244 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
1248 sub GetImportBatchNoMatchAction
{
1249 my ($batch_id) = @_;
1251 my $dbh = C4
::Context
->dbh;
1252 my $sth = $dbh->prepare("SELECT nomatch_action FROM import_batches WHERE import_batch_id = ?");
1253 $sth->execute($batch_id);
1254 my ($nomatch_action) = $sth->fetchrow_array();
1256 return $nomatch_action;
1261 =head2 SetImportBatchNoMatchAction
1263 SetImportBatchNoMatchAction($batch_id, $new_nomatch_action);
1267 sub SetImportBatchNoMatchAction
{
1268 my ($batch_id, $new_nomatch_action) = @_;
1270 my $dbh = C4
::Context
->dbh;
1271 my $sth = $dbh->prepare("UPDATE import_batches SET nomatch_action = ? WHERE import_batch_id = ?");
1272 $sth->execute($new_nomatch_action, $batch_id);
1277 =head2 GetImportBatchItemAction
1279 my $item_action = GetImportBatchItemAction($batch_id);
1283 sub GetImportBatchItemAction
{
1284 my ($batch_id) = @_;
1286 my $dbh = C4
::Context
->dbh;
1287 my $sth = $dbh->prepare("SELECT item_action FROM import_batches WHERE import_batch_id = ?");
1288 $sth->execute($batch_id);
1289 my ($item_action) = $sth->fetchrow_array();
1291 return $item_action;
1296 =head2 SetImportBatchItemAction
1298 SetImportBatchItemAction($batch_id, $new_item_action);
1302 sub SetImportBatchItemAction
{
1303 my ($batch_id, $new_item_action) = @_;
1305 my $dbh = C4
::Context
->dbh;
1306 my $sth = $dbh->prepare("UPDATE import_batches SET item_action = ? WHERE import_batch_id = ?");
1307 $sth->execute($new_item_action, $batch_id);
1312 =head2 GetImportBatchMatcher
1314 my $matcher_id = GetImportBatchMatcher($batch_id);
1318 sub GetImportBatchMatcher
{
1319 my ($batch_id) = @_;
1321 my $dbh = C4
::Context
->dbh;
1322 my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
1323 $sth->execute($batch_id);
1324 my ($matcher_id) = $sth->fetchrow_array();
1331 =head2 SetImportBatchMatcher
1333 SetImportBatchMatcher($batch_id, $new_matcher_id);
1337 sub SetImportBatchMatcher
{
1338 my ($batch_id, $new_matcher_id) = @_;
1340 my $dbh = C4
::Context
->dbh;
1341 my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
1342 $sth->execute($new_matcher_id, $batch_id);
1347 =head2 GetImportRecordOverlayStatus
1349 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
1353 sub GetImportRecordOverlayStatus
{
1354 my ($import_record_id) = @_;
1356 my $dbh = C4
::Context
->dbh;
1357 my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
1358 $sth->execute($import_record_id);
1359 my ($overlay_status) = $sth->fetchrow_array();
1361 return $overlay_status;
1366 =head2 SetImportRecordOverlayStatus
1368 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
1372 sub SetImportRecordOverlayStatus
{
1373 my ($import_record_id, $new_overlay_status) = @_;
1375 my $dbh = C4
::Context
->dbh;
1376 my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
1377 $sth->execute($new_overlay_status, $import_record_id);
1382 =head2 GetImportRecordStatus
1384 my $status = GetImportRecordStatus($import_record_id);
1388 sub GetImportRecordStatus
{
1389 my ($import_record_id) = @_;
1391 my $dbh = C4
::Context
->dbh;
1392 my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
1393 $sth->execute($import_record_id);
1394 my ($status) = $sth->fetchrow_array();
1401 =head2 SetImportRecordStatus
1403 SetImportRecordStatus($import_record_id, $new_status);
1407 sub SetImportRecordStatus
{
1408 my ($import_record_id, $new_status) = @_;
1410 my $dbh = C4
::Context
->dbh;
1411 my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
1412 $sth->execute($new_status, $import_record_id);
1417 =head2 GetImportRecordMatches
1419 my $results = GetImportRecordMatches($import_record_id, $best_only);
1423 sub GetImportRecordMatches
{
1424 my $import_record_id = shift;
1425 my $best_only = @_ ?
shift : 0;
1427 my $dbh = C4
::Context
->dbh;
1428 # FIXME currently biblio only
1429 my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber,
1430 candidate_match_id, score, record_type
1432 JOIN import_record_matches USING (import_record_id)
1433 LEFT JOIN biblio ON (biblionumber = candidate_match_id)
1434 WHERE import_record_id = ?
1435 ORDER BY score DESC, biblionumber DESC");
1436 $sth->bind_param(1, $import_record_id);
1439 while (my $row = $sth->fetchrow_hashref) {
1440 if ($row->{'record_type'} eq 'auth') {
1441 $row->{'authorized_heading'} = C4
::AuthoritiesMarc
::GetAuthorizedHeading
( { authid
=> $row->{'candidate_match_id'} } );
1443 next if ($row->{'record_type'} eq 'biblio' && not $row->{'biblionumber'});
1444 push @
$results, $row;
1454 =head2 SetImportRecordMatches
1456 SetImportRecordMatches($import_record_id, @matches);
1460 sub SetImportRecordMatches
{
1461 my $import_record_id = shift;
1464 my $dbh = C4
::Context
->dbh;
1465 my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
1466 $delsth->execute($import_record_id);
1469 my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
1471 foreach my $match (@matches) {
1472 $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
1477 # internal functions
1479 sub _create_import_record
{
1480 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random, $marc_type) = @_;
1482 my $dbh = C4
::Context
->dbh;
1483 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml,
1484 record_type, encoding, z3950random)
1485 VALUES (?, ?, ?, ?, ?, ?, ?)");
1486 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml($marc_type),
1487 $record_type, $encoding, $z3950random);
1488 my $import_record_id = $dbh->{'mysql_insertid'};
1490 return $import_record_id;
1493 sub _update_import_record_marc
{
1494 my ($import_record_id, $marc_record, $marc_type) = @_;
1496 my $dbh = C4
::Context
->dbh;
1497 my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
1498 WHERE import_record_id = ?");
1499 $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml($marc_type), $import_record_id);
1503 sub _add_auth_fields
{
1504 my ($import_record_id, $marc_record) = @_;
1507 if ($marc_record->field('001')) {
1508 $controlnumber = $marc_record->field('001')->data();
1510 my $authorized_heading = C4
::AuthoritiesMarc
::GetAuthorizedHeading
({ record
=> $marc_record });
1511 my $dbh = C4
::Context
->dbh;
1512 my $sth = $dbh->prepare("INSERT INTO import_auths (import_record_id, control_number, authorized_heading) VALUES (?, ?, ?)");
1513 $sth->execute($import_record_id, $controlnumber, $authorized_heading);
1517 sub _add_biblio_fields
{
1518 my ($import_record_id, $marc_record) = @_;
1520 my ($title, $author, $isbn, $issn) = _parse_biblio_fields
($marc_record);
1521 my $dbh = C4
::Context
->dbh;
1522 # FIXME no controlnumber, originalsource
1523 $isbn = C4
::Koha
::GetNormalizedISBN
($isbn);
1524 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
1525 $sth->execute($import_record_id, $title, $author, $isbn, $issn);
1530 sub _update_biblio_fields
{
1531 my ($import_record_id, $marc_record) = @_;
1533 my ($title, $author, $isbn, $issn) = _parse_biblio_fields
($marc_record);
1534 my $dbh = C4
::Context
->dbh;
1535 # FIXME no controlnumber, originalsource
1536 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1540 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
1541 WHERE import_record_id = ?");
1542 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
1546 sub _parse_biblio_fields
{
1547 my ($marc_record) = @_;
1549 my $dbh = C4
::Context
->dbh;
1550 my $bibliofields = TransformMarcToKoha
($dbh, $marc_record, '');
1551 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
1555 sub _update_batch_record_counts
{
1556 my ($batch_id) = @_;
1558 my $dbh = C4
::Context
->dbh;
1559 my $sth = $dbh->prepare_cached("UPDATE import_batches SET
1563 WHERE import_batch_id = import_batches.import_batch_id),
1567 JOIN import_items USING (import_record_id)
1568 WHERE import_batch_id = import_batches.import_batch_id
1569 AND record_type = 'biblio')
1570 WHERE import_batch_id = ?");
1571 $sth->bind_param(1, $batch_id);
1576 sub _get_commit_action
{
1577 my ($overlay_action, $nomatch_action, $item_action, $overlay_status, $import_record_id, $record_type) = @_;
1579 if ($record_type eq 'biblio') {
1580 my ($bib_result, $bib_match, $item_result);
1582 if ($overlay_status ne 'no_match') {
1583 $bib_match = GetBestRecordMatch
($import_record_id);
1584 if ($overlay_action eq 'replace') {
1585 $bib_result = defined($bib_match) ?
'replace' : 'create_new';
1586 } elsif ($overlay_action eq 'create_new') {
1587 $bib_result = 'create_new';
1588 } elsif ($overlay_action eq 'ignore') {
1589 $bib_result = 'ignore';
1591 if($item_action eq 'always_add' or $item_action eq 'add_only_for_matches'){
1592 $item_result = 'create_new';
1594 elsif($item_action eq 'replace'){
1595 $item_result = 'replace';
1598 $item_result = 'ignore';
1601 $bib_result = $nomatch_action;
1602 $item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_new') ?
'create_new' : 'ignore';
1604 return ($bib_result, $item_result, $bib_match);
1605 } else { # must be auths
1606 my ($auth_result, $auth_match);
1608 if ($overlay_status ne 'no_match') {
1609 $auth_match = GetBestRecordMatch
($import_record_id);
1610 if ($overlay_action eq 'replace') {
1611 $auth_result = defined($auth_match) ?
'replace' : 'create_new';
1612 } elsif ($overlay_action eq 'create_new') {
1613 $auth_result = 'create_new';
1614 } elsif ($overlay_action eq 'ignore') {
1615 $auth_result = 'ignore';
1618 $auth_result = $nomatch_action;
1621 return ($auth_result, undef, $auth_match);
1626 sub _get_revert_action
{
1627 my ($overlay_action, $overlay_status, $status) = @_;
1631 if ($status eq 'ignored') {
1632 $bib_result = 'ignore';
1634 if ($overlay_action eq 'create_new') {
1635 $bib_result = 'delete';
1637 $bib_result = ($overlay_status eq 'match_applied') ?
'restore' : 'delete';
1648 Koha Development Team <http://koha-community.org/>
1650 Galen Charlton <galen.charlton@liblime.com>