From c8717ecd27f0a1f3687cadcb776b34f84d852c80 Mon Sep 17 00:00:00 2001 From: Rolando Isidoro Date: Wed, 23 Jan 2013 14:58:01 +0000 Subject: [PATCH] Bug 9453: Update records acording to the new UNIMARCField100Language system preference Script to update field 100a language value with the one defined in the "UNIMARC field 100 default language" system preference This script aims to allow users to update UNIMARC field 100a language value with the one defined in the "UNIMARC field 100 default language" system preference in a easy way. The script uses KOHA's API methods to ensure that the DB data remains valid and that it gets reindexed. --- misc/maintenance/update_unimarc_100a_language.pl | 179 ++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 misc/maintenance/update_unimarc_100a_language.pl diff --git a/misc/maintenance/update_unimarc_100a_language.pl b/misc/maintenance/update_unimarc_100a_language.pl new file mode 100644 index 0000000..af93ebf --- /dev/null +++ b/misc/maintenance/update_unimarc_100a_language.pl @@ -0,0 +1,179 @@ +#!/usr/bin/perl +# +# Copyright (C) 2012 FCT/UNL +# +# 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., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +BEGIN { + # find Koha's Perl modules + # test carefully before changing this + use FindBin; + eval { require "$FindBin::Bin/../kohalib.pl" }; +} + +# possible modules to use +use Getopt::Long; +use C4::Context; +use C4::Biblio; +use Pod::Usage; +# use Data::Dumper; # for debugging purposes only + +sub usage { + pod2usage( -verbose => 2 ); + exit; +} + +# Database handle +my $dbh = C4::Context->dbh; + +# Benchmarking variables +my $startime = time(); +my $totalcount = 0; + +# Options +my $all; +my $biblionumber = ''; +my $whereclause = ''; +my $help; +my $filename; +my $output; +my $default_unimarc_language = C4::Context->preference("UNIMARCField100Language"); + +GetOptions( + 'a|all' => \$all, + 'b|biblionumber:s' => \$biblionumber, + 'h|help' => \$help, + 'o|output:s' => \$filename, + ); + +print $biblionumber; + + +# Show usage instructions if: +# 1 - requested using the -h or -help option +# 2 - no arguments where passed on +# 3 - -b or -biblionumber and -a options where used simultaneously +# 4 - neiter -b, -biblionumber or -a options where used +usage() if (($help) || (0 == $#ARGV) || ($biblionumber && $all) || (!($biblionumber || $all))); + +# Add filter search by biblionumber(s) if any was given +if ($biblionumber) { + $whereclause = "WHERE `biblionumber` IN ( $biblionumber )"; +} + +# Output log or STDOUT +if (defined $filename) { + open STDOUT, '>', $filename || die ("Can't open output file '$filename'"); +} + +# Fetch info from the search +my $sth1 = $dbh->prepare("SELECT biblionumber, frameworkcode FROM biblio $whereclause"); +$sth1->execute(); + +# Process each MARC biblio record +while (my ($biblionumber, $frameworkcode) = $sth1->fetchrow_array){ + # Get MARC biblio record + my $record = GetMarcBiblio($biblionumber); + + # Retrieve subfield 100$a + my $subfield = $record->subfield('100', 'a'); + + # Update the hardcoded 'fre' language code for the one set as UNIMARC default + $subfield =~ s/fre/$default_unimarc_language/g; + + # Print output for the currect record + print STDOUT "Biblionumber: $biblionumber, New 100\$a value: '$subfield'\n"; + + # Update field 100 with the new subfield a value + $record->field('100')->update('a' => $subfield); + + # Save record to the database + ModBiblioMarc($record, $biblionumber, $frameworkcode); + + $totalcount++; +} + +# Benchmarking +my $endtime = time(); +my $time = $endtime-$startime; +my $averagetime = 0; +unless ($time == 0) { $averagetime = $totalcount / $time; }; +print STDOUT "Updated $totalcount records in $time seconds\n"; + +if (defined $filename) { + close(STDOUT); +} + +=head1 NAME + +update_unimarc_100a_language.pl + +=head1 SYNOPSIS + +Update all biblio records' field 100a language value with the one +defined in the "UNIMARC field 100 default language" system preference. + +=head1 DESCRIPTION + +Due to bug 8347, Koha forced UNIMARC 100 field code language to 'fre' +This script aims to replace the value on all the records, or in a +given set of records, for a new value defined by the new system +preference "UNIMARC field 100 default language". + +=head1 REQUIRED ARGUMENTS + +The script must be ran with B of these two options: -a or -b (-biblionumber) + +=head1 USAGE + +=over 8 + +=item B<-help> + +Prints this help + + +=item B<-a> + +Update all records + + update_unimarc_100a_language.pl -a + + +=item B<-b> + +Limit the action to a fixed set of comma separated biblionumber values + + Update record with biblionumber 1: + update_unimarc_100a_language.pl -b=1 (or -biblionumber=1) + + Update records with biblionumbers 1, 2 and 3: + update_unimarc_100a_language.pl -b=1,2,3 (or -biblionumber=1,2,3) + + +=item B<-o> + +Sets the output of the script to a given file. Helpful for verification purposes on big sets of records + + Update records with script output being sent to file + update_unimarc_100a_language.pl -output=file.txt (or -o=file.txt) + +=back + +=cut + -- 1.7.10.4