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" };
38 my $update_frameworks;
41 my $result = GetOptions
(
44 'f' => \
$update_frameworks,
45 'h|help' => \
$show_help,
49 # warn and exit if we're running UNIMARC
50 if (C4
::Context
->preference('MARCFLAVOUR') eq 'UNIMARC') {
51 print "This script is useless when you're running UNIMARC\n";
54 if ( ! $result || $show_help ) {
59 my $dbh = C4
::Context
->dbh;
61 my $count_sth = $dbh->prepare(
63 SELECT COUNT
(biblionumber
)
65 WHERE format
='marcxml'
68 ExtractValue
(metadata
,'//datafield[@tag="440"]/subfield[@code="a"]')
69 OR ExtractValue
(metadata
,'//datafield[@tag="440"]/subfield[@code="v"]')
70 OR ExtractValue
(metadata
,'//datafield[@tag="440"]/subfield[@code="n"]')
71 OR ExtractValue
(metadata
,'//datafield[@tag="490"]/subfield[@code="a"]')
72 OR ExtractValue
(metadata
,'//datafield[@tag="490"]/subfield[@code="v"]')
77 my $bibs_sth = $dbh->prepare(
81 WHERE format
='marcxml'
84 ExtractValue
(metadata
,'//datafield[@tag="440"]/subfield[@code="a"]')
85 OR ExtractValue
(metadata
,'//datafield[@tag="440"]/subfield[@code="v"]')
86 OR ExtractValue
(metadata
,'//datafield[@tag="440"]/subfield[@code="n"]')
87 OR ExtractValue
(metadata
,'//datafield[@tag="490"]/subfield[@code="a"]')
88 OR ExtractValue
(metadata
,'//datafield[@tag="490"]/subfield[@code="v"]')
97 print "Examining MARC records...\n";
98 $count_sth->execute( C4
::Context
->preference('marcflavour') );
99 my ( $num_records ) = $count_sth->fetchrow;
102 if ( $num_records ) {
103 print "This action would change $num_records MARC records\n";
106 print "There appears to be no series information to change\n";
108 print "Please run this again with the '-c' option to change the records\n";
112 print "Changing $num_records MARC records...\n";
136 $bibs_sth->execute( C4
::Context
->preference('marcflavour') );
137 while ( my ( $biblionumber ) = $bibs_sth->fetchrow ) {
138 my $framework = GetFrameworkCode
( $biblionumber ) || '';
142 my $biblio = GetMarcBiblio
({ biblionumber
=> $biblionumber });
144 foreach my $field ( $biblio->field( '440' ) ) {
148 foreach my $subfield ( sort keys %{ $fields{'440'} } ) {
149 my @values = $field->subfield( $subfield );
151 if ( $add_links && @values ) {
152 if ( $subfield eq 'w' || $subfield eq '0' ) {
155 foreach my $v ( @values ) {
156 push @linksubfields, ( $subfield, $v );
160 if ( $subfield eq 'a' ) {
161 my @numbers = $field->subfield( 'n' );
162 my @parts = $field->subfield( 'p' );
164 while ( $i < @numbers || $i < @parts ) {
165 my @strings = grep {$_} ( $values[$i], $numbers[$i], $parts[$i] );
166 $values[$i] = join ' ', @strings;
171 if ( $fields{'490'}{$subfield} ) {
172 foreach my $v ( @values ) {
173 push @newsubfields, ( $subfield, $v );
178 if ( $has_links && @linksubfields ) {
179 my $link_field = MARC
::Field
->new(
181 $field->indicator(1), $field->indicator(2),
184 push @newfields, $link_field;
187 if ( @newsubfields ) {
188 my $new_field = MARC
::Field
->new( '490', $has_links, '',
190 push @newfields, $new_field;
193 $biblio->delete_fields( $field );
196 foreach my $field ( $biblio->field( '490' ) ) {
198 foreach my $subfield ( sort keys %{ $fields{'490'} } ) {
199 my @values = $field->subfield( $subfield );
201 if ( $fields{'440'}{$subfield} ) {
202 foreach my $v ( @values ) {
203 push @newsubfields, ( $subfield, $v );
208 if ( @newsubfields ) {
209 my $new_field = MARC
::Field
->new( '440', '', '',
211 push @newfields, $new_field;
214 $biblio->delete_fields( $field );
216 $biblio->insert_fields_ordered( @newfields );
219 print "Changing MARC for biblio number $biblionumber.\n";
224 ModBiblioMarc
( $biblio, $biblionumber, $framework );
228 if ( $update_frameworks ) {
229 print "Updating Koha to MARC mappings for seriestitle and volume\n";
231 # set new mappings for koha fields
233 "UPDATE marc_subfield_structure SET kohafield='seriestitle'
234 WHERE tagfield='490' AND tagsubfield='a'"
237 "UPDATE marc_subfield_structure SET kohafield='volume'
238 WHERE tagfield='490' AND tagsubfield='v'"
241 # empty old koha fields
243 "UPDATE marc_subfield_structure SET kohafield=''
244 WHERE kohafield='seriestitle' AND tagfield='440' AND tagsubfield='a'"
247 "UPDATE marc_subfield_structure SET kohafield=''
248 WHERE kohafield='volume' AND tagfield='440' AND tagsubfield='v'"
254 $0: switch MARC21
440 tag
and 490 tag contents
257 -c Commit the changes to the marc records
.
259 -l Add
830 tags with authority information from
440. Otherwise
260 this information will be ignored
.
262 -f Also update the Koha field to MARC framework mappings
for the
263 seriestitle
and volume Koha fields
.
265 -v Show more information as the records are being changed
.
267 --help
or -h show this message
.