Updating perlcriticrc to allow Modern::Perl to suffice instead of use warnings; and...
[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 #use warnings; FIXME - Bug 2505
28 BEGIN {
29     # find Koha's Perl modules
30     # test carefully before changing this
31     use FindBin;
32     eval { require "$FindBin::Bin/kohalib.pl" };
33 }
34 use C4::Context;
35 use MARC::File::XML;
36 use MARC::Record;
37 use Getopt::Long;
38
39 my ( $no_marcxml, $no_isbn, $help) = (0,0,0);
40
41 GetOptions(
42     'noisbn'    => \$no_isbn,
43     'noxml'     => \$no_marcxml,
44     'h'         => \$help,
45     'help'      => \$help,
46 );
47
48
49 $| = 1;
50 my $dbh   = C4::Context->dbh;
51
52 if($help){
53     print qq(
54         Option :
55             \t-h        show this help
56             \t-noisbn   don't remove '-' in biblioitems.isbn
57             \t-noxml    don't remove '-' in biblioitems.marcxml in field 010a
58             \n\n 
59     );
60     exit;
61 }
62
63 my $cpt_isbn = 0;
64 if(not $no_isbn){
65
66     my $query_isbn = "
67         SELECT biblioitemnumber,isbn FROM biblioitems WHERE isbn IS NOT NULL ORDER BY biblioitemnumber
68     ";
69
70     my $update_isbn = "
71         UPDATE biblioitems SET isbn=? WHERE biblioitemnumber = ?
72     ";
73
74     my $sth = $dbh->prepare($query_isbn);
75     $sth->execute;
76
77     while (my $data = $sth->fetchrow_arrayref){
78         my $biblioitemnumber = $data->[0];
79         print "\rremoving '-' on isbn for biblioitemnumber $biblioitemnumber";
80         
81         # suppression des tirets de l'isbn
82         my $isbn    = $data->[1];
83         if($isbn){
84             $isbn =~ s/-//g;
85             
86             #update 
87             my $sth = $dbh->prepare($update_isbn);
88             $sth->execute($isbn,$biblioitemnumber);
89         }
90         $cpt_isbn++;
91     }
92     print "$cpt_isbn updated";
93 }
94
95 if(not $no_marcxml){
96     
97     my $query_marcxml = "
98         SELECT biblioitemnumber,marcxml FROM biblioitems WHERE isbn IS NOT NULL ORDER BY biblioitemnumber
99     ";
100     
101     
102     my $update_marcxml = "
103         UPDATE biblioitems SET marcxml=? WHERE biblioitemnumber = ? 
104     ";
105
106     my $sth = $dbh->prepare($query_marcxml);
107     $sth->execute;
108     
109     while (my $data = $sth->fetchrow_arrayref){
110         
111        my $biblioitemnumber = $data->[0];
112        print "\rremoving '-' on marcxml for biblioitemnumber $biblioitemnumber";
113         
114         # suppression des tirets de l'isbn dans la notice
115         my $marcxml = $data->[1];
116         
117         eval{
118             my $record = MARC::Record->new_from_xml($marcxml,'UTF-8','UNIMARC');
119             my @field = $record->field('010');
120             my $flag = 0;
121             foreach my $field (@field){
122                 my $subfield = $field->subfield('a');
123                 if($subfield){
124                     my $isbn = $subfield;
125                     $isbn =~ s/-//g;
126                     $field->update('a' => $isbn);
127                     $flag = 1;
128                 }
129             }
130             if($flag){
131                 $marcxml = $record->as_xml_record('UNIMARC');
132                 # Update
133                 my $sth = $dbh->prepare($update_marcxml);
134                 $sth->execute($marcxml,$biblioitemnumber);
135             }
136         };
137         if($@){
138             print "\n /!\\ pb getting $biblioitemnumber : $@";
139         }
140     }
141 }