All pastes #580784 Raw Edit

bulkupdate.pl

public text v1 · immutable
·published 2007-06-21 07:34 UTC
rendered paste body
#!/usr/bin/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

# $Id: bulkupdate.pl,v 1.2 2007/06/19 15:58:23 toins Exp $


=head1 bulkupdate.pl 

    This script allows you to update your database/records after a bulkmarckimport integration.

=cut


use C4::Context;
use MARC::File::XML;
use MARC::Record;
use Getopt::Long;

my ( $process_marcxml, $process_isbn, $help) = (0,0,0);

GetOptions(
    'noisbn'    => \$process_isbn,
    'noxml'     => \$process_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 $process_isbn){

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

    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 $process_marcxml){
    
    my $query_marcxml = "
        SELECT biblioitemnumber,marcxml FROM biblioitems WHERE isbn IS NOT NULL
    ";
    
    
    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);
            my @field = $record->field('010');
	    foreach my $field (@field){
		    my $subfield = $field->subfield('a');
		    if($subfield){
			my $isbn = $subfield;
			$isbn =~ s/-//g;
			$field->update('a' => $isbn);
		    }
	    }
	    $marcxml = $record->as_xml;
	    # Update
	    my $sth = $dbh->prepare($update_marcxml);
	    $sth->execute($marcxml,$biblioitemnumber);
        };
        if($@){
            print "\n /!\\ pb getting $biblioitemnumber : $@";
        }
    }
}