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)) {
269 $marc_blob =~ s/^\s+//g;
270 $marc_blob =~ s/\s+$//g;
271 next unless $marc_blob;
273 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
274 &$progress_callback($rec_num);
276 my ($marc_record, $charset_guessed, $char_errors) =
277 MarcToUTF8Record($marc_blob, C4::Context->preference("marcflavour"));
278 my $import_record_id;
279 if (scalar($marc_record->fields()) == 0) {
280 push @invalid_records, $marc_blob;
283 $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $marc_flavor, int(rand(99999)), 0);
285 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0);
286 $num_items += scalar(@import_items_ids);
290 unless ($leave_as_staging) {
291 SetImportBatchStatus($batch_id, 'staged');
293 # FIXME branch_code, number of bibs, number of items
294 _update_batch_record_counts($batch_id);
295 return ($batch_id, $num_valid, $num_items, @invalid_records);
298 =head2 AddItemsToImportBiblio
302 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, $update_counts);
308 sub AddItemsToImportBiblio {
309 my $batch_id = shift;
310 my $import_record_id = shift;
311 my $marc_record = shift;
312 my $update_counts = @_ ? shift : 0;
314 my @import_items_ids = ();
316 my $dbh = C4::Context->dbh;
317 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
318 foreach my $item_field ($marc_record->field($item_tag)) {
319 my $item_marc = MARC::Record->new();
320 $item_marc->leader("00000 a "); # must set Leader/09 to 'a'
321 $item_marc->append_fields($item_field);
322 $marc_record->delete_field($item_field);
323 my $sth = $dbh->prepare_cached("INSERT INTO import_items (import_record_id, status, marcxml)
325 $sth->bind_param(1, $import_record_id);
326 $sth->bind_param(2, 'staged');
327 $sth->bind_param(3, $item_marc->as_xml());
329 push @import_items_ids, $dbh->{'mysql_insertid'};
333 if ($#import_items_ids > -1) {
334 _update_batch_record_counts($batch_id) if $update_counts;
335 _update_import_record_marc($import_record_id, $marc_record);
337 return @import_items_ids;
340 =head2 BatchFindBibDuplicates
344 my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, $max_matches, $progress_interval, $progress_callback);
348 Goes through the records loaded in the batch and attempts to
349 find duplicates for each one. Sets the overlay action to
350 "replace" if it was "create_new", and sets the overlay status
351 of each record to "no_match" or "auto_match" as appropriate.
353 The $max_matches parameter is optional; if it is not supplied,
356 The $progress_interval and $progress_callback parameters are
357 optional; if both are supplied, the sub referred to by
358 $progress_callback will be invoked every $progress_interval
359 records using the number of records processed as the
364 sub BatchFindBibDuplicates {
365 my $batch_id = shift;
367 my $max_matches = @_ ? shift : 10;
369 # optional callback to monitor status
371 my $progress_interval = 0;
372 my $progress_callback = undef;
374 $progress_interval = shift;
375 $progress_callback = shift;
376 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
377 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
380 my $dbh = C4::Context->dbh;
381 my $old_overlay_action = GetImportBatchOverlayAction($batch_id);
382 if ($old_overlay_action eq "create_new") {
383 SetImportBatchOverlayAction($batch_id, 'replace');
386 my $sth = $dbh->prepare("SELECT import_record_id, marc
388 JOIN import_biblios USING (import_record_id)
389 WHERE import_batch_id = ?");
390 $sth->execute($batch_id);
391 my $num_with_matches = 0;
393 while (my $rowref = $sth->fetchrow_hashref) {
395 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
396 &$progress_callback($rec_num);
398 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
400 if (defined $matcher) {
401 @matches = $matcher->get_matches($marc_record, $max_matches);
403 if (scalar(@matches) > 0) {
405 SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
406 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
408 SetImportRecordMatches($rowref->{'import_record_id'}, ());
409 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
413 return $num_with_matches;
416 =head2 BatchCommitBibRecords
420 my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) =
421 BatchCommitBibRecords($batch_id, $progress_interval, $progress_callback);
427 sub BatchCommitBibRecords {
428 my $batch_id = shift;
430 # optional callback to monitor status
432 my $progress_interval = 0;
433 my $progress_callback = undef;
435 $progress_interval = shift;
436 $progress_callback = shift;
437 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
438 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
443 my $num_items_added = 0;
444 my $num_items_errored = 0;
446 # commit (i.e., save, all records in the batch)
447 # FIXME biblio only at the moment
448 SetImportBatchStatus('importing');
449 my $overlay_action = GetImportBatchOverlayAction($batch_id);
450 my $dbh = C4::Context->dbh;
451 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marc, encoding
453 JOIN import_biblios USING (import_record_id)
454 WHERE import_batch_id = ?");
455 $sth->execute($batch_id);
457 while (my $rowref = $sth->fetchrow_hashref) {
459 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
460 &$progress_callback($rec_num);
462 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
466 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
468 # remove any item tags - rely on BatchCommitItems
469 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
470 foreach my $item_field ($marc_record->field($item_tag)) {
471 $marc_record->delete_field($item_field);
474 if ($overlay_action eq 'create_new' or
475 ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
477 my ($biblionumber, $biblioitemnumber) = AddBiblio($marc_record, '');
478 my $sth = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
479 $sth->execute($biblionumber, $rowref->{'import_record_id'});
481 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
482 $num_items_added += $bib_items_added;
483 $num_items_errored += $bib_items_errored;
484 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
487 my $biblionumber = GetBestRecordMatch($rowref->{'import_record_id'});
488 my ($count, $oldbiblio) = GetBiblio($biblionumber);
489 my $oldxml = GetXmlBiblio($biblionumber);
491 # remove item fields so that they don't get
492 # added again if record is reverted
493 my $old_marc = MARC::Record->new_from_xml(StripNonXmlChars($oldxml), 'UTF-8', $rowref->{'encoding'});
494 foreach my $item_field ($old_marc->field($item_tag)) {
495 $old_marc->delete_field($item_field);
498 ModBiblio($marc_record, $biblionumber, $oldbiblio->{'frameworkcode'});
499 my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
500 $sth->execute($old_marc->as_xml(), $rowref->{'import_record_id'});
502 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
503 $sth2->execute($biblionumber, $rowref->{'import_record_id'});
505 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
506 $num_items_added += $bib_items_added;
507 $num_items_errored += $bib_items_errored;
508 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
509 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
513 SetImportBatchStatus($batch_id, 'imported');
514 return ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored);
517 =head2 BatchCommitItems
521 ($num_items_added, $num_items_errored) = BatchCommitItems($import_record_id, $biblionumber);
527 sub BatchCommitItems {
528 my ($import_record_id, $biblionumber) = @_;
530 my $dbh = C4::Context->dbh;
532 my $num_items_added = 0;
533 my $num_items_errored = 0;
534 my $sth = $dbh->prepare("SELECT import_items_id, import_items.marcxml, encoding
536 JOIN import_records USING (import_record_id)
537 WHERE import_record_id = ?
538 ORDER BY import_items_id");
539 $sth->bind_param(1, $import_record_id);
541 while (my $row = $sth->fetchrow_hashref()) {
542 my $item_marc = MARC::Record->new_from_xml(StripNonXmlChars($row->{'marcxml'}), 'UTF-8', $row->{'encoding'});
543 # FIXME - duplicate barcode check needs to become part of AddItemFromMarc()
544 my $item = TransformMarcToKoha($dbh, $item_marc);
545 my $duplicate_barcode = exists($item->{'barcode'}) && GetItemnumberFromBarcode($item->{'barcode'});
546 if ($duplicate_barcode) {
547 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, import_error = ? WHERE import_items_id = ?");
548 $updsth->bind_param(1, 'error');
549 $updsth->bind_param(2, 'duplicate item barcode');
550 $updsth->bind_param(3, $row->{'import_items_id'});
552 $num_items_errored++;
554 my ($item_biblionumber, $biblioitemnumber, $itemnumber) = AddItemFromMarc($item_marc, $biblionumber);
555 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
556 $updsth->bind_param(1, 'imported');
557 $updsth->bind_param(2, $itemnumber);
558 $updsth->bind_param(3, $row->{'import_items_id'});
565 return ($num_items_added, $num_items_errored);
568 =head2 BatchRevertBibRecords
572 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) = BatchRevertBibRecords($batch_id);
578 sub BatchRevertBibRecords {
579 my $batch_id = shift;
583 my $num_reverted = 0;
584 my $num_items_deleted = 0;
586 # commit (i.e., save, all records in the batch)
587 # FIXME biblio only at the moment
588 SetImportBatchStatus('reverting');
589 my $overlay_action = GetImportBatchOverlayAction($batch_id);
590 my $dbh = C4::Context->dbh;
591 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marcxml_old, encoding, matched_biblionumber
593 JOIN import_biblios USING (import_record_id)
594 WHERE import_batch_id = ?");
595 $sth->execute($batch_id);
596 while (my $rowref = $sth->fetchrow_hashref) {
597 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
600 if ($overlay_action eq 'create_new' or
601 ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
602 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
603 my $error = DelBiblio($rowref->{'matched_biblionumber'});
604 if (defined $error) {
608 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
612 my $old_record = MARC::Record->new_from_xml(StripNonXmlChars($rowref->{'marcxml_old'}), 'UTF-8', $rowref->{'encoding'});
613 my $biblionumber = $rowref->{'matched_biblionumber'};
614 my ($count, $oldbiblio) = GetBiblio($biblionumber);
615 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
616 ModBiblio($old_record, $biblionumber, $oldbiblio->{'frameworkcode'});
617 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
621 SetImportBatchStatus($batch_id, 'reverted');
622 return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored);
625 =head2 BatchRevertItems
629 my $num_items_deleted = BatchRevertItems($import_record_id, $biblionumber);
635 sub BatchRevertItems {
636 my ($import_record_id, $biblionumber) = @_;
638 my $dbh = C4::Context->dbh;
639 my $num_items_deleted = 0;
641 my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
643 JOIN items USING (itemnumber)
644 WHERE import_record_id = ?");
645 $sth->bind_param(1, $import_record_id);
647 while (my $row = $sth->fetchrow_hashref()) {
648 DelItem($dbh, $biblionumber, $row->{'itemnumber'});
649 my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?");
650 $updsth->bind_param(1, 'reverted');
651 $updsth->bind_param(2, $row->{'import_items_id'});
654 $num_items_deleted++;
657 return $num_items_deleted;
660 =head2 GetAllImportBatches
664 my $results = GetAllImportBatches();
668 Returns a references to an array of hash references corresponding
669 to all import_batches rows (of batch_type 'batch'), sorted in
670 ascending order by import_batch_id.
674 sub GetAllImportBatches {
675 my $dbh = C4::Context->dbh;
676 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
677 WHERE batch_type = 'batch'
678 ORDER BY import_batch_id ASC");
682 while (my $row = $sth->fetchrow_hashref) {
683 push @$results, $row;
689 =head2 GetImportBatchRangeDesc
693 my $results = GetImportBatchRangeDesc($offset, $results_per_group);
697 Returns a reference to an array of hash references corresponding to
698 import_batches rows (sorted in descending order by import_batch_id)
699 start at the given offset.
703 sub GetImportBatchRangeDesc {
704 my ($offset, $results_per_group) = @_;
706 my $dbh = C4::Context->dbh;
707 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
708 WHERE batch_type = 'batch'
709 ORDER BY import_batch_id DESC
711 $sth->bind_param(1, $results_per_group);
712 $sth->bind_param(2, $offset);
716 while (my $row = $sth->fetchrow_hashref) {
717 push @$results, $row;
723 =head2 GetNumberOfImportBatches
727 my $count = GetNumberOfImportBatches();
733 sub GetNumberOfNonZ3950ImportBatches {
734 my $dbh = C4::Context->dbh;
735 my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type='batch'");
737 my ($count) = $sth->fetchrow_array();
742 =head2 GetImportBibliosRange
746 my $results = GetImportBibliosRange($batch_id, $offset, $results_per_group);
750 Returns a reference to an array of hash references corresponding to
751 import_biblios/import_records rows for a given batch
752 starting at the given offset.
756 sub GetImportBibliosRange {
757 my ($batch_id, $offset, $results_per_group) = @_;
759 my $dbh = C4::Context->dbh;
760 my $sth = $dbh->prepare_cached("SELECT title, author, isbn, issn, import_record_id, record_sequence,
761 status, overlay_status
763 JOIN import_biblios USING (import_record_id)
764 WHERE import_batch_id = ?
765 ORDER BY import_record_id LIMIT ? OFFSET ?");
766 $sth->bind_param(1, $batch_id);
767 $sth->bind_param(2, $results_per_group);
768 $sth->bind_param(3, $offset);
771 while (my $row = $sth->fetchrow_hashref) {
772 push @$results, $row;
779 =head2 GetBestRecordMatch
783 my $record_id = GetBestRecordMatch($import_record_id);
789 sub GetBestRecordMatch {
790 my ($import_record_id) = @_;
792 my $dbh = C4::Context->dbh;
793 my $sth = $dbh->prepare("SELECT candidate_match_id
794 FROM import_record_matches
795 WHERE import_record_id = ?
796 ORDER BY score DESC, candidate_match_id DESC");
797 $sth->execute($import_record_id);
798 my ($record_id) = $sth->fetchrow_array();
803 =head2 GetImportBatchStatus
807 my $status = GetImportBatchStatus($batch_id);
813 sub GetImportBatchStatus {
816 my $dbh = C4::Context->dbh;
817 my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE batch_id = ?");
818 $sth->execute($batch_id);
819 my ($status) = $sth->fetchrow_array();
826 =head2 SetImportBatchStatus
830 SetImportBatchStatus($batch_id, $new_status);
836 sub SetImportBatchStatus {
837 my ($batch_id, $new_status) = @_;
839 my $dbh = C4::Context->dbh;
840 my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
841 $sth->execute($new_status, $batch_id);
846 =head2 GetImportBatchOverlayAction
850 my $overlay_action = GetImportBatchOverlayAction($batch_id);
856 sub GetImportBatchOverlayAction {
859 my $dbh = C4::Context->dbh;
860 my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
861 $sth->execute($batch_id);
862 my ($overlay_action) = $sth->fetchrow_array();
864 return $overlay_action;
869 =head2 SetImportBatchOverlayAction
873 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
879 sub SetImportBatchOverlayAction {
880 my ($batch_id, $new_overlay_action) = @_;
882 my $dbh = C4::Context->dbh;
883 my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
884 $sth->execute($new_overlay_action, $batch_id);
889 =head2 GetImportBatchMatcher
893 my $matcher_id = GetImportBatchMatcher($batch_id);
899 sub GetImportBatchMatcher {
902 my $dbh = C4::Context->dbh;
903 my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
904 $sth->execute($batch_id);
905 my ($matcher_id) = $sth->fetchrow_array();
912 =head2 SetImportBatchMatcher
916 SetImportBatchMatcher($batch_id, $new_matcher_id);
922 sub SetImportBatchMatcher {
923 my ($batch_id, $new_matcher_id) = @_;
925 my $dbh = C4::Context->dbh;
926 my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
927 $sth->execute($new_matcher_id, $batch_id);
932 =head2 GetImportRecordOverlayStatus
936 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
942 sub GetImportRecordOverlayStatus {
943 my ($import_record_id) = @_;
945 my $dbh = C4::Context->dbh;
946 my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
947 $sth->execute($import_record_id);
948 my ($overlay_status) = $sth->fetchrow_array();
950 return $overlay_status;
955 =head2 SetImportRecordOverlayStatus
959 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
965 sub SetImportRecordOverlayStatus {
966 my ($import_record_id, $new_overlay_status) = @_;
968 my $dbh = C4::Context->dbh;
969 my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
970 $sth->execute($new_overlay_status, $import_record_id);
975 =head2 GetImportRecordStatus
979 my $overlay_status = GetImportRecordStatus($import_record_id);
985 sub GetImportRecordStatus {
986 my ($import_record_id) = @_;
988 my $dbh = C4::Context->dbh;
989 my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
990 $sth->execute($import_record_id);
991 my ($overlay_status) = $sth->fetchrow_array();
993 return $overlay_status;
998 =head2 SetImportRecordStatus
1002 SetImportRecordStatus($import_record_id, $new_overlay_status);
1008 sub SetImportRecordStatus {
1009 my ($import_record_id, $new_overlay_status) = @_;
1011 my $dbh = C4::Context->dbh;
1012 my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
1013 $sth->execute($new_overlay_status, $import_record_id);
1018 =head2 GetImportRecordMatches
1022 my $results = GetImportRecordMatches($import_record_id, $best_only);
1028 sub GetImportRecordMatches {
1029 my $import_record_id = shift;
1030 my $best_only = @_ ? shift : 0;
1032 my $dbh = C4::Context->dbh;
1033 # FIXME currently biblio only
1034 my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber, score
1036 JOIN import_record_matches USING (import_record_id)
1037 JOIN biblio ON (biblionumber = candidate_match_id)
1038 WHERE import_record_id = ?
1039 ORDER BY score DESC, biblionumber DESC");
1040 $sth->bind_param(1, $import_record_id);
1043 while (my $row = $sth->fetchrow_hashref) {
1044 push @$results, $row;
1054 =head2 SetImportRecordMatches
1058 SetImportRecordMatches($import_record_id, @matches);
1064 sub SetImportRecordMatches {
1065 my $import_record_id = shift;
1068 my $dbh = C4::Context->dbh;
1069 my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
1070 $delsth->execute($import_record_id);
1073 my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
1075 foreach my $match (@matches) {
1076 $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
1081 # internal functions
1083 sub _create_import_record {
1084 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random) = @_;
1086 my $dbh = C4::Context->dbh;
1087 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml,
1088 record_type, encoding, z3950random)
1089 VALUES (?, ?, ?, ?, ?, ?, ?)");
1090 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(),
1091 $record_type, $encoding, $z3950random);
1092 my $import_record_id = $dbh->{'mysql_insertid'};
1094 return $import_record_id;
1097 sub _update_import_record_marc {
1098 my ($import_record_id, $marc_record) = @_;
1100 my $dbh = C4::Context->dbh;
1101 my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
1102 WHERE import_record_id = ?");
1103 $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml(), $import_record_id);
1107 sub _add_biblio_fields {
1108 my ($import_record_id, $marc_record) = @_;
1110 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1111 my $dbh = C4::Context->dbh;
1112 # FIXME no controlnumber, originalsource
1113 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1117 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
1118 $sth->execute($import_record_id, $title, $author, $isbn, $issn);
1123 sub _update_biblio_fields {
1124 my ($import_record_id, $marc_record) = @_;
1126 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1127 my $dbh = C4::Context->dbh;
1128 # FIXME no controlnumber, originalsource
1129 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1133 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
1134 WHERE import_record_id = ?");
1135 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
1139 sub _parse_biblio_fields {
1140 my ($marc_record) = @_;
1142 my $dbh = C4::Context->dbh;
1143 my $bibliofields = TransformMarcToKoha($dbh, $marc_record, '');
1144 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
1148 sub _update_batch_record_counts {
1149 my ($batch_id) = @_;
1151 my $dbh = C4::Context->dbh;
1152 my $sth = $dbh->prepare_cached("UPDATE import_batches SET num_biblios = (
1155 WHERE import_batch_id = import_batches.import_batch_id
1156 AND record_type = 'biblio')
1157 WHERE import_batch_id = ?");
1158 $sth->bind_param(1, $batch_id);
1161 $sth = $dbh->prepare_cached("UPDATE import_batches SET num_items = (
1164 JOIN import_items USING (import_record_id)
1165 WHERE import_batch_id = import_batches.import_batch_id
1166 AND record_type = 'biblio')
1167 WHERE import_batch_id = ?");
1168 $sth->bind_param(1, $batch_id);
1179 Koha Development Team <info@koha.org>
1181 Galen Charlton <galen.charlton@liblime.com>