From 5192ab9942d1d4970a7d0814b118690aead994bf Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Fri, 14 Oct 2016 14:27:15 +0200
Subject: [PATCH] Bug 17447: Housekeeping: Tidy up
 misc/batchRebuildItemsTables.pl
Content-Type: text/plain; charset=utf-8

This script contained lots of complete if blocks on one line.
In this patch a lot of whitespace has been moved around.
A line with a call to _build_tag_directory has been removed; the results
were discarded.
Variable $extkey was not used either.
The script now does not only print help if you specify -t.

Test plan:
Run the script. Do not test on production data!
---
 misc/batchRebuildItemsTables.pl |   88 ++++++++++++++++++++++++---------------
 1 file changed, 54 insertions(+), 34 deletions(-)

diff --git a/misc/batchRebuildItemsTables.pl b/misc/batchRebuildItemsTables.pl
index 61431c4..cdd1122 100755
--- a/misc/batchRebuildItemsTables.pl
+++ b/misc/batchRebuildItemsTables.pl
@@ -1,4 +1,5 @@
 #!/usr/bin/perl
+
 use Modern::Perl;
 use Getopt::Long;
 use C4::Context;
@@ -34,7 +35,7 @@ my $errorcount = 0;
 my $starttime = gettimeofday;
 my @errors;
 my %opt;
-my ($confirm, $help, $test_parameter, $where);
+my ( $confirm, $help, $test_parameter, $where );
 GetOptions(
     'c' => \$confirm,
     'help|h' => \$help,
@@ -42,65 +43,84 @@ GetOptions(
     'where:s' => \$where,
 ) or pod2usage(2);
 
-pod2usage(1) if ($help || (!$confirm));
+pod2usage(1) if $help ;# || !$confirm;
 print "### Database will not be modified ###\n" if $test_parameter;
+
 #dbh
 my $dbh = C4::Context->dbh;
 $dbh->{AutoCommit} = 0;
+
 #sysprefs
 C4::Context->disable_syspref_cache() if ( defined( C4::Context->disable_syspref_cache() ) );
 my $CataloguingLog = C4::Context->preference('CataloguingLog');
 my $dontmerge = C4::Context->preference('dontmerge');
-$dontmerge="0" unless defnonull($dontmerge);
-$dbh->do("UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'");
-$dbh->do("UPDATE systempreferences SET value=1 where variable='dontmerge'");
+$dontmerge = "0" unless defnonull( $dontmerge );
+$dbh->do( "UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'" );
+$dbh->do( "UPDATE systempreferences SET value=1 where variable='dontmerge'" );
 $dbh->commit() unless $test_parameter;
-my ($itemfield,$itemnumbersubfield) = &GetMarcFromKohaField("items.itemnumber",'');
+
+my ( $itemfield, $itemnumbersubfield ) = GetMarcFromKohaField( "items.itemnumber", '' );
+
 #dbh query init
 my $query = qq{SELECT biblio.biblionumber AS biblionumber, biblioitems.biblioitemnumber AS biblioitemnumber, biblio.frameworkcode AS frameworkcode FROM biblio JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber};
-$query.=qq{ WHERE $where } if ($where);
+$query .= qq{ WHERE $where } if $where;
 
-my $sth = $dbh->prepare($query);
+my $sth = $dbh->prepare( $query );
 $sth->execute();
 while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow )
 {
     $count++;
     warn $count unless $count %1000;
-    my $extkey;
-    my $record = GetMarcBiblio($biblionumber,1);
-    unless ($record){ push @errors, "bad record biblionumber $biblionumber"; next; }
-    my ($tmptestfields,$tmptestdirectory,$reclen,$tmptestbaseaddress) = MARC::File::USMARC::_build_tag_directory($record);
-    #print "\n################################ record before ##################################\n".$record->as_formatted;#!test
-    unless ($test_parameter) {
-        my $rqitemnumber=$dbh->prepare("SELECT itemnumber, biblionumber from items where itemnumber = ? and biblionumber = ?");
-        foreach my $itemfield ($record->field($itemfield)){
-            my $marcitem=MARC::Record->new();
-            $marcitem->encoding('UTF-8');
-            $marcitem->append_fields($itemfield);
-            my $itemnum; my @itemnumbers = $itemfield->subfield($itemnumbersubfield);
-            foreach my $itemnumber ( @itemnumbers ){
-                $rqitemnumber->execute($itemnumber, $biblionumber);
-                if( my $row = $rqitemnumber->fetchrow_hashref ){ $itemnum = $row->{itemnumber};}
+    my $record = GetMarcBiblio( $biblionumber, 1 );
+    unless( $record ) {
+        push @errors, "bad record biblionumber $biblionumber";
+        next;
+    }
+    unless ( $test_parameter ) {
+        my $rqitemnumber = $dbh->prepare( "SELECT itemnumber, biblionumber from items where itemnumber = ? and biblionumber = ?" );
+        foreach my $itemfield ( $record->field($itemfield) ) {
+            my $marcitem = MARC::Record->new;
+            $marcitem->encoding( 'UTF-8' );
+            $marcitem->append_fields( $itemfield );
+            my $itemnum;
+            my @itemnumbers = $itemfield->subfield( $itemnumbersubfield );
+            foreach my $itemnumber ( @itemnumbers ) {
+                $rqitemnumber->execute( $itemnumber, $biblionumber );
+                if( my $row = $rqitemnumber->fetchrow_hashref ) {
+                    $itemnum = $row->{itemnumber};
+                }
+            }
+            eval {
+                if( $itemnum ) {
+                    ModItemFromMarc( $marcitem, $biblionumber, $itemnum );
+                } else {
+                    die "$biblionumber";
+                }
+            };
+            if( $@ ) {
+                warn "Problem with $biblionumber: $@";
+                warn $record->as_formatted;
             }
-            eval{ if($itemnum){ ModItemFromMarc($marcitem,$biblionumber,$itemnum) }else{ die("$biblionumber"); } };
-            if ($@){ warn "Problem with : $biblionumber : $@"; warn $record->as_formatted; }
         }
     }
-    unless ($test_parameter) {
+    unless( $test_parameter ) {
         $dbh->commit() unless $count %1000;
     }
 }
 
-my $sthCataloguingLog = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='CataloguingLog'");
-$sthCataloguingLog->execute($CataloguingLog);
-my $sthdontmerge = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='dontmerge'");
-$sthdontmerge->execute($dontmerge);
+my $sthCataloguingLog = $dbh->prepare( "UPDATE systempreferences SET value=? WHERE variable='CataloguingLog'" );
+$sthCataloguingLog->execute( $CataloguingLog );
+my $sthdontmerge = $dbh->prepare( "UPDATE systempreferences SET value=? WHERE variable='dontmerge'" );
+$sthdontmerge->execute( $dontmerge );
 $dbh->commit() unless $test_parameter;
 my $timeneeded = time() - $starttime;
 print "$count MARC record done in $timeneeded seconds\n";
-if (scalar(@errors) > 0) {
-    print "Some biblionumber could not be processed though: ", join(" ", @errors);
+if( scalar(@errors) > 0 ) {
+    print "Some biblionumber could not be processed though: ",
+        join( " ", @errors );
 }
 
-sub defnonull { my $var = shift; defined $var and $var ne "" }
-__END__
+sub defnonull {
+    my $var = shift;
+    return defined $var and $var ne "";
+}
-- 
1.7.10.4