Bug 21395: Make perlcritic happy
[srvgit] / misc / cronjobs / update_totalissues.pl
index 71da3aa..f1b621b 100755 (executable)
@@ -4,18 +4,18 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
@@ -30,8 +30,12 @@ BEGIN {
 
 use Getopt::Long;
 use Pod::Usage;
+
+use Koha::Script -cron;
+use Koha::DateUtils qw/ dt_from_string /;
 use C4::Context;
 use C4::Biblio;
+use C4::Log;
 use DateTime;
 use DateTime::Format::MySQL;
 use Time::HiRes qw/time/;
@@ -68,7 +72,7 @@ my $result = GetOptions(
     'h|help'       => \$want_help
 );
 
-binmode( STDOUT, ":utf8" );
+binmode( STDOUT, ":encoding(UTF-8)" );
 
 if ( defined $since && defined $interval ) {
     print "The --since and --interval options are mutually exclusive.\n\n";
@@ -94,10 +98,13 @@ if ( not $result or $want_help ) {
     usage();
 }
 
+cronlogaction();
+
 my $dbh = C4::Context->dbh;
 $dbh->{AutoCommit} = 0;
 
 my $num_bibs_processed = 0;
+my $num_bibs_error = 0;
 
 my $starttime = time();
 
@@ -116,7 +123,7 @@ sub process_items {
 
 sub process_stats {
     if ($interval) {
-        my $dt = DateTime->now;
+        my $dt = dt_from_string();
 
         my %units = (
             h => 'hours',
@@ -132,22 +139,16 @@ sub process_stats {
             $dt->subtract( $units{$unit} => $1 ) );
     }
     my $limit = '';
-    $limit = " AND statistics.datetime >= ?" if ( $interval || $since );
+    $limit = " WHERE statistics.datetime >= ?" if ( $interval || $since );
 
     my $query =
-"SELECT biblio.biblionumber, COUNT(statistics.itemnumber) FROM biblio LEFT JOIN items ON (biblio.biblionumber=items.biblionumber) LEFT JOIN statistics ON (items.itemnumber=statistics.itemnumber) WHERE statistics.type = 'issue' $limit GROUP BY biblio.biblionumber;";
+"SELECT biblio.biblionumber, COUNT(statistics.itemnumber) FROM biblio\
+ LEFT JOIN items ON (biblio.biblionumber=items.biblionumber)\
+ LEFT JOIN statistics ON (items.itemnumber=statistics.itemnumber AND statistics.type = 'issue')
+ $limit\
+ GROUP BY biblio.biblionumber";
     process_query( $query, $limit );
 
-    unless ($incremental) {
-        $query =
-"SELECT biblio.biblionumber, 0 FROM biblio LEFT JOIN items ON (biblio.biblionumber=items.biblionumber) LEFT JOIN statistics ON (items.itemnumber=statistics.itemnumber) WHERE statistics.itemnumber IS NULL GROUP BY biblio.biblionumber;";
-        process_query( $query, '' );
-
-        $query =
-"SELECT biblio.biblionumber, 0 FROM biblio LEFT JOIN items ON (biblio.biblionumber=items.biblionumber) WHERE items.itemnumber IS NULL GROUP BY biblio.biblionumber;";
-        process_query( $query, '' );
-    }
-
     $dbh->commit();
 }
 
@@ -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;