View | Details | Raw Unified | Return to bug 9453
Collapse All | Expand All

(-)a/misc/maintenance/update_unimarc_100a_language.pl (-1 / +156 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
#
3
# Copyright (C) 2012 FCT/UNL
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 2 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use strict;
21
use warnings;
22
BEGIN {
23
    # find Koha's Perl modules
24
    # test carefully before changing this
25
    use FindBin;
26
    eval { require "$FindBin::Bin/../kohalib.pl" };
27
}
28
29
# possible modules to use
30
use Getopt::Long;
31
use C4::Context;
32
use C4::Biblio;
33
use Pod::Usage;
34
35
36
sub usage {
37
    pod2usage( -verbose => 2 );
38
    exit;
39
}
40
41
# Database handle
42
my $dbh = C4::Context->dbh;
43
44
# Benchmarking variables
45
my $startime = time();
46
my $totalcount = 0;
47
48
# Options
49
my $biblionumber = '';
50
my $whereclause  = '';
51
my $help;
52
my $outfile;
53
my $default_unimarc_language = C4::Context->preference("UNIMARCField100Language");
54
GetOptions(
55
        'o|output:s'       => \$outfile,
56
        'b|biblionumber:s' => \$biblionumber,
57
        'h|help'           => \$help,
58
        );
59
60
usage() if $help;
61
62
# Add filter search by biblionumber(s) if any was given
63
if ($biblionumber) {
64
    $whereclause = "WHERE `biblionumber` IN ( $biblionumber )";
65
}
66
67
# Output log or STDOUT
68
if (defined $outfile) {
69
    open(OUT, ">$outfile") || die ("Cannot open output file");
70
} else {
71
    open(OUT, ">&STDOUT")  || die ("Couldn't duplicate STDOUT: $!");
72
}
73
74
# Fetch info from the search
75
my $sth1 = $dbh->prepare("SELECT biblionumber, frameworkcode FROM biblio $whereclause");
76
$sth1->execute();
77
78
# Process each MARC biblio record
79
while (my ($biblionumber, $frameworkcode) = $sth1->fetchrow_array){
80
    # Get MARC biblio record
81
    my $record = GetMarcBiblio($biblionumber);
82
83
    # Retrieve subfield 100$a
84
    my $subfield = $record->subfield('100', 'a');
85
86
    # Update the hardcoded 'fre' language code for the one set as UNIMARC default
87
    $subfield =~ s/fre/$default_unimarc_language/g;
88
89
    # Print output for the currect record
90
    print OUT "Biblionumber: $biblionumber, New 100\$a value: '$subfield'\n";
91
92
    # Update field 100 with the new subfield a value
93
    $record->field('100')->update('a' => $subfield);
94
95
    # Save record to the database
96
    ModBiblioMarc($record, $biblionumber, $frameworkcode);
97
98
    $totalcount++;
99
}
100
101
# Benchmarking
102
my $endtime = time();
103
my $time = $endtime-$startime;
104
my $averagetime = 0;
105
unless ($time == 0) { $averagetime = $totalcount / $time; };
106
print OUT "\nUpdated $totalcount records in $time seconds\n";
107
108
if (defined $outfile) {
109
    close(OUT);
110
}
111
112
=head1 NAME
113
114
update_unimarc_100a_language.pl
115
116
=head1 SYNOPSIS
117
118
Update all biblio records' field 100a language value with the one
119
defined in the "UNIMARC field 100 default language" system preference.
120
121
=head1 DESCRIPTION
122
123
Due to bug 8347, Koha forced UNIMARC 100 field code language to 'fre'
124
This script aims to replace the value on all the records, or in a
125
given set of records, for a new value defined by the new system
126
preference "UNIMARC field 100 default language".
127
128
=over 8
129
130
=item B<-help>
131
132
Prints this help
133
134
135
=item B<-b>
136
137
Limits the action to a fixed set of comma separated biblionumber values
138
139
    Update record with biblionumber 1:
140
    update_unimarc_100a_language.pl -biblionumber=1 (or -b=1)
141
142
143
    Update records with biblionumbers 1, 2 and 3:
144
    update_unimarc_100a_language.pl -biblionumber=1,2,3 (or -b=1,2,3)
145
146
147
=item B<-o>
148
149
Sets the output of the script to a given file. Helpful for verification purposes on big sets of records
150
151
    Update records with script output being sent to file
152
    update_unimarc_100a_language.pl -output=file.txt (or -o=file.txt)
153
154
=back
155
156
=cut

Return to bug 9453