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