3 # Copyright 2013 Michael Hafen <mdhafen@tech.washk12.org>
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 # Script to switch the MARC21 440$anv and 490$av information
26 # find Koha's Perl modules
27 # test carefully before changing this
29 eval { require "$FindBin::Bin/../kohalib.pl" };
39 my $update_frameworks;
42 my $result = GetOptions
(
45 'f' => \
$update_frameworks,
46 'h|help' => \
$show_help,
50 # warn and exit if we're running UNIMARC
51 if (C4
::Context
->preference('MARCFLAVOUR') eq 'UNIMARC') {
52 print "This script is useless when you're running UNIMARC\n";
55 if ( ! $result || $show_help ) {
60 my $dbh = C4
::Context
->dbh;
62 my $count_sth = $dbh->prepare(
64 SELECT COUNT
(biblionumber
)
66 WHERE format
='marcxml'
69 ExtractValue
(metadata
,'//datafield[@tag="440"]/subfield[@code="a"]')
70 OR ExtractValue
(metadata
,'//datafield[@tag="440"]/subfield[@code="v"]')
71 OR ExtractValue
(metadata
,'//datafield[@tag="440"]/subfield[@code="n"]')
72 OR ExtractValue
(metadata
,'//datafield[@tag="490"]/subfield[@code="a"]')
73 OR ExtractValue
(metadata
,'//datafield[@tag="490"]/subfield[@code="v"]')
78 my $bibs_sth = $dbh->prepare(
82 WHERE format
='marcxml'
85 ExtractValue
(metadata
,'//datafield[@tag="440"]/subfield[@code="a"]')
86 OR ExtractValue
(metadata
,'//datafield[@tag="440"]/subfield[@code="v"]')
87 OR ExtractValue
(metadata
,'//datafield[@tag="440"]/subfield[@code="n"]')
88 OR ExtractValue
(metadata
,'//datafield[@tag="490"]/subfield[@code="a"]')
89 OR ExtractValue
(metadata
,'//datafield[@tag="490"]/subfield[@code="v"]')
98 print "Examining MARC records...\n";
99 $count_sth->execute( C4
::Context
->preference('marcflavour') );
100 my ( $num_records ) = $count_sth->fetchrow;
103 if ( $num_records ) {
104 print "This action would change $num_records MARC records\n";
107 print "There appears to be no series information to change\n";
109 print "Please run this again with the '-c' option to change the records\n";
113 print "Changing $num_records MARC records...\n";
137 $bibs_sth->execute( C4
::Context
->preference('marcflavour') );
138 while ( my ( $biblionumber ) = $bibs_sth->fetchrow ) {
139 my $framework = GetFrameworkCode
( $biblionumber ) || '';
143 my $biblio = GetMarcBiblio
({ biblionumber
=> $biblionumber });
145 foreach my $field ( $biblio->field( '440' ) ) {
149 foreach my $subfield ( sort keys %{ $fields{'440'} } ) {
150 my @values = $field->subfield( $subfield );
152 if ( $add_links && @values ) {
153 if ( $subfield eq 'w' || $subfield eq '0' ) {
156 foreach my $v ( @values ) {
157 push @linksubfields, ( $subfield, $v );
161 if ( $subfield eq 'a' ) {
162 my @numbers = $field->subfield( 'n' );
163 my @parts = $field->subfield( 'p' );
165 while ( $i < @numbers || $i < @parts ) {
166 my @strings = grep {$_} ( $values[$i], $numbers[$i], $parts[$i] );
167 $values[$i] = join ' ', @strings;
172 if ( $fields{'490'}{$subfield} ) {
173 foreach my $v ( @values ) {
174 push @newsubfields, ( $subfield, $v );
179 if ( $has_links && @linksubfields ) {
180 my $link_field = MARC
::Field
->new(
182 $field->indicator(1), $field->indicator(2),
185 push @newfields, $link_field;
188 if ( @newsubfields ) {
189 my $new_field = MARC
::Field
->new( '490', $has_links, '',
191 push @newfields, $new_field;
194 $biblio->delete_fields( $field );
197 foreach my $field ( $biblio->field( '490' ) ) {
199 foreach my $subfield ( sort keys %{ $fields{'490'} } ) {
200 my @values = $field->subfield( $subfield );
202 if ( $fields{'440'}{$subfield} ) {
203 foreach my $v ( @values ) {
204 push @newsubfields, ( $subfield, $v );
209 if ( @newsubfields ) {
210 my $new_field = MARC
::Field
->new( '440', '', '',
212 push @newfields, $new_field;
215 $biblio->delete_fields( $field );
217 $biblio->insert_fields_ordered( @newfields );
220 print "Changing MARC for biblio number $biblionumber.\n";
225 ModBiblioMarc
( $biblio, $biblionumber, $framework );
229 if ( $update_frameworks ) {
230 print "Updating Koha to MARC mappings for seriestitle and volume\n";
232 # set new mappings for koha fields
234 "UPDATE marc_subfield_structure SET kohafield='seriestitle'
235 WHERE tagfield='490' AND tagsubfield='a'"
238 "UPDATE marc_subfield_structure SET kohafield='volume'
239 WHERE tagfield='490' AND tagsubfield='v'"
242 # empty old koha fields
244 "UPDATE marc_subfield_structure SET kohafield=''
245 WHERE kohafield='seriestitle' AND tagfield='440' AND tagsubfield='a'"
248 "UPDATE marc_subfield_structure SET kohafield=''
249 WHERE kohafield='volume' AND tagfield='440' AND tagsubfield='v'"
255 $0: switch MARC21
440 tag
and 490 tag contents
258 -c Commit the changes to the marc records
.
260 -l Add
830 tags with authority information from
440. Otherwise
261 this information will be ignored
.
263 -f Also update the Koha field to MARC framework mappings
for the
264 seriestitle
and volume Koha fields
.
266 -v Show more information as the records are being changed
.
268 --help
or -h show this message
.