From 74f161991802dfcf844c9fc18be77b1543f91cf1 Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Wed, 7 Sep 2011 18:05:30 +1200 Subject: [PATCH] Bug 6851 - script to set the MARC leader for resources with analytics This will find records that have a 773$w record that points to something else. For these, it will set the MARC leader position 19 value to 'c'. It will also find records that have a 001 value that is pointed to by another record's 773$w. For these, it sets the leader position 7 to 's'. With these set, Koha knows to present the links in the details views to connect the resources. Note: this is intended to be a post-migration script, in case your imported records had incomplete headers. --- misc/migration_tools/set_analytics_header | 146 +++++++++++++++++++++++++++++ 1 files changed, 146 insertions(+), 0 deletions(-) create mode 100755 misc/migration_tools/set_analytics_header diff --git a/misc/migration_tools/set_analytics_header b/misc/migration_tools/set_analytics_header new file mode 100755 index 0000000..849a9ae --- /dev/null +++ b/misc/migration_tools/set_analytics_header @@ -0,0 +1,146 @@ +#!/usr/bin/perl + +# Copyright 2011 Catalyst IT + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# This will run through the biblios and save the 001 field for every +# record, linking it to the biblionumber. It will then go through again, and +# for every record that has a 773$w field, it will look up the corresponding +# parent record (using the saved 001 map) and set its header to indicate it's +# the parent of a set. It will then set the header of the one with 773$w to +# show that it's a member of a set. Then Koha will know to link them up. + +# This is done by setting position 7 in the leader to 's' for the "parent" +# records, and setting position 19 to 'c'. This causes Koha to create links +# between them. + +use strict; +use warnings; + +use C4::Context; +use C4::Biblio qw/ ModBiblioMarc /; + +use MARC::Record; +use MARC::Field; +use MARC::File::XML; + +my $dbh = C4::Context->dbh; + +my $all_bibs_marc_sth = $dbh->prepare("SELECT biblionumber,marc FROM biblioitems"); +$all_bibs_marc_sth->execute(); + +# First pass catches all the 001s and maps them to biblionumbers +my %ctrl_to_bib; +my $count = 0; +while (my $row = $all_bibs_marc_sth->fetchrow_arrayref) { + display(\$count); + my $bib = $row->[0]; + my $marcval = $row->[1]; + die "No marcval!" if (!$marcval); + + my $marc = eval { MARC::Record->new_from_usmarc($marcval) }; + if ($@) { + warn "Error parsing MARC for bib $bib: $@\n"; + next; + } + + my $field = $marc->field('001'); + if (!$field) { + warn "No 001 field for bib $bib"; + next; + } + my $data = $field->data; + if (!$data) { + warn "No 001 data for bib $bib"; + next; + } + $ctrl_to_bib{$data} = $bib; +} + +# Now do much the same thing, but check out the 773$w field, and for each one +# we find, update the appropriate records. +print "Part duex\n"; +$count = 0; + +$all_bibs_marc_sth->execute(); +while (my $row = $all_bibs_marc_sth->fetchrow_arrayref) { + display(\$count); + my ($bib, $marcval) = @$row; + + my $marc = eval { MARC::Record->new_from_usmarc($marcval) }; + if ($@) { + warn "Error parsing MARC for bib $bib: $@\n"; + next; + } + my $field = $marc->field('773'); + next if !$field; + + my $subfield = $field->subfield('w'); + next if !$subfield; + + if (my $parent_bib = $ctrl_to_bib{$subfield}) { + update_parent($parent_bib); + delete $ctrl_to_bib{$subfield}; + } + update_child($bib, $marc); +} + +print "\n"; + +my $get_marc_sth; +sub update_parent { + my ($bib) = @_; + + $get_marc_sth ||= $dbh->prepare("SELECT marc FROM biblioitems WHERE biblionumber=?"); + $get_marc_sth->execute($bib); + my $row = $get_marc_sth->fetchrow_arrayref; + if (!$row) { + die "Failed to look up parent for bib $bib"; + } + my $marcval = $row->[0]; + die "No marc value for bib $bib" if !$marcval; + my $marc = MARC::Record->new_from_usmarc($marcval); + my $leader = $marc->leader; + + substr($leader, 7, 1) = 's'; + $marc->leader($leader); + # Using this function isn't recommended, but as all we're doing is + # re-writing the leader, it should be OK here. + ModBiblioMarc($marc, $bib, ''); + warn "Updating parent: $bib\n"; +} + +sub update_child { + my ($bib, $marc) = @_; + + my $leader = $marc->leader; + substr($leader, 19, 1) = 'c'; + $marc->leader($leader); + ModBiblioMarc($marc, $bib, ''); + warn "Updating child: $bib\n"; +} + +sub display { + my $count = shift; + + if ($$count % 70 == 0) { + print "\n$$count "; + } else { + print "."; + } + $$count++; +} -- 1.7.4.1