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
54 GetImportBatchOverlayAction
55 SetImportBatchOverlayAction
58 GetImportRecordOverlayStatus
59 SetImportRecordOverlayStatus
62 GetImportRecordMatches
63 SetImportRecordMatches
69 C4::ImportBatch - manage batches of imported MARC records
81 =head2 GetZ3950BatchId
85 my $batchid = GetZ3950BatchId($z3950server);
89 Retrieves the ID of the import batch for the Z39.50
90 reservoir for the given target. If necessary,
91 creates the import batch.
96 my ($z3950server) = @_;
98 my $dbh = C4::Context->dbh;
99 my $sth = $dbh->prepare("SELECT import_batch_id FROM import_batches
100 WHERE batch_type = 'z3950'
102 $sth->execute($z3950server);
103 my $rowref = $sth->fetchrow_arrayref();
105 if (defined $rowref) {
108 my $batch_id = AddImportBatch('create_new', 'staged', 'z3950', $z3950server, '');
114 =head2 GetImportRecordMarc
118 my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
124 sub GetImportRecordMarc {
125 my ($import_record_id) = @_;
127 my $dbh = C4::Context->dbh;
128 my $sth = $dbh->prepare("SELECT marc, encoding FROM import_records WHERE import_record_id = ?");
129 $sth->execute($import_record_id);
130 my ($marc, $encoding) = $sth->fetchrow();
136 =head2 AddImportBatch
140 my $batch_id = AddImportBatch($overlay_action, $import_status, $type, $file_name, $comments);
147 my ($overlay_action, $import_status, $type, $file_name, $comments) = @_;
149 my $dbh = C4::Context->dbh;
150 my $sth = $dbh->prepare("INSERT INTO import_batches (overlay_action, import_status, batch_type,
152 VALUES (?, ?, ?, ?, ?)");
153 $sth->execute($overlay_action, $import_status, $type, $file_name, $comments);
154 my $batch_id = $dbh->{'mysql_insertid'};
161 =head2 GetImportBatch
165 my $row = GetImportBatch($batch_id);
169 Retrieve a hashref of an import_batches row.
176 my $dbh = C4::Context->dbh;
177 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches WHERE import_batch_id = ?");
178 $sth->bind_param(1, $batch_id);
180 my $result = $sth->fetchrow_hashref;
186 =head2 AddBiblioToBatch
190 my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence, $marc_record, $encoding, $z3950random, $update_counts);
196 sub AddBiblioToBatch {
197 my $batch_id = shift;
198 my $record_sequence = shift;
199 my $marc_record = shift;
200 my $encoding = shift;
201 my $z3950random = shift;
202 my $update_counts = @_ ? shift : 1;
204 my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, $z3950random);
205 _add_biblio_fields($import_record_id, $marc_record);
206 _update_batch_record_counts($batch_id) if $update_counts;
207 return $import_record_id;
210 =head2 ModBiblioInBatch
214 ModBiblioInBatch($import_record_id, $marc_record);
220 sub ModBiblioInBatch {
221 my ($import_record_id, $marc_record) = @_;
223 _update_import_record_marc($import_record_id, $marc_record);
224 _update_biblio_fields($import_record_id, $marc_record);
228 =head2 BatchStageMarcRecords
232 ($batch_id, $num_records, $num_items, @invalid_records) =
233 BatchStageMarcRecords($marc_flavor, $marc_records, $file_name,
234 $comments, $branch_code, $parse_items,
236 $progress_interval, $progress_callback);
242 sub BatchStageMarcRecords {
243 my $marc_flavor = shift;
244 my $marc_records = shift;
245 my $file_name = shift;
246 my $comments = shift;
247 my $branch_code = shift;
248 my $parse_items = shift;
249 my $leave_as_staging = shift;
251 # optional callback to monitor status
253 my $progress_interval = 0;
254 my $progress_callback = undef;
256 $progress_interval = shift;
257 $progress_callback = shift;
258 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
259 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
262 my $batch_id = AddImportBatch('create_new', 'staging', 'batch', $file_name, $comments);
263 my @invalid_records = ();
266 # FIXME - for now, we're dealing only with bibs
268 foreach my $marc_blob (split(/\x1D/, $marc_records)) {
270 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
271 &$progress_callback($rec_num);
273 my ($marc_record, $charset_guessed, $char_errors) =
274 MarcToUTF8Record($marc_blob, C4::Context->preference("marcflavour"));
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->leader("00000 a "); # must set Leader/09 to 'a'
318 $item_marc->append_fields($item_field);
319 $marc_record->delete_field($item_field);
320 my $sth = $dbh->prepare_cached("INSERT INTO import_items (import_record_id, status, marcxml)
322 $sth->bind_param(1, $import_record_id);
323 $sth->bind_param(2, 'staged');
324 $sth->bind_param(3, $item_marc->as_xml());
326 push @import_items_ids, $dbh->{'mysql_insertid'};
330 if ($#import_items_ids > -1) {
331 _update_batch_record_counts($batch_id) if $update_counts;
332 _update_import_record_marc($import_record_id, $marc_record);
334 return @import_items_ids;
337 =head2 BatchFindBibDuplicates
341 my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, $max_matches, $progress_interval, $progress_callback);
345 Goes through the records loaded in the batch and attempts to
346 find duplicates for each one. Sets the overlay action to
347 "replace" if it was "create_new", and sets the overlay status
348 of each record to "no_match" or "auto_match" as appropriate.
350 The $max_matches parameter is optional; if it is not supplied,
353 The $progress_interval and $progress_callback parameters are
354 optional; if both are supplied, the sub referred to by
355 $progress_callback will be invoked every $progress_interval
356 records using the number of records processed as the
361 sub BatchFindBibDuplicates {
362 my $batch_id = shift;
364 my $max_matches = @_ ? shift : 10;
366 # optional callback to monitor status
368 my $progress_interval = 0;
369 my $progress_callback = undef;
371 $progress_interval = shift;
372 $progress_callback = shift;
373 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
374 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
377 my $dbh = C4::Context->dbh;
378 my $old_overlay_action = GetImportBatchOverlayAction($batch_id);
379 if ($old_overlay_action eq "create_new") {
380 SetImportBatchOverlayAction($batch_id, 'replace');
383 my $sth = $dbh->prepare("SELECT import_record_id, marc
385 JOIN import_biblios USING (import_record_id)
386 WHERE import_batch_id = ?");
387 $sth->execute($batch_id);
388 my $num_with_matches = 0;
390 while (my $rowref = $sth->fetchrow_hashref) {
392 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
393 &$progress_callback($rec_num);
395 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
397 if (defined $matcher) {
398 @matches = $matcher->get_matches($marc_record, $max_matches);
400 if (scalar(@matches) > 0) {
402 SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
403 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
405 SetImportRecordMatches($rowref->{'import_record_id'}, ());
406 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
410 return $num_with_matches;
413 =head2 BatchCommitBibRecords
417 my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) =
418 BatchCommitBibRecords($batch_id, $progress_interval, $progress_callback);
424 sub BatchCommitBibRecords {
425 my $batch_id = shift;
427 # optional callback to monitor status
429 my $progress_interval = 0;
430 my $progress_callback = undef;
432 $progress_interval = shift;
433 $progress_callback = shift;
434 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
435 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
440 my $num_items_added = 0;
441 my $num_items_errored = 0;
443 # commit (i.e., save, all records in the batch)
444 # FIXME biblio only at the moment
445 SetImportBatchStatus('importing');
446 my $overlay_action = GetImportBatchOverlayAction($batch_id);
447 my $dbh = C4::Context->dbh;
448 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marc, encoding
450 JOIN import_biblios USING (import_record_id)
451 WHERE import_batch_id = ?");
452 $sth->execute($batch_id);
454 while (my $rowref = $sth->fetchrow_hashref) {
456 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
457 &$progress_callback($rec_num);
459 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
463 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
465 # remove any item tags - rely on BatchCommitItems
466 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
467 foreach my $item_field ($marc_record->field($item_tag)) {
468 $marc_record->delete_field($item_field);
471 if ($overlay_action eq 'create_new' or
472 ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
474 my ($biblionumber, $biblioitemnumber) = AddBiblio($marc_record, '');
475 my $sth = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
476 $sth->execute($biblionumber, $rowref->{'import_record_id'});
478 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
479 $num_items_added += $bib_items_added;
480 $num_items_errored += $bib_items_errored;
481 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
484 my $biblionumber = GetBestRecordMatch($rowref->{'import_record_id'});
485 my ($count, $oldbiblio) = GetBiblio($biblionumber);
486 my $oldxml = GetXmlBiblio($biblionumber);
488 # remove item fields so that they don't get
489 # added again if record is reverted
490 my $old_marc = MARC::Record->new_from_xml(StripNonXmlChars($oldxml), 'UTF-8', $rowref->{'encoding'});
491 foreach my $item_field ($old_marc->field($item_tag)) {
492 $old_marc->delete_field($item_field);
495 ModBiblio($marc_record, $biblionumber, $oldbiblio->{'frameworkcode'});
496 my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
497 $sth->execute($old_marc->as_xml(), $rowref->{'import_record_id'});
499 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
500 $sth2->execute($biblionumber, $rowref->{'import_record_id'});
502 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
503 $num_items_added += $bib_items_added;
504 $num_items_errored += $bib_items_errored;
505 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
506 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
510 SetImportBatchStatus($batch_id, 'imported');
511 return ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored);
514 =head2 BatchCommitItems
518 ($num_items_added, $num_items_errored) = BatchCommitItems($import_record_id, $biblionumber);
524 sub BatchCommitItems {
525 my ($import_record_id, $biblionumber) = @_;
527 my $dbh = C4::Context->dbh;
529 my $num_items_added = 0;
530 my $num_items_errored = 0;
531 my $sth = $dbh->prepare("SELECT import_items_id, import_items.marcxml, encoding
533 JOIN import_records USING (import_record_id)
534 WHERE import_record_id = ?
535 ORDER BY import_items_id");
536 $sth->bind_param(1, $import_record_id);
538 while (my $row = $sth->fetchrow_hashref()) {
539 my $item_marc = MARC::Record->new_from_xml(StripNonXmlChars($row->{'marcxml'}), 'UTF-8', $row->{'encoding'});
540 # FIXME - duplicate barcode check needs to become part of AddItemFromMarc()
541 my $item = TransformMarcToKoha($dbh, $item_marc);
542 my $duplicate_barcode = exists($item->{'barcode'}) && GetItemnumberFromBarcode($item->{'barcode'});
543 if ($duplicate_barcode) {
544 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, import_error = ? WHERE import_items_id = ?");
545 $updsth->bind_param(1, 'error');
546 $updsth->bind_param(2, 'duplicate item barcode');
547 $updsth->bind_param(3, $row->{'import_items_id'});
549 $num_items_errored++;
551 my ($item_biblionumber, $biblioitemnumber, $itemnumber) = AddItemFromMarc($item_marc, $biblionumber);
552 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
553 $updsth->bind_param(1, 'imported');
554 $updsth->bind_param(2, $itemnumber);
555 $updsth->bind_param(3, $row->{'import_items_id'});
562 return ($num_items_added, $num_items_errored);
565 =head2 BatchRevertBibRecords
569 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) = BatchRevertBibRecords($batch_id);
575 sub BatchRevertBibRecords {
576 my $batch_id = shift;
580 my $num_reverted = 0;
581 my $num_items_deleted = 0;
583 # commit (i.e., save, all records in the batch)
584 # FIXME biblio only at the moment
585 SetImportBatchStatus('reverting');
586 my $overlay_action = GetImportBatchOverlayAction($batch_id);
587 my $dbh = C4::Context->dbh;
588 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marcxml_old, encoding, matched_biblionumber
590 JOIN import_biblios USING (import_record_id)
591 WHERE import_batch_id = ?");
592 $sth->execute($batch_id);
593 while (my $rowref = $sth->fetchrow_hashref) {
594 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
597 if ($overlay_action eq 'create_new' or
598 ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
599 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
600 my $error = DelBiblio($rowref->{'matched_biblionumber'});
601 if (defined $error) {
605 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
609 my $old_record = MARC::Record->new_from_xml(StripNonXmlChars($rowref->{'marcxml_old'}), 'UTF-8', $rowref->{'encoding'});
610 my $biblionumber = $rowref->{'matched_biblionumber'};
611 my ($count, $oldbiblio) = GetBiblio($biblionumber);
612 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
613 ModBiblio($old_record, $biblionumber, $oldbiblio->{'frameworkcode'});
614 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
618 SetImportBatchStatus($batch_id, 'reverted');
619 return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored);
622 =head2 BatchRevertItems
626 my $num_items_deleted = BatchRevertItems($import_record_id, $biblionumber);
632 sub BatchRevertItems {
633 my ($import_record_id, $biblionumber) = @_;
635 my $dbh = C4::Context->dbh;
636 my $num_items_deleted = 0;
638 my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
640 JOIN items USING (itemnumber)
641 WHERE import_record_id = ?");
642 $sth->bind_param(1, $import_record_id);
644 while (my $row = $sth->fetchrow_hashref()) {
645 DelItem($dbh, $biblionumber, $row->{'itemnumber'});
646 my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?");
647 $updsth->bind_param(1, 'reverted');
648 $updsth->bind_param(2, $row->{'import_items_id'});
651 $num_items_deleted++;
654 return $num_items_deleted;
657 =head2 GetAllImportBatches
661 my $results = GetAllImportBatches();
665 Returns a references to an array of hash references corresponding
666 to all import_batches rows (of batch_type 'batch'), sorted in
667 ascending order by import_batch_id.
671 sub GetAllImportBatches {
672 my $dbh = C4::Context->dbh;
673 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
674 WHERE batch_type = 'batch'
675 ORDER BY import_batch_id ASC");
679 while (my $row = $sth->fetchrow_hashref) {
680 push @$results, $row;
686 =head2 GetImportBatchRangeDesc
690 my $results = GetImportBatchRangeDesc($offset, $results_per_group);
694 Returns a reference to an array of hash references corresponding to
695 import_batches rows (sorted in descending order by import_batch_id)
696 start at the given offset.
700 sub GetImportBatchRangeDesc {
701 my ($offset, $results_per_group) = @_;
703 my $dbh = C4::Context->dbh;
704 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
705 WHERE batch_type = 'batch'
706 ORDER BY import_batch_id DESC
708 $sth->bind_param(1, $results_per_group);
709 $sth->bind_param(2, $offset);
713 while (my $row = $sth->fetchrow_hashref) {
714 push @$results, $row;
720 =head2 GetNumberOfImportBatches
724 my $count = GetNumberOfImportBatches();
730 sub GetNumberOfNonZ3950ImportBatches {
731 my $dbh = C4::Context->dbh;
732 my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type='batch'");
734 my ($count) = $sth->fetchrow_array();
739 =head2 GetImportBibliosRange
743 my $results = GetImportBibliosRange($batch_id, $offset, $results_per_group);
747 Returns a reference to an array of hash references corresponding to
748 import_biblios/import_records rows for a given batch
749 starting at the given offset.
753 sub GetImportBibliosRange {
754 my ($batch_id, $offset, $results_per_group) = @_;
756 my $dbh = C4::Context->dbh;
757 my $sth = $dbh->prepare_cached("SELECT title, author, isbn, issn, import_record_id, record_sequence,
758 status, overlay_status
760 JOIN import_biblios USING (import_record_id)
761 WHERE import_batch_id = ?
762 ORDER BY import_record_id LIMIT ? OFFSET ?");
763 $sth->bind_param(1, $batch_id);
764 $sth->bind_param(2, $results_per_group);
765 $sth->bind_param(3, $offset);
768 while (my $row = $sth->fetchrow_hashref) {
769 push @$results, $row;
776 =head2 GetBestRecordMatch
780 my $record_id = GetBestRecordMatch($import_record_id);
786 sub GetBestRecordMatch {
787 my ($import_record_id) = @_;
789 my $dbh = C4::Context->dbh;
790 my $sth = $dbh->prepare("SELECT candidate_match_id
791 FROM import_record_matches
792 WHERE import_record_id = ?
793 ORDER BY score DESC, candidate_match_id DESC");
794 $sth->execute($import_record_id);
795 my ($record_id) = $sth->fetchrow_array();
800 =head2 GetImportBatchStatus
804 my $status = GetImportBatchStatus($batch_id);
810 sub GetImportBatchStatus {
813 my $dbh = C4::Context->dbh;
814 my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE batch_id = ?");
815 $sth->execute($batch_id);
816 my ($status) = $sth->fetchrow_array();
823 =head2 SetImportBatchStatus
827 SetImportBatchStatus($batch_id, $new_status);
833 sub SetImportBatchStatus {
834 my ($batch_id, $new_status) = @_;
836 my $dbh = C4::Context->dbh;
837 my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
838 $sth->execute($new_status, $batch_id);
843 =head2 GetImportBatchOverlayAction
847 my $overlay_action = GetImportBatchOverlayAction($batch_id);
853 sub GetImportBatchOverlayAction {
856 my $dbh = C4::Context->dbh;
857 my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
858 $sth->execute($batch_id);
859 my ($overlay_action) = $sth->fetchrow_array();
861 return $overlay_action;
866 =head2 SetImportBatchOverlayAction
870 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
876 sub SetImportBatchOverlayAction {
877 my ($batch_id, $new_overlay_action) = @_;
879 my $dbh = C4::Context->dbh;
880 my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
881 $sth->execute($new_overlay_action, $batch_id);
886 =head2 GetImportBatchMatcher
890 my $matcher_id = GetImportBatchMatcher($batch_id);
896 sub GetImportBatchMatcher {
899 my $dbh = C4::Context->dbh;
900 my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
901 $sth->execute($batch_id);
902 my ($matcher_id) = $sth->fetchrow_array();
909 =head2 SetImportBatchMatcher
913 SetImportBatchMatcher($batch_id, $new_matcher_id);
919 sub SetImportBatchMatcher {
920 my ($batch_id, $new_matcher_id) = @_;
922 my $dbh = C4::Context->dbh;
923 my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
924 $sth->execute($new_matcher_id, $batch_id);
929 =head2 GetImportRecordOverlayStatus
933 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
939 sub GetImportRecordOverlayStatus {
940 my ($import_record_id) = @_;
942 my $dbh = C4::Context->dbh;
943 my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
944 $sth->execute($import_record_id);
945 my ($overlay_status) = $sth->fetchrow_array();
947 return $overlay_status;
952 =head2 SetImportRecordOverlayStatus
956 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
962 sub SetImportRecordOverlayStatus {
963 my ($import_record_id, $new_overlay_status) = @_;
965 my $dbh = C4::Context->dbh;
966 my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
967 $sth->execute($new_overlay_status, $import_record_id);
972 =head2 GetImportRecordStatus
976 my $overlay_status = GetImportRecordStatus($import_record_id);
982 sub GetImportRecordStatus {
983 my ($import_record_id) = @_;
985 my $dbh = C4::Context->dbh;
986 my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
987 $sth->execute($import_record_id);
988 my ($overlay_status) = $sth->fetchrow_array();
990 return $overlay_status;
995 =head2 SetImportRecordStatus
999 SetImportRecordStatus($import_record_id, $new_overlay_status);
1005 sub SetImportRecordStatus {
1006 my ($import_record_id, $new_overlay_status) = @_;
1008 my $dbh = C4::Context->dbh;
1009 my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
1010 $sth->execute($new_overlay_status, $import_record_id);
1015 =head2 GetImportRecordMatches
1019 my $results = GetImportRecordMatches($import_record_id, $best_only);
1025 sub GetImportRecordMatches {
1026 my $import_record_id = shift;
1027 my $best_only = @_ ? shift : 0;
1029 my $dbh = C4::Context->dbh;
1030 # FIXME currently biblio only
1031 my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber, score
1033 JOIN import_record_matches USING (import_record_id)
1034 JOIN biblio ON (biblionumber = candidate_match_id)
1035 WHERE import_record_id = ?
1036 ORDER BY score DESC, biblionumber DESC");
1037 $sth->bind_param(1, $import_record_id);
1040 while (my $row = $sth->fetchrow_hashref) {
1041 push @$results, $row;
1051 =head2 SetImportRecordMatches
1055 SetImportRecordMatches($import_record_id, @matches);
1061 sub SetImportRecordMatches {
1062 my $import_record_id = shift;
1065 my $dbh = C4::Context->dbh;
1066 my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
1067 $delsth->execute($import_record_id);
1070 my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
1072 foreach my $match (@matches) {
1073 $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
1078 # internal functions
1080 sub _create_import_record {
1081 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random) = @_;
1083 my $dbh = C4::Context->dbh;
1084 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml,
1085 record_type, encoding, z3950random)
1086 VALUES (?, ?, ?, ?, ?, ?, ?)");
1087 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(),
1088 $record_type, $encoding, $z3950random);
1089 my $import_record_id = $dbh->{'mysql_insertid'};
1091 return $import_record_id;
1094 sub _update_import_record_marc {
1095 my ($import_record_id, $marc_record) = @_;
1097 my $dbh = C4::Context->dbh;
1098 my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
1099 WHERE import_record_id = ?");
1100 $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml(), $import_record_id);
1104 sub _add_biblio_fields {
1105 my ($import_record_id, $marc_record) = @_;
1107 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1108 my $dbh = C4::Context->dbh;
1109 # FIXME no controlnumber, originalsource
1110 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1114 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
1115 $sth->execute($import_record_id, $title, $author, $isbn, $issn);
1120 sub _update_biblio_fields {
1121 my ($import_record_id, $marc_record) = @_;
1123 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1124 my $dbh = C4::Context->dbh;
1125 # FIXME no controlnumber, originalsource
1126 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1130 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
1131 WHERE import_record_id = ?");
1132 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
1136 sub _parse_biblio_fields {
1137 my ($marc_record) = @_;
1139 my $dbh = C4::Context->dbh;
1140 my $bibliofields = TransformMarcToKoha($dbh, $marc_record, '');
1141 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
1145 sub _update_batch_record_counts {
1146 my ($batch_id) = @_;
1148 my $dbh = C4::Context->dbh;
1149 my $sth = $dbh->prepare_cached("UPDATE import_batches SET num_biblios = (
1152 WHERE import_batch_id = import_batches.import_batch_id
1153 AND record_type = 'biblio')
1154 WHERE import_batch_id = ?");
1155 $sth->bind_param(1, $batch_id);
1158 $sth = $dbh->prepare_cached("UPDATE import_batches SET num_items = (
1161 JOIN import_items USING (import_record_id)
1162 WHERE import_batch_id = import_batches.import_batch_id
1163 AND record_type = 'biblio')
1164 WHERE import_batch_id = ?");
1165 $sth->bind_param(1, $batch_id);
1176 Koha Development Team <info@koha.org>
1178 Galen Charlton <galen.charlton@liblime.com>