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 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
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.
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.
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 if ( ! $result || $show_help ) {
54 my $dbh = C4::Context->dbh;
56 my $count_sth = $dbh->prepare( 'SELECT COUNT(biblionumber) FROM biblio CROSS JOIN biblioitems USING (biblionumber) WHERE ExtractValue(marcxml,\'//datafield[@tag="440"]/subfield[@code="a"]\') OR ExtractValue(marcxml,\'//datafield[@tag="440"]/subfield[@code="v"]\') OR ExtractValue(marcxml,\'//datafield[@tag="440"]/subfield[@code="n"]\') OR ExtractValue(marcxml,\'//datafield[@tag="490"]/subfield[@code="a"]\') OR ExtractValue(marcxml,\'//datafield[@tag="490"]/subfield[@code="v"]\')' );
58 my $bibs_sth = $dbh->prepare( 'SELECT biblionumber FROM biblio CROSS JOIN biblioitems USING (biblionumber) WHERE ExtractValue(marcxml,\'//datafield[@tag="440"]/subfield[@code="a"]\') OR ExtractValue(marcxml,\'//datafield[@tag="440"]/subfield[@code="v"]\') OR ExtractValue(marcxml,\'//datafield[@tag="440"]/subfield[@code="n"]\') OR ExtractValue(marcxml,\'//datafield[@tag="490"]/subfield[@code="a"]\') OR ExtractValue(marcxml,\'//datafield[@tag="490"]/subfield[@code="v"]\')' );
64 print "Examining MARC records...\n";
65 $count_sth->execute();
66 my ( $num_records ) = $count_sth->fetchrow;
70 print "This action would change $num_records MARC records\n";
73 print "There appears to be no series information to change\n";
75 print "Please run this again with the '-c' option to change the records\n";
79 print "Changing $num_records MARC records...\n";
103 $bibs_sth->execute();
104 while ( my ( $biblionumber ) = $bibs_sth->fetchrow ) {
105 my $framework = GetFrameworkCode( $biblionumber ) || '';
109 my $biblio = GetMarcBiblio( $biblionumber );
111 foreach my $field ( $biblio->field( '440' ) ) {
115 foreach my $subfield ( sort keys %{ $fields{'440'} } ) {
116 my @values = $field->subfield( $subfield );
118 if ( $add_links && @values ) {
119 if ( $subfield eq 'w' || $subfield eq '0' ) {
122 foreach my $v ( @values ) {
123 push @linksubfields, ( $subfield, $v );
127 if ( $subfield eq 'a' ) {
128 my @numbers = $field->subfield( 'n' );
129 my @parts = $field->subfield( 'p' );
131 while ( $i < @numbers || $i < @parts ) {
132 my @strings = grep {$_} ( $values[$i], $numbers[$i], $parts[$i] );
133 $values[$i] = join ' ', @strings;
138 if ( $fields{'490'}{$subfield} ) {
139 foreach my $v ( @values ) {
140 push @newsubfields, ( $subfield, $v );
145 if ( $has_links && @linksubfields ) {
146 my $link_field = MARC::Field->new(
148 $field->indicator(1), $field->indicator(2),
151 push @newfields, $link_field;
154 if ( @newsubfields ) {
155 my $new_field = MARC::Field->new( '490', $has_links, '',
157 push @newfields, $new_field;
160 $biblio->delete_fields( $field );
163 foreach my $field ( $biblio->field( '490' ) ) {
165 foreach my $subfield ( sort keys %{ $fields{'490'} } ) {
166 my @values = $field->subfield( $subfield );
168 if ( $fields{'440'}{$subfield} ) {
169 foreach my $v ( @values ) {
170 push @newsubfields, ( $subfield, $v );
175 if ( @newsubfields ) {
176 my $new_field = MARC::Field->new( '440', '', '',
178 push @newfields, $new_field;
181 $biblio->delete_fields( $field );
183 $biblio->insert_fields_ordered( @newfields );
186 print "Changing MARC for biblio number $biblionumber.\n";
191 ModBiblioMarc( $biblio, $biblionumber, $framework );
195 if ( $update_frameworks ) {
196 print "Updating Koha to MARC mappings for seriestitle and volume\n";
198 # set new mappings for koha fields
200 "UPDATE marc_subfield_structure SET kohafield='seriestitle'
201 WHERE tagfield='490' AND tagsubfield='a'"
204 "UPDATE marc_subfield_structure SET kohafield='volume'
205 WHERE tagfield='490' AND tagsubfield='v'"
208 # empty old koha fields
210 "UPDATE marc_subfield_structure SET kohafield=''
211 WHERE kohafield='seriestitle' AND tagfield='440' AND tagsubfield='a'"
214 "UPDATE marc_subfield_structure SET kohafield=''
215 WHERE kohafield='volume' AND tagfield='440' AND tagsubfield='v'"
221 $0: switch MARC21 440 tag and 490 tag contents
224 -c Commit the changes to the marc records.
226 -l Add 830 tags with authority information from 440. Otherwise
227 this information will be ignored.
229 -f Also update the Koha field to MARC framework mappings for the
230 seriestitle and volume Koha fields.
232 -v Show more information as the records are being changed.
234 --help or -h show this message.