Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2011 Catalyst IT |
4 |
|
5 |
# This file is part of Koha. |
6 |
# |
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 |
10 |
# version. |
11 |
# |
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. |
15 |
# |
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. |
19 |
|
20 |
# This will run through the biblios and save the 001 field for every |
21 |
# record, linking it to the biblionumber. It will then go through again, and |
22 |
# for every record that has a 773$w field, it will look up the corresponding |
23 |
# parent record (using the saved 001 map) and set its header to indicate it's |
24 |
# the parent of a set. It will then set the header of the one with 773$w to |
25 |
# show that it's a member of a set. Then Koha will know to link them up. |
26 |
|
27 |
# This is done by setting position 7 in the leader to 's' for the "parent" |
28 |
# records, and setting position 19 to 'c'. This causes Koha to create links |
29 |
# between them. |
30 |
|
31 |
use strict; |
32 |
use warnings; |
33 |
|
34 |
use C4::Context; |
35 |
use C4::Biblio qw/ ModBiblioMarc /; |
36 |
|
37 |
use MARC::Record; |
38 |
use MARC::Field; |
39 |
use MARC::File::XML; |
40 |
|
41 |
my $dbh = C4::Context->dbh; |
42 |
|
43 |
my $all_bibs_marc_sth = $dbh->prepare("SELECT biblionumber,marc FROM biblioitems"); |
44 |
$all_bibs_marc_sth->execute(); |
45 |
|
46 |
# First pass catches all the 001s and maps them to biblionumbers |
47 |
my %ctrl_to_bib; |
48 |
my $count = 0; |
49 |
while (my $row = $all_bibs_marc_sth->fetchrow_arrayref) { |
50 |
display(\$count); |
51 |
my $bib = $row->[0]; |
52 |
my $marcval = $row->[1]; |
53 |
die "No marcval!" if (!$marcval); |
54 |
|
55 |
my $marc = eval { MARC::Record->new_from_usmarc($marcval) }; |
56 |
if ($@) { |
57 |
warn "Error parsing MARC for bib $bib: $@\n"; |
58 |
next; |
59 |
} |
60 |
|
61 |
my $field = $marc->field('001'); |
62 |
if (!$field) { |
63 |
warn "No 001 field for bib $bib"; |
64 |
next; |
65 |
} |
66 |
my $data = $field->data; |
67 |
if (!$data) { |
68 |
warn "No 001 data for bib $bib"; |
69 |
next; |
70 |
} |
71 |
$ctrl_to_bib{$data} = $bib; |
72 |
} |
73 |
|
74 |
# Now do much the same thing, but check out the 773$w field, and for each one |
75 |
# we find, update the appropriate records. |
76 |
print "Part duex\n"; |
77 |
$count = 0; |
78 |
|
79 |
$all_bibs_marc_sth->execute(); |
80 |
while (my $row = $all_bibs_marc_sth->fetchrow_arrayref) { |
81 |
display(\$count); |
82 |
my ($bib, $marcval) = @$row; |
83 |
|
84 |
my $marc = eval { MARC::Record->new_from_usmarc($marcval) }; |
85 |
if ($@) { |
86 |
warn "Error parsing MARC for bib $bib: $@\n"; |
87 |
next; |
88 |
} |
89 |
my $field = $marc->field('773'); |
90 |
next if !$field; |
91 |
|
92 |
my $subfield = $field->subfield('w'); |
93 |
next if !$subfield; |
94 |
|
95 |
if (my $parent_bib = $ctrl_to_bib{$subfield}) { |
96 |
update_parent($parent_bib); |
97 |
delete $ctrl_to_bib{$subfield}; |
98 |
} |
99 |
update_child($bib, $marc); |
100 |
} |
101 |
|
102 |
print "\n"; |
103 |
|
104 |
my $get_marc_sth; |
105 |
sub update_parent { |
106 |
my ($bib) = @_; |
107 |
|
108 |
$get_marc_sth ||= $dbh->prepare("SELECT marc FROM biblioitems WHERE biblionumber=?"); |
109 |
$get_marc_sth->execute($bib); |
110 |
my $row = $get_marc_sth->fetchrow_arrayref; |
111 |
if (!$row) { |
112 |
die "Failed to look up parent for bib $bib"; |
113 |
} |
114 |
my $marcval = $row->[0]; |
115 |
die "No marc value for bib $bib" if !$marcval; |
116 |
my $marc = MARC::Record->new_from_usmarc($marcval); |
117 |
my $leader = $marc->leader; |
118 |
|
119 |
substr($leader, 7, 1) = 's'; |
120 |
$marc->leader($leader); |
121 |
# Using this function isn't recommended, but as all we're doing is |
122 |
# re-writing the leader, it should be OK here. |
123 |
ModBiblioMarc($marc, $bib, ''); |
124 |
warn "Updating parent: $bib\n"; |
125 |
} |
126 |
|
127 |
sub update_child { |
128 |
my ($bib, $marc) = @_; |
129 |
|
130 |
my $leader = $marc->leader; |
131 |
substr($leader, 19, 1) = 'c'; |
132 |
$marc->leader($leader); |
133 |
ModBiblioMarc($marc, $bib, ''); |
134 |
warn "Updating child: $bib\n"; |
135 |
} |
136 |
|
137 |
sub display { |
138 |
my $count = shift; |
139 |
|
140 |
if ($$count % 70 == 0) { |
141 |
print "\n$$count "; |
142 |
} else { |
143 |
print "."; |
144 |
} |
145 |
$$count++; |
146 |
} |