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