bug 4151: replace dep on YAML::XS with YAML::Syck
[koha.git] / misc / batchupdateISBNs.pl
blob03d5bba82feb4c7536034199a80d4fbca0929c31
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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
20 =head1 batchupdateISBNs.pl
22 This script batch updates ISBN fields
24 =cut
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" };
33 use C4::Context;
34 use MARC::File::XML;
35 use MARC::Record;
36 use Getopt::Long;
38 my ( $no_marcxml, $no_isbn, $help) = (0,0,0);
40 GetOptions(
41 'noisbn' => \$no_isbn,
42 'noxml' => \$no_marcxml,
43 'h' => \$help,
44 'help' => \$help,
48 $| = 1;
49 my $dbh = C4::Context->dbh;
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
59 exit;
62 my $cpt_isbn = 0;
63 if(not $no_isbn){
65 my $query_isbn = "
66 SELECT biblioitemnumber,isbn FROM biblioitems WHERE isbn IS NOT NULL ORDER BY biblioitemnumber
69 my $update_isbn = "
70 UPDATE biblioitems SET isbn=? WHERE biblioitemnumber = ?
73 my $sth = $dbh->prepare($query_isbn);
74 $sth->execute;
76 while (my $data = $sth->fetchrow_arrayref){
77 my $biblioitemnumber = $data->[0];
78 print "\rremoving '-' on isbn for biblioitemnumber $biblioitemnumber";
80 # suppression des tirets de l'isbn
81 my $isbn = $data->[1];
82 if($isbn){
83 $isbn =~ s/-//g;
85 #update
86 my $sth = $dbh->prepare($update_isbn);
87 $sth->execute($isbn,$biblioitemnumber);
89 $cpt_isbn++;
91 print "$cpt_isbn updated";
94 if(not $no_marcxml){
96 my $query_marcxml = "
97 SELECT biblioitemnumber,marcxml FROM biblioitems WHERE isbn IS NOT NULL ORDER BY biblioitemnumber
101 my $update_marcxml = "
102 UPDATE biblioitems SET marcxml=? WHERE biblioitemnumber = ?
105 my $sth = $dbh->prepare($query_marcxml);
106 $sth->execute;
108 while (my $data = $sth->fetchrow_arrayref){
110 my $biblioitemnumber = $data->[0];
111 print "\rremoving '-' on marcxml for biblioitemnumber $biblioitemnumber";
113 # suppression des tirets de l'isbn dans la notice
114 my $marcxml = $data->[1];
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;
129 if($flag){
130 $marcxml = $record->as_xml_record('UNIMARC');
131 # Update
132 my $sth = $dbh->prepare($update_marcxml);
133 $sth->execute($marcxml,$biblioitemnumber);
136 if($@){
137 print "\n /!\\ pb getting $biblioitemnumber : $@";