Bug 21068: Remove NorwegianPatronDB related code
[koha.git] / misc / cronjobs / update_totalissues.pl
1 #!/usr/bin/perl
2
3 # Copyright 2012 C & P Bibliography Services
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use strict;
21 use warnings;
22
23 BEGIN {
24
25     # find Koha's Perl modules
26     # test carefully before changing this
27     use FindBin;
28     eval { require "$FindBin::Bin/../kohalib.pl" };
29 }
30
31 use Getopt::Long;
32 use Pod::Usage;
33 use C4::Context;
34 use C4::Biblio;
35 use C4::Log;
36 use DateTime;
37 use DateTime::Format::MySQL;
38 use Time::HiRes qw/time/;
39 use POSIX qw/strftime ceil/;
40
41 sub usage {
42     pod2usage( -verbose => 2 );
43     exit;
44 }
45
46 $| = 1;
47
48 # command-line parameters
49 my $verbose   = 0;
50 my $test_only = 0;
51 my $want_help = 0;
52 my $since;
53 my $interval;
54 my $usestats    = 0;
55 my $useitems    = 0;
56 my $incremental = 0;
57 my $commit      = 100;
58 my $unit;
59
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
70 );
71
72 binmode( STDOUT, ":utf8" );
73
74 if ( defined $since && defined $interval ) {
75     print "The --since and --interval options are mutually exclusive.\n\n";
76     $want_help = 1;
77 }
78
79 if ( $useitems && $incremental ) {
80     print
81       "The --use-items and --incremental options are mutually exclusive.\n\n";
82     $want_help = 1;
83 }
84
85 if ( $incremental && !( defined $since || defined $interval ) ) {
86     $interval = '24h';
87 }
88
89 unless ( $usestats || $useitems ) {
90     print "You must specify either --use-stats and/or --use-items.\n\n";
91     $want_help = 1;
92 }
93
94 if ( not $result or $want_help ) {
95     usage();
96 }
97
98 cronlogaction();
99
100 my $dbh = C4::Context->dbh;
101 $dbh->{AutoCommit} = 0;
102
103 my $num_bibs_processed = 0;
104 my $num_bibs_error = 0;
105
106 my $starttime = time();
107
108 process_items() if $useitems;
109 process_stats() if $usestats;
110
111 report();
112
113 exit 0;
114
115 sub process_items {
116     my $query =
117 "SELECT items.biblionumber, SUM(items.issues) FROM items GROUP BY items.biblionumber;";
118     process_query($query);
119 }
120
121 sub process_stats {
122     if ($interval) {
123         my $dt = DateTime->now;
124
125         my %units = (
126             h => 'hours',
127             d => 'days',
128             w => 'weeks',
129             m => 'months',
130             y => 'years'
131         );
132
133         $interval =~ m/([0-9]*)([hdwmy]?)$/;
134         $unit = $2 || 'd';
135         $since = DateTime::Format::MySQL->format_datetime(
136             $dt->subtract( $units{$unit} => $1 ) );
137     }
138     my $limit = '';
139     $limit = " AND statistics.datetime >= ?" if ( $interval || $since );
140
141     my $query =
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 );
144
145     unless ($incremental) {
146         $query =
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, '' );
149
150         $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, '' );
153     }
154
155     $dbh->commit();
156 }
157
158 sub process_query {
159     my $query    = shift;
160     my $uselimit = shift;
161     my $sth      = $dbh->prepare($query);
162
163     if ( $since && $uselimit ) {
164         $sth->execute($since);
165     }
166     else {
167         $sth->execute();
168     }
169
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"
174           if $verbose;
175         if ( not $test_only ) {
176             my $ret;
177             if ( $incremental && $totalissues > 0 ) {
178                 $ret = UpdateTotalIssues( $biblionumber, $totalissues );
179             }
180             else {
181                 $ret = UpdateTotalIssues( $biblionumber, 0, $totalissues );
182             }
183             unless ($ret) {
184                 print "Error while processing bib $biblionumber\n" if $verbose;
185                 $num_bibs_error++;
186             }
187         }
188         if ( not $test_only and ( $num_bibs_processed % $commit ) == 0 ) {
189             print_progress_and_commit($num_bibs_processed);
190         }
191     }
192
193     $dbh->commit();
194 }
195
196 sub report {
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) );
201
202     my $summary = <<_SUMMARY_;
203
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
211 _SUMMARY_
212     $summary .= "\n****  Ran in test mode only  ****\n" if $test_only;
213     print $summary;
214 }
215
216 sub print_progress_and_commit {
217     my $recs = shift;
218     $dbh->commit();
219     print "... processed $recs records\n";
220 }
221
222 =head1 NAME
223
224 update_totalissues.pl
225
226 =head1 SYNOPSIS
227
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
233
234 =head1 DESCRIPTION
235
236 This batch job populates bibliographic records' total issues count based
237 on historical issue statistics.
238
239 =over 8
240
241 =item B<--help>
242
243 Prints this help
244
245 =item B<-v|--verbose>
246
247 Provide verbose log information (list every bib modified).
248
249 =item B<--use-stats>
250
251 Use the data in the statistics table for populating total issues.
252
253 =item B<--use-items>
254
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
259 misleading.
260
261 =item B<-s|--since=DATE>
262
263 Only process issues recorded in the statistics table since DATE.
264
265 =item B<-i|--interval=S>
266
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.
271
272 =item B<--incremental>
273
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.
279
280 =item B<--commit=N>
281
282 Commit the results to the database after every N records are processed.
283
284 =item B<--test>
285
286 Only test the popularity population script.
287
288 =back
289
290 =head1 WARNING
291
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
295 updating.
296
297 =head1 CREDITS
298
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.
302
303 =head1 AUTHOR
304
305 Jared Camins-Esakov <jcamins AT cpbibliography DOT com>
306
307 =cut