Bug 17600: Standardize our EXPORT_OK
[srvgit] / misc / maintenance / touch_all_items.pl
1 #!/usr/bin/perl
2 #
3 # Copyright (C) 2011 ByWater Solutions
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 BEGIN {
23     # find Koha's Perl modules
24     # test carefully before changing this
25     use FindBin ();
26     eval { require "$FindBin::Bin/../kohalib.pl" };
27 }
28
29 # possible modules to use
30 use Getopt::Long qw( GetOptions );
31
32 use Koha::Script;
33 use C4::Context;
34 use Koha::Items;
35 use Pod::Usage qw( pod2usage );
36
37
38 sub usage {
39     pod2usage( -verbose => 2 );
40     exit;
41 }
42
43 # Database handle
44 my $dbh = C4::Context->dbh;
45
46 # Benchmarking variables
47 my $startime = time();
48 my $goodcount = 0;
49 my $badcount = 0;
50 my $totalcount = 0;
51
52 # Options
53 my $verbose;
54 my $whereclause = '';
55 my $help;
56 my $outfile;
57
58 GetOptions(
59   'o|output:s' => \$outfile,
60   'v' => \$verbose,
61   'where:s' => \$whereclause,
62   'help|h'   => \$help,
63 );
64
65 usage() if $help;
66
67 if ($whereclause) {
68    $whereclause = "WHERE $whereclause";
69 }
70
71 # output log or STDOUT
72 my $fh;
73 if (defined $outfile) {
74    open ($fh, '>', $outfile) || die ("Cannot open output file");
75 } else {
76    open($fh, '>&', \*STDOUT) || die ("Couldn't duplicate STDOUT: $!");
77 }
78
79 # FIXME Would be better to call Koha::Items->search here
80 my $sth_fetch = $dbh->prepare("SELECT biblionumber, itemnumber, itemcallnumber FROM items $whereclause");
81 $sth_fetch->execute();
82
83 # fetch info from the search
84 while (my ($biblionumber, $itemnumber, $itemcallnumber) = $sth_fetch->fetchrow_array){
85
86   my $item = Koha::Items->find($itemnumber);
87   next unless $item;
88
89   for my $c (qw( itemcallnumber cn_source ) ){
90       $item->make_column_dirty($c);
91   }
92
93   eval { $item->store };
94   my $modok = $@ ? 0 : 1;
95
96   if ($modok) {
97      $goodcount++;
98      print $fh "Touched item $itemnumber\n" if (defined $verbose);
99   } else {
100      $badcount++;
101      print $fh "ERROR WITH ITEM $itemnumber !!!!\n";
102   }
103
104   $totalcount++;
105
106 }
107 close($fh);
108
109 # Benchmarking
110 my $endtime = time();
111 my $time = $endtime-$startime;
112 my $accuracy = ($goodcount / $totalcount) * 100; # this is a percentage
113 my $averagetime = 0;
114 $averagetime = $time / $totalcount if $totalcount;
115 print "Good: $goodcount, Bad: $badcount (of $totalcount) in $time seconds\n";
116 printf "Accuracy: %.2f%%\nAverage time per record: %.6f seconds\n", $accuracy, $averagetime if (defined $verbose);
117
118 =head1 NAME
119
120 touch_all_items.pl
121
122 =head1 SYNOPSIS
123
124   touch_all_items.pl
125   touch_all_items.pl -v
126   touch_all_items.pl --where=STRING
127
128 =head1 DESCRIPTION
129
130 When changes are made to ModItem (or the routines that are called by it), it is
131 sometimes necessary to run ModItem on all or some records in the catalog when
132 upgrading. This script does that.
133
134 =over 8
135
136 =item B<--help>
137
138 Prints this help
139
140 =item B<-v>
141
142 Provide verbose log information.
143
144 =item B<--where>
145
146 Limits the search with a user-specified WHERE clause.
147
148 =back
149
150 =cut
151