updated MARC21 indexes, with authorites too. v2
[koha.git] / misc / migration_tools / rebuild_nozebra.pl
1 #!/usr/bin/perl
2
3 use C4::Context;
4 use Getopt::Long;
5 use C4::Biblio;
6 use C4::AuthoritiesMarc;
7
8 use strict;
9
10 # script that fills the nozebra table
11 #
12 #
13
14 $|=1; # flushes output
15
16 # limit for database dumping
17 my $limit;# = "LIMIT 100";
18 my $directory;
19 my $skip_export;
20 my $keep_export;
21 my $reset;
22 my $biblios;
23 my $authorities;
24 my $sysprefs;
25 GetOptions(
26         'd:s'      => \$directory,
27         'reset'      => \$reset,
28         's'        => \$skip_export,
29         'k'        => \$keep_export,
30         'b'        => \$biblios,
31         'a'        => \$authorities,
32         's'        => \$sysprefs,  # rebuild 'NoZebraIndexes' syspref
33         );
34
35 $directory = "export" unless $directory;
36 my $dbh=C4::Context->dbh;
37 $dbh->do("update systempreferences set value=1 where variable='NoZebra'");
38
39 $dbh->do("truncate nozebra");
40
41 my %index = GetNoZebraIndexes();
42
43 if  (!%index || $sysprefs ) {
44     if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
45         $dbh->do("UPDATE systempreferences SET value=\"'title' => '200a,200c,200d,200e,225a,225d,225e,225f,225h,225i,225v,500*,501*,503*,510*,512*,513*,514*,515*,516*,517*,518*,519*,520*,530*,531*,532*,540*,541*,545*,604t,610t,605a',
46         'author' =>'200f,600a,601a,604a,700a,700b,700c,700d,700a,701b,701c,701d,702a,702b,702c,702d,710a,710b,710c,710d,711a,711b,711c,711d,712a,712b,712c,712d',
47         'isbn' => '010a',
48         'issn' => '011a',
49         'biblionumber' =>'0909',
50         'itemtype' => '200b',
51         'language' => '101a',
52         'publisher' => '210c',
53         'date' => '210d',
54         'note' => '300a,301a,302a,303a,304a,305a,306az,307a,308a,309a,310a,311a,312a,313a,314a,315a,316a,317a,318a,319a,320a,321a,322a,323a,324a,325a,326a,327a,328a,330a,332a,333a,336a,337a,345a',
55         'Koha-Auth-Number' => '6009,6019,6029,6039,6049,6059,6069,6109,7009,7019,7029,7109,7119,7129',
56         'subject' => '600*,601*,606*,610*',
57         'dewey' => '676a',
58         'host-item' => '995a,995c',\" where variable='NoZebraIndexes'");
59         %index = GetNoZebraIndexes();
60     } elsif (C4::Context->preference('marcflavour') eq 'MARC21') {
61         $dbh->do("UPDATE systempreferences SET value=\"'title' => '130a,210a,222a,240a,243a,245a,245b,246a,246b,247a,247b,250a,250b,440a,830a',
62 'author' => '100a,100b,100c,100d,110a,111a,111b,111c,111d,245c,700a,710a,711a,800a,810a,811a',
63 'isbn' => '020a',
64 'issn' => '022a',
65 'lccn' => '010a',
66 'biblionumber => '999c',
67 'itemtype' => '942c',
68 'publisher' => '260b',
69 'date' => '260c',
70 'note' => '500a, 501a,504a,505a,508a,511a,518a,520a,521a,522a,524a,526a,530a,533a,538a,541a,546a,555a,556a,562a,563a,583a,585a,582a',
71 'subject' => '600*,610*,611*,630*,650*,651*,653*,654*,655*,662*,690*',
72 'dewey' => '082',
73 'bc' => '952p',
74 'callnum' => '952o',
75 'an' => '6009,6109,6119',
76 'host-item' => '952a,952c',\" where variable='NoZebraIndexes'");
77
78         %index = GetNoZebraIndexes();
79     }
80 }
81 $|=1;
82
83 print "***********************************\n";
84 print "***** building BIBLIO indexes *****\n";
85 print "***********************************\n";
86 my $sth;
87 $sth=$dbh->prepare("select biblionumber from biblioitems order by biblionumber $limit");
88 $sth->execute();
89 my $i=0;
90 my %result;
91 while (my ($biblionumber) = $sth->fetchrow) {
92         $i++;
93         print "\r$i";
94         my  $record;
95     eval{
96             $record = GetMarcBiblio($biblionumber);
97     };
98     if($@){
99             print "  There was some pb getting biblionumber : ".$biblionumber."\n";
100             next;
101     }
102     next unless $record;
103     # get title of the record (to store the 10 first letters with the index)
104     my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
105     my $title = lc($record->subfield($titletag,$titlesubfield));
106
107     # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
108     $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
109     # limit to 10 char, should be enough, and limit the DB size
110     $title = substr($title,0,10);
111     #parse each field
112     foreach my $field ($record->fields()) {
113         #parse each subfield
114         next if $field->tag <10;
115         foreach my $subfield ($field->subfields()) {
116             my $tag = $field->tag();
117             my $subfieldcode = $subfield->[0];
118             my $indexed=0;
119             # check each index to see if the subfield is stored somewhere
120             # otherwise, store it in __RAW__ index
121             foreach my $key (keys %index) {
122                 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
123                     $indexed=1;
124                     my $line= lc $subfield->[1];
125                     # remove meaningless value in the field...
126                     $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
127                     # ... and split in words
128                     foreach (split / /,$line) {
129                         next unless $_; # skip  empty values (multiple spaces)
130                         # remove any accented char
131                         # if the entry is already here, improve weight
132                         if ($result{$key}->{"$_"} =~ /$biblionumber,$title\-(\d);/) {
133                             my $weight=$1+1;
134                             $result{$key}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
135                             $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
136                         # otherwise, create it, with weight=1
137                         } else {
138                             $result{$key}->{"$_"}.="$biblionumber,$title-1;";
139                         }
140                     }
141                 }
142             }
143             # the subfield is not indexed, store it in __RAW__ index anyway
144             unless ($indexed) {
145                 my $line= lc $subfield->[1];
146                 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g;
147                 foreach (split / /,$line) {
148                         next unless $_;
149 #                     warn $record->as_formatted."$_ =>".$title;
150                         if ($result{__RAW__}->{"$_"} =~ /$biblionumber,$title\-(\d);/) {
151                             my $weight=$1+1;
152 #                             $weight++;
153                             $result{__RAW__}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
154                             $result{__RAW__}->{"$_"} .= "$biblionumber,$title-$weight;";
155                         } else {
156                             $result{__RAW__}->{"$_"}.="$biblionumber,$title-1;";
157                         }
158                 }
159             }
160         }
161     }
162 }
163 print "\nInserting records...\n";
164 $i=0;
165 my $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('biblioserver',?,?,?)");
166 foreach my $key (keys %result) {
167     foreach my $index (keys %{$result{$key}}) {
168         if (length($result{$key}->{$index}) > 1000000) {
169             print "very long index (".length($result{$key}->{$index}).")for $key / $index. update mySQL config file if you have an error just after this warning (max_paquet_size parameter)\n";
170         }
171         print "\r$i";
172         $i++;
173         $sth->execute($key,$index,$result{$key}->{$index});
174     }
175 }
176 print "\nbiblios done\n";
177
178 print "\n***********************************\n";
179 print "***** building AUTHORITIES indexes *****\n";
180 print "***********************************\n";
181
182 my $sth;
183 $sth=$dbh->prepare("select authid from auth_header order by authid $limit");
184 $sth->execute();
185 my $i=0;
186 my %result;
187 while (my ($authid) = $sth->fetchrow) {
188     $i++;
189     print "\r$i";
190     my $record;
191     eval{
192         $record = GetAuthority($authid);
193     };
194     if($@){
195         print "  There was some pb getting authnumber : ".$authid."\n";
196         next;
197     }
198     
199     my %index;
200     # for authorities, the "title" is the $a mainentry
201     my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield(152,'b'));
202
203     warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
204     my $title = $record->subfield($authref->{auth_tag_to_report},'a');
205     $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
206     $index{'mainentry'}    = $authref->{'auth_tag_to_report'}.'*';
207     $index{'auth_type'}    = '152b';
208
209     # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
210     $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
211     $title = quotemeta $title;
212     # limit to 10 char, should be enough, and limit the DB size
213     $title = substr($title,0,10);
214     #parse each field
215     foreach my $field ($record->fields()) {
216         #parse each subfield
217         next if $field->tag <10;
218         foreach my $subfield ($field->subfields()) {
219             my $tag = $field->tag();
220             my $subfieldcode = $subfield->[0];
221             my $indexed=0;
222             # check each index to see if the subfield is stored somewhere
223             # otherwise, store it in __RAW__ index
224             foreach my $key (keys %index) {
225                 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
226                     $indexed=1;
227                     my $line= lc $subfield->[1];
228                     # remove meaningless value in the field...
229                     $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
230                     # ... and split in words
231                     foreach (split / /,$line) {
232                         next unless $_; # skip  empty values (multiple spaces)
233                         # if the entry is already here, improve weight
234                         if ($result{$key}->{"$_"} =~ /$authid,$title\-(\d);/) {
235                             my $weight=$1+1;
236                             $result{$key}->{"$_"} =~ s/$authid,$title\-(\d);//;
237                             $result{$key}->{"$_"} .= "$authid,$title-$weight;";
238                         # otherwise, create it, with weight=1
239                         } else {
240                             $result{$key}->{"$_"}.="$authid,$title-1;";
241                         }
242                     }
243                 }
244             }
245             # the subfield is not indexed, store it in __RAW__ index anyway
246             unless ($indexed) {
247                 my $line= lc $subfield->[1];
248                 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g;
249                 foreach (split / /,$line) {
250                         next unless $_;
251 #                     warn $record->as_formatted."$_ =>".$title;
252                         if ($result{__RAW__}->{"$_"} =~ /$authid,$title\-(\d);/) {
253                             my $weight=$1+1;
254 #                             $weight++;
255                             $result{__RAW__}->{"$_"} =~ s/$authid,$title\-(\d);//;
256                             $result{__RAW__}->{"$_"} .= "$authid,$title-$weight;";
257                         } else {
258                             $result{__RAW__}->{"$_"}.="$authid,$title-1;";
259                         }
260                 }
261             }
262         }
263     }
264 }
265 print "\nInserting...\n";
266 $i=0;
267 my $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('authorityserver',?,?,?)");
268 foreach my $key (keys %result) {
269     foreach my $index (keys %{$result{$key}}) {
270         if (length($result{$key}->{$index}) > 1000000) {
271             print "very long index (".length($result{$key}->{$index}).")for $key / $index. update mySQL config file if you have an error just after this warning (max_paquet_size parameter)\n";
272         }
273         print "\r$i";
274         $i++;
275         $sth->execute($key,$index,$result{$key}->{$index});
276     }
277 }
278 print "\nauthorities done\n";