From 979282933f72b52439680c6aae10b1427928e6a1 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Tue, 6 Nov 2007 16:11:32 -0600 Subject: [PATCH] new batch job to commit a batch of imported bibs Signed-off-by: Chris Cormack Signed-off-by: Joshua Ferraro --- C4/ImportBatch.pm | 49 ++++++++++++++++- misc/commit_biblios_file.pl | 103 ++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100755 misc/commit_biblios_file.pl diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index 94c5f83f99..59c328494d 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -60,6 +60,7 @@ use C4::ImportBatch; BatchCommitBibRecords BatchRevertBibRecords + GetAllImportBatches GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches GetImportBibliosRange @@ -363,7 +364,8 @@ sub BatchFindBibDuplicates { =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 @@ -372,6 +374,17 @@ my ($num_added, $num_updated, $num_items_added, $num_ignored) = BatchCommitBibRe 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; @@ -386,7 +399,12 @@ sub BatchCommitBibRecords { 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++; } @@ -551,6 +569,35 @@ sub BatchRevertItems { 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 diff --git a/misc/commit_biblios_file.pl b/misc/commit_biblios_file.pl new file mode 100755 index 0000000000..09febad7ab --- /dev/null +++ b/misc/commit_biblios_file.pl @@ -0,0 +1,103 @@ +#!/usr/bin/perl + +use strict; + +use C4::Context; +use C4::ImportBatch; +use Getopt::Long; + +$| = 1; + +# command-line parameters +my $batch_number = ""; +my $list_batches = 0; +my $want_help = 0; + +my $result = GetOptions( + 'batch-number:s' => \$batch_number, + 'list-batches' => \$list_batches, + 'h|help' => \$want_help +); + +if ($want_help or (not $batch_number and not $list_batches)) { + print_usage(); + exit 0; +} + +if ($list_batches) { + list_batches(); + exit 0; +} + +# FIXME dummy user so that logging won't fail +# in future, probably should tie to a real user account +C4::Context->set_userenv(0, 'batch', 0, 'batch', 'batch', 'batch', 'batch', 'batch'); + +if ($batch_number =~ /^\d+$/ and $batch_number > 0) { + my $batch = GetImportBatch($batch_number); + die "$0: import batch $batch_number does not exist in database\n" unless defined $batch; + die "$0: import batch $batch_number status is '" . $batch->{'import_status'} . "', and therefore cannot be imported\n" + unless $batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted"; + process_batch($batch_number); +} else { + die "$0: please specify a numeric batch ID\n"; +} + +exit 0; + +sub list_batches { + my $results = GetAllImportBatches(); + print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n", "#", "File name", "Batch comments", "Status"); + print '-' x 5, ' ' , '-' x 25, ' ', '-' x 25, ' ', '-' x 10, "\n" ; + foreach my $batch (@{ $results}) { + if ($batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted") { + print sprintf("%5.5s %-25.25s %-25.25s %-10.10s\n", + $batch->{'import_batch_id'}, + $batch->{'file_name'}, + $batch->{'comments'}, + $batch->{'import_status'}); + } + } +} + +sub process_batch { + my ($import_batch_id) = @_; + + print "... importing MARC records -- please wait\n"; + my ($num_added, $num_updated, $num_items_added, $num_ignored) = BatchCommitBibRecords($import_batch_id, 100, \&print_progress); + print "... finished importing MARC records\n"; + + print <<_SUMMARY_; + +MARC record import report +---------------------------------------- +Batch number: $import_batch_id +Number of new bibs added: $num_added +Number of bibs replaced: $num_updated +Number of bibs ignored: $num_ignored +Number of items added: $num_items_added +_SUMMARY_ +} + +sub print_progress { + my $recs = shift; + print "... processed $recs records\n"; +} + +sub print_usage { + print <<_USAGE_; +$0: import a batch of staged MARC records into database. + +Use this batch job to complete the import of a batch of +MARC records that was staged either by the batch job +stage_biblios_file.pl or by the Koha Tools option +"Stage MARC Records for Import". + +Parameters: + --batch-number <#> number of the record batch + to import + --list-batches print a list of record batches + available to commit + --help or -h show this message. +_USAGE_ +} -- 2.20.1