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>.
23 use Getopt::Long qw( GetOptions );
24 use Pod::Usage qw( pod2usage );
26 use Koha::Script -cron;
27 use Koha::DateUtils qw( dt_from_string );
29 use C4::Biblio qw( UpdateTotalIssues );
30 use C4::Log qw( cronlogaction );
32 use DateTime::Format::MySQL;
33 use Time::HiRes qw( time );
34 use POSIX qw( ceil strftime );
37 pod2usage( -verbose => 2 );
43 # command-line parameters
55 my $result = GetOptions(
56 'v|verbose' => \$verbose,
57 't|test' => \$test_only,
58 's|since=s' => \$since,
59 'i|interval=s' => \$interval,
60 'use-stats' => \$usestats,
61 'use-items' => \$useitems,
62 'incremental' => \$incremental,
63 'c|commit=i' => \$commit,
64 'h|help' => \$want_help
67 binmode( STDOUT, ":encoding(UTF-8)" );
69 if ( defined $since && defined $interval ) {
70 print "The --since and --interval options are mutually exclusive.\n\n";
74 if ( $useitems && $incremental ) {
76 "The --use-items and --incremental options are mutually exclusive.\n\n";
80 if ( $incremental && !( defined $since || defined $interval ) ) {
84 unless ( $usestats || $useitems ) {
85 print "You must specify either --use-stats and/or --use-items.\n\n";
89 if ( not $result or $want_help ) {
95 my $dbh = C4::Context->dbh;
96 $dbh->{AutoCommit} = 0;
98 my $num_bibs_processed = 0;
99 my $num_bibs_error = 0;
101 my $starttime = time();
103 process_items() if $useitems;
104 process_stats() if $usestats;
112 "SELECT items.biblionumber, SUM(items.issues) FROM items GROUP BY items.biblionumber;";
113 process_query($query);
118 my $dt = dt_from_string();
128 $interval =~ m/([0-9]*)([hdwmy]?)$/;
130 $since = DateTime::Format::MySQL->format_datetime(
131 $dt->subtract( $units{$unit} => $1 ) );
134 $limit = " WHERE statistics.datetime >= ?" if ( $interval || $since );
137 "SELECT biblio.biblionumber, COUNT(statistics.itemnumber) FROM biblio\
138 LEFT JOIN items ON (biblio.biblionumber=items.biblionumber)\
139 LEFT JOIN statistics ON (items.itemnumber=statistics.itemnumber AND statistics.type = 'issue')
141 GROUP BY biblio.biblionumber";
142 process_query( $query, $limit );
149 my $uselimit = shift;
150 my $sth = $dbh->prepare($query);
152 if ( $since && $uselimit ) {
153 $sth->execute($since);
159 while ( my ( $biblionumber, $totalissues ) = $sth->fetchrow_array() ) {
160 $num_bibs_processed++;
161 $totalissues = 0 unless $totalissues;
162 print "Processing bib $biblionumber ($totalissues issues)\n"
164 if ( not $test_only ) {
166 if ( $incremental && $totalissues > 0 ) {
167 $ret = UpdateTotalIssues( $biblionumber, $totalissues );
170 $ret = UpdateTotalIssues( $biblionumber, 0, $totalissues );
173 print "Error while processing bib $biblionumber\n" if $verbose;
177 if ( not $test_only and ( $num_bibs_processed % $commit ) == 0 ) {
178 print_progress_and_commit($num_bibs_processed);
186 my $endtime = time();
187 my $totaltime = ceil( ( $endtime - $starttime ) * 1000 );
188 $starttime = strftime( '%D %T', localtime($starttime) );
189 $endtime = strftime( '%D %T', localtime($endtime) );
191 my $summary = <<_SUMMARY_;
193 Update total issues count script report
194 =======================================================
195 Run started at: $starttime
196 Run ended at: $endtime
197 Total run time: $totaltime ms
198 Number of bibs modified: $num_bibs_processed
199 Number of bibs with error: $num_bibs_error
201 $summary .= "\n**** Ran in test mode only ****\n" if $test_only;
205 sub print_progress_and_commit {
208 print "... processed $recs records\n";
213 update_totalissues.pl
217 update_totalissues.pl --use-stats
218 update_totalissues.pl --use-items
219 update_totalissues.pl --commit=1000
220 update_totalissues.pl --since='2012-01-01'
221 update_totalissues.pl --interval=30d
225 This batch job populates bibliographic records' total issues count based
226 on historical issue statistics.
234 =item B<-v|--verbose>
236 Provide verbose log information (list every bib modified).
240 Use the data in the statistics table for populating total issues.
244 Use items.issues data for populating total issues. Note that issues
245 data from the items table does not respect the --since or --interval
246 options, by definition. Also note that if both --use-stats and
247 --use-items are specified, the count of biblios processed will be
250 =item B<-s|--since=DATE>
252 Only process issues recorded in the statistics table since DATE.
254 =item B<-i|--interval=S>
256 Only process issues recorded in the statistics table in the last N
257 units of time. The interval should consist of a number with a one-letter
258 unit suffix. The valid suffixes are h (hours), d (days), w (weeks),
259 m (months), and y (years). The default unit is days.
261 =item B<--incremental>
263 Add the number of issues found in the statistics table to the existing
264 total issues count. Intended so that this script can be used as a cron
265 job to update popularity information during low-usage periods. If neither
266 --since or --interval are specified, incremental mode will default to
267 processing the last twenty-four hours.
271 Commit the results to the database after every N records are processed.
275 Only test the popularity population script.
281 If the time on your database server does not match the time on your Koha
282 server you will need to take that into account, and probably use the
283 --since argument instead of the --interval argument for incremental
288 This patch to Koha was sponsored by the Arcadia Public Library and the
289 Arcadia Public Library Foundation in honor of Jackie Faust-Moreno, late
290 director of the Arcadia Public Library.
294 Jared Camins-Esakov <jcamins AT cpbibliography DOT com>