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