Bug 21395: Make perlcritic happy
[koha.git] / misc / migration_tools / buildEDITORS.pl
blobbb11d458815f5d7ae89a15eafcaac333109f32d4
1 #!/usr/bin/perl
2 # script that rebuild EDITORS
4 use strict;
5 #use warnings; FIXME - Bug 2505
7 # Koha modules used
8 use MARC::File::USMARC;
9 use MARC::Record;
10 use MARC::Batch;
11 use Koha::Script;
12 use C4::Context;
13 use C4::Biblio;
14 use C4::AuthoritiesMarc;
15 use Time::HiRes qw(gettimeofday);
17 use Getopt::Long;
18 my ( $input_marc_file, $number) = ('',0);
19 my ($version, $verbose, $test_parameter, $confirm,$delete);
20 GetOptions(
21 'h' => \$version,
22 'd' => \$delete,
23 't' => \$test_parameter,
24 'v' => \$verbose,
25 'c' => \$confirm,
28 if ($version or !$confirm) {
29 print <<EOF
30 small script to recreate a authority table into Koha.
31 This will parse all your biblios to recreate isbn / editor / collections for the unimarc_210c and unimarc_225a plugins.
33 Remember those plugins will work only if you have an EDITORS authority type, with
34 \t200a being the first 2 parts of an ISBN
35 \t200b being the editor name
36 \t200c (repeatable) being the series title
38 parameters :
39 \t-c : confirmation flag. the script will run only with this flag. Otherwise, it will just show this help screen.
40 \t-d : delete existing EDITORS before rebuilding them
41 \t-t : test parameters : run the script but don't create really the EDITORS
42 EOF
43 ;#'
45 exit;
48 my $dbh = C4::Context->dbh;
49 if ($delete) {
50 print "deleting EDITORS\n";
51 my $del1 = $dbh->prepare("delete from auth_subfield_table where authid=?");
52 my $del2 = $dbh->prepare("delete from auth_word where authid=?");
53 my $sth = $dbh->prepare("select authid from auth_header where authtypecode='EDITORS'");
54 $sth->execute;
55 while (my ($authid) = $sth->fetchrow) {
56 $del1->execute($authid);
57 $del2->execute($authid);
59 $dbh->do("delete from auth_header where authtypecode='EDITORS'");
62 if ($test_parameter) {
63 print "TESTING MODE ONLY\n DOING NOTHING\n===============\n";
65 $|=1; # flushes output
66 my $starttime = gettimeofday;
67 my $sth = $dbh->prepare("select bibid from marc_biblio");
68 $sth->execute;
69 my $i=1;
70 my $counter;
71 my %hash;
72 while (my ($bibid) = $sth->fetchrow) {
73 my $record = GetMarcBiblio({ biblionumber => $bibid });
74 my $isbnField = $record->field('010');
75 next unless $isbnField;
76 my $isbn=$isbnField->subfield('a');
77 my $seg1;
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);
86 } else {
87 $seg1 = substr($isbn, 0, 5);
89 my $x = substr($isbn, length($seg1));
90 my $seg2;
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);
102 } else {
103 $seg2 = substr($x, 0, 7);
105 $counter++;
106 print ".";
107 my $timeneeded = gettimeofday - $starttime;
108 print "$counter in $timeneeded s\n" unless ($counter % 100);
110 my $field = $record->field('210');
111 my $editor;
112 $editor=$field->subfield('c') if $field;
114 $field = $record->field('225');
115 my $collection;
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};
127 my $seriestitlelist;
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;
142 exit;
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);
149 # my $totdone=0;
150 # my $authid;
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.*/;
167 # $totdone++;
168 # if ($alreadydone{$hashentry}) {
169 # $authid = $alreadydone{$hashentry};
170 # print ".";
171 # } else {
172 # print "*";
173 # #create authority.
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'),
178 # );
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({ biblionumber => $bibid });
187 # my $isOK = 0;
188 # foreach my $in7xx ($inbiblio->field($fieldnumber)) {
189 # # !!!!! ici, il faut reconstruire l'entr� de la table de hachage comme ci dessus
190 # # sinon,
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({ biblionumber => $bibid });
207 # my $isOK = 0;
208 # foreach my $in7xx ($inbiblio->field($fieldnumber)) {
209 # # !!!!! ici, il faut reconstruire l'entr� de la table de hachage comme ci dessus
210 # # sinon,
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({ biblionumber => $bibid });
227 # my $isOK = 0;
228 # foreach my $in7xx ($inbiblio->field($fieldnumber)) {
229 # # !!!!! ici, il faut reconstruire l'entr� de la table de hachage comme ci dessus
230 # # sinon,
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;
247 # $i++;
249 # my $timeneeded = gettimeofday - $starttime;
250 # print "$i entries done in $timeneeded seconds (".($i/$timeneeded)." per second)\n";