Reverting to previous version, kohastructure.sql file didn't work for me, I got an...
[koha.git] / misc / bulkupdate.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 # $Id$
19
20
21 =head1 bulkupdate.pl 
22
23     This script allows you to update your database/records after a bulkmarckimport integration.
24
25 =cut
26
27
28 use C4::Context;
29 use MARC::File::XML;
30 use MARC::Record;
31 use Getopt::Long;
32
33 my ( $no_marcxml, $no_isbn, $help) = (0,0,0);
34
35 GetOptions(
36     'noisbn'    => \$no_isbn,
37     'noxml'     => \$no_marcxml,
38     'h'       => \$help,
39     'help'    => \$help,
40 );
41
42
43 $| = 1;
44 my $dbh   = C4::Context->dbh;
45
46 if($help){
47     print qq(
48         Option :
49             \t-h        show this help
50             \t-noisbn   don't remove '-' in biblioitems.isbn
51             \t-noxml    don't remove '-' in biblioitems.marcxml in field 010a
52             \n\n 
53     );
54     exit;
55 }
56
57 my $cpt_isbn = 0;
58 if(not $no_isbn){
59
60     my $query_isbn = "
61         SELECT biblioitemnumber,isbn FROM biblioitems WHERE isbn IS NOT NULL
62     ";
63
64     my $update_isbn = "
65         UPDATE biblioitems SET isbn=? WHERE biblioitemnumber = ?
66     ";
67
68     my $sth = $dbh->prepare($query_isbn);
69     $sth->execute;
70
71     while (my $data = $sth->fetchrow_arrayref){
72         my $biblioitemnumber = $data->[0];
73         print "\rremoving '-' on isbn for biblioitemnumber $biblioitemnumber";
74         
75         # suppression des tirets de l'isbn
76         my $isbn    = $data->[1];
77         if($isbn){
78             $isbn =~ s/-//g;
79             
80             #update 
81             my $sth = $dbh->prepare($update_isbn);
82             $sth->execute($isbn,$biblioitemnumber);
83         }
84         $cpt_isbn++;
85     }
86     print "$cpt_isbn updated";
87 }
88
89 if(not $no_marcxml){
90     
91     my $query_marcxml = "
92         SELECT biblioitemnumber,marcxml FROM biblioitems WHERE isbn IS NOT NULL
93     ";
94     
95     
96     my $update_marcxml = "
97         UPDATE biblioitems SET marcxml=? WHERE biblioitemnumber = ? 
98     ";
99
100     my $sth = $dbh->prepare($query_marcxml);
101     $sth->execute;
102     
103     while (my $data = $sth->fetchrow_arrayref){
104         
105         my $biblioitemnumber = $data->[0];
106        print "\rremoving '-' on marcxml for biblioitemnumber $biblioitemnumber";
107         
108         # suppression des tirets de l'isbn dans la notice
109         my $marcxml = $data->[1];
110         
111         eval{
112             my $record = MARC::Record->new_from_xml($marcxml);
113             my @field = $record->field('010');
114             foreach my $field (@field){
115                 my $subfield = $field->subfield('a');
116                 if($subfield){
117                     my $isbn = $subfield;
118                     $isbn =~ s/-//g;
119                     $field->update('a' => $isbn);
120                 }
121             }
122             $marcxml = $record->as_xml;
123             # Update
124             my $sth = $dbh->prepare($update_marcxml);
125             $sth->execute($marcxml,$biblioitemnumber);
126         };
127         if($@){
128             print "\n /!\\ pb getting $biblioitemnumber : $@";
129         }
130     }
131 }