#
# 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;
-BEGIN {
+use Getopt::Long qw( GetOptions );
+use Pod::Usage qw( pod2usage );
- # find Koha's Perl modules
- # test carefully before changing this
- use FindBin;
- eval { require "$FindBin::Bin/../kohalib.pl" };
-}
-
-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::Biblio qw( UpdateTotalIssues );
+use C4::Log qw( cronlogaction );
use DateTime;
use DateTime::Format::MySQL;
-use Time::HiRes qw/time/;
-use POSIX qw/strftime ceil/;
+use Time::HiRes qw( time );
+use POSIX qw( ceil strftime );
sub usage {
pod2usage( -verbose => 2 );
my $commit = 100;
my $unit;
+my $command_line_options = join(" ",@ARGV);
+
my $result = GetOptions(
'v|verbose' => \$verbose,
't|test' => \$test_only,
'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";
usage();
}
+cronlogaction({ info => $command_line_options });
+
my $dbh = C4::Context->dbh;
$dbh->{AutoCommit} = 0;
my $num_bibs_processed = 0;
+my $num_bibs_error = 0;
my $starttime = time();
report();
+cronlogaction({ action => 'End', info => "COMPLETED" });
+
exit 0;
sub process_items {
sub process_stats {
if ($interval) {
- my $dt = DateTime->now;
+ my $dt = dt_from_string();
my %units = (
h => 'hours',
$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();
}
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 ) {
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;