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
59 GetImportRecordOverlayStatus
60 SetImportRecordOverlayStatus
63 GetImportRecordMatches
64 SetImportRecordMatches
70 C4::ImportBatch - manage batches of imported MARC records
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)) {
270 $marc_blob =~ s/^\s+//g;
271 $marc_blob =~ s/\s+$//g;
272 next unless $marc_blob;
274 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
275 &$progress_callback($rec_num);
277 my ($marc_record, $charset_guessed, $char_errors) =
278 MarcToUTF8Record($marc_blob, C4::Context->preference("marcflavour"));
279 my $import_record_id;
280 if (scalar($marc_record->fields()) == 0) {
281 push @invalid_records, $marc_blob;
284 $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $marc_flavor, int(rand(99999)), 0);
286 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0);
287 $num_items += scalar(@import_items_ids);
291 unless ($leave_as_staging) {
292 SetImportBatchStatus($batch_id, 'staged');
294 # FIXME branch_code, number of bibs, number of items
295 _update_batch_record_counts($batch_id);
296 return ($batch_id, $num_valid, $num_items, @invalid_records);
299 =head2 AddItemsToImportBiblio
303 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, $update_counts);
309 sub AddItemsToImportBiblio {
310 my $batch_id = shift;
311 my $import_record_id = shift;
312 my $marc_record = shift;
313 my $update_counts = @_ ? shift : 0;
315 my @import_items_ids = ();
317 my $dbh = C4::Context->dbh;
318 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
319 foreach my $item_field ($marc_record->field($item_tag)) {
320 my $item_marc = MARC::Record->new();
321 $item_marc->leader("00000 a "); # must set Leader/09 to 'a'
322 $item_marc->append_fields($item_field);
323 $marc_record->delete_field($item_field);
324 my $sth = $dbh->prepare_cached("INSERT INTO import_items (import_record_id, status, marcxml)
326 $sth->bind_param(1, $import_record_id);
327 $sth->bind_param(2, 'staged');
328 $sth->bind_param(3, $item_marc->as_xml());
330 push @import_items_ids, $dbh->{'mysql_insertid'};
334 if ($#import_items_ids > -1) {
335 _update_batch_record_counts($batch_id) if $update_counts;
336 _update_import_record_marc($import_record_id, $marc_record);
338 return @import_items_ids;
341 =head2 BatchFindBibDuplicates
345 my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, $max_matches, $progress_interval, $progress_callback);
349 Goes through the records loaded in the batch and attempts to
350 find duplicates for each one. Sets the overlay action to
351 "replace" if it was "create_new", and sets the overlay status
352 of each record to "no_match" or "auto_match" as appropriate.
354 The $max_matches parameter is optional; if it is not supplied,
357 The $progress_interval and $progress_callback parameters are
358 optional; if both are supplied, the sub referred to by
359 $progress_callback will be invoked every $progress_interval
360 records using the number of records processed as the
365 sub BatchFindBibDuplicates {
366 my $batch_id = shift;
368 my $max_matches = @_ ? shift : 10;
370 # optional callback to monitor status
372 my $progress_interval = 0;
373 my $progress_callback = undef;
375 $progress_interval = shift;
376 $progress_callback = shift;
377 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
378 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
381 my $dbh = C4::Context->dbh;
382 my $old_overlay_action = GetImportBatchOverlayAction($batch_id);
383 if ($old_overlay_action eq "create_new") {
384 SetImportBatchOverlayAction($batch_id, 'replace');
387 my $sth = $dbh->prepare("SELECT import_record_id, marc
389 JOIN import_biblios USING (import_record_id)
390 WHERE import_batch_id = ?");
391 $sth->execute($batch_id);
392 my $num_with_matches = 0;
394 while (my $rowref = $sth->fetchrow_hashref) {
396 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
397 &$progress_callback($rec_num);
399 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
401 if (defined $matcher) {
402 @matches = $matcher->get_matches($marc_record, $max_matches);
404 if (scalar(@matches) > 0) {
406 SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
407 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
409 SetImportRecordMatches($rowref->{'import_record_id'}, ());
410 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
414 return $num_with_matches;
417 =head2 BatchCommitBibRecords
421 my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) =
422 BatchCommitBibRecords($batch_id, $progress_interval, $progress_callback);
428 sub BatchCommitBibRecords {
429 my $batch_id = shift;
431 # optional callback to monitor status
433 my $progress_interval = 0;
434 my $progress_callback = undef;
436 $progress_interval = shift;
437 $progress_callback = shift;
438 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
439 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
444 my $num_items_added = 0;
445 my $num_items_errored = 0;
447 # commit (i.e., save, all records in the batch)
448 # FIXME biblio only at the moment
449 SetImportBatchStatus('importing');
450 my $overlay_action = GetImportBatchOverlayAction($batch_id);
451 my $dbh = C4::Context->dbh;
452 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marc, encoding
454 JOIN import_biblios USING (import_record_id)
455 WHERE import_batch_id = ?");
456 $sth->execute($batch_id);
458 while (my $rowref = $sth->fetchrow_hashref) {
460 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
461 &$progress_callback($rec_num);
463 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
467 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
469 # remove any item tags - rely on BatchCommitItems
470 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
471 foreach my $item_field ($marc_record->field($item_tag)) {
472 $marc_record->delete_field($item_field);
475 if ($overlay_action eq 'create_new' or
476 ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
478 my ($biblionumber, $biblioitemnumber) = AddBiblio($marc_record, '');
479 my $sth = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
480 $sth->execute($biblionumber, $rowref->{'import_record_id'});
482 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
483 $num_items_added += $bib_items_added;
484 $num_items_errored += $bib_items_errored;
485 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
488 my $biblionumber = GetBestRecordMatch($rowref->{'import_record_id'});
489 my ($count, $oldbiblio) = GetBiblio($biblionumber);
490 my $oldxml = GetXmlBiblio($biblionumber);
492 # remove item fields so that they don't get
493 # added again if record is reverted
494 my $old_marc = MARC::Record->new_from_xml(StripNonXmlChars($oldxml), 'UTF-8', $rowref->{'encoding'});
495 foreach my $item_field ($old_marc->field($item_tag)) {
496 $old_marc->delete_field($item_field);
499 ModBiblio($marc_record, $biblionumber, $oldbiblio->{'frameworkcode'});
500 my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
501 $sth->execute($old_marc->as_xml(), $rowref->{'import_record_id'});
503 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
504 $sth2->execute($biblionumber, $rowref->{'import_record_id'});
506 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
507 $num_items_added += $bib_items_added;
508 $num_items_errored += $bib_items_errored;
509 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
510 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
514 SetImportBatchStatus($batch_id, 'imported');
515 return ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored);
518 =head2 BatchCommitItems
522 ($num_items_added, $num_items_errored) = BatchCommitItems($import_record_id, $biblionumber);
528 sub BatchCommitItems {
529 my ($import_record_id, $biblionumber) = @_;
531 my $dbh = C4::Context->dbh;
533 my $num_items_added = 0;
534 my $num_items_errored = 0;
535 my $sth = $dbh->prepare("SELECT import_items_id, import_items.marcxml, encoding
537 JOIN import_records USING (import_record_id)
538 WHERE import_record_id = ?
539 ORDER BY import_items_id");
540 $sth->bind_param(1, $import_record_id);
542 while (my $row = $sth->fetchrow_hashref()) {
543 my $item_marc = MARC::Record->new_from_xml(StripNonXmlChars($row->{'marcxml'}), 'UTF-8', $row->{'encoding'});
544 # FIXME - duplicate barcode check needs to become part of AddItemFromMarc()
545 my $item = TransformMarcToKoha($dbh, $item_marc);
546 my $duplicate_barcode = exists($item->{'barcode'}) && GetItemnumberFromBarcode($item->{'barcode'});
547 if ($duplicate_barcode) {
548 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, import_error = ? WHERE import_items_id = ?");
549 $updsth->bind_param(1, 'error');
550 $updsth->bind_param(2, 'duplicate item barcode');
551 $updsth->bind_param(3, $row->{'import_items_id'});
553 $num_items_errored++;
555 my ($item_biblionumber, $biblioitemnumber, $itemnumber) = AddItemFromMarc($item_marc, $biblionumber);
556 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
557 $updsth->bind_param(1, 'imported');
558 $updsth->bind_param(2, $itemnumber);
559 $updsth->bind_param(3, $row->{'import_items_id'});
566 return ($num_items_added, $num_items_errored);
569 =head2 BatchRevertBibRecords
573 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) = BatchRevertBibRecords($batch_id);
579 sub BatchRevertBibRecords {
580 my $batch_id = shift;
584 my $num_reverted = 0;
585 my $num_items_deleted = 0;
587 # commit (i.e., save, all records in the batch)
588 # FIXME biblio only at the moment
589 SetImportBatchStatus('reverting');
590 my $overlay_action = GetImportBatchOverlayAction($batch_id);
591 my $dbh = C4::Context->dbh;
592 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marcxml_old, encoding, matched_biblionumber
594 JOIN import_biblios USING (import_record_id)
595 WHERE import_batch_id = ?");
596 $sth->execute($batch_id);
597 while (my $rowref = $sth->fetchrow_hashref) {
598 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
601 if ($overlay_action eq 'create_new' or
602 ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
603 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
604 my $error = DelBiblio($rowref->{'matched_biblionumber'});
605 if (defined $error) {
609 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
613 my $old_record = MARC::Record->new_from_xml(StripNonXmlChars($rowref->{'marcxml_old'}), 'UTF-8', $rowref->{'encoding'});
614 my $biblionumber = $rowref->{'matched_biblionumber'};
615 my ($count, $oldbiblio) = GetBiblio($biblionumber);
616 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
617 ModBiblio($old_record, $biblionumber, $oldbiblio->{'frameworkcode'});
618 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
622 SetImportBatchStatus($batch_id, 'reverted');
623 return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored);
626 =head2 BatchRevertItems
630 my $num_items_deleted = BatchRevertItems($import_record_id, $biblionumber);
636 sub BatchRevertItems {
637 my ($import_record_id, $biblionumber) = @_;
639 my $dbh = C4::Context->dbh;
640 my $num_items_deleted = 0;
642 my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
644 JOIN items USING (itemnumber)
645 WHERE import_record_id = ?");
646 $sth->bind_param(1, $import_record_id);
648 while (my $row = $sth->fetchrow_hashref()) {
649 DelItem($dbh, $biblionumber, $row->{'itemnumber'});
650 my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?");
651 $updsth->bind_param(1, 'reverted');
652 $updsth->bind_param(2, $row->{'import_items_id'});
655 $num_items_deleted++;
658 return $num_items_deleted;
661 =head2 GetAllImportBatches
665 my $results = GetAllImportBatches();
669 Returns a references to an array of hash references corresponding
670 to all import_batches rows (of batch_type 'batch'), sorted in
671 ascending order by import_batch_id.
675 sub GetAllImportBatches {
676 my $dbh = C4::Context->dbh;
677 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
678 WHERE batch_type = 'batch'
679 ORDER BY import_batch_id ASC");
683 while (my $row = $sth->fetchrow_hashref) {
684 push @$results, $row;
690 =head2 GetImportBatchRangeDesc
694 my $results = GetImportBatchRangeDesc($offset, $results_per_group);
698 Returns a reference to an array of hash references corresponding to
699 import_batches rows (sorted in descending order by import_batch_id)
700 start at the given offset.
704 sub GetImportBatchRangeDesc {
705 my ($offset, $results_per_group) = @_;
707 my $dbh = C4::Context->dbh;
708 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
709 WHERE batch_type = 'batch'
710 ORDER BY import_batch_id DESC
712 $sth->bind_param(1, $results_per_group);
713 $sth->bind_param(2, $offset);
717 while (my $row = $sth->fetchrow_hashref) {
718 push @$results, $row;
724 =head2 GetItemNumbersFromImportBatch
730 sub GetItemNumbersFromImportBatch {
732 my $dbh = C4::Context->dbh;
733 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=?");
734 $sth->execute($batch_id);
736 while ( my ($itm) = $sth->fetchrow_array ) {
742 =head2 GetNumberOfImportBatches
746 my $count = GetNumberOfImportBatches();
752 sub GetNumberOfNonZ3950ImportBatches {
753 my $dbh = C4::Context->dbh;
754 my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type='batch'");
756 my ($count) = $sth->fetchrow_array();
761 =head2 GetImportBibliosRange
765 my $results = GetImportBibliosRange($batch_id, $offset, $results_per_group);
769 Returns a reference to an array of hash references corresponding to
770 import_biblios/import_records rows for a given batch
771 starting at the given offset.
775 sub GetImportBibliosRange {
776 my ($batch_id, $offset, $results_per_group) = @_;
778 my $dbh = C4::Context->dbh;
779 my $sth = $dbh->prepare_cached("SELECT title, author, isbn, issn, import_record_id, record_sequence,
780 status, overlay_status
782 JOIN import_biblios USING (import_record_id)
783 WHERE import_batch_id = ?
784 ORDER BY import_record_id LIMIT ? OFFSET ?");
785 $sth->bind_param(1, $batch_id);
786 $sth->bind_param(2, $results_per_group);
787 $sth->bind_param(3, $offset);
790 while (my $row = $sth->fetchrow_hashref) {
791 push @$results, $row;
798 =head2 GetBestRecordMatch
802 my $record_id = GetBestRecordMatch($import_record_id);
808 sub GetBestRecordMatch {
809 my ($import_record_id) = @_;
811 my $dbh = C4::Context->dbh;
812 my $sth = $dbh->prepare("SELECT candidate_match_id
813 FROM import_record_matches
814 WHERE import_record_id = ?
815 ORDER BY score DESC, candidate_match_id DESC");
816 $sth->execute($import_record_id);
817 my ($record_id) = $sth->fetchrow_array();
822 =head2 GetImportBatchStatus
826 my $status = GetImportBatchStatus($batch_id);
832 sub GetImportBatchStatus {
835 my $dbh = C4::Context->dbh;
836 my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE batch_id = ?");
837 $sth->execute($batch_id);
838 my ($status) = $sth->fetchrow_array();
845 =head2 SetImportBatchStatus
849 SetImportBatchStatus($batch_id, $new_status);
855 sub SetImportBatchStatus {
856 my ($batch_id, $new_status) = @_;
858 my $dbh = C4::Context->dbh;
859 my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
860 $sth->execute($new_status, $batch_id);
865 =head2 GetImportBatchOverlayAction
869 my $overlay_action = GetImportBatchOverlayAction($batch_id);
875 sub GetImportBatchOverlayAction {
878 my $dbh = C4::Context->dbh;
879 my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
880 $sth->execute($batch_id);
881 my ($overlay_action) = $sth->fetchrow_array();
883 return $overlay_action;
888 =head2 SetImportBatchOverlayAction
892 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
898 sub SetImportBatchOverlayAction {
899 my ($batch_id, $new_overlay_action) = @_;
901 my $dbh = C4::Context->dbh;
902 my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
903 $sth->execute($new_overlay_action, $batch_id);
908 =head2 GetImportBatchMatcher
912 my $matcher_id = GetImportBatchMatcher($batch_id);
918 sub GetImportBatchMatcher {
921 my $dbh = C4::Context->dbh;
922 my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
923 $sth->execute($batch_id);
924 my ($matcher_id) = $sth->fetchrow_array();
931 =head2 SetImportBatchMatcher
935 SetImportBatchMatcher($batch_id, $new_matcher_id);
941 sub SetImportBatchMatcher {
942 my ($batch_id, $new_matcher_id) = @_;
944 my $dbh = C4::Context->dbh;
945 my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
946 $sth->execute($new_matcher_id, $batch_id);
951 =head2 GetImportRecordOverlayStatus
955 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
961 sub GetImportRecordOverlayStatus {
962 my ($import_record_id) = @_;
964 my $dbh = C4::Context->dbh;
965 my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
966 $sth->execute($import_record_id);
967 my ($overlay_status) = $sth->fetchrow_array();
969 return $overlay_status;
974 =head2 SetImportRecordOverlayStatus
978 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
984 sub SetImportRecordOverlayStatus {
985 my ($import_record_id, $new_overlay_status) = @_;
987 my $dbh = C4::Context->dbh;
988 my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
989 $sth->execute($new_overlay_status, $import_record_id);
994 =head2 GetImportRecordStatus
998 my $overlay_status = GetImportRecordStatus($import_record_id);
1004 sub GetImportRecordStatus {
1005 my ($import_record_id) = @_;
1007 my $dbh = C4::Context->dbh;
1008 my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
1009 $sth->execute($import_record_id);
1010 my ($overlay_status) = $sth->fetchrow_array();
1012 return $overlay_status;
1017 =head2 SetImportRecordStatus
1021 SetImportRecordStatus($import_record_id, $new_overlay_status);
1027 sub SetImportRecordStatus {
1028 my ($import_record_id, $new_overlay_status) = @_;
1030 my $dbh = C4::Context->dbh;
1031 my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
1032 $sth->execute($new_overlay_status, $import_record_id);
1037 =head2 GetImportRecordMatches
1041 my $results = GetImportRecordMatches($import_record_id, $best_only);
1047 sub GetImportRecordMatches {
1048 my $import_record_id = shift;
1049 my $best_only = @_ ? shift : 0;
1051 my $dbh = C4::Context->dbh;
1052 # FIXME currently biblio only
1053 my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber, score
1055 JOIN import_record_matches USING (import_record_id)
1056 JOIN biblio ON (biblionumber = candidate_match_id)
1057 WHERE import_record_id = ?
1058 ORDER BY score DESC, biblionumber DESC");
1059 $sth->bind_param(1, $import_record_id);
1062 while (my $row = $sth->fetchrow_hashref) {
1063 push @$results, $row;
1073 =head2 SetImportRecordMatches
1077 SetImportRecordMatches($import_record_id, @matches);
1083 sub SetImportRecordMatches {
1084 my $import_record_id = shift;
1087 my $dbh = C4::Context->dbh;
1088 my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
1089 $delsth->execute($import_record_id);
1092 my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
1094 foreach my $match (@matches) {
1095 $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
1100 # internal functions
1102 sub _create_import_record {
1103 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random) = @_;
1105 my $dbh = C4::Context->dbh;
1106 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml,
1107 record_type, encoding, z3950random)
1108 VALUES (?, ?, ?, ?, ?, ?, ?)");
1109 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(),
1110 $record_type, $encoding, $z3950random);
1111 my $import_record_id = $dbh->{'mysql_insertid'};
1113 return $import_record_id;
1116 sub _update_import_record_marc {
1117 my ($import_record_id, $marc_record) = @_;
1119 my $dbh = C4::Context->dbh;
1120 my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
1121 WHERE import_record_id = ?");
1122 $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml(), $import_record_id);
1126 sub _add_biblio_fields {
1127 my ($import_record_id, $marc_record) = @_;
1129 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1130 my $dbh = C4::Context->dbh;
1131 # FIXME no controlnumber, originalsource
1132 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1136 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
1137 $sth->execute($import_record_id, $title, $author, $isbn, $issn);
1142 sub _update_biblio_fields {
1143 my ($import_record_id, $marc_record) = @_;
1145 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1146 my $dbh = C4::Context->dbh;
1147 # FIXME no controlnumber, originalsource
1148 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1152 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
1153 WHERE import_record_id = ?");
1154 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
1158 sub _parse_biblio_fields {
1159 my ($marc_record) = @_;
1161 my $dbh = C4::Context->dbh;
1162 my $bibliofields = TransformMarcToKoha($dbh, $marc_record, '');
1163 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
1167 sub _update_batch_record_counts {
1168 my ($batch_id) = @_;
1170 my $dbh = C4::Context->dbh;
1171 my $sth = $dbh->prepare_cached("UPDATE import_batches SET num_biblios = (
1174 WHERE import_batch_id = import_batches.import_batch_id
1175 AND record_type = 'biblio')
1176 WHERE import_batch_id = ?");
1177 $sth->bind_param(1, $batch_id);
1180 $sth = $dbh->prepare_cached("UPDATE import_batches SET num_items = (
1183 JOIN import_items USING (import_record_id)
1184 WHERE import_batch_id = import_batches.import_batch_id
1185 AND record_type = 'biblio')
1186 WHERE import_batch_id = ?");
1187 $sth->bind_param(1, $batch_id);
1198 Koha Development Team <info@koha.org>
1200 Galen Charlton <galen.charlton@liblime.com>