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" };
37 use DateTime::Format::MySQL;
38 use Time::HiRes qw/time/;
39 use POSIX qw/strftime ceil/;
42 pod2usage( -verbose => 2 );
48 # command-line parameters
60 my $result = GetOptions(
61 'v|verbose' => \$verbose,
62 't|test' => \$test_only,
63 's|since=s' => \$since,
64 'i|interval=s' => \$interval,
65 'use-stats' => \$usestats,
66 'use-items' => \$useitems,
67 'incremental' => \$incremental,
68 'c|commit=i' => \$commit,
69 'h|help' => \$want_help
72 binmode( STDOUT, ":utf8" );
74 if ( defined $since && defined $interval ) {
75 print "The --since and --interval options are mutually exclusive.\n\n";
79 if ( $useitems && $incremental ) {
81 "The --use-items and --incremental options are mutually exclusive.\n\n";
85 if ( $incremental && !( defined $since || defined $interval ) ) {
89 unless ( $usestats || $useitems ) {
90 print "You must specify either --use-stats and/or --use-items.\n\n";
94 if ( not $result or $want_help ) {
100 my $dbh = C4::Context->dbh;
101 $dbh->{AutoCommit} = 0;
103 my $num_bibs_processed = 0;
104 my $num_bibs_error = 0;
106 my $starttime = time();
108 process_items() if $useitems;
109 process_stats() if $usestats;
117 "SELECT items.biblionumber, SUM(items.issues) FROM items GROUP BY items.biblionumber;";
118 process_query($query);
123 my $dt = DateTime->now;
133 $interval =~ m/([0-9]*)([hdwmy]?)$/;
135 $since = DateTime::Format::MySQL->format_datetime(
136 $dt->subtract( $units{$unit} => $1 ) );
139 $limit = " AND statistics.datetime >= ?" if ( $interval || $since );
142 "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;";
143 process_query( $query, $limit );
145 unless ($incremental) {
147 "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;";
148 process_query( $query, '' );
151 "SELECT biblio.biblionumber, 0 FROM biblio LEFT JOIN items ON (biblio.biblionumber=items.biblionumber) WHERE items.itemnumber IS NULL GROUP BY biblio.biblionumber;";
152 process_query( $query, '' );
160 my $uselimit = shift;
161 my $sth = $dbh->prepare($query);
163 if ( $since && $uselimit ) {
164 $sth->execute($since);
170 while ( my ( $biblionumber, $totalissues ) = $sth->fetchrow_array() ) {
171 $num_bibs_processed++;
172 $totalissues = 0 unless $totalissues;
173 print "Processing bib $biblionumber ($totalissues issues)\n"
175 if ( not $test_only ) {
177 if ( $incremental && $totalissues > 0 ) {
178 $ret = UpdateTotalIssues( $biblionumber, $totalissues );
181 $ret = UpdateTotalIssues( $biblionumber, 0, $totalissues );
184 print "Error while processing bib $biblionumber\n" if $verbose;
188 if ( not $test_only and ( $num_bibs_processed % $commit ) == 0 ) {
189 print_progress_and_commit($num_bibs_processed);
197 my $endtime = time();
198 my $totaltime = ceil( ( $endtime - $starttime ) * 1000 );
199 $starttime = strftime( '%D %T', localtime($starttime) );
200 $endtime = strftime( '%D %T', localtime($endtime) );
202 my $summary = <<_SUMMARY_;
204 Update total issues count script report
205 =======================================================
206 Run started at: $starttime
207 Run ended at: $endtime
208 Total run time: $totaltime ms
209 Number of bibs modified: $num_bibs_processed
210 Number of bibs with error: $num_bibs_error
212 $summary .= "\n**** Ran in test mode only ****\n" if $test_only;
216 sub print_progress_and_commit {
219 print "... processed $recs records\n";
224 update_totalissues.pl
228 update_totalissues.pl --use-stats
229 update_totalissues.pl --use-items
230 update_totalissues.pl --commit=1000
231 update_totalissues.pl --since='2012-01-01'
232 update_totalissues.pl --interval=30d
236 This batch job populates bibliographic records' total issues count based
237 on historical issue statistics.
245 =item B<-v|--verbose>
247 Provide verbose log information (list every bib modified).
251 Use the data in the statistics table for populating total issues.
255 Use items.issues data for populating total issues. Note that issues
256 data from the items table does not respect the --since or --interval
257 options, by definition. Also note that if both --use-stats and
258 --use-items are specified, the count of biblios processed will be
261 =item B<-s|--since=DATE>
263 Only process issues recorded in the statistics table since DATE.
265 =item B<-i|--interval=S>
267 Only process issues recorded in the statistics table in the last N
268 units of time. The interval should consist of a number with a one-letter
269 unit suffix. The valid suffixes are h (hours), d (days), w (weeks),
270 m (months), and y (years). The default unit is days.
272 =item B<--incremental>
274 Add the number of issues found in the statistics table to the existing
275 total issues count. Intended so that this script can be used as a cron
276 job to update popularity information during low-usage periods. If neither
277 --since or --interval are specified, incremental mode will default to
278 processing the last twenty-four hours.
282 Commit the results to the database after every N records are processed.
286 Only test the popularity population script.
292 If the time on your database server does not match the time on your Koha
293 server you will need to take that into account, and probably use the
294 --since argument instead of the --interval argument for incremental
299 This patch to Koha was sponsored by the Arcadia Public Library and the
300 Arcadia Public Library Foundation in honor of Jackie Faust-Moreno, late
301 director of the Arcadia Public Library.
305 Jared Camins-Esakov <jcamins AT cpbibliography DOT com>