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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32 # set the version for version checking
45 BatchFindBibDuplicates
51 GetImportBatchRangeDesc
52 GetNumberOfNonZ3950ImportBatches
54 GetItemNumbersFromImportBatch
58 GetImportBatchOverlayAction
59 SetImportBatchOverlayAction
60 GetImportBatchNoMatchAction
61 SetImportBatchNoMatchAction
62 GetImportBatchItemAction
63 SetImportBatchItemAction
66 GetImportRecordOverlayStatus
67 SetImportRecordOverlayStatus
70 GetImportRecordMatches
71 SetImportRecordMatches
77 C4::ImportBatch - manage batches of imported MARC records
85 =head2 GetZ3950BatchId
87 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
116 my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
120 sub GetImportRecordMarc {
121 my ($import_record_id) = @_;
123 my $dbh = C4::Context->dbh;
124 my $sth = $dbh->prepare("SELECT marc, encoding FROM import_records WHERE import_record_id = ?");
125 $sth->execute($import_record_id);
126 my ($marc, $encoding) = $sth->fetchrow();
128 return $marc, $encoding;
132 =head2 AddImportBatch
134 my $batch_id = AddImportBatch($overlay_action, $import_status, $type,
135 $file_name, $comments);
140 my ($overlay_action, $import_status, $type, $file_name, $comments) = @_;
142 my $dbh = C4::Context->dbh;
143 my $sth = $dbh->prepare("INSERT INTO import_batches (overlay_action, import_status, batch_type,
145 VALUES (?, ?, ?, ?, ?)");
146 $sth->execute($overlay_action, $import_status, $type, $file_name, $comments);
147 my $batch_id = $dbh->{'mysql_insertid'};
154 =head2 GetImportBatch
156 my $row = GetImportBatch($batch_id);
158 Retrieve a hashref of an import_batches row.
165 my $dbh = C4::Context->dbh;
166 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches WHERE import_batch_id = ?");
167 $sth->bind_param(1, $batch_id);
169 my $result = $sth->fetchrow_hashref;
175 =head2 AddBiblioToBatch
177 my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence,
178 $marc_record, $encoding, $z3950random, $update_counts);
182 sub AddBiblioToBatch {
183 my $batch_id = shift;
184 my $record_sequence = shift;
185 my $marc_record = shift;
186 my $encoding = shift;
187 my $z3950random = shift;
188 my $update_counts = @_ ? shift : 1;
190 my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, $z3950random);
191 _add_biblio_fields($import_record_id, $marc_record);
192 _update_batch_record_counts($batch_id) if $update_counts;
193 return $import_record_id;
196 =head2 ModBiblioInBatch
198 ModBiblioInBatch($import_record_id, $marc_record);
202 sub ModBiblioInBatch {
203 my ($import_record_id, $marc_record) = @_;
205 _update_import_record_marc($import_record_id, $marc_record);
206 _update_biblio_fields($import_record_id, $marc_record);
210 =head2 BatchStageMarcRecords
212 ($batch_id, $num_records, $num_items, @invalid_records) =
213 BatchStageMarcRecords($marc_flavor, $marc_records, $file_name,
214 $comments, $branch_code, $parse_items,
216 $progress_interval, $progress_callback);
220 sub BatchStageMarcRecords {
221 my $marc_flavor = shift;
222 my $marc_records = shift;
223 my $file_name = shift;
224 my $comments = shift;
225 my $branch_code = shift;
226 my $parse_items = shift;
227 my $leave_as_staging = shift;
229 # optional callback to monitor status
231 my $progress_interval = 0;
232 my $progress_callback = undef;
234 $progress_interval = shift;
235 $progress_callback = shift;
236 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
237 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
240 my $batch_id = AddImportBatch('create_new', 'staging', 'batch', $file_name, $comments);
242 SetImportBatchItemAction($batch_id, 'always_add');
244 SetImportBatchItemAction($batch_id, 'ignore');
247 my @invalid_records = ();
250 # FIXME - for now, we're dealing only with bibs
252 foreach my $marc_blob (split(/\x1D/, $marc_records)) {
253 $marc_blob =~ s/^\s+//g;
254 $marc_blob =~ s/\s+$//g;
255 next unless $marc_blob;
257 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
258 &$progress_callback($rec_num);
260 my ($marc_record, $charset_guessed, $char_errors) =
261 MarcToUTF8Record($marc_blob, C4::Context->preference("marcflavour"));
262 my $import_record_id;
263 if (scalar($marc_record->fields()) == 0) {
264 push @invalid_records, $marc_blob;
267 $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $marc_flavor, int(rand(99999)), 0);
269 my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0);
270 $num_items += scalar(@import_items_ids);
274 unless ($leave_as_staging) {
275 SetImportBatchStatus($batch_id, 'staged');
277 # FIXME branch_code, number of bibs, number of items
278 _update_batch_record_counts($batch_id);
279 return ($batch_id, $num_valid, $num_items, @invalid_records);
282 =head2 AddItemsToImportBiblio
284 my @import_items_ids = AddItemsToImportBiblio($batch_id,
285 $import_record_id, $marc_record, $update_counts);
289 sub AddItemsToImportBiblio {
290 my $batch_id = shift;
291 my $import_record_id = shift;
292 my $marc_record = shift;
293 my $update_counts = @_ ? shift : 0;
295 my @import_items_ids = ();
297 my $dbh = C4::Context->dbh;
298 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
299 foreach my $item_field ($marc_record->field($item_tag)) {
300 my $item_marc = MARC::Record->new();
301 $item_marc->leader("00000 a "); # must set Leader/09 to 'a'
302 $item_marc->append_fields($item_field);
303 $marc_record->delete_field($item_field);
304 my $sth = $dbh->prepare_cached("INSERT INTO import_items (import_record_id, status, marcxml)
306 $sth->bind_param(1, $import_record_id);
307 $sth->bind_param(2, 'staged');
308 $sth->bind_param(3, $item_marc->as_xml());
310 push @import_items_ids, $dbh->{'mysql_insertid'};
314 if ($#import_items_ids > -1) {
315 _update_batch_record_counts($batch_id) if $update_counts;
316 _update_import_record_marc($import_record_id, $marc_record);
318 return @import_items_ids;
321 =head2 BatchFindBibDuplicates
323 my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher,
324 $max_matches, $progress_interval, $progress_callback);
326 Goes through the records loaded in the batch and attempts to
327 find duplicates for each one. Sets the matching status
328 of each record to "no_match" or "auto_match" as appropriate.
330 The $max_matches parameter is optional; if it is not supplied,
333 The $progress_interval and $progress_callback parameters are
334 optional; if both are supplied, the sub referred to by
335 $progress_callback will be invoked every $progress_interval
336 records using the number of records processed as the
341 sub BatchFindBibDuplicates {
342 my $batch_id = shift;
344 my $max_matches = @_ ? shift : 10;
346 # optional callback to monitor status
348 my $progress_interval = 0;
349 my $progress_callback = undef;
351 $progress_interval = shift;
352 $progress_callback = shift;
353 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
354 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
357 my $dbh = C4::Context->dbh;
359 my $sth = $dbh->prepare("SELECT import_record_id, marc
361 JOIN import_biblios USING (import_record_id)
362 WHERE import_batch_id = ?");
363 $sth->execute($batch_id);
364 my $num_with_matches = 0;
366 while (my $rowref = $sth->fetchrow_hashref) {
368 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
369 &$progress_callback($rec_num);
371 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
373 if (defined $matcher) {
374 @matches = $matcher->get_matches($marc_record, $max_matches);
376 if (scalar(@matches) > 0) {
378 SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
379 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
381 SetImportRecordMatches($rowref->{'import_record_id'}, ());
382 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
386 return $num_with_matches;
389 =head2 BatchCommitBibRecords
391 my ($num_added, $num_updated, $num_items_added, $num_items_errored,
392 $num_ignored) = BatchCommitBibRecords($batch_id, $framework,
393 $progress_interval, $progress_callback);
397 sub BatchCommitBibRecords {
398 my $batch_id = shift;
399 my $framework = shift;
401 # optional callback to monitor status
403 my $progress_interval = 0;
404 my $progress_callback = undef;
406 $progress_interval = shift;
407 $progress_callback = shift;
408 $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
409 $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
414 my $num_items_added = 0;
415 my $num_items_errored = 0;
417 # commit (i.e., save, all records in the batch)
418 # FIXME biblio only at the moment
419 SetImportBatchStatus('importing');
420 my $overlay_action = GetImportBatchOverlayAction($batch_id);
421 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
422 my $item_action = GetImportBatchItemAction($batch_id);
423 my $dbh = C4::Context->dbh;
424 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marc, encoding
426 JOIN import_biblios USING (import_record_id)
427 WHERE import_batch_id = ?");
428 $sth->execute($batch_id);
430 while (my $rowref = $sth->fetchrow_hashref) {
432 if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
433 &$progress_callback($rec_num);
435 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
440 my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
442 # remove any item tags - rely on BatchCommitItems
443 my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
444 foreach my $item_field ($marc_record->field($item_tag)) {
445 $marc_record->delete_field($item_field);
448 # decide what what to do with the bib and item records
449 my ($bib_result, $item_result, $bib_match) =
450 _get_commit_action($overlay_action, $nomatch_action, $item_action,
451 $rowref->{'overlay_status'}, $rowref->{'import_record_id'});
453 if ($bib_result eq 'create_new') {
455 my ($biblionumber, $biblioitemnumber) = AddBiblio($marc_record, $framework);
456 my $sth = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
457 $sth->execute($biblionumber, $rowref->{'import_record_id'});
459 if ($item_result eq 'create_new') {
460 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
461 $num_items_added += $bib_items_added;
462 $num_items_errored += $bib_items_errored;
464 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
465 } elsif ($bib_result eq 'replace') {
467 my $biblionumber = $bib_match;
468 my ($count, $oldbiblio) = GetBiblio($biblionumber);
469 my $oldxml = GetXmlBiblio($biblionumber);
471 # remove item fields so that they don't get
472 # added again if record is reverted
473 my $old_marc = MARC::Record->new_from_xml(StripNonXmlChars($oldxml), 'UTF-8', $rowref->{'encoding'});
474 foreach my $item_field ($old_marc->field($item_tag)) {
475 $old_marc->delete_field($item_field);
478 ModBiblio($marc_record, $biblionumber, $oldbiblio->{'frameworkcode'});
479 my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
480 $sth->execute($old_marc->as_xml(), $rowref->{'import_record_id'});
482 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
483 $sth2->execute($biblionumber, $rowref->{'import_record_id'});
485 if ($item_result eq 'create_new') {
486 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
487 $num_items_added += $bib_items_added;
488 $num_items_errored += $bib_items_errored;
490 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
491 SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
492 } elsif ($bib_result eq 'ignore') {
494 my $biblionumber = $bib_match;
495 if (defined $biblionumber and $item_result eq 'create_new') {
496 my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber);
497 $num_items_added += $bib_items_added;
498 $num_items_errored += $bib_items_errored;
499 # still need to record the matched biblionumber so that the
500 # items can be reverted
501 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
502 $sth2->execute($biblionumber, $rowref->{'import_record_id'});
503 SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
505 SetImportRecordStatus($rowref->{'import_record_id'}, 'ignored');
509 SetImportBatchStatus($batch_id, 'imported');
510 return ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored);
513 =head2 BatchCommitItems
515 ($num_items_added, $num_items_errored) =
516 BatchCommitItems($import_record_id, $biblionumber);
520 sub BatchCommitItems {
521 my ($import_record_id, $biblionumber) = @_;
523 my $dbh = C4::Context->dbh;
525 my $num_items_added = 0;
526 my $num_items_errored = 0;
527 my $sth = $dbh->prepare("SELECT import_items_id, import_items.marcxml, encoding
529 JOIN import_records USING (import_record_id)
530 WHERE import_record_id = ?
531 ORDER BY import_items_id");
532 $sth->bind_param(1, $import_record_id);
534 while (my $row = $sth->fetchrow_hashref()) {
535 my $item_marc = MARC::Record->new_from_xml(StripNonXmlChars($row->{'marcxml'}), 'UTF-8', $row->{'encoding'});
536 # FIXME - duplicate barcode check needs to become part of AddItemFromMarc()
537 my $item = TransformMarcToKoha($dbh, $item_marc);
538 my $duplicate_barcode = exists($item->{'barcode'}) && GetItemnumberFromBarcode($item->{'barcode'});
539 if ($duplicate_barcode) {
540 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, import_error = ? WHERE import_items_id = ?");
541 $updsth->bind_param(1, 'error');
542 $updsth->bind_param(2, 'duplicate item barcode');
543 $updsth->bind_param(3, $row->{'import_items_id'});
545 $num_items_errored++;
547 my ($item_biblionumber, $biblioitemnumber, $itemnumber) = AddItemFromMarc($item_marc, $biblionumber);
548 my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
549 $updsth->bind_param(1, 'imported');
550 $updsth->bind_param(2, $itemnumber);
551 $updsth->bind_param(3, $row->{'import_items_id'});
558 return ($num_items_added, $num_items_errored);
561 =head2 BatchRevertBibRecords
563 my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted,
564 $num_ignored) = BatchRevertBibRecords($batch_id);
568 sub BatchRevertBibRecords {
569 my $batch_id = shift;
573 my $num_reverted = 0;
574 my $num_items_deleted = 0;
576 # commit (i.e., save, all records in the batch)
577 # FIXME biblio only at the moment
578 SetImportBatchStatus('reverting');
579 my $overlay_action = GetImportBatchOverlayAction($batch_id);
580 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
581 my $dbh = C4::Context->dbh;
582 my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marcxml_old, encoding, matched_biblionumber
584 JOIN import_biblios USING (import_record_id)
585 WHERE import_batch_id = ?");
586 $sth->execute($batch_id);
587 while (my $rowref = $sth->fetchrow_hashref) {
588 if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
593 my $bib_result = _get_revert_action($overlay_action, $rowref->{'overlay_status'}, $rowref->{'status'});
595 if ($bib_result eq 'delete') {
596 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
597 my $error = DelBiblio($rowref->{'matched_biblionumber'});
598 if (defined $error) {
602 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
604 } elsif ($bib_result eq 'restore') {
606 my $old_record = MARC::Record->new_from_xml(StripNonXmlChars($rowref->{'marcxml_old'}), 'UTF-8', $rowref->{'encoding'});
607 my $biblionumber = $rowref->{'matched_biblionumber'};
608 my ($count, $oldbiblio) = GetBiblio($biblionumber);
609 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
610 ModBiblio($old_record, $biblionumber, $oldbiblio->{'frameworkcode'});
611 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
612 } elsif ($bib_result eq 'ignore') {
613 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
614 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
616 my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = NULL WHERE import_record_id = ?");
617 $sth2->execute($rowref->{'import_record_id'});
621 SetImportBatchStatus($batch_id, 'reverted');
622 return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored);
625 =head2 BatchRevertItems
627 my $num_items_deleted = BatchRevertItems($import_record_id, $biblionumber);
631 sub BatchRevertItems {
632 my ($import_record_id, $biblionumber) = @_;
634 my $dbh = C4::Context->dbh;
635 my $num_items_deleted = 0;
637 my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
639 JOIN items USING (itemnumber)
640 WHERE import_record_id = ?");
641 $sth->bind_param(1, $import_record_id);
643 while (my $row = $sth->fetchrow_hashref()) {
644 DelItem($dbh, $biblionumber, $row->{'itemnumber'});
645 my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?");
646 $updsth->bind_param(1, 'reverted');
647 $updsth->bind_param(2, $row->{'import_items_id'});
650 $num_items_deleted++;
653 return $num_items_deleted;
658 CleanBatch($batch_id)
660 Deletes all staged records from the import batch
661 and sets the status of the batch to 'cleaned'. Note
662 that deleting a stage record does *not* affect
663 any record that has been committed to the database.
668 my $batch_id = shift;
669 return unless defined $batch_id;
671 C4::Context->dbh->do('DELETE FROM import_records WHERE import_batch_id = ?', {}, $batch_id);
672 SetImportBatchStatus($batch_id, 'cleaned');
675 =head2 GetAllImportBatches
677 my $results = GetAllImportBatches();
679 Returns a references to an array of hash references corresponding
680 to all import_batches rows (of batch_type 'batch'), sorted in
681 ascending order by import_batch_id.
685 sub GetAllImportBatches {
686 my $dbh = C4::Context->dbh;
687 my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
688 WHERE batch_type = 'batch'
689 ORDER BY import_batch_id ASC");
693 while (my $row = $sth->fetchrow_hashref) {
694 push @$results, $row;
700 =head2 GetImportBatchRangeDesc
702 my $results = GetImportBatchRangeDesc($offset, $results_per_group);
704 Returns a reference to an array of hash references corresponding to
705 import_batches rows (sorted in descending order by import_batch_id)
706 start at the given offset.
710 sub GetImportBatchRangeDesc {
711 my ($offset, $results_per_group) = @_;
713 my $dbh = C4::Context->dbh;
714 my $query = "SELECT * FROM import_batches
715 WHERE batch_type = 'batch'
716 ORDER BY import_batch_id DESC";
718 if ($results_per_group){
719 $query .= " LIMIT ?";
720 push(@params, $results_per_group);
723 $query .= " OFFSET ?";
724 push(@params, $offset);
726 my $sth = $dbh->prepare_cached($query);
727 $sth->execute(@params);
728 my $results = $sth->fetchall_arrayref({});
733 =head2 GetItemNumbersFromImportBatch
735 my @itemsnos = GetItemNumbersFromImportBatch($batch_id);
739 sub GetItemNumbersFromImportBatch {
741 my $dbh = C4::Context->dbh;
742 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=?");
743 $sth->execute($batch_id);
745 while ( my ($itm) = $sth->fetchrow_array ) {
751 =head2 GetNumberOfImportBatches
753 my $count = GetNumberOfImportBatches();
757 sub GetNumberOfNonZ3950ImportBatches {
758 my $dbh = C4::Context->dbh;
759 my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type='batch'");
761 my ($count) = $sth->fetchrow_array();
766 =head2 GetImportBibliosRange
768 my $results = GetImportBibliosRange($batch_id, $offset, $results_per_group);
770 Returns a reference to an array of hash references corresponding to
771 import_biblios/import_records rows for a given batch
772 starting at the given offset.
776 sub GetImportBibliosRange {
777 my ($batch_id, $offset, $results_per_group, $status) = @_;
779 my $dbh = C4::Context->dbh;
780 my $query = "SELECT title, author, isbn, issn, import_record_id, record_sequence,
781 status, overlay_status, matched_biblionumber
783 JOIN import_biblios USING (import_record_id)
784 WHERE import_batch_id = ?";
786 push(@params, $batch_id);
788 $query .= " AND status=?";
789 push(@params,$status);
791 $query.=" ORDER BY import_record_id";
793 if($results_per_group){
794 $query .= " LIMIT ?";
795 push(@params, $results_per_group);
798 $query .= " OFFSET ?";
799 push(@params, $offset);
801 my $sth = $dbh->prepare_cached($query);
802 $sth->execute(@params);
803 my $results = $sth->fetchall_arrayref({});
809 =head2 GetBestRecordMatch
811 my $record_id = GetBestRecordMatch($import_record_id);
815 sub GetBestRecordMatch {
816 my ($import_record_id) = @_;
818 my $dbh = C4::Context->dbh;
819 my $sth = $dbh->prepare("SELECT candidate_match_id
820 FROM import_record_matches
821 WHERE import_record_id = ?
822 ORDER BY score DESC, candidate_match_id DESC");
823 $sth->execute($import_record_id);
824 my ($record_id) = $sth->fetchrow_array();
829 =head2 GetImportBatchStatus
831 my $status = GetImportBatchStatus($batch_id);
835 sub GetImportBatchStatus {
838 my $dbh = C4::Context->dbh;
839 my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE import_batch_id = ?");
840 $sth->execute($batch_id);
841 my ($status) = $sth->fetchrow_array();
847 =head2 SetImportBatchStatus
849 SetImportBatchStatus($batch_id, $new_status);
853 sub SetImportBatchStatus {
854 my ($batch_id, $new_status) = @_;
856 my $dbh = C4::Context->dbh;
857 my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
858 $sth->execute($new_status, $batch_id);
863 =head2 GetImportBatchOverlayAction
865 my $overlay_action = GetImportBatchOverlayAction($batch_id);
869 sub GetImportBatchOverlayAction {
872 my $dbh = C4::Context->dbh;
873 my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
874 $sth->execute($batch_id);
875 my ($overlay_action) = $sth->fetchrow_array();
877 return $overlay_action;
882 =head2 SetImportBatchOverlayAction
884 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
888 sub SetImportBatchOverlayAction {
889 my ($batch_id, $new_overlay_action) = @_;
891 my $dbh = C4::Context->dbh;
892 my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
893 $sth->execute($new_overlay_action, $batch_id);
898 =head2 GetImportBatchNoMatchAction
900 my $nomatch_action = GetImportBatchNoMatchAction($batch_id);
904 sub GetImportBatchNoMatchAction {
907 my $dbh = C4::Context->dbh;
908 my $sth = $dbh->prepare("SELECT nomatch_action FROM import_batches WHERE import_batch_id = ?");
909 $sth->execute($batch_id);
910 my ($nomatch_action) = $sth->fetchrow_array();
912 return $nomatch_action;
917 =head2 SetImportBatchNoMatchAction
919 SetImportBatchNoMatchAction($batch_id, $new_nomatch_action);
923 sub SetImportBatchNoMatchAction {
924 my ($batch_id, $new_nomatch_action) = @_;
926 my $dbh = C4::Context->dbh;
927 my $sth = $dbh->prepare("UPDATE import_batches SET nomatch_action = ? WHERE import_batch_id = ?");
928 $sth->execute($new_nomatch_action, $batch_id);
933 =head2 GetImportBatchItemAction
935 my $item_action = GetImportBatchItemAction($batch_id);
939 sub GetImportBatchItemAction {
942 my $dbh = C4::Context->dbh;
943 my $sth = $dbh->prepare("SELECT item_action FROM import_batches WHERE import_batch_id = ?");
944 $sth->execute($batch_id);
945 my ($item_action) = $sth->fetchrow_array();
952 =head2 SetImportBatchItemAction
954 SetImportBatchItemAction($batch_id, $new_item_action);
958 sub SetImportBatchItemAction {
959 my ($batch_id, $new_item_action) = @_;
961 my $dbh = C4::Context->dbh;
962 my $sth = $dbh->prepare("UPDATE import_batches SET item_action = ? WHERE import_batch_id = ?");
963 $sth->execute($new_item_action, $batch_id);
968 =head2 GetImportBatchMatcher
970 my $matcher_id = GetImportBatchMatcher($batch_id);
974 sub GetImportBatchMatcher {
977 my $dbh = C4::Context->dbh;
978 my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
979 $sth->execute($batch_id);
980 my ($matcher_id) = $sth->fetchrow_array();
987 =head2 SetImportBatchMatcher
989 SetImportBatchMatcher($batch_id, $new_matcher_id);
993 sub SetImportBatchMatcher {
994 my ($batch_id, $new_matcher_id) = @_;
996 my $dbh = C4::Context->dbh;
997 my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
998 $sth->execute($new_matcher_id, $batch_id);
1003 =head2 GetImportRecordOverlayStatus
1005 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
1009 sub GetImportRecordOverlayStatus {
1010 my ($import_record_id) = @_;
1012 my $dbh = C4::Context->dbh;
1013 my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
1014 $sth->execute($import_record_id);
1015 my ($overlay_status) = $sth->fetchrow_array();
1017 return $overlay_status;
1022 =head2 SetImportRecordOverlayStatus
1024 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
1028 sub SetImportRecordOverlayStatus {
1029 my ($import_record_id, $new_overlay_status) = @_;
1031 my $dbh = C4::Context->dbh;
1032 my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
1033 $sth->execute($new_overlay_status, $import_record_id);
1038 =head2 GetImportRecordStatus
1040 my $overlay_status = GetImportRecordStatus($import_record_id);
1044 sub GetImportRecordStatus {
1045 my ($import_record_id) = @_;
1047 my $dbh = C4::Context->dbh;
1048 my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
1049 $sth->execute($import_record_id);
1050 my ($overlay_status) = $sth->fetchrow_array();
1052 return $overlay_status;
1057 =head2 SetImportRecordStatus
1059 SetImportRecordStatus($import_record_id, $new_overlay_status);
1063 sub SetImportRecordStatus {
1064 my ($import_record_id, $new_overlay_status) = @_;
1066 my $dbh = C4::Context->dbh;
1067 my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
1068 $sth->execute($new_overlay_status, $import_record_id);
1073 =head2 GetImportRecordMatches
1075 my $results = GetImportRecordMatches($import_record_id, $best_only);
1079 sub GetImportRecordMatches {
1080 my $import_record_id = shift;
1081 my $best_only = @_ ? shift : 0;
1083 my $dbh = C4::Context->dbh;
1084 # FIXME currently biblio only
1085 my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber, score
1087 JOIN import_record_matches USING (import_record_id)
1088 JOIN biblio ON (biblionumber = candidate_match_id)
1089 WHERE import_record_id = ?
1090 ORDER BY score DESC, biblionumber DESC");
1091 $sth->bind_param(1, $import_record_id);
1094 while (my $row = $sth->fetchrow_hashref) {
1095 push @$results, $row;
1105 =head2 SetImportRecordMatches
1107 SetImportRecordMatches($import_record_id, @matches);
1111 sub SetImportRecordMatches {
1112 my $import_record_id = shift;
1115 my $dbh = C4::Context->dbh;
1116 my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
1117 $delsth->execute($import_record_id);
1120 my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
1122 foreach my $match (@matches) {
1123 $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
1128 # internal functions
1130 sub _create_import_record {
1131 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random) = @_;
1133 my $dbh = C4::Context->dbh;
1134 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml,
1135 record_type, encoding, z3950random)
1136 VALUES (?, ?, ?, ?, ?, ?, ?)");
1137 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(),
1138 $record_type, $encoding, $z3950random);
1139 my $import_record_id = $dbh->{'mysql_insertid'};
1141 return $import_record_id;
1144 sub _update_import_record_marc {
1145 my ($import_record_id, $marc_record) = @_;
1147 my $dbh = C4::Context->dbh;
1148 my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
1149 WHERE import_record_id = ?");
1150 $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml(), $import_record_id);
1154 sub _add_biblio_fields {
1155 my ($import_record_id, $marc_record) = @_;
1157 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1158 my $dbh = C4::Context->dbh;
1159 # FIXME no controlnumber, originalsource
1160 $isbn = C4::Koha::_isbn_cleanup($isbn); # FIXME C4::Koha::_isbn_cleanup should be made public
1161 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
1162 $sth->execute($import_record_id, $title, $author, $isbn, $issn);
1167 sub _update_biblio_fields {
1168 my ($import_record_id, $marc_record) = @_;
1170 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
1171 my $dbh = C4::Context->dbh;
1172 # FIXME no controlnumber, originalsource
1173 # FIXME 2 - should regularize normalization of ISBN wherever it is done
1177 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
1178 WHERE import_record_id = ?");
1179 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
1183 sub _parse_biblio_fields {
1184 my ($marc_record) = @_;
1186 my $dbh = C4::Context->dbh;
1187 my $bibliofields = TransformMarcToKoha($dbh, $marc_record, '');
1188 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
1192 sub _update_batch_record_counts {
1193 my ($batch_id) = @_;
1195 my $dbh = C4::Context->dbh;
1196 my $sth = $dbh->prepare_cached("UPDATE import_batches SET num_biblios = (
1199 WHERE import_batch_id = import_batches.import_batch_id
1200 AND record_type = 'biblio')
1201 WHERE import_batch_id = ?");
1202 $sth->bind_param(1, $batch_id);
1205 $sth = $dbh->prepare_cached("UPDATE import_batches SET num_items = (
1208 JOIN import_items USING (import_record_id)
1209 WHERE import_batch_id = import_batches.import_batch_id
1210 AND record_type = 'biblio')
1211 WHERE import_batch_id = ?");
1212 $sth->bind_param(1, $batch_id);
1218 sub _get_commit_action {
1219 my ($overlay_action, $nomatch_action, $item_action, $overlay_status, $import_record_id) = @_;
1221 my ($bib_result, $bib_match, $item_result);
1223 if ($overlay_status ne 'no_match') {
1224 $bib_match = GetBestRecordMatch($import_record_id);
1225 if ($overlay_action eq 'replace') {
1226 $bib_result = defined($bib_match) ? 'replace' : 'create_new';
1227 } elsif ($overlay_action eq 'create_new') {
1228 $bib_result = 'create_new';
1229 } elsif ($overlay_action eq 'ignore') {
1230 $bib_result = 'ignore';
1232 $item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_matches') ? 'create_new' : 'ignore';
1234 $bib_result = $nomatch_action;
1235 $item_result = ($item_action eq 'always_add' or $item_action eq 'add_only_for_new') ? 'create_new' : 'ignore';
1238 return ($bib_result, $item_result, $bib_match);
1241 sub _get_revert_action {
1242 my ($overlay_action, $overlay_status, $status) = @_;
1246 if ($status eq 'ignored') {
1247 $bib_result = 'ignore';
1249 if ($overlay_action eq 'create_new') {
1250 $bib_result = 'delete';
1252 $bib_result = ($overlay_status eq 'match_applied') ? 'restore' : 'delete';
1263 Koha Development Team <http://koha-community.org/>
1265 Galen Charlton <galen.charlton@liblime.com>