Bug 17600: Standardize our EXPORT_OK
[srvgit] / misc / maintenance / touch_all_items.pl
index fdbce5c..0879144 100755 (executable)
@@ -22,15 +22,17 @@ use warnings;
 BEGIN {
     # find Koha's Perl modules
     # test carefully before changing this
-    use FindBin;
+    use FindBin ();
     eval { require "$FindBin::Bin/../kohalib.pl" };
 }
 
 # possible modules to use
-use Getopt::Long;
+use Getopt::Long qw( GetOptions );
+
+use Koha::Script;
 use C4::Context;
-use C4::Items;
-use Pod::Usage;
+use Koha::Items;
+use Pod::Usage qw( pod2usage );
 
 
 sub usage {
@@ -67,38 +69,49 @@ if ($whereclause) {
 }
 
 # output log or STDOUT
+my $fh;
 if (defined $outfile) {
-   open (OUT, ">$outfile") || die ("Cannot open output file");
+   open ($fh, '>', $outfile) || die ("Cannot open output file");
 } else {
-   open(OUT, ">&STDOUT") || die ("Couldn't duplicate STDOUT: $!");
+   open($fh, '>&', \*STDOUT) || die ("Couldn't duplicate STDOUT: $!");
 }
 
+# FIXME Would be better to call Koha::Items->search here
 my $sth_fetch = $dbh->prepare("SELECT biblionumber, itemnumber, itemcallnumber FROM items $whereclause");
 $sth_fetch->execute();
 
 # fetch info from the search
 while (my ($biblionumber, $itemnumber, $itemcallnumber) = $sth_fetch->fetchrow_array){
-   
-  my $modok = ModItem({itemcallnumber => $itemcallnumber}, $biblionumber, $itemnumber);
+
+  my $item = Koha::Items->find($itemnumber);
+  next unless $item;
+
+  for my $c (qw( itemcallnumber cn_source ) ){
+      $item->make_column_dirty($c);
+  }
+
+  eval { $item->store };
+  my $modok = $@ ? 0 : 1;
 
   if ($modok) {
      $goodcount++;
-     print OUT "Touched item $itemnumber\n" if (defined $verbose);
+     print $fh "Touched item $itemnumber\n" if (defined $verbose);
   } else {
      $badcount++;
-     print OUT "ERROR WITH ITEM $itemnumber !!!!\n";
+     print $fh "ERROR WITH ITEM $itemnumber !!!!\n";
   }
 
   $totalcount++;
 
 }
+close($fh);
 
 # Benchmarking
 my $endtime = time();
 my $time = $endtime-$startime;
 my $accuracy = ($goodcount / $totalcount) * 100; # this is a percentage
 my $averagetime = 0;
-unless ($time == 0) {$averagetime = $totalcount / $time;};
+$averagetime = $time / $totalcount if $totalcount;
 print "Good: $goodcount, Bad: $badcount (of $totalcount) in $time seconds\n";
 printf "Accuracy: %.2f%%\nAverage time per record: %.6f seconds\n", $accuracy, $averagetime if (defined $verbose);