Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 141 lines (113 sloc) 3.83 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#!/usr/bin/env perl

# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA



=head1 batchupdateISBNs.pl

This script batch updates ISBN fields

=cut

use strict;
BEGIN {
    # find Koha's Perl modules
    # test carefully before changing this
    use FindBin;
    eval { require "$FindBin::Bin/kohalib.pl" };
}
use Koha;
use C4::Context;
use MARC::File::XML;
use MARC::Record;
use Getopt::Long;

my ( $no_marcxml, $no_isbn, $help) = (0,0,0);

GetOptions(
    'noisbn' => \$no_isbn,
    'noxml' => \$no_marcxml,
    'h' => \$help,
    'help' => \$help,
);


$| = 1;
my $dbh = C4::Context->dbh;

if($help){
    print qq(
Option :
\t-h show this help
\t-noisbn don't remove '-' in biblioitems.isbn
\t-noxml don't remove '-' in biblioitems.marcxml in field 010a
\n\n
);
    exit;
}

my $cpt_isbn = 0;
if(not $no_isbn){

    my $query_isbn = "
SELECT biblioitemnumber,isbn FROM biblioitems WHERE isbn IS NOT NULL ORDER BY biblioitemnumber
";

    my $update_isbn = "
UPDATE biblioitems SET isbn=? WHERE biblioitemnumber = ?
";

    my $sth = $dbh->prepare($query_isbn);
    $sth->execute;

    while (my $data = $sth->fetchrow_arrayref){
        my $biblioitemnumber = $data->[0];
        print "\rremoving '-' on isbn for biblioitemnumber $biblioitemnumber";
        
        # suppression des tirets de l'isbn
        my $isbn = $data->[1];
        if($isbn){
            $isbn =~ s/-//g;
            
            #update
            my $sth = $dbh->prepare($update_isbn);
            $sth->execute($isbn,$biblioitemnumber);
        }
        $cpt_isbn++;
    }
    print "$cpt_isbn updated";
}

if(not $no_marcxml){
    
    my $query_marcxml = "
SELECT biblioitemnumber,marcxml FROM biblioitems WHERE isbn IS NOT NULL ORDER BY biblioitemnumber
";
    
    
    my $update_marcxml = "
UPDATE biblioitems SET marcxml=? WHERE biblioitemnumber = ?
";

    my $sth = $dbh->prepare($query_marcxml);
    $sth->execute;
    
    while (my $data = $sth->fetchrow_arrayref){
        
       my $biblioitemnumber = $data->[0];
       print "\rremoving '-' on marcxml for biblioitemnumber $biblioitemnumber";
        
        # suppression des tirets de l'isbn dans la notice
        my $marcxml = $data->[1];
        
        eval{
            my $record = MARC::Record->new_from_xml($marcxml,'UTF-8','UNIMARC');
            my @field = $record->field('010');
            my $flag = 0;
foreach my $field (@field){
                my $subfield = $field->subfield('a');
                if($subfield){
                    my $isbn = $subfield;
                    $isbn =~ s/-//g;
                    $field->update('a' => $isbn);
                    $flag = 1;
                }
}
            if($flag){
                $marcxml = $record->as_xml_record('UNIMARC');
                # Update
                my $sth = $dbh->prepare($update_marcxml);
                $sth->execute($marcxml,$biblioitemnumber);
            }
        };
        if($@){
            print "\n /!\\ pb getting $biblioitemnumber : $@";
        }
    }
}
Something went wrong with that request. Please try again.