ordering by biblioitemnumber for best performance
[koha.git] / misc / batchupdateISBNs.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18
19
20 =head1 batchupdateISBNs.pl 
21
22     This script batch updates ISBN fields
23
24 =cut
25
26 use strict;
27 BEGIN {
28     # find Koha's Perl modules
29     # test carefully before changing this
30     use FindBin;
31     eval { require "$FindBin::Bin/kohalib.pl" };
32 }
33 use C4::Context;
34 use MARC::File::XML;
35 use MARC::Record;
36 use Getopt::Long;
37
38 my ( $no_marcxml, $no_isbn, $help) = (0,0,0);
39
40 GetOptions(
41     'noisbn'    => \$no_isbn,
42     'noxml'     => \$no_marcxml,
43     'h'         => \$help,
44     'help'      => \$help,
45 );
46
47
48 $| = 1;
49 my $dbh   = C4::Context->dbh;
50
51 if($help){
52     print qq(
53         Option :
54             \t-h        show this help
55             \t-noisbn   don't remove '-' in biblioitems.isbn
56             \t-noxml    don't remove '-' in biblioitems.marcxml in field 010a
57             \n\n 
58     );
59     exit;
60 }
61
62 my $cpt_isbn = 0;
63 if(not $no_isbn){
64
65     my $query_isbn = "
66         SELECT biblioitemnumber,isbn FROM biblioitems WHERE isbn IS NOT NULL ORDER BY biblioitemnumber
67     ";
68
69     my $update_isbn = "
70         UPDATE biblioitems SET isbn=? WHERE biblioitemnumber = ?
71     ";
72
73     my $sth = $dbh->prepare($query_isbn);
74     $sth->execute;
75
76     while (my $data = $sth->fetchrow_arrayref){
77         my $biblioitemnumber = $data->[0];
78         print "\rremoving '-' on isbn for biblioitemnumber $biblioitemnumber";
79         
80         # suppression des tirets de l'isbn
81         my $isbn    = $data->[1];
82         if($isbn){
83             $isbn =~ s/-//g;
84             
85             #update 
86             my $sth = $dbh->prepare($update_isbn);
87             $sth->execute($isbn,$biblioitemnumber);
88         }
89         $cpt_isbn++;
90     }
91     print "$cpt_isbn updated";
92 }
93
94 if(not $no_marcxml){
95     
96     my $query_marcxml = "
97         SELECT biblioitemnumber,marcxml FROM biblioitems WHERE isbn IS NOT NULL ORDER BY biblioitemnumber
98     ";
99     
100     
101     my $update_marcxml = "
102         UPDATE biblioitems SET marcxml=? WHERE biblioitemnumber = ? 
103     ";
104
105     my $sth = $dbh->prepare($query_marcxml);
106     $sth->execute;
107     
108     while (my $data = $sth->fetchrow_arrayref){
109         
110        my $biblioitemnumber = $data->[0];
111        print "\rremoving '-' on marcxml for biblioitemnumber $biblioitemnumber";
112         
113         # suppression des tirets de l'isbn dans la notice
114         my $marcxml = $data->[1];
115         
116         eval{
117             my $record = MARC::Record->new_from_xml($marcxml,'UTF-8','UNIMARC');
118             my @field = $record->field('010');
119             my $flag = 0;
120             foreach my $field (@field){
121                 my $subfield = $field->subfield('a');
122                 if($subfield){
123                     my $isbn = $subfield;
124                     $isbn =~ s/-//g;
125                     $field->update('a' => $isbn);
126                     $flag = 1;
127                 }
128             }
129             if($flag){
130                 $marcxml = $record->as_xml;
131                 # Update
132                 my $sth = $dbh->prepare($update_marcxml);
133                 $sth->execute($marcxml,$biblioitemnumber);
134             }
135         };
136         if($@){
137             print "\n /!\\ pb getting $biblioitemnumber : $@";
138         }
139     }
140 }