Bug 29794: Add missing include in delete_items.pl
[koha.git] / misc / maintenance / touch_all_biblios.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
23 # possible modules to use
24 use Getopt::Long qw( GetOptions );
25
26 use Koha::Script;
27 use C4::Context;
28 use C4::Biblio qw( GetMarcBiblio ModBiblio );
29 use Pod::Usage qw( pod2usage );
30
31
32 sub usage {
33     pod2usage( -verbose => 2 );
34     exit;
35 }
36
37 # Database handle
38 my $dbh = C4::Context->dbh;
39
40 # Benchmarking variables
41 my $startime = time();
42 my $goodcount = 0;
43 my $badcount = 0;
44 my $totalcount = 0;
45
46 # Options
47 my $verbose;
48 my $whereclause = '';
49 my $help;
50 my $outfile;
51
52 GetOptions(
53   'o|output:s' => \$outfile,
54   'v' => \$verbose,
55   'where:s' => \$whereclause,
56   'help|h'   => \$help,
57 );
58
59 usage() if $help;
60
61 if ($whereclause) {
62    $whereclause = "WHERE $whereclause";
63 }
64
65 # output log or STDOUT
66 my $fh;
67 if (defined $outfile) {
68    open ($fh, '>', $outfile) || die ("Cannot open output file");
69 } else {
70    open($fh, '>&', \*STDOUT) || die ("Couldn't duplicate STDOUT: $!");
71 }
72
73 my $sth1 = $dbh->prepare("SELECT biblionumber, frameworkcode FROM biblio $whereclause");
74 $sth1->execute();
75
76 # fetch info from the search
77 while (my ($biblionumber, $frameworkcode) = $sth1->fetchrow_array){
78   my $record = GetMarcBiblio({ biblionumber => $biblionumber });
79  
80   my $modok = ModBiblio($record, $biblionumber, $frameworkcode);
81
82   if ($modok) {
83      $goodcount++;
84      print $fh "Touched biblio $biblionumber\n" if (defined $verbose);
85   } else {
86      $badcount++;
87      print $fh "ERROR WITH BIBLIO $biblionumber !!!!\n";
88   }
89
90   $totalcount++;
91
92 }
93 close($fh);
94
95 # Benchmarking
96 my $endtime = time();
97 my $time = $endtime-$startime;
98 my $accuracy = $totalcount ? ($goodcount / $totalcount) * 100 : 0; # this is a percentage
99 my $averagetime = 0;
100 $averagetime = $time / $totalcount if $totalcount;
101 print "Good: $goodcount, Bad: $badcount (of $totalcount) in $time seconds\n";
102 printf "Accuracy: %.2f%%\nAverage time per record: %.6f seconds\n", $accuracy, $averagetime if (defined $verbose);
103
104 =head1 NAME
105
106 touch_all_biblios.pl
107
108 =head1 SYNOPSIS
109
110   touch_all_biblios.pl
111   touch_all_biblios.pl -v
112   touch_all_biblios.pl --where=STRING
113
114 =head1 DESCRIPTION
115
116 When changes are made to ModBiblio (or the routines that are called by those),
117 it is sometimes necessary to run ModBiblio on all or some records in the catalog
118 when upgrading. This script does this.
119
120 =over 8
121
122 =item B<--help>
123
124 Prints this help
125
126 =item B<-v>
127
128 Provide verbose log information.
129
130 =item B<--where>
131
132 Limits the search with a user-specified WHERE clause.
133
134 =back
135
136 =cut
137