3 # Copyright 2012 C & P Bibliography Services
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 # find Koha's Perl modules
26 # test carefully before changing this
28 eval { require "$FindBin::Bin/../kohalib.pl" };
34 use Koha::Script -cron;
39 use DateTime::Format::MySQL;
40 use Time::HiRes qw/time/;
41 use POSIX qw/strftime ceil/;
44 pod2usage( -verbose => 2 );
50 # command-line parameters
62 my $result = GetOptions(
63 'v|verbose' => \$verbose,
64 't|test' => \$test_only,
65 's|since=s' => \$since,
66 'i|interval=s' => \$interval,
67 'use-stats' => \$usestats,
68 'use-items' => \$useitems,
69 'incremental' => \$incremental,
70 'c|commit=i' => \$commit,
71 'h|help' => \$want_help
74 binmode( STDOUT, ":utf8" );
76 if ( defined $since && defined $interval ) {
77 print "The --since and --interval options are mutually exclusive.\n\n";
81 if ( $useitems && $incremental ) {
83 "The --use-items and --incremental options are mutually exclusive.\n\n";
87 if ( $incremental && !( defined $since || defined $interval ) ) {
91 unless ( $usestats || $useitems ) {
92 print "You must specify either --use-stats and/or --use-items.\n\n";
96 if ( not $result or $want_help ) {
102 my $dbh = C4::Context->dbh;
103 $dbh->{AutoCommit} = 0;
105 my $num_bibs_processed = 0;
106 my $num_bibs_error = 0;
108 my $starttime = time();
110 process_items() if $useitems;
111 process_stats() if $usestats;
119 "SELECT items.biblionumber, SUM(items.issues) FROM items GROUP BY items.biblionumber;";
120 process_query($query);
125 my $dt = DateTime->now;
135 $interval =~ m/([0-9]*)([hdwmy]?)$/;
137 $since = DateTime::Format::MySQL->format_datetime(
138 $dt->subtract( $units{$unit} => $1 ) );
141 $limit = " WHERE statistics.datetime >= ?" if ( $interval || $since );
144 "SELECT biblio.biblionumber, COUNT(statistics.itemnumber) FROM biblio\
145 LEFT JOIN items ON (biblio.biblionumber=items.biblionumber)\
146 LEFT JOIN statistics ON (items.itemnumber=statistics.itemnumber AND statistics.type = 'issue')
148 GROUP BY biblio.biblionumber";
149 process_query( $query, $limit );
156 my $uselimit = shift;
157 my $sth = $dbh->prepare($query);
159 if ( $since && $uselimit ) {
160 $sth->execute($since);
166 while ( my ( $biblionumber, $totalissues ) = $sth->fetchrow_array() ) {
167 $num_bibs_processed++;
168 $totalissues = 0 unless $totalissues;
169 print "Processing bib $biblionumber ($totalissues issues)\n"
171 if ( not $test_only ) {
173 if ( $incremental && $totalissues > 0 ) {
174 $ret = UpdateTotalIssues( $biblionumber, $totalissues );
177 $ret = UpdateTotalIssues( $biblionumber, 0, $totalissues );
180 print "Error while processing bib $biblionumber\n" if $verbose;
184 if ( not $test_only and ( $num_bibs_processed % $commit ) == 0 ) {
185 print_progress_and_commit($num_bibs_processed);
193 my $endtime = time();
194 my $totaltime = ceil( ( $endtime - $starttime ) * 1000 );
195 $starttime = strftime( '%D %T', localtime($starttime) );
196 $endtime = strftime( '%D %T', localtime($endtime) );
198 my $summary = <<_SUMMARY_;
200 Update total issues count script report
201 =======================================================
202 Run started at: $starttime
203 Run ended at: $endtime
204 Total run time: $totaltime ms
205 Number of bibs modified: $num_bibs_processed
206 Number of bibs with error: $num_bibs_error
208 $summary .= "\n**** Ran in test mode only ****\n" if $test_only;
212 sub print_progress_and_commit {
215 print "... processed $recs records\n";
220 update_totalissues.pl
224 update_totalissues.pl --use-stats
225 update_totalissues.pl --use-items
226 update_totalissues.pl --commit=1000
227 update_totalissues.pl --since='2012-01-01'
228 update_totalissues.pl --interval=30d
232 This batch job populates bibliographic records' total issues count based
233 on historical issue statistics.
241 =item B<-v|--verbose>
243 Provide verbose log information (list every bib modified).
247 Use the data in the statistics table for populating total issues.
251 Use items.issues data for populating total issues. Note that issues
252 data from the items table does not respect the --since or --interval
253 options, by definition. Also note that if both --use-stats and
254 --use-items are specified, the count of biblios processed will be
257 =item B<-s|--since=DATE>
259 Only process issues recorded in the statistics table since DATE.
261 =item B<-i|--interval=S>
263 Only process issues recorded in the statistics table in the last N
264 units of time. The interval should consist of a number with a one-letter
265 unit suffix. The valid suffixes are h (hours), d (days), w (weeks),
266 m (months), and y (years). The default unit is days.
268 =item B<--incremental>
270 Add the number of issues found in the statistics table to the existing
271 total issues count. Intended so that this script can be used as a cron
272 job to update popularity information during low-usage periods. If neither
273 --since or --interval are specified, incremental mode will default to
274 processing the last twenty-four hours.
278 Commit the results to the database after every N records are processed.
282 Only test the popularity population script.
288 If the time on your database server does not match the time on your Koha
289 server you will need to take that into account, and probably use the
290 --since argument instead of the --interval argument for incremental
295 This patch to Koha was sponsored by the Arcadia Public Library and the
296 Arcadia Public Library Foundation in honor of Jackie Faust-Moreno, late
297 director of the Arcadia Public Library.
301 Jared Camins-Esakov <jcamins AT cpbibliography DOT com>