From f3f3f64b39909ee8a8336c0f115b95f63b1924d4 Mon Sep 17 00:00:00 2001
From: Admin User Koha <koha@localhost>
Date: Mon, 20 Oct 2014 15:07:29 +0200
Subject: [PATCH] Bug 12995 - script update_totalissues.pl stops on corrupted
 record

When running update_totalissues.pl cronjob, it will stop on a corrupted record.

This patch changes UpdateTotalIssues so that it return 1 if processing record has succeded.
Also, if mapping with biblioitems.totalissues does not exist, the method has nothing to do so it stops and returns 1.

When processing a corrupted record, script now alerts about the error on this biblionumber (if script is verbose) and process next record.
A total number of records with error will be printed at the end of the script.

Test plan :
- Create a dabase with a few biblios and some issues
- Modify first biblio record (use direct sql update) : set empty value in biblioitems.marcxml
- Launch script : misc/cronjobs/update_totalissues.pl --use-stats --commit=1000 -v
=> Without patch : the script stops at first record
=> With patch : the script prints error for first record and processes all records
---
 C4/Biblio.pm                        | 19 ++++++++++++++-----
 misc/cronjobs/update_totalissues.pl | 11 +++++++++--
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/C4/Biblio.pm b/C4/Biblio.pm
index ec4c71e..8bdb25f 100644
--- a/C4/Biblio.pm
+++ b/C4/Biblio.pm
@@ -3684,16 +3684,26 @@ sub UpdateTotalIssues {
     my ($biblionumber, $increase, $value) = @_;
     my $totalissues;
 
+    my $record = GetMarcBiblio($biblionumber);
+    unless ($record) {
+        carp "UpdateTotalIssues could not get biblio record";
+        return;
+    }
     my $data = GetBiblioData($biblionumber);
+    unless ($data) {
+        carp "UpdateTotalIssues could not get datas of biblio";
+        return;
+    }
+    my ($totalissuestag, $totalissuessubfield) = GetMarcFromKohaField('biblioitems.totalissues', $data->{'frameworkcode'});
+    unless ($totalissuestag) {
+        return 1; # There is nothing to do
+    }
 
     if (defined $value) {
         $totalissues = $value;
     } else {
         $totalissues = $data->{'totalissues'} + $increase;
     }
-     my ($totalissuestag, $totalissuessubfield) = GetMarcFromKohaField('biblioitems.totalissues', $data->{'frameworkcode'});
-
-     my $record = GetMarcBiblio($biblionumber);
 
      my $field = $record->field($totalissuestag);
      if (defined $field) {
@@ -3704,8 +3714,7 @@ sub UpdateTotalIssues {
          $record->insert_grouped_field($field);
      }
 
-     ModBiblio($record, $biblionumber, $data->{'frameworkcode'});
-     return;
+     return ModBiblio($record, $biblionumber, $data->{'frameworkcode'});
 }
 
 =head2 RemoveAllNsb
diff --git a/misc/cronjobs/update_totalissues.pl b/misc/cronjobs/update_totalissues.pl
index 71da3aa..ba29225 100755
--- a/misc/cronjobs/update_totalissues.pl
+++ b/misc/cronjobs/update_totalissues.pl
@@ -98,6 +98,7 @@ my $dbh = C4::Context->dbh;
 $dbh->{AutoCommit} = 0;
 
 my $num_bibs_processed = 0;
+my $num_bibs_error = 0;
 
 my $starttime = time();
 
@@ -169,11 +170,16 @@ sub process_query {
         print "Processing bib $biblionumber ($totalissues issues)\n"
           if $verbose;
         if ( not $test_only ) {
+            my $ret;
             if ( $incremental && $totalissues > 0 ) {
-                UpdateTotalIssues( $biblionumber, $totalissues );
+                $ret = UpdateTotalIssues( $biblionumber, $totalissues );
             }
             else {
-                UpdateTotalIssues( $biblionumber, 0, $totalissues );
+                $ret = UpdateTotalIssues( $biblionumber, 0, $totalissues );
+            }
+            unless ($ret) {
+                print "Error while processing bib $biblionumber\n" if $verbose;
+                $num_bibs_error++;
             }
         }
         if ( not $test_only and ( $num_bibs_processed % $commit ) == 0 ) {
@@ -198,6 +204,7 @@ Run started at:                         $starttime
 Run ended at:                           $endtime
 Total run time:                         $totaltime ms
 Number of bibs modified:                $num_bibs_processed
+Number of bibs with error:              $num_bibs_error
 _SUMMARY_
     $summary .= "\n****  Ran in test mode only  ****\n" if $test_only;
     print $summary;
-- 
1.9.1