use C4::Context;
use C4::Koha;
use C4::Biblio;
-use C4::Matcher;
require Exporter;
BatchCommitBibRecords
BatchRevertBibRecords
+ GetAllImportBatches
GetImportBatchRangeDesc
GetNumberOfNonZ3950ImportBatches
GetImportBibliosRange
SetImportBatchStatus
GetImportBatchOverlayAction
SetImportBatchOverlayAction
+ GetImportBatchMatcher
+ SetImportBatchMatcher
GetImportRecordOverlayStatus
SetImportRecordOverlayStatus
GetImportRecordStatus
=head2 GetImportRecordMarc
-=over4
+=over 4
my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
=over 4
-($batch_id, $num_records, $num_items, @invalid_records) = BatchStageMarcRecords($marc_flavor, $marc_records, $file_name,
- $comments, $branch_code, $parse_items,
- $leave_as_staging);
+($batch_id, $num_records, $num_items, @invalid_records) =
+ BatchStageMarcRecords($marc_flavor, $marc_records, $file_name,
+ $comments, $branch_code, $parse_items,
+ $leave_as_staging,
+ $progress_interval, $progress_callback);
=back
=cut
sub BatchStageMarcRecords {
- my ($marc_flavor, $marc_records, $file_name, $comments, $branch_code, $parse_items, $leave_as_staging) = @_;
-
+ my $marc_flavor = shift;
+ my $marc_records = shift;
+ my $file_name = shift;
+ my $comments = shift;
+ my $branch_code = shift;
+ my $parse_items = shift;
+ my $leave_as_staging = shift;
+
+ # optional callback to monitor status
+ # of job
+ my $progress_interval = 0;
+ my $progress_callback = undef;
+ if ($#_ == 1) {
+ $progress_interval = shift;
+ $progress_callback = shift;
+ $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
+ $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
+ }
+
my $batch_id = AddImportBatch('create_new', 'staging', 'batch', $file_name, $comments);
my @invalid_records = ();
my $num_valid = 0;
my $rec_num = 0;
foreach my $marc_blob (split(/\x1D/, $marc_records)) {
$rec_num++;
+ if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
+ &$progress_callback($rec_num);
+ }
my $marc_record = FixEncoding($marc_blob, "\x1D");
my $import_record_id;
if (scalar($marc_record->fields()) == 0) {
=head2 BatchFindBibDuplicates
-=over4
+=over 4
-my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, $max_matches);
+my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, $max_matches, $progress_interval, $progress_callback);
=back
Goes through the records loaded in the batch and attempts to
find duplicates for each one. Sets the overlay action to
-'replace' if it was 'create_new', and sets the overlay status
-of each record to 'no_match' or 'auto_match' as appropriate.
+"replace" if it was "create_new", and sets the overlay status
+of each record to "no_match" or "auto_match" as appropriate.
The $max_matches parameter is optional; if it is not supplied,
it defaults to 10.
+The $progress_interval and $progress_callback parameters are
+optional; if both are supplied, the sub referred to by
+$progress_callback will be invoked every $progress_interval
+records using the number of records processed as the
+singular argument.
+
=cut
sub BatchFindBibDuplicates {
my $matcher = shift;
my $max_matches = @_ ? shift : 10;
+ # optional callback to monitor status
+ # of job
+ my $progress_interval = 0;
+ my $progress_callback = undef;
+ if ($#_ == 1) {
+ $progress_interval = shift;
+ $progress_callback = shift;
+ $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
+ $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
+ }
+
my $dbh = C4::Context->dbh;
my $old_overlay_action = GetImportBatchOverlayAction($batch_id);
if ($old_overlay_action eq "create_new") {
WHERE import_batch_id = ?");
$sth->execute($batch_id);
my $num_with_matches = 0;
+ my $rec_num = 0;
while (my $rowref = $sth->fetchrow_hashref) {
+ $rec_num++;
+ if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
+ &$progress_callback($rec_num);
+ }
my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
- my @matches = $matcher->get_matches($marc_record, $max_matches);
+ my @matches = ();
+ if (defined $matcher) {
+ @matches = $matcher->get_matches($marc_record, $max_matches);
+ }
if (scalar(@matches) > 0) {
$num_with_matches++;
SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
} else {
+ SetImportRecordMatches($rowref->{'import_record_id'}, ());
SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
}
}
=over 4
-my ($num_added, $num_updated, $num_items_added, $num_ignored) = BatchCommitBibRecords($batch_id);
+my ($num_added, $num_updated, $num_items_added, $num_ignored) =
+ BatchCommitBibRecords($batch_id, $progress_interval, $progress_callback);
=back
sub BatchCommitBibRecords {
my $batch_id = shift;
+ # optional callback to monitor status
+ # of job
+ my $progress_interval = 0;
+ my $progress_callback = undef;
+ if ($#_ == 1) {
+ $progress_interval = shift;
+ $progress_callback = shift;
+ $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
+ $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
+ }
+
my $num_added = 0;
my $num_updated = 0;
my $num_items_added = 0;
JOIN import_biblios USING (import_record_id)
WHERE import_batch_id = ?");
$sth->execute($batch_id);
+ my $rec_num = 0;
while (my $rowref = $sth->fetchrow_hashref) {
+ $rec_num++;
+ if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
+ &$progress_callback($rec_num);
+ }
if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
$num_ignored++;
}
my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
FROM import_items
+ JOIN items USING (itemnumber)
WHERE import_record_id = ?");
$sth->bind_param(1, $import_record_id);
$sth->execute();
return $num_items_deleted;
}
+=head2 GetAllImportBatches
+
+=over 4
+
+my $results = GetAllImportBatches();
+
+=back
+
+Returns a references to an array of hash references corresponding
+to all import_batches rows (of batch_type 'batch'), sorted in
+ascending order by import_batch_id.
+
+=cut
+
+sub GetAllImportBatches {
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
+ WHERE batch_type = 'batch'
+ ORDER BY import_batch_id ASC");
+
+ my $results = [];
+ $sth->execute();
+ while (my $row = $sth->fetchrow_hashref) {
+ push @$results, $row;
+ }
+ $sth->finish();
+ return $results;
+}
+
=head2 GetImportBatchRangeDesc
=over 4
}
+=head2 GetImportBatchMatcher
+
+=over 4
+
+my $matcher_id = GetImportBatchMatcher($batch_id);
+
+=back
+
+=cut
+
+sub GetImportBatchMatcher {
+ my ($batch_id) = @_;
+
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
+ $sth->execute($batch_id);
+ my ($matcher_id) = $sth->fetchrow_array();
+ $sth->finish();
+ return $matcher_id;
+
+}
+
+
+=head2 SetImportBatchMatcher
+
+=over 4
+
+SetImportBatchMatcher($batch_id, $new_matcher_id);
+
+=back
+
+=cut
+
+sub SetImportBatchMatcher {
+ my ($batch_id, $new_matcher_id) = @_;
+
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
+ $sth->execute($new_matcher_id, $batch_id);
+ $sth->finish();
+
+}
+
=head2 GetImportRecordOverlayStatus
=over 4