-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathbuildEDITORS.pl
executable file
·256 lines (237 loc) · 9.15 KB
/
buildEDITORS.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/perl
# script that rebuild EDITORS
use strict;
#use warnings; FIXME - Bug 2505
# Koha modules used
use MARC::File::USMARC;
use MARC::Record;
use MARC::Batch;
use Koha::Script;
use C4::Context;
use C4::AuthoritiesMarc;
use Koha::Biblios;
use Time::HiRes qw( gettimeofday );
use Getopt::Long qw( GetOptions );
my ( $input_marc_file, $number ) = ( '', 0 );
my ( $version, $verbose, $test_parameter, $confirm, $delete );
GetOptions(
'h' => \$version,
'd' => \$delete,
't' => \$test_parameter,
'v' => \$verbose,
'c' => \$confirm,
);
if ( $version or !$confirm ) {
print <<EOF
small script to recreate a authority table into Koha.
This will parse all your biblios to recreate isbn / editor / collections for the unimarc_210c and unimarc_225a plugins.
Remember those plugins will work only if you have an EDITORS authority type, with
\t200a being the first 2 parts of an ISBN
\t200b being the editor name
\t200c (repeatable) being the series title
parameters :
\t-c : confirmation flag. the script will run only with this flag. Otherwise, it will just show this help screen.
\t-d : delete existing EDITORS before rebuilding them
\t-t : test parameters : run the script but don't create really the EDITORS
EOF
; #'
exit;
}
my $dbh = C4::Context->dbh;
if ($delete) {
print "deleting EDITORS\n";
my $del1 = $dbh->prepare("delete from auth_subfield_table where authid=?");
my $del2 = $dbh->prepare("delete from auth_word where authid=?");
my $sth = $dbh->prepare("select authid from auth_header where authtypecode='EDITORS'");
$sth->execute;
while ( my ($authid) = $sth->fetchrow ) {
$del1->execute($authid);
$del2->execute($authid);
}
$dbh->do("delete from auth_header where authtypecode='EDITORS'");
}
if ($test_parameter) {
print "TESTING MODE ONLY\n DOING NOTHING\n===============\n";
}
$| = 1; # flushes output
my $starttime = gettimeofday;
my $sth = $dbh->prepare("select bibid from marc_biblio");
$sth->execute;
my $i = 1;
my $counter;
my %hash;
while ( my ($bibid) = $sth->fetchrow ) {
my $biblio = Koha::Biblios->find($bibid);
my $record = $biblio->metadata->record;
my $isbnField = $record->field('010');
next unless $isbnField;
my $isbn = $isbnField->subfield('a');
my $seg1;
if ( substr( $isbn, 0, 1 ) <= 7 ) {
$seg1 = substr( $isbn, 0, 1 );
} elsif ( substr( $isbn, 0, 2 ) <= 94 ) {
$seg1 = substr( $isbn, 0, 2 );
} elsif ( substr( $isbn, 0, 3 ) <= 995 ) {
$seg1 = substr( $isbn, 0, 3 );
} elsif ( substr( $isbn, 0, 4 ) <= 9989 ) {
$seg1 = substr( $isbn, 0, 4 );
} else {
$seg1 = substr( $isbn, 0, 5 );
}
my $x = substr( $isbn, length($seg1) );
my $seg2;
if ( substr( $x, 0, 2 ) <= 19 ) {
# if(sTmp2 < 10) sTmp2 = "0" sTmp2;
$seg2 = substr( $x, 0, 2 );
} elsif ( substr( $x, 0, 3 ) <= 699 ) {
$seg2 = substr( $x, 0, 3 );
} elsif ( substr( $x, 0, 4 ) <= 8399 ) {
$seg2 = substr( $x, 0, 4 );
} elsif ( substr( $x, 0, 5 ) <= 89999 ) {
$seg2 = substr( $x, 0, 5 );
} elsif ( substr( $x, 0, 6 ) <= 9499999 ) {
$seg2 = substr( $x, 0, 6 );
} else {
$seg2 = substr( $x, 0, 7 );
}
$counter++;
print ".";
my $timeneeded = gettimeofday - $starttime;
print "$counter in $timeneeded s\n" unless ( $counter % 100 );
my $field = $record->field('210');
my $editor;
$editor = $field->subfield('c') if $field;
$field = $record->field('225');
my $collection;
$collection = $field->subfield('a') if $field;
# print "WARNING : editor empty for ".$record->as_formatted unless $editor and !$verbose;
$hash{ $seg1 . $seg2 }->{editors} = $editor unless ( $hash{ $seg1 . $seg2 }->{editors} );
$hash{ $seg1 . $seg2 }->{collections}->{$collection}++ if $collection;
}
foreach my $isbnstart ( sort keys %hash ) {
print "$isbnstart -- " . $hash{$isbnstart}->{editors} if $verbose;
my $collections = $hash{$isbnstart}->{collections};
my $seriestitlelist;
foreach my $collection ( sort keys %$collections ) {
print " CC $collection : " . $collections->{$collection} if $verbose;
$seriestitlelist .= $collection . "|";
}
my $authorityRecord = MARC::Record->new();
my $newfield = MARC::Field->new(
200, '', '', 'a' => "" . $isbnstart,
'b' => "" . $hash{$isbnstart}->{editors},
'c' => "" . $seriestitlelist
);
$authorityRecord->insert_fields_ordered($newfield);
my $authid = AUTHaddauthority( $dbh, $authorityRecord, '', 'EDITORS' );
# print $authorityRecord->as_formatted."\n";
print "\n" if $verbose;
}
exit;
# my $timeneeded = gettimeofday - $starttime;
# print "$i in $timeneeded s\n" unless ($i % 50);
# foreach my $field ($record->field(995)) {
# $record->delete_field($field);
# }
# my $totdone=0;
# my $authid;
# foreach my $fieldnumber (('710','711','712')) {
# foreach my $field ($record->field($fieldnumber)) {
# # print "=>".$field->as_formatted."\n";
# foreach my $authentry ($field->subfield("a")) {
# my $hashentry = $authentry;
# # la particularit�de ce script l� c'est que l'entr� dans la table d'autorit�est $a -- $b (et pas $x -- $x -- $x -- $a comme pour les autorit� NC)
# # si n�essaire, compl�er avec le $c (n'existe pas dans le fichier que j'ai migr�avec cette moulinette
# # supprimer les accents, certaines entr�s sont sans, d'autres avec !
# # mysql ne diff�encie pas, mais les hash perl oui !
# $hashentry =~ s/���e/g;
# $hashentry =~ s/��a/g;
# $hashentry =~ s/�i/g;
# $hashentry =~ s/�o/g;
# $hashentry =~ s/|/u/g;
# $hashentry = uc($hashentry);
# print "==>$hashentry" if $hashentry =~ /.*ETATS.*/;
# $totdone++;
# if ($alreadydone{$hashentry}) {
# $authid = $alreadydone{$hashentry};
# print ".";
# } else {
# print "*";
# #create authority.
# my $authorityRecord = MARC::Record->new();
# my $newfield = MARC::Field->new(210,'','','a' => "".$authentry,
# 'b' => "".$field->subfield('b'),
# 'c' => "".$field->subfield('c'),
# );
# $authorityRecord->insert_fields_ordered($newfield);
# $authid=AUTHaddauthority($dbh,$authorityRecord,'','CO');
# $alreadydone{$hashentry} = $authid;
# # OK, on garde la notice d'autorit� on cherche les notices biblio et on les met �jour...
# if ($fieldnumber eq '710') {
# $sthBIBLIOS710->execute($authentry);
# while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS710->fetchrow) {
# my $inbiblio = GetMarcBiblio({ biblionumber => $bibid });
# my $isOK = 0;
# foreach my $in7xx ($inbiblio->field($fieldnumber)) {
# # !!!!! ici, il faut reconstruire l'entr� de la table de hachage comme ci dessus
# # sinon,
# my $inEntry = $in7xx->subfield('a');
# $inEntry =~ s/���e/g;
# $inEntry =~ s/��a/g;
# $inEntry =~ s/�i/g;
# $inEntry =~ s/�o/g;
# $inEntry =~ s/|/u/g;
# $inEntry = uc($inEntry);
# $isOK=1 if $inEntry eq $hashentry;
# }
# C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
# }
# }
# if ($fieldnumber eq '711') {
# $sthBIBLIOS711->execute($authentry);
# while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS711->fetchrow) {
# my $inbiblio = GetMarcBiblio({ biblionumber => $bibid });
# my $isOK = 0;
# foreach my $in7xx ($inbiblio->field($fieldnumber)) {
# # !!!!! ici, il faut reconstruire l'entr� de la table de hachage comme ci dessus
# # sinon,
# my $inEntry = $in7xx->subfield('a');
# $inEntry =~ s/���e/g;
# $inEntry =~ s/��a/g;
# $inEntry =~ s/�i/g;
# $inEntry =~ s/�o/g;
# $inEntry =~ s/|/u/g;
# $inEntry = uc($inEntry);
# $isOK=1 if $inEntry eq $hashentry;
# }
# C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
# }
# }
# if ($fieldnumber eq '712') {
# $sthBIBLIOS712->execute($authentry);
# while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS712->fetchrow) {
# my $inbiblio = GetMarcBiblio({ biblionumber => $bibid });
# my $isOK = 0;
# foreach my $in7xx ($inbiblio->field($fieldnumber)) {
# # !!!!! ici, il faut reconstruire l'entr� de la table de hachage comme ci dessus
# # sinon,
# my $inEntry = $in7xx->subfield('a');
# $inEntry =~ s/���e/g;
# $inEntry =~ s/��a/g;
# $inEntry =~ s/�i/g;
# $inEntry =~ s/�o/g;
# $inEntry =~ s/|/u/g;
# $inEntry = uc($inEntry);
# $isOK=1 if $inEntry eq $hashentry;
# }
# C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
# }
# }
# }
# }
# }
# }
# $i++;
# }
# my $timeneeded = gettimeofday - $starttime;
# print "$i entries done in $timeneeded seconds (".($i/$timeneeded)." per second)\n";