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 Modern::Perl; |
21 |
|
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 |
# use Data::Dumper; # for debugging purposes only |
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 $all; |
50 |
my $biblionumber = ''; |
51 |
my $whereclause = ''; |
52 |
my $help; |
53 |
my $filename; |
54 |
my $output; |
55 |
my $default_unimarc_language = C4::Context->preference("UNIMARCField100Language"); |
56 |
|
57 |
GetOptions( |
58 |
'a|all' => \$all, |
59 |
'b|biblionumber:s' => \$biblionumber, |
60 |
'h|help' => \$help, |
61 |
'o|output:s' => \$filename, |
62 |
); |
63 |
|
64 |
print $biblionumber; |
65 |
|
66 |
|
67 |
# Show usage instructions if: |
68 |
# 1 - requested using the -h or -help option |
69 |
# 2 - no arguments where passed on |
70 |
# 3 - -b or -biblionumber and -a options where used simultaneously |
71 |
# 4 - neiter -b, -biblionumber or -a options where used |
72 |
usage() if (($help) || (0 == $#ARGV) || ($biblionumber && $all) || (!($biblionumber || $all))); |
73 |
|
74 |
# Add filter search by biblionumber(s) if any was given |
75 |
if ($biblionumber) { |
76 |
$whereclause = "WHERE `biblionumber` IN ( $biblionumber )"; |
77 |
} |
78 |
|
79 |
# Output log or STDOUT |
80 |
if (defined $filename) { |
81 |
open STDOUT, '>', $filename || die ("Can't open output file '$filename'"); |
82 |
} |
83 |
|
84 |
# Fetch info from the search |
85 |
my $sth1 = $dbh->prepare("SELECT biblionumber, frameworkcode FROM biblio $whereclause"); |
86 |
$sth1->execute(); |
87 |
|
88 |
# Process each MARC biblio record |
89 |
while (my ($biblionumber, $frameworkcode) = $sth1->fetchrow_array){ |
90 |
# Get MARC biblio record |
91 |
my $record = GetMarcBiblio($biblionumber); |
92 |
|
93 |
# Retrieve subfield 100$a |
94 |
my $subfield = $record->subfield('100', 'a'); |
95 |
|
96 |
# Update the hardcoded 'fre' language code for the one set as UNIMARC default |
97 |
$subfield =~ s/fre/$default_unimarc_language/g; |
98 |
|
99 |
# Print output for the currect record |
100 |
print STDOUT "Biblionumber: $biblionumber, New 100\$a value: '$subfield'\n"; |
101 |
|
102 |
# Update field 100 with the new subfield a value |
103 |
$record->field('100')->update('a' => $subfield); |
104 |
|
105 |
# Save record to the database |
106 |
ModBiblioMarc($record, $biblionumber, $frameworkcode); |
107 |
|
108 |
$totalcount++; |
109 |
} |
110 |
|
111 |
# Benchmarking |
112 |
my $endtime = time(); |
113 |
my $time = $endtime-$startime; |
114 |
my $averagetime = 0; |
115 |
unless ($time == 0) { $averagetime = $totalcount / $time; }; |
116 |
print STDOUT "Updated $totalcount records in $time seconds\n"; |
117 |
|
118 |
if (defined $filename) { |
119 |
close(STDOUT); |
120 |
} |
121 |
|
122 |
=head1 NAME |
123 |
|
124 |
update_unimarc_100a_language.pl |
125 |
|
126 |
=head1 SYNOPSIS |
127 |
|
128 |
Update all biblio records' field 100a language value with the one |
129 |
defined in the "UNIMARC field 100 default language" system preference. |
130 |
|
131 |
=head1 DESCRIPTION |
132 |
|
133 |
Due to bug 8347, Koha forced UNIMARC 100 field code language to 'fre' |
134 |
This script aims to replace the value on all the records, or in a |
135 |
given set of records, for a new value defined by the new system |
136 |
preference "UNIMARC field 100 default language". |
137 |
|
138 |
=head1 REQUIRED ARGUMENTS |
139 |
|
140 |
The script must be ran with B<one and only one> of these two options: -a or -b (-biblionumber) |
141 |
|
142 |
=head1 USAGE |
143 |
|
144 |
=over 8 |
145 |
|
146 |
=item B<-help> |
147 |
|
148 |
Prints this help |
149 |
|
150 |
|
151 |
=item B<-a> |
152 |
|
153 |
Update all records |
154 |
|
155 |
update_unimarc_100a_language.pl -a |
156 |
|
157 |
|
158 |
=item B<-b> |
159 |
|
160 |
Limit the action to a fixed set of comma separated biblionumber values |
161 |
|
162 |
Update record with biblionumber 1: |
163 |
update_unimarc_100a_language.pl -b=1 (or -biblionumber=1) |
164 |
|
165 |
Update records with biblionumbers 1, 2 and 3: |
166 |
update_unimarc_100a_language.pl -b=1,2,3 (or -biblionumber=1,2,3) |
167 |
|
168 |
|
169 |
=item B<-o> |
170 |
|
171 |
Sets the output of the script to a given file. Helpful for verification purposes on big sets of records |
172 |
|
173 |
Update records with script output being sent to file |
174 |
update_unimarc_100a_language.pl -output=file.txt (or -o=file.txt) |
175 |
|
176 |
=back |
177 |
|
178 |
=cut |
179 |
|