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);
29 # set the version for version checking
34 C4::ImportBatch - manage batches of imported MARC records
58 BatchFindBibDuplicates
63 GetImportBatchRangeDesc
64 GetNumberOfNonZ3950ImportBatches
69 GetImportBatchOverlayAction
70 SetImportBatchOverlayAction
73 GetImportRecordOverlayStatus
74 SetImportRecordOverlayStatus
77 GetImportRecordMatches
78 SetImportRecordMatches
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 = FixEncoding($marc_blob, "\x1D");
274 my $import_record_id;
275 if (scalar($marc_record->fields()) == 0) {
276 push @invalid_records, $marc_blob;
279 $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $marc_flavor, int(rand(99999)), 0);
281 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0);
282 $num_items += scalar(@import_items_ids);
286 unless ($leave_as_staging) {
287 SetImportBatchStatus($batch_id, 'staged');
289 # FIXME branch_code, number of bibs, number of items
290 _update_batch_record_counts($batch_id);
291 return ($batch_id, $num_valid, $num_items, @invalid_records);
294 =head2 AddItemsToImportBiblio
298 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, $update_counts);
304 sub AddItemsToImportBiblio {
305 my $batch_id = shift;
306 my $import_record_id = shift;
307 my $marc_record = shift;
308 my $update_counts = @_ ? shift : 0;
310 my @import_items_ids = ();
312 my $dbh = C4::Context->dbh;
313 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
314 foreach my $item_field ($marc_record->field($item_tag)) {
315 my $item_marc = MARC::Record->new();
316 $item_marc->append_fields($item_field);
317 $marc_record->delete_field($item_field);
318 my $sth = $dbh->prepare_cached("INSERT INTO import_items (import_record_id, status, marcxml)
320 $sth->bind_param(1, $import_record_id);
321 $sth->bind_param(2, 'staged');
322 $sth->bind_param(3, $item_marc->as_xml());
324 push @import_items_ids, $dbh->{'mysql_insertid'};
328 if ($#import_items_ids > -1) {
329 _update_batch_record_counts($batch_id) if $update_counts;
330 _update_import_record_marc($import_record_id, $marc_record);
332 return @import_items_ids;
335 =head2 BatchFindBibDuplicates
339 my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, $max_matches, $progress_interval, $progress_callback);
343 Goes through the records loaded in the batch and attempts to
344 find duplicates for each one. Sets the overlay action to
345 "replace" if it was "create_new", and sets the overlay status
346 of each record to "no_match" or "auto_match" as appropriate.
348 The $max_matches parameter is optional; if it is not supplied,
351 The $progress_interval and $progress_callback parameters are
352 optional; if both are supplied, the sub referred to by
353 $progress_callback will be invoked every $progress_interval
354 records using the number of records processed as the
359 sub BatchFindBibDuplicates {
360 my $batch_id = shift;
362 my $max_matches = @_ ? shift : 10;
364 # optional callback to monitor status
366 my $progress_interval = 0;
367 my $progress_callback = undef;
369 $progress_interval = shift;
370 $progress_callback = shift;
371 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
372 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
375 my $dbh = C4::Context->dbh;
376 my $old_overlay_action = GetImportBatchOverlayAction($batch_id);
377 if ($old_overlay_action eq "create_new") {
378 SetImportBatchOverlayAction($batch_id, 'replace');
381 my $sth = $dbh->prepare("SELECT import_record_id, marc
383 JOIN import_biblios USING (import_record_id)
384 WHERE import_batch_id = ?");
385 $sth->execute($batch_id);
386 my $num_with_matches = 0;
388 while (my $rowref = $sth->fetchrow_hashref) {
390 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
391 &$progress_callback($rec_num);
393 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
395 if (defined $matcher) {
396 @matches = $matcher->get_matches($marc_record, $max_matches);
398 if (scalar(@matches) > 0) {
400 SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
401 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
403 SetImportRecordMatches($rowref->{'import_record_id'}, ());
404 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
408 return $num_with_matches;
411 =head2 BatchCommitBibRecords
415 my ($num_added, $num_updated, $num_items_added, $num_ignored) =
416 BatchCommitBibRecords($batch_id, $progress_interval, $progress_callback);
422 sub BatchCommitBibRecords {
423 my $batch_id = shift;
425 # optional callback to monitor status
427 my $progress_interval = 0;
428 my $progress_callback = undef;
430 $progress_interval = shift;
431 $progress_callback = shift;
432 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
433 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
438 my $num_items_added = 0;
440 # commit (i.e., save, all records in the batch)
441 # FIXME biblio only at the moment
442 SetImportBatchStatus('importing');
443 my $overlay_action = GetImportBatchOverlayAction($batch_id);
444 my $dbh = C4::Context->dbh;
445 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marc
447 JOIN import_biblios USING (import_record_id)
448 WHERE import_batch_id = ?");
449 $sth->execute($batch_id);
451 while (my $rowref = $sth->fetchrow_hashref) {
453 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
454 &$progress_callback($rec_num);
456 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
459 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
460 if ($overlay_action eq 'create_new' or
461 ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
463 my ($biblionumber, $biblioitemnumber) = AddBiblio($marc_record, '');
464 my $sth = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
465 $sth->execute($biblionumber, $rowref->{'import_record_id'});
467 $num_items_added += BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
468 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
471 my $biblionumber = GetBestRecordMatch($rowref->{'import_record_id'});
472 my ($count, $oldbiblio) = GetBiblio($biblionumber);
473 my $oldxml = GetXmlBiblio($biblionumber);
474 ModBiblio($marc_record, $biblionumber, $oldbiblio->{'frameworkcode'});
475 my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
476 $sth->execute($oldxml, $rowref->{'import_record_id'});
478 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
479 $sth2->execute($biblionumber, $rowref->{'import_record_id'});
481 $num_items_added += BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
482 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
483 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
487 SetImportBatchStatus($batch_id, 'imported');
488 return ($num_added, $num_updated, $num_items_added, $num_ignored);
491 =head2 BatchCommitItems
495 $num_items_added = BatchCommitItems($import_record_id, $biblionumber);
501 sub BatchCommitItems {
502 my ($import_record_id, $biblionumber) = @_;
504 my $dbh = C4::Context->dbh;
506 my $num_items_added = 0;
507 my $sth = $dbh->prepare("SELECT import_items_id, import_items.marcxml, encoding
509 JOIN import_records USING (import_record_id)
510 WHERE import_record_id = ?
511 ORDER BY import_items_id");
512 $sth->bind_param(1, $import_record_id);
514 while (my $row = $sth->fetchrow_hashref()) {
515 my $item_marc = MARC::Record->new_from_xml($row->{'marcxml'}, 'UTF-8', $row->{'encoding'});
516 my ($item_biblionumber, $biblionumber, $itemnumber) = AddItem($item_marc, $biblionumber);
517 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
518 $updsth->bind_param(1, 'imported');
519 $updsth->bind_param(2, $itemnumber);
520 $updsth->bind_param(3, $row->{'import_items_id'});
526 return $num_items_added;
529 =head2 BatchRevertBibRecords
533 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) = BatchRevertBibRecords($batch_id);
539 sub BatchRevertBibRecords {
540 my $batch_id = shift;
544 my $num_reverted = 0;
545 my $num_items_deleted = 0;
547 # commit (i.e., save, all records in the batch)
548 # FIXME biblio only at the moment
549 SetImportBatchStatus('reverting');
550 my $overlay_action = GetImportBatchOverlayAction($batch_id);
551 my $dbh = C4::Context->dbh;
552 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marcxml_old, encoding, matched_biblionumber
554 JOIN import_biblios USING (import_record_id)
555 WHERE import_batch_id = ?");
556 $sth->execute($batch_id);
557 while (my $rowref = $sth->fetchrow_hashref) {
558 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
561 if ($overlay_action eq 'create_new' or
562 ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
563 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
564 my $error = DelBiblio($rowref->{'matched_biblionumber'});
565 if (defined $error) {
569 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
573 my $old_record = MARC::Record->new_from_xml($rowref->{'marcxml_old'}, 'UTF-8', $rowref->{'encoding'});
574 my $biblionumber = $rowref->{'matched_biblionumber'};
575 my ($count, $oldbiblio) = GetBiblio($biblionumber);
576 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
577 ModBiblio($old_record, $biblionumber, $oldbiblio->{'frameworkcode'});
578 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
582 SetImportBatchStatus($batch_id, 'reverted');
583 return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored);
586 =head2 BatchRevertItems
590 my $num_items_deleted = BatchRevertItems($import_record_id, $biblionumber);
596 sub BatchRevertItems {
597 my ($import_record_id, $biblionumber) = @_;
599 my $dbh = C4::Context->dbh;
600 my $num_items_deleted = 0;
602 my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
604 JOIN items USING (itemnumber)
605 WHERE import_record_id = ?");
606 $sth->bind_param(1, $import_record_id);
608 while (my $row = $sth->fetchrow_hashref()) {
609 DelItem($dbh, $biblionumber, $row->{'itemnumber'});
610 my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?");
611 $updsth->bind_param(1, 'reverted');
612 $updsth->bind_param(2, $row->{'import_items_id'});
615 $num_items_deleted++;
618 return $num_items_deleted;
621 =head2 GetAllImportBatches
625 my $results = GetAllImportBatches();
629 Returns a references to an array of hash references corresponding
630 to all import_batches rows (of batch_type 'batch'), sorted in
631 ascending order by import_batch_id.
635 sub GetAllImportBatches {
636 my $dbh = C4::Context->dbh;
637 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
638 WHERE batch_type = 'batch'
639 ORDER BY import_batch_id ASC");
643 while (my $row = $sth->fetchrow_hashref) {
644 push @$results, $row;
650 =head2 GetImportBatchRangeDesc
654 my $results = GetImportBatchRangeDesc($offset, $results_per_group);
658 Returns a reference to an array of hash references corresponding to
659 import_batches rows (sorted in descending order by import_batch_id)
660 start at the given offset.
664 sub GetImportBatchRangeDesc {
665 my ($offset, $results_per_group) = @_;
667 my $dbh = C4::Context->dbh;
668 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
669 WHERE batch_type = 'batch'
670 ORDER BY import_batch_id DESC
672 $sth->bind_param(1, $results_per_group);
673 $sth->bind_param(2, $offset);
677 while (my $row = $sth->fetchrow_hashref) {
678 push @$results, $row;
684 =head2 GetNumberOfImportBatches
688 my $count = GetNumberOfImportBatches();
694 sub GetNumberOfNonZ3950ImportBatches {
695 my $dbh = C4::Context->dbh;
696 my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type='batch'");
698 my ($count) = $sth->fetchrow_array();
703 =head2 GetImportBibliosRange
707 my $results = GetImportBibliosRange($batch_id, $offset, $results_per_group);
711 Returns a reference to an array of hash references corresponding to
712 import_biblios/import_records rows for a given batch
713 starting at the given offset.
717 sub GetImportBibliosRange {
718 my ($batch_id, $offset, $results_per_group) = @_;
720 my $dbh = C4::Context->dbh;
721 my $sth = $dbh->prepare_cached("SELECT title, author, isbn, issn, import_record_id, record_sequence,
722 status, overlay_status
724 JOIN import_biblios USING (import_record_id)
725 WHERE import_batch_id = ?
726 ORDER BY import_record_id LIMIT ? OFFSET ?");
727 $sth->bind_param(1, $batch_id);
728 $sth->bind_param(2, $results_per_group);
729 $sth->bind_param(3, $offset);
732 while (my $row = $sth->fetchrow_hashref) {
733 push @$results, $row;
740 =head2 GetBestRecordMatch
744 my $record_id = GetBestRecordMatch($import_record_id);
750 sub GetBestRecordMatch {
751 my ($import_record_id) = @_;
753 my $dbh = C4::Context->dbh;
754 my $sth = $dbh->prepare("SELECT candidate_match_id
755 FROM import_record_matches
756 WHERE import_record_id = ?
757 ORDER BY score DESC, candidate_match_id DESC");
758 $sth->execute($import_record_id);
759 my ($record_id) = $sth->fetchrow_array();
764 =head2 GetImportBatchStatus
768 my $status = GetImportBatchStatus($batch_id);
774 sub GetImportBatchStatus {
777 my $dbh = C4::Context->dbh;
778 my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE batch_id = ?");
779 $sth->execute($batch_id);
780 my ($status) = $sth->fetchrow_array();
787 =head2 SetImportBatchStatus
791 SetImportBatchStatus($batch_id, $new_status);
797 sub SetImportBatchStatus {
798 my ($batch_id, $new_status) = @_;
800 my $dbh = C4::Context->dbh;
801 my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
802 $sth->execute($new_status, $batch_id);
807 =head2 GetImportBatchOverlayAction
811 my $overlay_action = GetImportBatchOverlayAction($batch_id);
817 sub GetImportBatchOverlayAction {
820 my $dbh = C4::Context->dbh;
821 my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
822 $sth->execute($batch_id);
823 my ($overlay_action) = $sth->fetchrow_array();
825 return $overlay_action;
830 =head2 SetImportBatchOverlayAction
834 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
840 sub SetImportBatchOverlayAction {
841 my ($batch_id, $new_overlay_action) = @_;
843 my $dbh = C4::Context->dbh;
844 my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
845 $sth->execute($new_overlay_action, $batch_id);
850 =head2 GetImportBatchMatcher
854 my $matcher_id = GetImportBatchMatcher($batch_id);
860 sub GetImportBatchMatcher {
863 my $dbh = C4::Context->dbh;
864 my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
865 $sth->execute($batch_id);
866 my ($matcher_id) = $sth->fetchrow_array();
873 =head2 SetImportBatchMatcher
877 SetImportBatchMatcher($batch_id, $new_matcher_id);
883 sub SetImportBatchMatcher {
884 my ($batch_id, $new_matcher_id) = @_;
886 my $dbh = C4::Context->dbh;
887 my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
888 $sth->execute($new_matcher_id, $batch_id);
893 =head2 GetImportRecordOverlayStatus
897 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
903 sub GetImportRecordOverlayStatus {
904 my ($import_record_id) = @_;
906 my $dbh = C4::Context->dbh;
907 my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
908 $sth->execute($import_record_id);
909 my ($overlay_status) = $sth->fetchrow_array();
911 return $overlay_status;
916 =head2 SetImportRecordOverlayStatus
920 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
926 sub SetImportRecordOverlayStatus {
927 my ($import_record_id, $new_overlay_status) = @_;
929 my $dbh = C4::Context->dbh;
930 my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
931 $sth->execute($new_overlay_status, $import_record_id);
936 =head2 GetImportRecordStatus
940 my $overlay_status = GetImportRecordStatus($import_record_id);
946 sub GetImportRecordStatus {
947 my ($import_record_id) = @_;
949 my $dbh = C4::Context->dbh;
950 my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
951 $sth->execute($import_record_id);
952 my ($overlay_status) = $sth->fetchrow_array();
954 return $overlay_status;
959 =head2 SetImportRecordStatus
963 SetImportRecordStatus($import_record_id, $new_overlay_status);
969 sub SetImportRecordStatus {
970 my ($import_record_id, $new_overlay_status) = @_;
972 my $dbh = C4::Context->dbh;
973 my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
974 $sth->execute($new_overlay_status, $import_record_id);
979 =head2 GetImportRecordMatches
983 my $results = GetImportRecordMatches($import_record_id, $best_only);
989 sub GetImportRecordMatches {
990 my $import_record_id = shift;
991 my $best_only = @_ ? shift : 0;
993 my $dbh = C4::Context->dbh;
994 # FIXME currently biblio only
995 my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber, score
997 JOIN import_record_matches USING (import_record_id)
998 JOIN biblio ON (biblionumber = candidate_match_id)
999 WHERE import_record_id = ?
1000 ORDER BY score DESC, biblionumber DESC");
1001 $sth->bind_param(1, $import_record_id);
1004 while (my $row = $sth->fetchrow_hashref) {
1005 push @$results, $row;
1015 =head2 SetImportRecordMatches
1019 SetImportRecordMatches($import_record_id, @matches);
1025 sub SetImportRecordMatches {
1026 my $import_record_id = shift;
1029 my $dbh = C4::Context->dbh;
1030 my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
1031 $delsth->execute($import_record_id);
1034 my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
1036 foreach my $match (@matches) {
1037 $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
1042 # internal functions
1044 sub _create_import_record {
1045 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random) = @_;
1047 my $dbh = C4::Context->dbh;
1048 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml,
1049 record_type, encoding, z3950random)
1050 VALUES (?, ?, ?, ?, ?, ?, ?)");
1051 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(),
1052 $record_type, $encoding, $z3950random);
1053 my $import_record_id = $dbh->{'mysql_insertid'};
1055 return $import_record_id;
1058 sub _update_import_record_marc {
1059 my ($import_record_id, $marc_record) = @_;
1061 my $dbh = C4::Context->dbh;
1062 my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
1063 WHERE import_record_id = ?");
1064 $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml(), $import_record_id);
1068 sub _add_biblio_fields {
1069 my ($import_record_id, $marc_record) = @_;
1071 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1072 my $dbh = C4::Context->dbh;
1073 # FIXME no controlnumber, originalsource
1074 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1078 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
1079 $sth->execute($import_record_id, $title, $author, $isbn, $issn);
1084 sub _update_biblio_fields {
1085 my ($import_record_id, $marc_record) = @_;
1087 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1088 my $dbh = C4::Context->dbh;
1089 # FIXME no controlnumber, originalsource
1090 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1094 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
1095 WHERE import_record_id = ?");
1096 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
1100 sub _parse_biblio_fields {
1101 my ($marc_record) = @_;
1103 my $dbh = C4::Context->dbh;
1104 my $bibliofields = TransformMarcToKoha($dbh, $marc_record, '');
1105 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
1109 sub _update_batch_record_counts {
1110 my ($batch_id) = @_;
1112 my $dbh = C4::Context->dbh;
1113 my $sth = $dbh->prepare_cached("UPDATE import_batches SET num_biblios = (
1116 WHERE import_batch_id = import_batches.import_batch_id
1117 AND record_type = 'biblio')
1118 WHERE import_batch_id = ?");
1119 $sth->bind_param(1, $batch_id);
1122 $sth = $dbh->prepare_cached("UPDATE import_batches SET num_items = (
1125 JOIN import_items USING (import_record_id)
1126 WHERE import_batch_id = import_batches.import_batch_id
1127 AND record_type = 'biblio')
1128 WHERE import_batch_id = ?");
1129 $sth->bind_param(1, $batch_id);
1139 Koha Development Team <info@koha.org>
1141 Galen Charlton <galen.charlton@liblime.com>