Bug 23463: Remove DelItemCheck and ItemSafeToDelete
[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
34 use Koha::Script -cron;
35 use C4::Context;
36 use C4::Biblio;
37 use C4::Log;
38 use DateTime;
39 use DateTime::Format::MySQL;
40 use Time::HiRes qw/time/;
41 use POSIX qw/strftime ceil/;
42
43 sub usage {
44     pod2usage( -verbose => 2 );
45     exit;
46 }
47
48 $| = 1;
49
50 # command-line parameters
51 my $verbose   = 0;
52 my $test_only = 0;
53 my $want_help = 0;
54 my $since;
55 my $interval;
56 my $usestats    = 0;
57 my $useitems    = 0;
58 my $incremental = 0;
59 my $commit      = 100;
60 my $unit;
61
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
72 );
73
74 binmode( STDOUT, ":utf8" );
75
76 if ( defined $since && defined $interval ) {
77     print "The --since and --interval options are mutually exclusive.\n\n";
78     $want_help = 1;
79 }
80
81 if ( $useitems && $incremental ) {
82     print
83       "The --use-items and --incremental options are mutually exclusive.\n\n";
84     $want_help = 1;
85 }
86
87 if ( $incremental && !( defined $since || defined $interval ) ) {
88     $interval = '24h';
89 }
90
91 unless ( $usestats || $useitems ) {
92     print "You must specify either --use-stats and/or --use-items.\n\n";
93     $want_help = 1;
94 }
95
96 if ( not $result or $want_help ) {
97     usage();
98 }
99
100 cronlogaction();
101
102 my $dbh = C4::Context->dbh;
103 $dbh->{AutoCommit} = 0;
104
105 my $num_bibs_processed = 0;
106 my $num_bibs_error = 0;
107
108 my $starttime = time();
109
110 process_items() if $useitems;
111 process_stats() if $usestats;
112
113 report();
114
115 exit 0;
116
117 sub process_items {
118     my $query =
119 "SELECT items.biblionumber, SUM(items.issues) FROM items GROUP BY items.biblionumber;";
120     process_query($query);
121 }
122
123 sub process_stats {
124     if ($interval) {
125         my $dt = DateTime->now;
126
127         my %units = (
128             h => 'hours',
129             d => 'days',
130             w => 'weeks',
131             m => 'months',
132             y => 'years'
133         );
134
135         $interval =~ m/([0-9]*)([hdwmy]?)$/;
136         $unit = $2 || 'd';
137         $since = DateTime::Format::MySQL->format_datetime(
138             $dt->subtract( $units{$unit} => $1 ) );
139     }
140     my $limit = '';
141     $limit = " WHERE statistics.datetime >= ?" if ( $interval || $since );
142
143     my $query =
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')
147  $limit\
148  GROUP BY biblio.biblionumber";
149     process_query( $query, $limit );
150
151     $dbh->commit();
152 }
153
154 sub process_query {
155     my $query    = shift;
156     my $uselimit = shift;
157     my $sth      = $dbh->prepare($query);
158
159     if ( $since && $uselimit ) {
160         $sth->execute($since);
161     }
162     else {
163         $sth->execute();
164     }
165
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"
170           if $verbose;
171         if ( not $test_only ) {
172             my $ret;
173             if ( $incremental && $totalissues > 0 ) {
174                 $ret = UpdateTotalIssues( $biblionumber, $totalissues );
175             }
176             else {
177                 $ret = UpdateTotalIssues( $biblionumber, 0, $totalissues );
178             }
179             unless ($ret) {
180                 print "Error while processing bib $biblionumber\n" if $verbose;
181                 $num_bibs_error++;
182             }
183         }
184         if ( not $test_only and ( $num_bibs_processed % $commit ) == 0 ) {
185             print_progress_and_commit($num_bibs_processed);
186         }
187     }
188
189     $dbh->commit();
190 }
191
192 sub report {
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) );
197
198     my $summary = <<_SUMMARY_;
199
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
207 _SUMMARY_
208     $summary .= "\n****  Ran in test mode only  ****\n" if $test_only;
209     print $summary;
210 }
211
212 sub print_progress_and_commit {
213     my $recs = shift;
214     $dbh->commit();
215     print "... processed $recs records\n";
216 }
217
218 =head1 NAME
219
220 update_totalissues.pl
221
222 =head1 SYNOPSIS
223
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
229
230 =head1 DESCRIPTION
231
232 This batch job populates bibliographic records' total issues count based
233 on historical issue statistics.
234
235 =over 8
236
237 =item B<--help>
238
239 Prints this help
240
241 =item B<-v|--verbose>
242
243 Provide verbose log information (list every bib modified).
244
245 =item B<--use-stats>
246
247 Use the data in the statistics table for populating total issues.
248
249 =item B<--use-items>
250
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
255 misleading.
256
257 =item B<-s|--since=DATE>
258
259 Only process issues recorded in the statistics table since DATE.
260
261 =item B<-i|--interval=S>
262
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.
267
268 =item B<--incremental>
269
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.
275
276 =item B<--commit=N>
277
278 Commit the results to the database after every N records are processed.
279
280 =item B<--test>
281
282 Only test the popularity population script.
283
284 =back
285
286 =head1 WARNING
287
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
291 updating.
292
293 =head1 CREDITS
294
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.
298
299 =head1 AUTHOR
300
301 Jared Camins-Esakov <jcamins AT cpbibliography DOT com>
302
303 =cut