6 # Script to switch the MARC21 440$anv and 490$av information
9 # find Koha's Perl modules
10 # test carefully before changing this
12 eval { require "$FindBin::Bin/../kohalib.pl" };
20 my $update_frameworks;
23 my $result = GetOptions(
25 'm' => \$update_frameworks,
26 'h|help' => \$show_help,
30 if ( ! $result || $show_help ) {
35 my $dbh = C4::Context->dbh;
37 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"]\')' );
39 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"]\')' );
45 print "Examining MARC records...\n";
46 $count_sth->execute();
47 my ( $num_records ) = $count_sth->fetchrow;
51 print "This action would change $num_records MARC records\n";
54 print "There appears to be no series information to change\n";
59 print "Changing $num_records MARC records...\n";
62 while ( my ( $biblionumber ) = $bibs_sth->fetchrow ) {
63 my $framework = GetFrameworkCode( $biblionumber ) || '';
67 my ( $series1_t, $series1_f ) = ( '440', 'a' );
68 my ( $volume1_t, $volume1_f ) = ( '440', 'v' );
69 my ( $number1_t, $number1_f ) = ( '440', 'n' );
71 my ( $series2_t, $series2_f ) = ( '490', 'a' );
72 my ( $volume2_t, $volume2_f ) = ( '490', 'v' );
75 my $biblio = GetMarcBiblio( $biblionumber );
77 foreach my $field ( $biblio->field( $series1_t ) ) {
79 my @series1 = $field->subfield( $series1_f );
80 my @volume1 = $field->subfield( $volume1_f );
81 my @number1 = $field->subfield( $number1_f );
83 foreach my $num ( @number1 ) {
84 $volume1[$i] .= " " if ( $volume1[$i] );
85 $volume1[$i++] .= $num if ( $num );
88 while ( @series1 || @volume1 ) {
90 push @newsubfields, ( $series2_f, shift @series1 );
93 push @newsubfields, ( $volume2_f, shift @volume1 );
97 my $new_field = MARC::Field->new( $series2_t, '', '',
100 $biblio->delete_fields( $field );
101 push @newfields, $new_field;
104 foreach my $field ( $biblio->field( $series2_t ) ) {
106 my @series2 = $field->subfield( $series2_f );
107 my @volume2 = $field->subfield( $volume2_f );
109 while ( @series2 || @volume2 ) {
111 push @newsubfields, ( $series1_f, shift @series2 );
114 push @newsubfields, ( $volume1_f, shift @volume2 );
118 my $new_field = MARC::Field->new( $series1_t, '', '',
121 $biblio->delete_fields( $field );
122 push @newfields, $new_field;
124 $biblio->insert_fields_ordered( @newfields );
126 ModBiblioMarc( $biblio, $biblionumber, $framework );
128 print "Changing MARC for biblio number $biblionumber.\n";
136 if ( $update_frameworks ) {
137 print "Updating Koha to MARC mappings for seriestitle and volume\n";
139 # set new mappings for koha fields
141 "UPDATE marc_subfield_structure SET kohafield='seriestitle'
142 WHERE tagfield='490' AND tagsubfield='a'"
145 "UPDATE marc_subfield_structure SET kohafield='volume'
146 WHERE tagfield='490' AND tagsubfield='v'"
149 # empty old koha fields
151 "UPDATE marc_subfield_structure SET kohafield=''
152 WHERE kohafield='seriestitle' AND tagfield='440' AND tagsubfield='a'"
155 "UPDATE marc_subfield_structure SET kohafield=''
156 WHERE kohafield='volume' AND tagfield='440' AND tagsubfield='v'"
162 $0: switch MARC21 440 tag and 490 tag contents
165 -c Commit the changes to the marc records
167 -m Also update the Koha field to MARC framework mappings for the
168 seriestitle and volume Koha fields.
170 --help or -h show this message.