Translation for 16.05.02
[koha.git] / misc / batchupdateISBNs.pl
blobf3108590bf71ef6ac2f8bc6ed24e3fc057c8ac00
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 =head1 batchupdateISBNs.pl
22 This script batch updates ISBN fields
24 =cut
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" };
34 use C4::Context;
35 use MARC::File::XML;
36 use MARC::Record;
37 use Getopt::Long;
39 my ( $no_marcxml, $no_isbn, $help) = (0,0,0);
41 GetOptions(
42 'noisbn' => \$no_isbn,
43 'noxml' => \$no_marcxml,
44 'h' => \$help,
45 'help' => \$help,
49 $| = 1;
50 my $dbh = C4::Context->dbh;
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
60 exit;
63 my $cpt_isbn = 0;
64 if(not $no_isbn){
66 my $query_isbn = "
67 SELECT biblioitemnumber,isbn FROM biblioitems WHERE isbn IS NOT NULL ORDER BY biblioitemnumber
70 my $update_isbn = "
71 UPDATE biblioitems SET isbn=? WHERE biblioitemnumber = ?
74 my $sth = $dbh->prepare($query_isbn);
75 $sth->execute;
77 while (my $data = $sth->fetchrow_arrayref){
78 my $biblioitemnumber = $data->[0];
79 print "\rremoving '-' on isbn for biblioitemnumber $biblioitemnumber";
81 # suppression des tirets de l'isbn
82 my $isbn = $data->[1];
83 if($isbn){
84 $isbn =~ s/-//g;
86 #update
87 my $sth = $dbh->prepare($update_isbn);
88 $sth->execute($isbn,$biblioitemnumber);
90 $cpt_isbn++;
92 print "$cpt_isbn updated";
95 if(not $no_marcxml){
97 my $query_marcxml = "
98 SELECT biblioitemnumber,marcxml FROM biblioitems WHERE isbn IS NOT NULL ORDER BY biblioitemnumber
102 my $update_marcxml = "
103 UPDATE biblioitems SET marcxml=? WHERE biblioitemnumber = ?
106 my $sth = $dbh->prepare($query_marcxml);
107 $sth->execute;
109 while (my $data = $sth->fetchrow_arrayref){
111 my $biblioitemnumber = $data->[0];
112 print "\rremoving '-' on marcxml for biblioitemnumber $biblioitemnumber";
114 # suppression des tirets de l'isbn dans la notice
115 my $marcxml = $data->[1];
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;
130 if($flag){
131 $marcxml = $record->as_xml_record('UNIMARC');
132 # Update
133 my $sth = $dbh->prepare($update_marcxml);
134 $sth->execute($marcxml,$biblioitemnumber);
137 if($@){
138 print "\n /!\\ pb getting $biblioitemnumber : $@";