2 # script that rebuild EDITORS
5 #use warnings; FIXME - Bug 2505
8 use MARC
::File
::USMARC
;
13 use C4
::AuthoritiesMarc
;
14 use Time
::HiRes
qw(gettimeofday);
17 my ( $input_marc_file, $number) = ('',0);
18 my ($version, $verbose, $test_parameter, $confirm,$delete);
22 't' => \
$test_parameter,
27 if ($version or !$confirm) {
29 small script to recreate a authority table into Koha.
30 This will parse all your biblios to recreate isbn / editor / collections for the unimarc_210c and unimarc_225a plugins.
32 Remember those plugins will work only if you have an EDITORS authority type, with
33 \t200a being the first 2 parts of an ISBN
34 \t200b being the editor name
35 \t200c (repeatable) being the series title
38 \t-c : confirmation flag. the script will run only with this flag. Otherwise, it will just show this help screen.
39 \t-d : delete existing EDITORS before rebuilding them
40 \t-t : test parameters : run the script but don't create really the EDITORS
47 my $dbh = C4
::Context
->dbh;
49 print "deleting EDITORS\n";
50 my $del1 = $dbh->prepare("delete from auth_subfield_table where authid=?");
51 my $del2 = $dbh->prepare("delete from auth_word where authid=?");
52 my $sth = $dbh->prepare("select authid from auth_header where authtypecode='EDITORS'");
54 while (my ($authid) = $sth->fetchrow) {
55 $del1->execute($authid);
56 $del2->execute($authid);
58 $dbh->do("delete from auth_header where authtypecode='EDITORS'");
61 if ($test_parameter) {
62 print "TESTING MODE ONLY\n DOING NOTHING\n===============\n";
64 $|=1; # flushes output
65 my $starttime = gettimeofday
;
66 my $sth = $dbh->prepare("select bibid from marc_biblio");
72 while (my ($bibid) = $sth->fetchrow) {
73 my $record = GetMarcBiblio
($bibid);
74 my $isbnField = $record->field('010');
75 next unless $isbnField;
76 my $isbn=$isbnField->subfield('a');
78 if(substr($isbn, 0, 1) <=7) {
79 $seg1 = substr($isbn, 0, 1);
80 } elsif(substr($isbn, 0, 2) <= 94) {
81 $seg1 = substr($isbn, 0, 2);
82 } elsif(substr($isbn, 0, 3) <= 995) {
83 $seg1 = substr($isbn, 0, 3);
84 } elsif(substr($isbn, 0, 4) <= 9989) {
85 $seg1 = substr($isbn, 0, 4);
87 $seg1 = substr($isbn, 0, 5);
89 my $x = substr($isbn, length($seg1));
91 if(substr($x, 0, 2) <= 19) {
92 # if(sTmp2 < 10) sTmp2 = "0" sTmp2;
93 $seg2 = substr($x, 0, 2);
94 } elsif(substr($x, 0, 3) <= 699) {
95 $seg2 = substr($x, 0, 3);
96 } elsif(substr($x, 0, 4) <= 8399) {
97 $seg2 = substr($x, 0, 4);
98 } elsif(substr($x, 0, 5) <= 89999) {
99 $seg2 = substr($x, 0, 5);
100 } elsif(substr($x, 0, 6) <= 9499999) {
101 $seg2 = substr($x, 0, 6);
103 $seg2 = substr($x, 0, 7);
107 my $timeneeded = gettimeofday
- $starttime;
108 print "$counter in $timeneeded s\n" unless ($counter % 100);
110 my $field = $record->field('210');
112 $editor=$field->subfield('c') if $field;
114 $field = $record->field('225');
116 $collection=$field->subfield('a') if $field;
118 # print "WARNING : editor empty for ".$record->as_formatted unless $editor and !$verbose;
120 $hash{$seg1.$seg2}->{editors
} = $editor unless ($hash{$seg1.$seg2}->{editors
});
121 $hash{$seg1.$seg2}->{collections
}->{$collection}++ if $collection;
124 foreach my $isbnstart (sort keys %hash) {
125 print "$isbnstart -- ".$hash{$isbnstart}->{editors
} if $verbose;
126 my $collections = $hash{$isbnstart}->{collections
};
128 foreach my $collection (sort keys %$collections) {
129 print " CC $collection : ".$collections->{$collection} if $verbose;
130 $seriestitlelist.=$collection."|";
132 my $authorityRecord = MARC
::Record
->new();
133 my $newfield = MARC
::Field
->new(200,'','','a' => "".$isbnstart,
134 'b' => "".$hash{$isbnstart}->{editors
},
135 'c' => "".$seriestitlelist);
136 $authorityRecord->insert_fields_ordered($newfield);
137 my $authid=AUTHaddauthority
($dbh,$authorityRecord,'','EDITORS');
139 # print $authorityRecord->as_formatted."\n";
140 print "\n" if $verbose;
144 # my $timeneeded = gettimeofday - $starttime;
145 # print "$i in $timeneeded s\n" unless ($i % 50);
146 # foreach my $field ($record->field(995)) {
147 # $record->delete_field($field);
151 # foreach my $fieldnumber (('710','711','712')) {
152 # foreach my $field ($record->field($fieldnumber)) {
153 # # print "=>".$field->as_formatted."\n";
154 # foreach my $authentry ($field->subfield("a")) {
155 # my $hashentry = $authentry;
156 # # 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)
157 # # si n�essaire, compl�er avec le $c (n'existe pas dans le fichier que j'ai migr�avec cette moulinette
158 # # supprimer les accents, certaines entr�s sont sans, d'autres avec !
159 # # mysql ne diff�encie pas, mais les hash perl oui !
160 # $hashentry =~ s/���e/g;
161 # $hashentry =~ s/��a/g;
162 # $hashentry =~ s/�i/g;
163 # $hashentry =~ s/�o/g;
164 # $hashentry =~ s/|/u/g;
165 # $hashentry = uc($hashentry);
166 # print "==>$hashentry" if $hashentry =~ /.*ETATS.*/;
168 # if ($alreadydone{$hashentry}) {
169 # $authid = $alreadydone{$hashentry};
174 # my $authorityRecord = MARC::Record->new();
175 # my $newfield = MARC::Field->new(210,'','','a' => "".$authentry,
176 # 'b' => "".$field->subfield('b'),
177 # 'c' => "".$field->subfield('c'),
179 # $authorityRecord->insert_fields_ordered($newfield);
180 # $authid=AUTHaddauthority($dbh,$authorityRecord,'','CO');
181 # $alreadydone{$hashentry} = $authid;
182 # # OK, on garde la notice d'autorit� on cherche les notices biblio et on les met �jour...
183 # if ($fieldnumber eq '710') {
184 # $sthBIBLIOS710->execute($authentry);
185 # while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS710->fetchrow) {
186 # my $inbiblio = GetMarcBiblio($bibid);
188 # foreach my $in7xx ($inbiblio->field($fieldnumber)) {
189 # # !!!!! ici, il faut reconstruire l'entr� de la table de hachage comme ci dessus
191 # my $inEntry = $in7xx->subfield('a');
192 # $inEntry =~ s/���e/g;
193 # $inEntry =~ s/��a/g;
194 # $inEntry =~ s/�i/g;
195 # $inEntry =~ s/�o/g;
196 # $inEntry =~ s/|/u/g;
197 # $inEntry = uc($inEntry);
198 # $isOK=1 if $inEntry eq $hashentry;
200 # C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
203 # if ($fieldnumber eq '711') {
204 # $sthBIBLIOS711->execute($authentry);
205 # while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS711->fetchrow) {
206 # my $inbiblio = GetMarcBiblio($bibid);
208 # foreach my $in7xx ($inbiblio->field($fieldnumber)) {
209 # # !!!!! ici, il faut reconstruire l'entr� de la table de hachage comme ci dessus
211 # my $inEntry = $in7xx->subfield('a');
212 # $inEntry =~ s/���e/g;
213 # $inEntry =~ s/��a/g;
214 # $inEntry =~ s/�i/g;
215 # $inEntry =~ s/�o/g;
216 # $inEntry =~ s/|/u/g;
217 # $inEntry = uc($inEntry);
218 # $isOK=1 if $inEntry eq $hashentry;
220 # C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
223 # if ($fieldnumber eq '712') {
224 # $sthBIBLIOS712->execute($authentry);
225 # while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS712->fetchrow) {
226 # my $inbiblio = GetMarcBiblio($bibid);
228 # foreach my $in7xx ($inbiblio->field($fieldnumber)) {
229 # # !!!!! ici, il faut reconstruire l'entr� de la table de hachage comme ci dessus
231 # my $inEntry = $in7xx->subfield('a');
232 # $inEntry =~ s/���e/g;
233 # $inEntry =~ s/��a/g;
234 # $inEntry =~ s/�i/g;
235 # $inEntry =~ s/�o/g;
236 # $inEntry =~ s/|/u/g;
237 # $inEntry = uc($inEntry);
238 # $isOK=1 if $inEntry eq $hashentry;
240 # C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
249 # my $timeneeded = gettimeofday - $starttime;
250 # print "$i entries done in $timeneeded seconds (".($i/$timeneeded)." per second)\n";