From 9ca952560c77e218f0744e8af1be180e35cb034c Mon Sep 17 00:00:00 2001
From: Doug Kingston <dpk@efdss.org>
Date: Sun, 31 Oct 2010 12:34:37 +0000
Subject: [PATCH] Add batch revert capability (--revert) to
 commit_biblios_file.pl. Also add --progress-interval to
 facilitate debugging bad MARC batches.

The basis for this enhancement was a bad batch of MARC records that
had some unknown Unicode characters in them which caused the batch
import function from the web interface to abort after importing only
a portion of the records.  At this point you are completely stuck using
the web user interface.  You can't go forward because the only option
is to try the import again from the beginning (which fails again).
You cannot revert either since there is no way to revert a partially
committed batch.

This change uses the existing BatchRevertBibRecords function.
Bug reference: 5332

Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Rebased to current master 2012-06-11 - please test.
---
 misc/commit_biblios_file.pl |   44 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 39 insertions(+), 5 deletions(-)

diff --git a/misc/commit_biblios_file.pl b/misc/commit_biblios_file.pl
index b4be670..0024f43 100755
--- a/misc/commit_biblios_file.pl
+++ b/misc/commit_biblios_file.pl
@@ -18,12 +18,16 @@ $| = 1;
 # command-line parameters
 my $batch_number = "";
 my $list_batches = 0;
+my $progress_interval = 100;
 my $want_help = 0;
+my $revert = 0;
 
 my $result = GetOptions(
-    'batch-number:s' => \$batch_number,
-    'list-batches'   => \$list_batches,
-    'h|help'         => \$want_help
+    'batch-number:s'     => \$batch_number,
+    'list-batches'       => \$list_batches,
+    'progress-interval'  => \$progress_interval,
+    'revert'             => \$revert,
+    'h|help'             => \$want_help
 );
 
 if ($want_help or (not $batch_number and not $list_batches)) {
@@ -47,7 +51,11 @@ if ($batch_number =~ /^\d+$/ and $batch_number > 0) {
     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);
+    if ($revert) {
+	revert_batch($batch_number);
+    } else {
+	process_batch($batch_number);
+    }
     $dbh->commit();
 } else {
     die "$0: please specify a numeric batch ID\n";
@@ -75,7 +83,7 @@ sub process_batch {
 
     print "... importing MARC records -- please wait\n";
     my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) = 
-        BatchCommitBibRecords($import_batch_id, '', 100, \&print_progress_and_commit);
+        BatchCommitBibRecords($import_batch_id, $progress_interval, \&print_progress_and_commit);
     print "... finished importing MARC records\n";
 
     print <<_SUMMARY_;
@@ -94,6 +102,29 @@ duplicate of one already in the database.
 _SUMMARY_
 }
 
+sub revert_batch {
+    my ($import_batch_id) = @_;
+
+    print "... reverting MARC records -- please wait\n";
+    my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) =
+	BatchRevertBibRecords($import_batch_id);
+    print "... finished revrting MARC records\n";
+
+    print <<_SUMMARY_;
+
+MARC record reversion report
+----------------------------------------
+Batch number:                    $import_batch_id
+Number of bibs deleted:          $num_deleted
+Number of errors:                $num_errors
+Number of bibs reverted:         $num_reverted
+Number of items deleted:         $num_items_deleted
+Number of ignored:               $num_ignored
+
+Note: an item is ignored if its in batch but not actually commited.
+_SUMMARY_
+}
+
 sub print_progress_and_commit {
     my $recs = shift;
     print "... processed $recs records\n";
@@ -112,6 +143,9 @@ stage_biblios_file.pl or by the Koha Tools option
 Parameters:
     --batch-number <#>   number of the record batch
                          to import
+    --progress-interval <#>  update import progress every <#> records
+                         (default 100)
+    --revert             revert the batch rather than commiting it
     --list-batches       print a list of record batches
                          available to commit
     --help or -h            show this message.
-- 
1.7.9.5