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
26 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
29 # set the version for version checking
42 BatchFindBibDuplicates
47 GetImportBatchRangeDesc
48 GetNumberOfNonZ3950ImportBatches
53 GetImportBatchOverlayAction
54 SetImportBatchOverlayAction
57 GetImportRecordOverlayStatus
58 SetImportRecordOverlayStatus
61 GetImportRecordMatches
62 SetImportRecordMatches
68 C4::ImportBatch - manage batches of imported MARC records
80 =head2 GetZ3950BatchId
84 my $batchid = GetZ3950BatchId($z3950server);
88 Retrieves the ID of the import batch for the Z39.50
89 reservoir for the given target. If necessary,
90 creates the import batch.
95 my ($z3950server) = @_;
97 my $dbh = C4::Context->dbh;
98 my $sth = $dbh->prepare("SELECT import_batch_id FROM import_batches
99 WHERE batch_type = 'z3950'
101 $sth->execute($z3950server);
102 my $rowref = $sth->fetchrow_arrayref();
104 if (defined $rowref) {
107 my $batch_id = AddImportBatch('create_new', 'staged', 'z3950', $z3950server, '');
113 =head2 GetImportRecordMarc
117 my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
123 sub GetImportRecordMarc {
124 my ($import_record_id) = @_;
126 my $dbh = C4::Context->dbh;
127 my $sth = $dbh->prepare("SELECT marc, encoding FROM import_records WHERE import_record_id = ?");
128 $sth->execute($import_record_id);
129 my ($marc, $encoding) = $sth->fetchrow();
135 =head2 AddImportBatch
139 my $batch_id = AddImportBatch($overlay_action, $import_status, $type, $file_name, $comments);
146 my ($overlay_action, $import_status, $type, $file_name, $comments) = @_;
148 my $dbh = C4::Context->dbh;
149 my $sth = $dbh->prepare("INSERT INTO import_batches (overlay_action, import_status, batch_type,
151 VALUES (?, ?, ?, ?, ?)");
152 $sth->execute($overlay_action, $import_status, $type, $file_name, $comments);
153 my $batch_id = $dbh->{'mysql_insertid'};
160 =head2 GetImportBatch
164 my $row = GetImportBatch($batch_id);
168 Retrieve a hashref of an import_batches row.
175 my $dbh = C4::Context->dbh;
176 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches WHERE import_batch_id = ?");
177 $sth->bind_param(1, $batch_id);
179 my $result = $sth->fetchrow_hashref;
185 =head2 AddBiblioToBatch
189 my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence, $marc_record, $encoding, $z3950random, $update_counts);
195 sub AddBiblioToBatch {
196 my $batch_id = shift;
197 my $record_sequence = shift;
198 my $marc_record = shift;
199 my $encoding = shift;
200 my $z3950random = shift;
201 my $update_counts = @_ ? shift : 1;
203 my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, $z3950random);
204 _add_biblio_fields($import_record_id, $marc_record);
205 _update_batch_record_counts($batch_id) if $update_counts;
206 return $import_record_id;
209 =head2 ModBiblioInBatch
213 ModBiblioInBatch($import_record_id, $marc_record);
219 sub ModBiblioInBatch {
220 my ($import_record_id, $marc_record) = @_;
222 _update_import_record_marc($import_record_id, $marc_record);
223 _update_biblio_fields($import_record_id, $marc_record);
227 =head2 BatchStageMarcRecords
231 ($batch_id, $num_records, $num_items, @invalid_records) =
232 BatchStageMarcRecords($marc_flavor, $marc_records, $file_name,
233 $comments, $branch_code, $parse_items,
235 $progress_interval, $progress_callback);
241 sub BatchStageMarcRecords {
242 my $marc_flavor = shift;
243 my $marc_records = shift;
244 my $file_name = shift;
245 my $comments = shift;
246 my $branch_code = shift;
247 my $parse_items = shift;
248 my $leave_as_staging = shift;
250 # optional callback to monitor status
252 my $progress_interval = 0;
253 my $progress_callback = undef;
255 $progress_interval = shift;
256 $progress_callback = shift;
257 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
258 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
261 my $batch_id = AddImportBatch('create_new', 'staging', 'batch', $file_name, $comments);
262 my @invalid_records = ();
265 # FIXME - for now, we're dealing only with bibs
267 foreach my $marc_blob (split(/\x1D/, $marc_records)) {
269 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
270 &$progress_callback($rec_num);
272 my $marc_record = FixEncoding($marc_blob);
273 my $import_record_id;
274 if (scalar($marc_record->fields()) == 0) {
275 push @invalid_records, $marc_blob;
278 $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $marc_flavor, int(rand(99999)), 0);
280 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0);
281 $num_items += scalar(@import_items_ids);
285 unless ($leave_as_staging) {
286 SetImportBatchStatus($batch_id, 'staged');
288 # FIXME branch_code, number of bibs, number of items
289 _update_batch_record_counts($batch_id);
290 return ($batch_id, $num_valid, $num_items, @invalid_records);
293 =head2 AddItemsToImportBiblio
297 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, $update_counts);
303 sub AddItemsToImportBiblio {
304 my $batch_id = shift;
305 my $import_record_id = shift;
306 my $marc_record = shift;
307 my $update_counts = @_ ? shift : 0;
309 my @import_items_ids = ();
311 my $dbh = C4::Context->dbh;
312 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
313 foreach my $item_field ($marc_record->field($item_tag)) {
314 my $item_marc = MARC::Record->new();
315 $item_marc->leader("00000 a "); # must set Leader/09 to 'a'
316 $item_marc->append_fields($item_field);
317 $marc_record->delete_field($item_field);
318 my $sth = $dbh->prepare_cached("INSERT INTO import_items (import_record_id, status, marcxml)
320 $sth->bind_param(1, $import_record_id);
321 $sth->bind_param(2, 'staged');
322 $sth->bind_param(3, $item_marc->as_xml());
324 push @import_items_ids, $dbh->{'mysql_insertid'};
328 if ($#import_items_ids > -1) {
329 _update_batch_record_counts($batch_id) if $update_counts;
330 _update_import_record_marc($import_record_id, $marc_record);
332 return @import_items_ids;
335 =head2 BatchFindBibDuplicates
339 my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, $max_matches, $progress_interval, $progress_callback);
343 Goes through the records loaded in the batch and attempts to
344 find duplicates for each one. Sets the overlay action to
345 "replace" if it was "create_new", and sets the overlay status
346 of each record to "no_match" or "auto_match" as appropriate.
348 The $max_matches parameter is optional; if it is not supplied,
351 The $progress_interval and $progress_callback parameters are
352 optional; if both are supplied, the sub referred to by
353 $progress_callback will be invoked every $progress_interval
354 records using the number of records processed as the
359 sub BatchFindBibDuplicates {
360 my $batch_id = shift;
362 my $max_matches = @_ ? shift : 10;
364 # optional callback to monitor status
366 my $progress_interval = 0;
367 my $progress_callback = undef;
369 $progress_interval = shift;
370 $progress_callback = shift;
371 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
372 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
375 my $dbh = C4::Context->dbh;
376 my $old_overlay_action = GetImportBatchOverlayAction($batch_id);
377 if ($old_overlay_action eq "create_new") {
378 SetImportBatchOverlayAction($batch_id, 'replace');
381 my $sth = $dbh->prepare("SELECT import_record_id, marc
383 JOIN import_biblios USING (import_record_id)
384 WHERE import_batch_id = ?");
385 $sth->execute($batch_id);
386 my $num_with_matches = 0;
388 while (my $rowref = $sth->fetchrow_hashref) {
390 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
391 &$progress_callback($rec_num);
393 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
395 if (defined $matcher) {
396 @matches = $matcher->get_matches($marc_record, $max_matches);
398 if (scalar(@matches) > 0) {
400 SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
401 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
403 SetImportRecordMatches($rowref->{'import_record_id'}, ());
404 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
408 return $num_with_matches;
411 =head2 BatchCommitBibRecords
415 my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) =
416 BatchCommitBibRecords($batch_id, $progress_interval, $progress_callback);
422 sub BatchCommitBibRecords {
423 my $batch_id = shift;
425 # optional callback to monitor status
427 my $progress_interval = 0;
428 my $progress_callback = undef;
430 $progress_interval = shift;
431 $progress_callback = shift;
432 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
433 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
438 my $num_items_added = 0;
439 my $num_items_errored = 0;
441 # commit (i.e., save, all records in the batch)
442 # FIXME biblio only at the moment
443 SetImportBatchStatus('importing');
444 my $overlay_action = GetImportBatchOverlayAction($batch_id);
445 my $dbh = C4::Context->dbh;
446 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marc, encoding
448 JOIN import_biblios USING (import_record_id)
449 WHERE import_batch_id = ?");
450 $sth->execute($batch_id);
452 while (my $rowref = $sth->fetchrow_hashref) {
454 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
455 &$progress_callback($rec_num);
457 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
461 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
463 # remove any item tags - rely on BatchCommitItems
464 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
465 foreach my $item_field ($marc_record->field($item_tag)) {
466 $marc_record->delete_field($item_field);
469 if ($overlay_action eq 'create_new' or
470 ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
472 my ($biblionumber, $biblioitemnumber) = AddBiblio($marc_record, '');
473 my $sth = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
474 $sth->execute($biblionumber, $rowref->{'import_record_id'});
476 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
477 $num_items_added += $bib_items_added;
478 $num_items_errored += $bib_items_errored;
479 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
482 my $biblionumber = GetBestRecordMatch($rowref->{'import_record_id'});
483 my ($count, $oldbiblio) = GetBiblio($biblionumber);
484 my $oldxml = GetXmlBiblio($biblionumber);
486 # remove item fields so that they don't get
487 # added again if record is reverted
488 my $old_marc = MARC::Record->new_from_xml($oldxml, 'UTF-8', $rowref->{'encoding'});
489 foreach my $item_field ($old_marc->field($item_tag)) {
490 $old_marc->delete_field($item_field);
493 ModBiblio($marc_record, $biblionumber, $oldbiblio->{'frameworkcode'});
494 my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
495 $sth->execute($old_marc->as_xml(), $rowref->{'import_record_id'});
497 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
498 $sth2->execute($biblionumber, $rowref->{'import_record_id'});
500 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
501 $num_items_added += $bib_items_added;
502 $num_items_errored += $bib_items_errored;
503 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
504 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
508 SetImportBatchStatus($batch_id, 'imported');
509 return ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored);
512 =head2 BatchCommitItems
516 ($num_items_added, $num_items_errored) = BatchCommitItems($import_record_id, $biblionumber);
522 sub BatchCommitItems {
523 my ($import_record_id, $biblionumber) = @_;
525 my $dbh = C4::Context->dbh;
527 my $num_items_added = 0;
528 my $num_items_errored = 0;
529 my $sth = $dbh->prepare("SELECT import_items_id, import_items.marcxml, encoding
531 JOIN import_records USING (import_record_id)
532 WHERE import_record_id = ?
533 ORDER BY import_items_id");
534 $sth->bind_param(1, $import_record_id);
536 while (my $row = $sth->fetchrow_hashref()) {
537 my $item_marc = MARC::Record->new_from_xml($row->{'marcxml'}, 'UTF-8', $row->{'encoding'});
538 # FIXME - duplicate barcode check needs to become part of AddItemFromMarc()
539 my $item = TransformMarcToKoha($dbh, $item_marc);
540 my $duplicate_barcode = exists($item->{'barcode'}) && GetItemnumberFromBarcode($item->{'barcode'});
541 if ($duplicate_barcode) {
542 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, import_error = ? WHERE import_items_id = ?");
543 $updsth->bind_param(1, 'error');
544 $updsth->bind_param(2, 'duplicate item barcode');
545 $updsth->bind_param(3, $row->{'import_items_id'});
547 $num_items_errored++;
549 my ($item_biblionumber, $biblioitemnumber, $itemnumber) = AddItemFromMarc($item_marc, $biblionumber);
550 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
551 $updsth->bind_param(1, 'imported');
552 $updsth->bind_param(2, $itemnumber);
553 $updsth->bind_param(3, $row->{'import_items_id'});
560 return ($num_items_added, $num_items_errored);
563 =head2 BatchRevertBibRecords
567 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) = BatchRevertBibRecords($batch_id);
573 sub BatchRevertBibRecords {
574 my $batch_id = shift;
578 my $num_reverted = 0;
579 my $num_items_deleted = 0;
581 # commit (i.e., save, all records in the batch)
582 # FIXME biblio only at the moment
583 SetImportBatchStatus('reverting');
584 my $overlay_action = GetImportBatchOverlayAction($batch_id);
585 my $dbh = C4::Context->dbh;
586 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marcxml_old, encoding, matched_biblionumber
588 JOIN import_biblios USING (import_record_id)
589 WHERE import_batch_id = ?");
590 $sth->execute($batch_id);
591 while (my $rowref = $sth->fetchrow_hashref) {
592 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
595 if ($overlay_action eq 'create_new' or
596 ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
597 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
598 my $error = DelBiblio($rowref->{'matched_biblionumber'});
599 if (defined $error) {
603 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
607 my $old_record = MARC::Record->new_from_xml($rowref->{'marcxml_old'}, 'UTF-8', $rowref->{'encoding'});
608 my $biblionumber = $rowref->{'matched_biblionumber'};
609 my ($count, $oldbiblio) = GetBiblio($biblionumber);
610 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
611 ModBiblio($old_record, $biblionumber, $oldbiblio->{'frameworkcode'});
612 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
616 SetImportBatchStatus($batch_id, 'reverted');
617 return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored);
620 =head2 BatchRevertItems
624 my $num_items_deleted = BatchRevertItems($import_record_id, $biblionumber);
630 sub BatchRevertItems {
631 my ($import_record_id, $biblionumber) = @_;
633 my $dbh = C4::Context->dbh;
634 my $num_items_deleted = 0;
636 my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
638 JOIN items USING (itemnumber)
639 WHERE import_record_id = ?");
640 $sth->bind_param(1, $import_record_id);
642 while (my $row = $sth->fetchrow_hashref()) {
643 DelItem($dbh, $biblionumber, $row->{'itemnumber'});
644 my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?");
645 $updsth->bind_param(1, 'reverted');
646 $updsth->bind_param(2, $row->{'import_items_id'});
649 $num_items_deleted++;
652 return $num_items_deleted;
655 =head2 GetAllImportBatches
659 my $results = GetAllImportBatches();
663 Returns a references to an array of hash references corresponding
664 to all import_batches rows (of batch_type 'batch'), sorted in
665 ascending order by import_batch_id.
669 sub GetAllImportBatches {
670 my $dbh = C4::Context->dbh;
671 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
672 WHERE batch_type = 'batch'
673 ORDER BY import_batch_id ASC");
677 while (my $row = $sth->fetchrow_hashref) {
678 push @$results, $row;
684 =head2 GetImportBatchRangeDesc
688 my $results = GetImportBatchRangeDesc($offset, $results_per_group);
692 Returns a reference to an array of hash references corresponding to
693 import_batches rows (sorted in descending order by import_batch_id)
694 start at the given offset.
698 sub GetImportBatchRangeDesc {
699 my ($offset, $results_per_group) = @_;
701 my $dbh = C4::Context->dbh;
702 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
703 WHERE batch_type = 'batch'
704 ORDER BY import_batch_id DESC
706 $sth->bind_param(1, $results_per_group);
707 $sth->bind_param(2, $offset);
711 while (my $row = $sth->fetchrow_hashref) {
712 push @$results, $row;
718 =head2 GetNumberOfImportBatches
722 my $count = GetNumberOfImportBatches();
728 sub GetNumberOfNonZ3950ImportBatches {
729 my $dbh = C4::Context->dbh;
730 my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type='batch'");
732 my ($count) = $sth->fetchrow_array();
737 =head2 GetImportBibliosRange
741 my $results = GetImportBibliosRange($batch_id, $offset, $results_per_group);
745 Returns a reference to an array of hash references corresponding to
746 import_biblios/import_records rows for a given batch
747 starting at the given offset.
751 sub GetImportBibliosRange {
752 my ($batch_id, $offset, $results_per_group) = @_;
754 my $dbh = C4::Context->dbh;
755 my $sth = $dbh->prepare_cached("SELECT title, author, isbn, issn, import_record_id, record_sequence,
756 status, overlay_status
758 JOIN import_biblios USING (import_record_id)
759 WHERE import_batch_id = ?
760 ORDER BY import_record_id LIMIT ? OFFSET ?");
761 $sth->bind_param(1, $batch_id);
762 $sth->bind_param(2, $results_per_group);
763 $sth->bind_param(3, $offset);
766 while (my $row = $sth->fetchrow_hashref) {
767 push @$results, $row;
774 =head2 GetBestRecordMatch
778 my $record_id = GetBestRecordMatch($import_record_id);
784 sub GetBestRecordMatch {
785 my ($import_record_id) = @_;
787 my $dbh = C4::Context->dbh;
788 my $sth = $dbh->prepare("SELECT candidate_match_id
789 FROM import_record_matches
790 WHERE import_record_id = ?
791 ORDER BY score DESC, candidate_match_id DESC");
792 $sth->execute($import_record_id);
793 my ($record_id) = $sth->fetchrow_array();
798 =head2 GetImportBatchStatus
802 my $status = GetImportBatchStatus($batch_id);
808 sub GetImportBatchStatus {
811 my $dbh = C4::Context->dbh;
812 my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE batch_id = ?");
813 $sth->execute($batch_id);
814 my ($status) = $sth->fetchrow_array();
821 =head2 SetImportBatchStatus
825 SetImportBatchStatus($batch_id, $new_status);
831 sub SetImportBatchStatus {
832 my ($batch_id, $new_status) = @_;
834 my $dbh = C4::Context->dbh;
835 my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
836 $sth->execute($new_status, $batch_id);
841 =head2 GetImportBatchOverlayAction
845 my $overlay_action = GetImportBatchOverlayAction($batch_id);
851 sub GetImportBatchOverlayAction {
854 my $dbh = C4::Context->dbh;
855 my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
856 $sth->execute($batch_id);
857 my ($overlay_action) = $sth->fetchrow_array();
859 return $overlay_action;
864 =head2 SetImportBatchOverlayAction
868 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
874 sub SetImportBatchOverlayAction {
875 my ($batch_id, $new_overlay_action) = @_;
877 my $dbh = C4::Context->dbh;
878 my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
879 $sth->execute($new_overlay_action, $batch_id);
884 =head2 GetImportBatchMatcher
888 my $matcher_id = GetImportBatchMatcher($batch_id);
894 sub GetImportBatchMatcher {
897 my $dbh = C4::Context->dbh;
898 my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
899 $sth->execute($batch_id);
900 my ($matcher_id) = $sth->fetchrow_array();
907 =head2 SetImportBatchMatcher
911 SetImportBatchMatcher($batch_id, $new_matcher_id);
917 sub SetImportBatchMatcher {
918 my ($batch_id, $new_matcher_id) = @_;
920 my $dbh = C4::Context->dbh;
921 my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
922 $sth->execute($new_matcher_id, $batch_id);
927 =head2 GetImportRecordOverlayStatus
931 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
937 sub GetImportRecordOverlayStatus {
938 my ($import_record_id) = @_;
940 my $dbh = C4::Context->dbh;
941 my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
942 $sth->execute($import_record_id);
943 my ($overlay_status) = $sth->fetchrow_array();
945 return $overlay_status;
950 =head2 SetImportRecordOverlayStatus
954 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
960 sub SetImportRecordOverlayStatus {
961 my ($import_record_id, $new_overlay_status) = @_;
963 my $dbh = C4::Context->dbh;
964 my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
965 $sth->execute($new_overlay_status, $import_record_id);
970 =head2 GetImportRecordStatus
974 my $overlay_status = GetImportRecordStatus($import_record_id);
980 sub GetImportRecordStatus {
981 my ($import_record_id) = @_;
983 my $dbh = C4::Context->dbh;
984 my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
985 $sth->execute($import_record_id);
986 my ($overlay_status) = $sth->fetchrow_array();
988 return $overlay_status;
993 =head2 SetImportRecordStatus
997 SetImportRecordStatus($import_record_id, $new_overlay_status);
1003 sub SetImportRecordStatus {
1004 my ($import_record_id, $new_overlay_status) = @_;
1006 my $dbh = C4::Context->dbh;
1007 my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
1008 $sth->execute($new_overlay_status, $import_record_id);
1013 =head2 GetImportRecordMatches
1017 my $results = GetImportRecordMatches($import_record_id, $best_only);
1023 sub GetImportRecordMatches {
1024 my $import_record_id = shift;
1025 my $best_only = @_ ? shift : 0;
1027 my $dbh = C4::Context->dbh;
1028 # FIXME currently biblio only
1029 my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber, score
1031 JOIN import_record_matches USING (import_record_id)
1032 JOIN biblio ON (biblionumber = candidate_match_id)
1033 WHERE import_record_id = ?
1034 ORDER BY score DESC, biblionumber DESC");
1035 $sth->bind_param(1, $import_record_id);
1038 while (my $row = $sth->fetchrow_hashref) {
1039 push @$results, $row;
1049 =head2 SetImportRecordMatches
1053 SetImportRecordMatches($import_record_id, @matches);
1059 sub SetImportRecordMatches {
1060 my $import_record_id = shift;
1063 my $dbh = C4::Context->dbh;
1064 my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
1065 $delsth->execute($import_record_id);
1068 my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
1070 foreach my $match (@matches) {
1071 $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
1076 # internal functions
1078 sub _create_import_record {
1079 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random) = @_;
1081 my $dbh = C4::Context->dbh;
1082 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml,
1083 record_type, encoding, z3950random)
1084 VALUES (?, ?, ?, ?, ?, ?, ?)");
1085 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(),
1086 $record_type, $encoding, $z3950random);
1087 my $import_record_id = $dbh->{'mysql_insertid'};
1089 return $import_record_id;
1092 sub _update_import_record_marc {
1093 my ($import_record_id, $marc_record) = @_;
1095 my $dbh = C4::Context->dbh;
1096 my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
1097 WHERE import_record_id = ?");
1098 $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml(), $import_record_id);
1102 sub _add_biblio_fields {
1103 my ($import_record_id, $marc_record) = @_;
1105 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1106 my $dbh = C4::Context->dbh;
1107 # FIXME no controlnumber, originalsource
1108 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1112 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
1113 $sth->execute($import_record_id, $title, $author, $isbn, $issn);
1118 sub _update_biblio_fields {
1119 my ($import_record_id, $marc_record) = @_;
1121 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1122 my $dbh = C4::Context->dbh;
1123 # FIXME no controlnumber, originalsource
1124 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1128 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
1129 WHERE import_record_id = ?");
1130 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
1134 sub _parse_biblio_fields {
1135 my ($marc_record) = @_;
1137 my $dbh = C4::Context->dbh;
1138 my $bibliofields = TransformMarcToKoha($dbh, $marc_record, '');
1139 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
1143 sub _update_batch_record_counts {
1144 my ($batch_id) = @_;
1146 my $dbh = C4::Context->dbh;
1147 my $sth = $dbh->prepare_cached("UPDATE import_batches SET num_biblios = (
1150 WHERE import_batch_id = import_batches.import_batch_id
1151 AND record_type = 'biblio')
1152 WHERE import_batch_id = ?");
1153 $sth->bind_param(1, $batch_id);
1156 $sth = $dbh->prepare_cached("UPDATE import_batches SET num_items = (
1159 JOIN import_items USING (import_record_id)
1160 WHERE import_batch_id = import_batches.import_batch_id
1161 AND record_type = 'biblio')
1162 WHERE import_batch_id = ?");
1163 $sth->bind_param(1, $batch_id);
1174 Koha Development Team <info@koha.org>
1176 Galen Charlton <galen.charlton@liblime.com>