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
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30 # set the version for version checking
35 C4::ImportBatch - manage batches of imported MARC records
59 BatchFindBibDuplicates
64 GetImportBatchRangeDesc
65 GetNumberOfNonZ3950ImportBatches
70 GetImportBatchOverlayAction
71 SetImportBatchOverlayAction
74 GetImportRecordOverlayStatus
75 SetImportRecordOverlayStatus
78 GetImportRecordMatches
79 SetImportRecordMatches
82 =head2 GetZ3950BatchId
86 my $batchid = GetZ3950BatchId($z3950server);
90 Retrieves the ID of the import batch for the Z39.50
91 reservoir for the given target. If necessary,
92 creates the import batch.
97 my ($z3950server) = @_;
99 my $dbh = C4::Context->dbh;
100 my $sth = $dbh->prepare("SELECT import_batch_id FROM import_batches
101 WHERE batch_type = 'z3950'
103 $sth->execute($z3950server);
104 my $rowref = $sth->fetchrow_arrayref();
106 if (defined $rowref) {
109 my $batch_id = AddImportBatch('create_new', 'staged', 'z3950', $z3950server, '');
115 =head2 GetImportRecordMarc
119 my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
125 sub GetImportRecordMarc {
126 my ($import_record_id) = @_;
128 my $dbh = C4::Context->dbh;
129 my $sth = $dbh->prepare("SELECT marc, encoding FROM import_records WHERE import_record_id = ?");
130 $sth->execute($import_record_id);
131 my ($marc, $encoding) = $sth->fetchrow();
137 =head2 AddImportBatch
141 my $batch_id = AddImportBatch($overlay_action, $import_status, $type, $file_name, $comments);
148 my ($overlay_action, $import_status, $type, $file_name, $comments) = @_;
150 my $dbh = C4::Context->dbh;
151 my $sth = $dbh->prepare("INSERT INTO import_batches (overlay_action, import_status, batch_type,
153 VALUES (?, ?, ?, ?, ?)");
154 $sth->execute($overlay_action, $import_status, $type, $file_name, $comments);
155 my $batch_id = $dbh->{'mysql_insertid'};
162 =head2 GetImportBatch
166 my $row = GetImportBatch($batch_id);
170 Retrieve a hashref of an import_batches row.
177 my $dbh = C4::Context->dbh;
178 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches WHERE import_batch_id = ?");
179 $sth->bind_param(1, $batch_id);
181 my $result = $sth->fetchrow_hashref;
187 =head2 AddBiblioToBatch
191 my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence, $marc_record, $encoding, $z3950random, $update_counts);
197 sub AddBiblioToBatch {
198 my $batch_id = shift;
199 my $record_sequence = shift;
200 my $marc_record = shift;
201 my $encoding = shift;
202 my $z3950random = shift;
203 my $update_counts = @_ ? shift : 1;
205 my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, $z3950random);
206 _add_biblio_fields($import_record_id, $marc_record);
207 _update_batch_record_counts($batch_id) if $update_counts;
208 return $import_record_id;
211 =head2 ModBiblioInBatch
215 ModBiblioInBatch($import_record_id, $marc_record);
221 sub ModBiblioInBatch {
222 my ($import_record_id, $marc_record) = @_;
224 _update_import_record_marc($import_record_id, $marc_record);
225 _update_biblio_fields($import_record_id, $marc_record);
229 =head2 BatchStageMarcRecords
233 ($batch_id, $num_records, $num_items, @invalid_records) =
234 BatchStageMarcRecords($marc_flavor, $marc_records, $file_name,
235 $comments, $branch_code, $parse_items,
237 $progress_interval, $progress_callback);
243 sub BatchStageMarcRecords {
244 my $marc_flavor = shift;
245 my $marc_records = shift;
246 my $file_name = shift;
247 my $comments = shift;
248 my $branch_code = shift;
249 my $parse_items = shift;
250 my $leave_as_staging = shift;
252 # optional callback to monitor status
254 my $progress_interval = 0;
255 my $progress_callback = undef;
257 $progress_interval = shift;
258 $progress_callback = shift;
259 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
260 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
263 my $batch_id = AddImportBatch('create_new', 'staging', 'batch', $file_name, $comments);
264 my @invalid_records = ();
267 # FIXME - for now, we're dealing only with bibs
269 foreach my $marc_blob (split(/\x1D/, $marc_records)) {
271 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
272 &$progress_callback($rec_num);
274 my $marc_record = FixEncoding($marc_blob, "\x1D");
275 my $import_record_id;
276 if (scalar($marc_record->fields()) == 0) {
277 push @invalid_records, $marc_blob;
280 $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $marc_flavor, int(rand(99999)), 0);
282 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0);
283 $num_items += scalar(@import_items_ids);
287 unless ($leave_as_staging) {
288 SetImportBatchStatus($batch_id, 'staged');
290 # FIXME branch_code, number of bibs, number of items
291 _update_batch_record_counts($batch_id);
292 return ($batch_id, $num_valid, $num_items, @invalid_records);
295 =head2 AddItemsToImportBiblio
299 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, $update_counts);
305 sub AddItemsToImportBiblio {
306 my $batch_id = shift;
307 my $import_record_id = shift;
308 my $marc_record = shift;
309 my $update_counts = @_ ? shift : 0;
311 my @import_items_ids = ();
313 my $dbh = C4::Context->dbh;
314 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
315 foreach my $item_field ($marc_record->field($item_tag)) {
316 my $item_marc = MARC::Record->new();
317 $item_marc->append_fields($item_field);
318 $marc_record->delete_field($item_field);
319 my $sth = $dbh->prepare_cached("INSERT INTO import_items (import_record_id, status, marcxml)
321 $sth->bind_param(1, $import_record_id);
322 $sth->bind_param(2, 'staged');
323 $sth->bind_param(3, $item_marc->as_xml());
325 push @import_items_ids, $dbh->{'mysql_insertid'};
329 if ($#import_items_ids > -1) {
330 _update_batch_record_counts($batch_id) if $update_counts;
331 _update_import_record_marc($import_record_id, $marc_record);
333 return @import_items_ids;
336 =head2 BatchFindBibDuplicates
340 my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, $max_matches, $progress_interval, $progress_callback);
344 Goes through the records loaded in the batch and attempts to
345 find duplicates for each one. Sets the overlay action to
346 "replace" if it was "create_new", and sets the overlay status
347 of each record to "no_match" or "auto_match" as appropriate.
349 The $max_matches parameter is optional; if it is not supplied,
352 The $progress_interval and $progress_callback parameters are
353 optional; if both are supplied, the sub referred to by
354 $progress_callback will be invoked every $progress_interval
355 records using the number of records processed as the
360 sub BatchFindBibDuplicates {
361 my $batch_id = shift;
363 my $max_matches = @_ ? shift : 10;
365 # optional callback to monitor status
367 my $progress_interval = 0;
368 my $progress_callback = undef;
370 $progress_interval = shift;
371 $progress_callback = shift;
372 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
373 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
376 my $dbh = C4::Context->dbh;
377 my $old_overlay_action = GetImportBatchOverlayAction($batch_id);
378 if ($old_overlay_action eq "create_new") {
379 SetImportBatchOverlayAction($batch_id, 'replace');
382 my $sth = $dbh->prepare("SELECT import_record_id, marc
384 JOIN import_biblios USING (import_record_id)
385 WHERE import_batch_id = ?");
386 $sth->execute($batch_id);
387 my $num_with_matches = 0;
389 while (my $rowref = $sth->fetchrow_hashref) {
391 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
392 &$progress_callback($rec_num);
394 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
396 if (defined $matcher) {
397 @matches = $matcher->get_matches($marc_record, $max_matches);
399 if (scalar(@matches) > 0) {
401 SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
402 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
404 SetImportRecordMatches($rowref->{'import_record_id'}, ());
405 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
409 return $num_with_matches;
412 =head2 BatchCommitBibRecords
416 my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) =
417 BatchCommitBibRecords($batch_id, $progress_interval, $progress_callback);
423 sub BatchCommitBibRecords {
424 my $batch_id = shift;
426 # optional callback to monitor status
428 my $progress_interval = 0;
429 my $progress_callback = undef;
431 $progress_interval = shift;
432 $progress_callback = shift;
433 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
434 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
439 my $num_items_added = 0;
440 my $num_items_errored = 0;
442 # commit (i.e., save, all records in the batch)
443 # FIXME biblio only at the moment
444 SetImportBatchStatus('importing');
445 my $overlay_action = GetImportBatchOverlayAction($batch_id);
446 my $dbh = C4::Context->dbh;
447 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marc, encoding
449 JOIN import_biblios USING (import_record_id)
450 WHERE import_batch_id = ?");
451 $sth->execute($batch_id);
453 while (my $rowref = $sth->fetchrow_hashref) {
455 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
456 &$progress_callback($rec_num);
458 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
462 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
464 # remove any item tags - rely on BatchCommitItems
465 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
466 foreach my $item_field ($marc_record->field($item_tag)) {
467 $marc_record->delete_field($item_field);
470 if ($overlay_action eq 'create_new' or
471 ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
473 my ($biblionumber, $biblioitemnumber) = AddBiblio($marc_record, '');
474 my $sth = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
475 $sth->execute($biblionumber, $rowref->{'import_record_id'});
477 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
478 $num_items_added += $bib_items_added;
479 $num_items_errored += $bib_items_errored;
480 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
483 my $biblionumber = GetBestRecordMatch($rowref->{'import_record_id'});
484 my ($count, $oldbiblio) = GetBiblio($biblionumber);
485 my $oldxml = GetXmlBiblio($biblionumber);
487 # remove item fields so that they don't get
488 # added again if record is reverted
489 my $old_marc = MARC::Record->new_from_xml($oldxml, 'UTF-8', $rowref->{'encoding'});
490 foreach my $item_field ($old_marc->field($item_tag)) {
491 $old_marc->delete_field($item_field);
494 ModBiblio($marc_record, $biblionumber, $oldbiblio->{'frameworkcode'});
495 my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
496 $sth->execute($old_marc->as_xml(), $rowref->{'import_record_id'});
498 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
499 $sth2->execute($biblionumber, $rowref->{'import_record_id'});
501 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
502 $num_items_added += $bib_items_added;
503 $num_items_errored += $bib_items_errored;
504 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
505 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
509 SetImportBatchStatus($batch_id, 'imported');
510 return ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored);
513 =head2 BatchCommitItems
517 ($num_items_added, $num_items_errored) = BatchCommitItems($import_record_id, $biblionumber);
523 sub BatchCommitItems {
524 my ($import_record_id, $biblionumber) = @_;
526 my $dbh = C4::Context->dbh;
528 my $num_items_added = 0;
529 my $num_items_errored = 0;
530 my $sth = $dbh->prepare("SELECT import_items_id, import_items.marcxml, encoding
532 JOIN import_records USING (import_record_id)
533 WHERE import_record_id = ?
534 ORDER BY import_items_id");
535 $sth->bind_param(1, $import_record_id);
537 while (my $row = $sth->fetchrow_hashref()) {
538 my $item_marc = MARC::Record->new_from_xml($row->{'marcxml'}, 'UTF-8', $row->{'encoding'});
539 # FIXME - duplicate barcode check needs to become part of AddItemFromMarc()
540 my $item = TransformMarcToKoha($dbh, $item_marc);
541 my $duplicate_barcode = exists($item->{'barcode'}) && GetItemnumberFromBarcode($item->{'barcode'});
542 if ($duplicate_barcode) {
543 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, import_error = ? WHERE import_items_id = ?");
544 $updsth->bind_param(1, 'error');
545 $updsth->bind_param(2, 'duplicate item barcode');
546 $updsth->bind_param(3, $row->{'import_items_id'});
548 $num_items_errored++;
550 my ($item_biblionumber, $biblioitemnumber, $itemnumber) = AddItemFromMarc($item_marc, $biblionumber);
551 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
552 $updsth->bind_param(1, 'imported');
553 $updsth->bind_param(2, $itemnumber);
554 $updsth->bind_param(3, $row->{'import_items_id'});
561 return ($num_items_added, $num_items_errored);
564 =head2 BatchRevertBibRecords
568 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) = BatchRevertBibRecords($batch_id);
574 sub BatchRevertBibRecords {
575 my $batch_id = shift;
579 my $num_reverted = 0;
580 my $num_items_deleted = 0;
582 # commit (i.e., save, all records in the batch)
583 # FIXME biblio only at the moment
584 SetImportBatchStatus('reverting');
585 my $overlay_action = GetImportBatchOverlayAction($batch_id);
586 my $dbh = C4::Context->dbh;
587 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marcxml_old, encoding, matched_biblionumber
589 JOIN import_biblios USING (import_record_id)
590 WHERE import_batch_id = ?");
591 $sth->execute($batch_id);
592 while (my $rowref = $sth->fetchrow_hashref) {
593 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
596 if ($overlay_action eq 'create_new' or
597 ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
598 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
599 my $error = DelBiblio($rowref->{'matched_biblionumber'});
600 if (defined $error) {
604 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
608 my $old_record = MARC::Record->new_from_xml($rowref->{'marcxml_old'}, 'UTF-8', $rowref->{'encoding'});
609 my $biblionumber = $rowref->{'matched_biblionumber'};
610 my ($count, $oldbiblio) = GetBiblio($biblionumber);
611 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
612 ModBiblio($old_record, $biblionumber, $oldbiblio->{'frameworkcode'});
613 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
617 SetImportBatchStatus($batch_id, 'reverted');
618 return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored);
621 =head2 BatchRevertItems
625 my $num_items_deleted = BatchRevertItems($import_record_id, $biblionumber);
631 sub BatchRevertItems {
632 my ($import_record_id, $biblionumber) = @_;
634 my $dbh = C4::Context->dbh;
635 my $num_items_deleted = 0;
637 my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
639 JOIN items USING (itemnumber)
640 WHERE import_record_id = ?");
641 $sth->bind_param(1, $import_record_id);
643 while (my $row = $sth->fetchrow_hashref()) {
644 DelItem($dbh, $biblionumber, $row->{'itemnumber'});
645 my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?");
646 $updsth->bind_param(1, 'reverted');
647 $updsth->bind_param(2, $row->{'import_items_id'});
650 $num_items_deleted++;
653 return $num_items_deleted;
656 =head2 GetAllImportBatches
660 my $results = GetAllImportBatches();
664 Returns a references to an array of hash references corresponding
665 to all import_batches rows (of batch_type 'batch'), sorted in
666 ascending order by import_batch_id.
670 sub GetAllImportBatches {
671 my $dbh = C4::Context->dbh;
672 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
673 WHERE batch_type = 'batch'
674 ORDER BY import_batch_id ASC");
678 while (my $row = $sth->fetchrow_hashref) {
679 push @$results, $row;
685 =head2 GetImportBatchRangeDesc
689 my $results = GetImportBatchRangeDesc($offset, $results_per_group);
693 Returns a reference to an array of hash references corresponding to
694 import_batches rows (sorted in descending order by import_batch_id)
695 start at the given offset.
699 sub GetImportBatchRangeDesc {
700 my ($offset, $results_per_group) = @_;
702 my $dbh = C4::Context->dbh;
703 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
704 WHERE batch_type = 'batch'
705 ORDER BY import_batch_id DESC
707 $sth->bind_param(1, $results_per_group);
708 $sth->bind_param(2, $offset);
712 while (my $row = $sth->fetchrow_hashref) {
713 push @$results, $row;
719 =head2 GetNumberOfImportBatches
723 my $count = GetNumberOfImportBatches();
729 sub GetNumberOfNonZ3950ImportBatches {
730 my $dbh = C4::Context->dbh;
731 my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type='batch'");
733 my ($count) = $sth->fetchrow_array();
738 =head2 GetImportBibliosRange
742 my $results = GetImportBibliosRange($batch_id, $offset, $results_per_group);
746 Returns a reference to an array of hash references corresponding to
747 import_biblios/import_records rows for a given batch
748 starting at the given offset.
752 sub GetImportBibliosRange {
753 my ($batch_id, $offset, $results_per_group) = @_;
755 my $dbh = C4::Context->dbh;
756 my $sth = $dbh->prepare_cached("SELECT title, author, isbn, issn, import_record_id, record_sequence,
757 status, overlay_status
759 JOIN import_biblios USING (import_record_id)
760 WHERE import_batch_id = ?
761 ORDER BY import_record_id LIMIT ? OFFSET ?");
762 $sth->bind_param(1, $batch_id);
763 $sth->bind_param(2, $results_per_group);
764 $sth->bind_param(3, $offset);
767 while (my $row = $sth->fetchrow_hashref) {
768 push @$results, $row;
775 =head2 GetBestRecordMatch
779 my $record_id = GetBestRecordMatch($import_record_id);
785 sub GetBestRecordMatch {
786 my ($import_record_id) = @_;
788 my $dbh = C4::Context->dbh;
789 my $sth = $dbh->prepare("SELECT candidate_match_id
790 FROM import_record_matches
791 WHERE import_record_id = ?
792 ORDER BY score DESC, candidate_match_id DESC");
793 $sth->execute($import_record_id);
794 my ($record_id) = $sth->fetchrow_array();
799 =head2 GetImportBatchStatus
803 my $status = GetImportBatchStatus($batch_id);
809 sub GetImportBatchStatus {
812 my $dbh = C4::Context->dbh;
813 my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE batch_id = ?");
814 $sth->execute($batch_id);
815 my ($status) = $sth->fetchrow_array();
822 =head2 SetImportBatchStatus
826 SetImportBatchStatus($batch_id, $new_status);
832 sub SetImportBatchStatus {
833 my ($batch_id, $new_status) = @_;
835 my $dbh = C4::Context->dbh;
836 my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
837 $sth->execute($new_status, $batch_id);
842 =head2 GetImportBatchOverlayAction
846 my $overlay_action = GetImportBatchOverlayAction($batch_id);
852 sub GetImportBatchOverlayAction {
855 my $dbh = C4::Context->dbh;
856 my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
857 $sth->execute($batch_id);
858 my ($overlay_action) = $sth->fetchrow_array();
860 return $overlay_action;
865 =head2 SetImportBatchOverlayAction
869 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
875 sub SetImportBatchOverlayAction {
876 my ($batch_id, $new_overlay_action) = @_;
878 my $dbh = C4::Context->dbh;
879 my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
880 $sth->execute($new_overlay_action, $batch_id);
885 =head2 GetImportBatchMatcher
889 my $matcher_id = GetImportBatchMatcher($batch_id);
895 sub GetImportBatchMatcher {
898 my $dbh = C4::Context->dbh;
899 my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
900 $sth->execute($batch_id);
901 my ($matcher_id) = $sth->fetchrow_array();
908 =head2 SetImportBatchMatcher
912 SetImportBatchMatcher($batch_id, $new_matcher_id);
918 sub SetImportBatchMatcher {
919 my ($batch_id, $new_matcher_id) = @_;
921 my $dbh = C4::Context->dbh;
922 my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
923 $sth->execute($new_matcher_id, $batch_id);
928 =head2 GetImportRecordOverlayStatus
932 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
938 sub GetImportRecordOverlayStatus {
939 my ($import_record_id) = @_;
941 my $dbh = C4::Context->dbh;
942 my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
943 $sth->execute($import_record_id);
944 my ($overlay_status) = $sth->fetchrow_array();
946 return $overlay_status;
951 =head2 SetImportRecordOverlayStatus
955 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
961 sub SetImportRecordOverlayStatus {
962 my ($import_record_id, $new_overlay_status) = @_;
964 my $dbh = C4::Context->dbh;
965 my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
966 $sth->execute($new_overlay_status, $import_record_id);
971 =head2 GetImportRecordStatus
975 my $overlay_status = GetImportRecordStatus($import_record_id);
981 sub GetImportRecordStatus {
982 my ($import_record_id) = @_;
984 my $dbh = C4::Context->dbh;
985 my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
986 $sth->execute($import_record_id);
987 my ($overlay_status) = $sth->fetchrow_array();
989 return $overlay_status;
994 =head2 SetImportRecordStatus
998 SetImportRecordStatus($import_record_id, $new_overlay_status);
1004 sub SetImportRecordStatus {
1005 my ($import_record_id, $new_overlay_status) = @_;
1007 my $dbh = C4::Context->dbh;
1008 my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
1009 $sth->execute($new_overlay_status, $import_record_id);
1014 =head2 GetImportRecordMatches
1018 my $results = GetImportRecordMatches($import_record_id, $best_only);
1024 sub GetImportRecordMatches {
1025 my $import_record_id = shift;
1026 my $best_only = @_ ? shift : 0;
1028 my $dbh = C4::Context->dbh;
1029 # FIXME currently biblio only
1030 my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber, score
1032 JOIN import_record_matches USING (import_record_id)
1033 JOIN biblio ON (biblionumber = candidate_match_id)
1034 WHERE import_record_id = ?
1035 ORDER BY score DESC, biblionumber DESC");
1036 $sth->bind_param(1, $import_record_id);
1039 while (my $row = $sth->fetchrow_hashref) {
1040 push @$results, $row;
1050 =head2 SetImportRecordMatches
1054 SetImportRecordMatches($import_record_id, @matches);
1060 sub SetImportRecordMatches {
1061 my $import_record_id = shift;
1064 my $dbh = C4::Context->dbh;
1065 my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
1066 $delsth->execute($import_record_id);
1069 my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
1071 foreach my $match (@matches) {
1072 $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
1077 # internal functions
1079 sub _create_import_record {
1080 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random) = @_;
1082 my $dbh = C4::Context->dbh;
1083 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml,
1084 record_type, encoding, z3950random)
1085 VALUES (?, ?, ?, ?, ?, ?, ?)");
1086 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(),
1087 $record_type, $encoding, $z3950random);
1088 my $import_record_id = $dbh->{'mysql_insertid'};
1090 return $import_record_id;
1093 sub _update_import_record_marc {
1094 my ($import_record_id, $marc_record) = @_;
1096 my $dbh = C4::Context->dbh;
1097 my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
1098 WHERE import_record_id = ?");
1099 $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml(), $import_record_id);
1103 sub _add_biblio_fields {
1104 my ($import_record_id, $marc_record) = @_;
1106 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1107 my $dbh = C4::Context->dbh;
1108 # FIXME no controlnumber, originalsource
1109 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1113 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
1114 $sth->execute($import_record_id, $title, $author, $isbn, $issn);
1119 sub _update_biblio_fields {
1120 my ($import_record_id, $marc_record) = @_;
1122 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1123 my $dbh = C4::Context->dbh;
1124 # FIXME no controlnumber, originalsource
1125 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1129 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
1130 WHERE import_record_id = ?");
1131 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
1135 sub _parse_biblio_fields {
1136 my ($marc_record) = @_;
1138 my $dbh = C4::Context->dbh;
1139 my $bibliofields = TransformMarcToKoha($dbh, $marc_record, '');
1140 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
1144 sub _update_batch_record_counts {
1145 my ($batch_id) = @_;
1147 my $dbh = C4::Context->dbh;
1148 my $sth = $dbh->prepare_cached("UPDATE import_batches SET num_biblios = (
1151 WHERE import_batch_id = import_batches.import_batch_id
1152 AND record_type = 'biblio')
1153 WHERE import_batch_id = ?");
1154 $sth->bind_param(1, $batch_id);
1157 $sth = $dbh->prepare_cached("UPDATE import_batches SET num_items = (
1160 JOIN import_items USING (import_record_id)
1161 WHERE import_batch_id = import_batches.import_batch_id
1162 AND record_type = 'biblio')
1163 WHERE import_batch_id = ?");
1164 $sth->bind_param(1, $batch_id);
1174 Koha Development Team <info@koha.org>
1176 Galen Charlton <galen.charlton@liblime.com>