Bug 2720 - Overdues which debar automatically should undebar automatically when returned
[koha.git] / misc / migration_tools / buildEDITORS.pl
1 #!/usr/bin/perl
2 # script that rebuild EDITORS
3
4 use strict;
5 #use warnings; FIXME - Bug 2505
6
7 # Koha modules used
8 use MARC::File::USMARC;
9 use MARC::Record;
10 use MARC::Batch;
11 use C4::Context;
12 use C4::Biblio;
13 use C4::AuthoritiesMarc;
14 use Time::HiRes qw(gettimeofday);
15
16 use Getopt::Long;
17 my ( $input_marc_file, $number) = ('',0);
18 my ($version, $verbose, $test_parameter, $confirm,$delete);
19 GetOptions(
20     'h' => \$version,
21     'd' => \$delete,
22     't' => \$test_parameter,
23     'v' => \$verbose,
24     'c' => \$confirm,
25 );
26
27 if ($version or !$confirm) {
28         print <<EOF
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.
31
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
36
37 parameters :
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
41 EOF
42 ;#'
43
44 exit;
45 }
46
47 my $dbh = C4::Context->dbh;
48 if ($delete) {
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'");
53         $sth->execute;
54         while (my ($authid) = $sth->fetchrow) {
55                 $del1->execute($authid);
56                 $del2->execute($authid);
57         }
58         $dbh->do("delete from auth_header where authtypecode='EDITORS'");
59 }
60
61 if ($test_parameter) {
62         print "TESTING MODE ONLY\n    DOING NOTHING\n===============\n";
63 }
64 $|=1; # flushes output
65 my $starttime = gettimeofday;
66 my $sth = $dbh->prepare("select bibid from marc_biblio");
67 $sth->execute;
68 my $i=1;
69 my %alreadydone;
70 my $counter;
71 my %hash;
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');
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);
88         }
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);
104         }
105         $counter++;
106         print ".";
107         my $timeneeded = gettimeofday - $starttime;
108         print "$counter in $timeneeded s\n" unless ($counter % 100);
109         
110         my $field = $record->field('210');
111         my $editor;
112         $editor=$field->subfield('c') if $field;
113         
114         $field = $record->field('225');
115         my $collection;
116         $collection=$field->subfield('a') if $field;
117         
118 #       print "WARNING : editor empty for ".$record->as_formatted unless $editor and !$verbose;
119
120         $hash{$seg1.$seg2}->{editors} = $editor unless ($hash{$seg1.$seg2}->{editors});
121         $hash{$seg1.$seg2}->{collections}->{$collection}++ if $collection;
122 }
123
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."|";
131         }
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');
138
139 #       print $authorityRecord->as_formatted."\n";
140         print "\n" if $verbose;
141 }
142 exit;
143
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);
148 #       }
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($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;
199 #                                                       }
200 #                                                       C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
201 #                                               }
202 #                                       }
203 #                                       if ($fieldnumber eq '711') {
204 #                                               $sthBIBLIOS711->execute($authentry);
205 #                                               while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS711->fetchrow) {
206 #                                                       my $inbiblio = GetMarcBiblio($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;
219 #                                                       }
220 #                                                       C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
221 #                                               }
222 #                                       }
223 #                                       if ($fieldnumber eq '712') {
224 #                                               $sthBIBLIOS712->execute($authentry);
225 #                                               while (my ($bibid,$tag,$tagorder,$subfieldorder) = $sthBIBLIOS712->fetchrow) {
226 #                                                       my $inbiblio = GetMarcBiblio($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;
239 #                                                       }
240 #                                                       C4::Biblio::MARCaddsubfield($dbh,$bibid,$tag,'',$tagorder,9,$subfieldorder,$authid) if $isOK;
241 #                                               }
242 #                                       }
243 #                               }
244 #                       }
245 #               }
246 #       }
247 #       $i++;
248 # }
249 # my $timeneeded = gettimeofday - $starttime;
250 # print "$i entries done in $timeneeded seconds (".($i/$timeneeded)." per second)\n";