1 package C4::ImportBatch;
3 # Copyright (C) 2007 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30 # set the version for version checking
43 BatchFindBibDuplicates
48 GetImportBatchRangeDesc
49 GetNumberOfNonZ3950ImportBatches
51 GetItemNumbersFromImportBatch
55 GetImportBatchOverlayAction
56 SetImportBatchOverlayAction
57 GetImportBatchNoMatchAction
58 SetImportBatchNoMatchAction
59 GetImportBatchItemAction
60 SetImportBatchItemAction
63 GetImportRecordOverlayStatus
64 SetImportRecordOverlayStatus
67 GetImportRecordMatches
68 SetImportRecordMatches
74 C4::ImportBatch - manage batches of imported MARC records
86 =head2 GetZ3950BatchId
90 my $batchid = GetZ3950BatchId($z3950server);
94 Retrieves the ID of the import batch for the Z39.50
95 reservoir for the given target. If necessary,
96 creates the import batch.
100 sub GetZ3950BatchId {
101 my ($z3950server) = @_;
103 my $dbh = C4::Context->dbh;
104 my $sth = $dbh->prepare("SELECT import_batch_id FROM import_batches
105 WHERE batch_type = 'z3950'
107 $sth->execute($z3950server);
108 my $rowref = $sth->fetchrow_arrayref();
110 if (defined $rowref) {
113 my $batch_id = AddImportBatch('create_new', 'staged', 'z3950', $z3950server, '');
119 =head2 GetImportRecordMarc
123 my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
129 sub GetImportRecordMarc {
130 my ($import_record_id) = @_;
132 my $dbh = C4::Context->dbh;
133 my $sth = $dbh->prepare("SELECT marc, encoding FROM import_records WHERE import_record_id = ?");
134 $sth->execute($import_record_id);
135 my ($marc, $encoding) = $sth->fetchrow();
141 =head2 AddImportBatch
145 my $batch_id = AddImportBatch($overlay_action, $import_status, $type, $file_name, $comments);
152 my ($overlay_action, $import_status, $type, $file_name, $comments) = @_;
154 my $dbh = C4::Context->dbh;
155 my $sth = $dbh->prepare("INSERT INTO import_batches (overlay_action, import_status, batch_type,
157 VALUES (?, ?, ?, ?, ?)");
158 $sth->execute($overlay_action, $import_status, $type, $file_name, $comments);
159 my $batch_id = $dbh->{'mysql_insertid'};
166 =head2 GetImportBatch
170 my $row = GetImportBatch($batch_id);
174 Retrieve a hashref of an import_batches row.
181 my $dbh = C4::Context->dbh;
182 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches WHERE import_batch_id = ?");
183 $sth->bind_param(1, $batch_id);
185 my $result = $sth->fetchrow_hashref;
191 =head2 AddBiblioToBatch
195 my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence, $marc_record, $encoding, $z3950random, $update_counts);
201 sub AddBiblioToBatch {
202 my $batch_id = shift;
203 my $record_sequence = shift;
204 my $marc_record = shift;
205 my $encoding = shift;
206 my $z3950random = shift;
207 my $update_counts = @_ ? shift : 1;
209 my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, $z3950random);
210 _add_biblio_fields($import_record_id, $marc_record);
211 _update_batch_record_counts($batch_id) if $update_counts;
212 return $import_record_id;
215 =head2 ModBiblioInBatch
219 ModBiblioInBatch($import_record_id, $marc_record);
225 sub ModBiblioInBatch {
226 my ($import_record_id, $marc_record) = @_;
228 _update_import_record_marc($import_record_id, $marc_record);
229 _update_biblio_fields($import_record_id, $marc_record);
233 =head2 BatchStageMarcRecords
237 ($batch_id, $num_records, $num_items, @invalid_records) =
238 BatchStageMarcRecords($marc_flavor, $marc_records, $file_name,
239 $comments, $branch_code, $parse_items,
241 $progress_interval, $progress_callback);
247 sub BatchStageMarcRecords {
248 my $marc_flavor = shift;
249 my $marc_records = shift;
250 my $file_name = shift;
251 my $comments = shift;
252 my $branch_code = shift;
253 my $parse_items = shift;
254 my $leave_as_staging = shift;
256 # optional callback to monitor status
258 my $progress_interval = 0;
259 my $progress_callback = undef;
261 $progress_interval = shift;
262 $progress_callback = shift;
263 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
264 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
267 my $batch_id = AddImportBatch('create_new', 'staging', 'batch', $file_name, $comments);
269 SetImportBatchItemAction($batch_id, 'always_add');
271 SetImportBatchItemAction($batch_id, 'ignore');
274 my @invalid_records = ();
277 # FIXME - for now, we're dealing only with bibs
279 foreach my $marc_blob (split(/\x1D/, $marc_records)) {
280 $marc_blob =~ s/^\s+//g;
281 $marc_blob =~ s/\s+$//g;
282 next unless $marc_blob;
284 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
285 &$progress_callback($rec_num);
287 my ($marc_record, $charset_guessed, $char_errors) =
288 MarcToUTF8Record($marc_blob, C4::Context->preference("marcflavour"));
289 my $import_record_id;
290 if (scalar($marc_record->fields()) == 0) {
291 push @invalid_records, $marc_blob;
294 $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $marc_flavor, int(rand(99999)), 0);
296 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0);
297 $num_items += scalar(@import_items_ids);
301 unless ($leave_as_staging) {
302 SetImportBatchStatus($batch_id, 'staged');
304 # FIXME branch_code, number of bibs, number of items
305 _update_batch_record_counts($batch_id);
306 return ($batch_id, $num_valid, $num_items, @invalid_records);
309 =head2 AddItemsToImportBiblio
313 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, $update_counts);
319 sub AddItemsToImportBiblio {
320 my $batch_id = shift;
321 my $import_record_id = shift;
322 my $marc_record = shift;
323 my $update_counts = @_ ? shift : 0;
325 my @import_items_ids = ();
327 my $dbh = C4::Context->dbh;
328 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
329 foreach my $item_field ($marc_record->field($item_tag)) {
330 my $item_marc = MARC::Record->new();
331 $item_marc->leader("00000 a "); # must set Leader/09 to 'a'
332 $item_marc->append_fields($item_field);
333 $marc_record->delete_field($item_field);
334 my $sth = $dbh->prepare_cached("INSERT INTO import_items (import_record_id, status, marcxml)
336 $sth->bind_param(1, $import_record_id);
337 $sth->bind_param(2, 'staged');
338 $sth->bind_param(3, $item_marc->as_xml());
340 push @import_items_ids, $dbh->{'mysql_insertid'};
344 if ($#import_items_ids > -1) {
345 _update_batch_record_counts($batch_id) if $update_counts;
346 _update_import_record_marc($import_record_id, $marc_record);
348 return @import_items_ids;
351 =head2 BatchFindBibDuplicates
355 my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, $max_matches, $progress_interval, $progress_callback);
359 Goes through the records loaded in the batch and attempts to
360 find duplicates for each one. Sets the matching status
361 of each record to "no_match" or "auto_match" as appropriate.
363 The $max_matches parameter is optional; if it is not supplied,
366 The $progress_interval and $progress_callback parameters are
367 optional; if both are supplied, the sub referred to by
368 $progress_callback will be invoked every $progress_interval
369 records using the number of records processed as the
374 sub BatchFindBibDuplicates {
375 my $batch_id = shift;
377 my $max_matches = @_ ? shift : 10;
379 # optional callback to monitor status
381 my $progress_interval = 0;
382 my $progress_callback = undef;
384 $progress_interval = shift;
385 $progress_callback = shift;
386 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
387 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
390 my $dbh = C4::Context->dbh;
392 my $sth = $dbh->prepare("SELECT import_record_id, marc
394 JOIN import_biblios USING (import_record_id)
395 WHERE import_batch_id = ?");
396 $sth->execute($batch_id);
397 my $num_with_matches = 0;
399 while (my $rowref = $sth->fetchrow_hashref) {
401 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
402 &$progress_callback($rec_num);
404 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
406 if (defined $matcher) {
407 @matches = $matcher->get_matches($marc_record, $max_matches);
409 if (scalar(@matches) > 0) {
411 SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
412 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
414 SetImportRecordMatches($rowref->{'import_record_id'}, ());
415 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
419 return $num_with_matches;
422 =head2 BatchCommitBibRecords
426 my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) =
427 BatchCommitBibRecords($batch_id, $progress_interval, $progress_callback);
433 sub BatchCommitBibRecords {
434 my $batch_id = shift;
436 # optional callback to monitor status
438 my $progress_interval = 0;
439 my $progress_callback = undef;
441 $progress_interval = shift;
442 $progress_callback = shift;
443 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
444 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
449 my $num_items_added = 0;
450 my $num_items_errored = 0;
452 # commit (i.e., save, all records in the batch)
453 # FIXME biblio only at the moment
454 SetImportBatchStatus('importing');
455 my $overlay_action = GetImportBatchOverlayAction($batch_id);
456 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
457 my $item_action = GetImportBatchItemAction($batch_id);
458 my $dbh = C4::Context->dbh;
459 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marc, encoding
461 JOIN import_biblios USING (import_record_id)
462 WHERE import_batch_id = ?");
463 $sth->execute($batch_id);
465 while (my $rowref = $sth->fetchrow_hashref) {
467 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
468 &$progress_callback($rec_num);
470 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
475 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
477 # remove any item tags - rely on BatchCommitItems
478 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
479 foreach my $item_field ($marc_record->field($item_tag)) {
480 $marc_record->delete_field($item_field);
483 # decide what what to do with the bib and item records
484 my ($bib_result, $item_result, $bib_match) =
485 _get_commit_action($overlay_action, $nomatch_action, $item_action,
486 $rowref->{'overlay_status'}, $rowref->{'import_record_id'});
488 if ($bib_result eq 'create_new') {
490 my ($biblionumber, $biblioitemnumber) = AddBiblio($marc_record, '');
491 my $sth = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
492 $sth->execute($biblionumber, $rowref->{'import_record_id'});
494 if ($item_result eq 'create_new') {
495 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
496 $num_items_added += $bib_items_added;
497 $num_items_errored += $bib_items_errored;
499 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
500 } elsif ($bib_result eq 'replace') {
502 my $biblionumber = $bib_match;
503 my ($count, $oldbiblio) = GetBiblio($biblionumber);
504 my $oldxml = GetXmlBiblio($biblionumber);
506 # remove item fields so that they don't get
507 # added again if record is reverted
508 my $old_marc = MARC::Record->new_from_xml(StripNonXmlChars($oldxml), 'UTF-8', $rowref->{'encoding'});
509 foreach my $item_field ($old_marc->field($item_tag)) {
510 $old_marc->delete_field($item_field);
513 ModBiblio($marc_record, $biblionumber, $oldbiblio->{'frameworkcode'});
514 my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
515 $sth->execute($old_marc->as_xml(), $rowref->{'import_record_id'});
517 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
518 $sth2->execute($biblionumber, $rowref->{'import_record_id'});
520 if ($item_result eq 'create_new') {
521 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
522 $num_items_added += $bib_items_added;
523 $num_items_errored += $bib_items_errored;
525 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
526 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
527 } elsif ($bib_result eq 'ignore') {
529 my $biblionumber = $bib_match;
530 if (defined $biblionumber and $item_result eq 'create_new') {
531 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
532 $num_items_added += $bib_items_added;
533 $num_items_errored += $bib_items_errored;
534 # still need to record the matched biblionumber so that the
535 # items can be reverted
536 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
537 $sth2->execute($biblionumber, $rowref->{'import_record_id'});
538 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
540 SetImportRecordStatus($rowref->{'import_record_id'}, 'ignored');
544 SetImportBatchStatus($batch_id, 'imported');
545 return ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored);
548 =head2 BatchCommitItems
552 ($num_items_added, $num_items_errored) = BatchCommitItems($import_record_id, $biblionumber);
558 sub BatchCommitItems {
559 my ($import_record_id, $biblionumber) = @_;
561 my $dbh = C4::Context->dbh;
563 my $num_items_added = 0;
564 my $num_items_errored = 0;
565 my $sth = $dbh->prepare("SELECT import_items_id, import_items.marcxml, encoding
567 JOIN import_records USING (import_record_id)
568 WHERE import_record_id = ?
569 ORDER BY import_items_id");
570 $sth->bind_param(1, $import_record_id);
572 while (my $row = $sth->fetchrow_hashref()) {
573 my $item_marc = MARC::Record->new_from_xml(StripNonXmlChars($row->{'marcxml'}), 'UTF-8', $row->{'encoding'});
574 # FIXME - duplicate barcode check needs to become part of AddItemFromMarc()
575 my $item = TransformMarcToKoha($dbh, $item_marc);
576 my $duplicate_barcode = exists($item->{'barcode'}) && GetItemnumberFromBarcode($item->{'barcode'});
577 if ($duplicate_barcode) {
578 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, import_error = ? WHERE import_items_id = ?");
579 $updsth->bind_param(1, 'error');
580 $updsth->bind_param(2, 'duplicate item barcode');
581 $updsth->bind_param(3, $row->{'import_items_id'});
583 $num_items_errored++;
585 my ($item_biblionumber, $biblioitemnumber, $itemnumber) = AddItemFromMarc($item_marc, $biblionumber);
586 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
587 $updsth->bind_param(1, 'imported');
588 $updsth->bind_param(2, $itemnumber);
589 $updsth->bind_param(3, $row->{'import_items_id'});
596 return ($num_items_added, $num_items_errored);
599 =head2 BatchRevertBibRecords
603 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) = BatchRevertBibRecords($batch_id);
609 sub BatchRevertBibRecords {
610 my $batch_id = shift;
614 my $num_reverted = 0;
615 my $num_items_deleted = 0;
617 # commit (i.e., save, all records in the batch)
618 # FIXME biblio only at the moment
619 SetImportBatchStatus('reverting');
620 my $overlay_action = GetImportBatchOverlayAction($batch_id);
621 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
622 my $dbh = C4::Context->dbh;
623 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marcxml_old, encoding, matched_biblionumber
625 JOIN import_biblios USING (import_record_id)
626 WHERE import_batch_id = ?");
627 $sth->execute($batch_id);
628 while (my $rowref = $sth->fetchrow_hashref) {
629 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
634 my $bib_result = _get_revert_action($overlay_action, $rowref->{'overlay_status'}, $rowref->{'status'});
636 if ($bib_result eq 'delete') {
637 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
638 my $error = DelBiblio($rowref->{'matched_biblionumber'});
639 if (defined $error) {
643 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
645 } elsif ($bib_result eq 'restore') {
647 my $old_record = MARC::Record->new_from_xml(StripNonXmlChars($rowref->{'marcxml_old'}), 'UTF-8', $rowref->{'encoding'});
648 my $biblionumber = $rowref->{'matched_biblionumber'};
649 my ($count, $oldbiblio) = GetBiblio($biblionumber);
650 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
651 ModBiblio($old_record, $biblionumber, $oldbiblio->{'frameworkcode'});
652 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
653 } elsif ($bib_result eq 'ignore') {
654 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
655 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
660 SetImportBatchStatus($batch_id, 'reverted');
661 return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored);
664 =head2 BatchRevertItems
668 my $num_items_deleted = BatchRevertItems($import_record_id, $biblionumber);
674 sub BatchRevertItems {
675 my ($import_record_id, $biblionumber) = @_;
677 my $dbh = C4::Context->dbh;
678 my $num_items_deleted = 0;
680 my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
682 JOIN items USING (itemnumber)
683 WHERE import_record_id = ?");
684 $sth->bind_param(1, $import_record_id);
686 while (my $row = $sth->fetchrow_hashref()) {
687 DelItem($dbh, $biblionumber, $row->{'itemnumber'});
688 my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?");
689 $updsth->bind_param(1, 'reverted');
690 $updsth->bind_param(2, $row->{'import_items_id'});
693 $num_items_deleted++;
696 return $num_items_deleted;
699 =head2 GetAllImportBatches
703 my $results = GetAllImportBatches();
707 Returns a references to an array of hash references corresponding
708 to all import_batches rows (of batch_type 'batch'), sorted in
709 ascending order by import_batch_id.
713 sub GetAllImportBatches {
714 my $dbh = C4::Context->dbh;
715 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
716 WHERE batch_type = 'batch'
717 ORDER BY import_batch_id ASC");
721 while (my $row = $sth->fetchrow_hashref) {
722 push @$results, $row;
728 =head2 GetImportBatchRangeDesc
732 my $results = GetImportBatchRangeDesc($offset, $results_per_group);
736 Returns a reference to an array of hash references corresponding to
737 import_batches rows (sorted in descending order by import_batch_id)
738 start at the given offset.
742 sub GetImportBatchRangeDesc {
743 my ($offset, $results_per_group) = @_;
745 my $dbh = C4::Context->dbh;
746 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
747 WHERE batch_type = 'batch'
748 ORDER BY import_batch_id DESC
750 $sth->bind_param(1, $results_per_group);
751 $sth->bind_param(2, $offset);
755 while (my $row = $sth->fetchrow_hashref) {
756 push @$results, $row;
762 =head2 GetItemNumbersFromImportBatch
766 sub GetItemNumbersFromImportBatch {
768 my $dbh = C4::Context->dbh;
769 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=?");
770 $sth->execute($batch_id);
772 while ( my ($itm) = $sth->fetchrow_array ) {
778 =head2 GetNumberOfImportBatches
782 my $count = GetNumberOfImportBatches();
788 sub GetNumberOfNonZ3950ImportBatches {
789 my $dbh = C4::Context->dbh;
790 my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type='batch'");
792 my ($count) = $sth->fetchrow_array();
797 =head2 GetImportBibliosRange
801 my $results = GetImportBibliosRange($batch_id, $offset, $results_per_group);
805 Returns a reference to an array of hash references corresponding to
806 import_biblios/import_records rows for a given batch
807 starting at the given offset.
811 sub GetImportBibliosRange {
812 my ($batch_id, $offset, $results_per_group) = @_;
814 my $dbh = C4::Context->dbh;
815 my $sth = $dbh->prepare_cached("SELECT title, author, isbn, issn, import_record_id, record_sequence,
816 matched_biblionumber, status, overlay_status
818 JOIN import_biblios USING (import_record_id)
819 WHERE import_batch_id = ?
820 ORDER BY import_record_id LIMIT ? OFFSET ?");
821 $sth->bind_param(1, $batch_id);
822 $sth->bind_param(2, $results_per_group);
823 $sth->bind_param(3, $offset);
826 while (my $row = $sth->fetchrow_hashref) {
827 push @$results, $row;
834 =head2 GetBestRecordMatch
838 my $record_id = GetBestRecordMatch($import_record_id);
844 sub GetBestRecordMatch {
845 my ($import_record_id) = @_;
847 my $dbh = C4::Context->dbh;
848 my $sth = $dbh->prepare("SELECT candidate_match_id
849 FROM import_record_matches
850 WHERE import_record_id = ?
851 ORDER BY score DESC, candidate_match_id DESC");
852 $sth->execute($import_record_id);
853 my ($record_id) = $sth->fetchrow_array();
858 =head2 GetImportBatchStatus
862 my $status = GetImportBatchStatus($batch_id);
868 sub GetImportBatchStatus {
871 my $dbh = C4::Context->dbh;
872 my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE import_batch_id = ?");
873 $sth->execute($batch_id);
874 my ($status) = $sth->fetchrow_array();
880 =head2 SetImportBatchStatus
884 SetImportBatchStatus($batch_id, $new_status);
890 sub SetImportBatchStatus {
891 my ($batch_id, $new_status) = @_;
893 my $dbh = C4::Context->dbh;
894 my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
895 $sth->execute($new_status, $batch_id);
900 =head2 GetImportBatchOverlayAction
904 my $overlay_action = GetImportBatchOverlayAction($batch_id);
910 sub GetImportBatchOverlayAction {
913 my $dbh = C4::Context->dbh;
914 my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
915 $sth->execute($batch_id);
916 my ($overlay_action) = $sth->fetchrow_array();
918 return $overlay_action;
923 =head2 SetImportBatchOverlayAction
927 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
933 sub SetImportBatchOverlayAction {
934 my ($batch_id, $new_overlay_action) = @_;
936 my $dbh = C4::Context->dbh;
937 my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
938 $sth->execute($new_overlay_action, $batch_id);
943 =head2 GetImportBatchNoMatchAction
947 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
953 sub GetImportBatchNoMatchAction {
956 my $dbh = C4::Context->dbh;
957 my $sth = $dbh->prepare("SELECT nomatch_action FROM import_batches WHERE import_batch_id = ?");
958 $sth->execute($batch_id);
959 my ($nomatch_action) = $sth->fetchrow_array();
961 return $nomatch_action;
966 =head2 SetImportBatchNoMatchAction
970 SetImportBatchNoMatchAction($batch_id, $new_nomatch_action);
976 sub SetImportBatchNoMatchAction {
977 my ($batch_id, $new_nomatch_action) = @_;
979 my $dbh = C4::Context->dbh;
980 my $sth = $dbh->prepare("UPDATE import_batches SET nomatch_action = ? WHERE import_batch_id = ?");
981 $sth->execute($new_nomatch_action, $batch_id);
986 =head2 GetImportBatchItemAction
990 my $item_action = GetImportBatchItemAction($batch_id);
996 sub GetImportBatchItemAction {
999 my $dbh = C4::Context->dbh;
1000 my $sth = $dbh->prepare("SELECT item_action FROM import_batches WHERE import_batch_id = ?");
1001 $sth->execute($batch_id);
1002 my ($item_action) = $sth->fetchrow_array();
1004 return $item_action;
1009 =head2 SetImportBatchItemAction
1013 SetImportBatchItemAction($batch_id, $new_item_action);
1019 sub SetImportBatchItemAction {
1020 my ($batch_id, $new_item_action) = @_;
1022 my $dbh = C4::Context->dbh;
1023 my $sth = $dbh->prepare("UPDATE import_batches SET item_action = ? WHERE import_batch_id = ?");
1024 $sth->execute($new_item_action, $batch_id);
1029 =head2 GetImportBatchMatcher
1033 my $matcher_id = GetImportBatchMatcher($batch_id);
1039 sub GetImportBatchMatcher {
1040 my ($batch_id) = @_;
1042 my $dbh = C4::Context->dbh;
1043 my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
1044 $sth->execute($batch_id);
1045 my ($matcher_id) = $sth->fetchrow_array();
1052 =head2 SetImportBatchMatcher
1056 SetImportBatchMatcher($batch_id, $new_matcher_id);
1062 sub SetImportBatchMatcher {
1063 my ($batch_id, $new_matcher_id) = @_;
1065 my $dbh = C4::Context->dbh;
1066 my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
1067 $sth->execute($new_matcher_id, $batch_id);
1072 =head2 GetImportRecordOverlayStatus
1076 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
1082 sub GetImportRecordOverlayStatus {
1083 my ($import_record_id) = @_;
1085 my $dbh = C4::Context->dbh;
1086 my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
1087 $sth->execute($import_record_id);
1088 my ($overlay_status) = $sth->fetchrow_array();
1090 return $overlay_status;
1095 =head2 SetImportRecordOverlayStatus
1099 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
1105 sub SetImportRecordOverlayStatus {
1106 my ($import_record_id, $new_overlay_status) = @_;
1108 my $dbh = C4::Context->dbh;
1109 my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
1110 $sth->execute($new_overlay_status, $import_record_id);
1115 =head2 GetImportRecordStatus
1119 my $overlay_status = GetImportRecordStatus($import_record_id);
1125 sub GetImportRecordStatus {
1126 my ($import_record_id) = @_;
1128 my $dbh = C4::Context->dbh;
1129 my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
1130 $sth->execute($import_record_id);
1131 my ($overlay_status) = $sth->fetchrow_array();
1133 return $overlay_status;
1138 =head2 SetImportRecordStatus
1142 SetImportRecordStatus($import_record_id, $new_overlay_status);
1148 sub SetImportRecordStatus {
1149 my ($import_record_id, $new_overlay_status) = @_;
1151 my $dbh = C4::Context->dbh;
1152 my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
1153 $sth->execute($new_overlay_status, $import_record_id);
1158 =head2 GetImportRecordMatches
1162 my $results = GetImportRecordMatches($import_record_id, $best_only);
1168 sub GetImportRecordMatches {
1169 my $import_record_id = shift;
1170 my $best_only = @_ ? shift : 0;
1172 my $dbh = C4::Context->dbh;
1173 # FIXME currently biblio only
1174 my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber, score
1176 JOIN import_record_matches USING (import_record_id)
1177 JOIN biblio ON (biblionumber = candidate_match_id)
1178 WHERE import_record_id = ?
1179 ORDER BY score DESC, biblionumber DESC");
1180 $sth->bind_param(1, $import_record_id);
1183 while (my $row = $sth->fetchrow_hashref) {
1184 push @$results, $row;
1194 =head2 SetImportRecordMatches
1198 SetImportRecordMatches($import_record_id, @matches);
1204 sub SetImportRecordMatches {
1205 my $import_record_id = shift;
1208 my $dbh = C4::Context->dbh;
1209 my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
1210 $delsth->execute($import_record_id);
1213 my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
1215 foreach my $match (@matches) {
1216 $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
1221 # internal functions
1223 sub _create_import_record {
1224 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random) = @_;
1226 my $dbh = C4::Context->dbh;
1227 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml,
1228 record_type, encoding, z3950random)
1229 VALUES (?, ?, ?, ?, ?, ?, ?)");
1230 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(),
1231 $record_type, $encoding, $z3950random);
1232 my $import_record_id = $dbh->{'mysql_insertid'};
1234 return $import_record_id;
1237 sub _update_import_record_marc {
1238 my ($import_record_id, $marc_record) = @_;
1240 my $dbh = C4::Context->dbh;
1241 my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
1242 WHERE import_record_id = ?");
1243 $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml(), $import_record_id);
1247 sub _add_biblio_fields {
1248 my ($import_record_id, $marc_record) = @_;
1250 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1251 my $dbh = C4::Context->dbh;
1252 # FIXME no controlnumber, originalsource
1253 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1257 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
1258 $sth->execute($import_record_id, $title, $author, $isbn, $issn);
1263 sub _update_biblio_fields {
1264 my ($import_record_id, $marc_record) = @_;
1266 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1267 my $dbh = C4::Context->dbh;
1268 # FIXME no controlnumber, originalsource
1269 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1273 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
1274 WHERE import_record_id = ?");
1275 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
1279 sub _parse_biblio_fields {
1280 my ($marc_record) = @_;
1282 my $dbh = C4::Context->dbh;
1283 my $bibliofields = TransformMarcToKoha($dbh, $marc_record, '');
1284 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
1288 sub _update_batch_record_counts {
1289 my ($batch_id) = @_;
1291 my $dbh = C4::Context->dbh;
1292 my $sth = $dbh->prepare_cached("UPDATE import_batches SET num_biblios = (
1295 WHERE import_batch_id = import_batches.import_batch_id
1296 AND record_type = 'biblio')
1297 WHERE import_batch_id = ?");
1298 $sth->bind_param(1, $batch_id);
1301 $sth = $dbh->prepare_cached("UPDATE import_batches SET num_items = (
1304 JOIN import_items USING (import_record_id)
1305 WHERE import_batch_id = import_batches.import_batch_id
1306 AND record_type = 'biblio')
1307 WHERE import_batch_id = ?");
1308 $sth->bind_param(1, $batch_id);
1314 sub _get_commit_action {
1315 my ($overlay_action, $nomatch_action, $item_action, $overlay_status, $import_record_id) = @_;
1317 my ($bib_result, $bib_match, $item_result);
1319 if ($overlay_status ne 'no_match') {
1320 $bib_match = GetBestRecordMatch($import_record_id);
1321 if ($overlay_action eq 'replace') {
1322 $bib_result = defined($bib_match) ? 'replace' : 'create_new';
1323 } elsif ($overlay_action eq 'create_new') {
1324 $bib_result = 'create_new';
1325 } elsif ($overlay_action eq 'ignore') {
1326 $bib_result = 'ignore';
1328 $item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_matches') ? 'create_new' : 'ignore';
1330 $bib_result = $nomatch_action;
1331 $item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_new') ? 'create_new' : 'ignore';
1334 return ($bib_result, $item_result, $bib_match);
1337 sub _get_revert_action {
1338 my ($overlay_action, $overlay_status, $status) = @_;
1342 if ($status eq 'ignored') {
1343 $bib_result = 'ignore';
1345 if ($overlay_action eq 'create_new') {
1346 $bib_result = 'delete';
1348 $bib_result = ($overlay_status eq 'match_applied') ? 'restore' : 'delete';
1359 Koha Development Team <info@koha.org>
1361 Galen Charlton <galen.charlton@liblime.com>