added 440* and 490* 'series' indexes
[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=\"
62 'title' => '130a,210a,222a,240a,243a,245a,245b,246a,246b,247a,247b,250a,250b,440a,830a',
63 'author' => '100a,100b,100c,100d,110a,111a,111b,111c,111d,245c,700a,710a,711a,800a,810a,811a',
64 'isbn' => '020a',
65 'issn' => '022a',
66 'lccn' => '010a',
67 'biblionumber => '999c',
68 'itemtype' => '942c',
69 'publisher' => '260b',
70 'date' => '260c',
71 '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',
72 'subject' => '600*,610*,611*,630*,650*,651*,653*,654*,655*,662*,690*',
73 'dewey' => '082',
74 'bc' => '952p',
75 'callnum' => '952o',
76 'an' => '6009,6109,6119',
77 'series' => 440*,490*,
78 'host-item' => '952a,952c',
79 \"WHERE variable='NoZebraIndexes'");
80
81         %index = GetNoZebraIndexes();
82     }
83 }
84 $|=1;
85
86 print "***********************************\n";
87 print "***** building BIBLIO indexes *****\n";
88 print "***********************************\n";
89 my $sth;
90 $sth=$dbh->prepare("select biblionumber from biblioitems order by biblionumber $limit");
91 $sth->execute();
92 my $i=0;
93 my %result;
94 while (my ($biblionumber) = $sth->fetchrow) {
95         $i++;
96         print "\r$i";
97         my  $record;
98     eval{
99             $record = GetMarcBiblio($biblionumber);
100     };
101     if($@){
102             print "  There was some pb getting biblionumber : ".$biblionumber."\n";
103             next;
104     }
105     next unless $record;
106     # get title of the record (to store the 10 first letters with the index)
107     my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
108     my $title = lc($record->subfield($titletag,$titlesubfield));
109
110     # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
111     $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
112     # limit to 10 char, should be enough, and limit the DB size
113     $title = substr($title,0,10);
114     #parse each field
115     foreach my $field ($record->fields()) {
116         #parse each subfield
117         next if $field->tag <10;
118         foreach my $subfield ($field->subfields()) {
119             my $tag = $field->tag();
120             my $subfieldcode = $subfield->[0];
121             my $indexed=0;
122             # check each index to see if the subfield is stored somewhere
123             # otherwise, store it in __RAW__ index
124             foreach my $key (keys %index) {
125                 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
126                     $indexed=1;
127                     my $line= lc $subfield->[1];
128                     # remove meaningless value in the field...
129                     $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
130                     # ... and split in words
131                     foreach (split / /,$line) {
132                         next unless $_; # skip  empty values (multiple spaces)
133                         # remove any accented char
134                         # if the entry is already here, improve weight
135                         if ($result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d);/) {
136                             my $weight=$1+1;
137                             $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d);//;
138                             $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
139                         # otherwise, create it, with weight=1
140                         } else {
141                             $result{$key}->{"$_"}.="$biblionumber,$title-1;";
142                         }
143                     }
144                 }
145             }
146             # the subfield is not indexed, store it in __RAW__ index anyway
147             unless ($indexed) {
148                 my $line= lc $subfield->[1];
149                 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g;
150                 foreach (split / /,$line) {
151                         next unless $_;
152 #                     warn $record->as_formatted."$_ =>".$title;
153                         if ($result{__RAW__}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d);/) {
154                             my $weight=$1+1;
155 #                             $weight++;
156                             $result{__RAW__}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d);//;
157                             $result{__RAW__}->{"$_"} .= "$biblionumber,$title-$weight;";
158                         } else {
159                             $result{__RAW__}->{"$_"}.="$biblionumber,$title-1;";
160                         }
161                 }
162             }
163         }
164     }
165 }
166 print "\nInserting records...\n";
167 $i=0;
168 $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('biblioserver',?,?,?)");
169 foreach my $key (keys %result) {
170     foreach my $index (keys %{$result{$key}}) {
171         if (length($result{$key}->{$index}) > 1000000) {
172             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";
173         }
174         print "\r$i";
175         $i++;
176         $sth->execute($key,$index,$result{$key}->{$index});
177     }
178 }
179 print "\nbiblios done\n";
180
181 print "\n***********************************\n";
182 print "***** building AUTHORITIES indexes *****\n";
183 print "***********************************\n";
184
185 $sth=$dbh->prepare("select authid from auth_header order by authid $limit");
186 $sth->execute();
187 $i=0;
188 %result = ();
189 while (my ($authid) = $sth->fetchrow) {
190     $i++;
191     print "\r$i";
192     my $record;
193     eval{
194         $record = GetAuthority($authid);
195     };
196     if($@){
197         print "  There was some pb getting authnumber : ".$authid."\n";
198         next;
199     }
200     
201     my %index;
202     # for authorities, the "title" is the $a mainentry
203     my $authref = C4::AuthoritiesMarc::GetAuthType(C4::AuthoritiesMarc::GetAuthTypeCode($authid));
204
205     warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
206     my $title = $record->subfield($authref->{auth_tag_to_report},'a');
207     $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
208     $index{'mainentry'}    = $authref->{'auth_tag_to_report'}.'*';
209     $index{'auth_type'}    = '152b';
210
211     # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
212     $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
213     $title = quotemeta $title;
214     # limit to 10 char, should be enough, and limit the DB size
215     $title = substr($title,0,10);
216     #parse each field
217     foreach my $field ($record->fields()) {
218         #parse each subfield
219         next if $field->tag <10;
220         foreach my $subfield ($field->subfields()) {
221             my $tag = $field->tag();
222             my $subfieldcode = $subfield->[0];
223             my $indexed=0;
224             # check each index to see if the subfield is stored somewhere
225             # otherwise, store it in __RAW__ index
226             foreach my $key (keys %index) {
227                 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
228                     $indexed=1;
229                     my $line= lc $subfield->[1];
230                     # remove meaningless value in the field...
231                     $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
232                     # ... and split in words
233                     foreach (split / /,$line) {
234                         next unless $_; # skip  empty values (multiple spaces)
235                         # if the entry is already here, improve weight
236                         if ($result{$key}->{"$_"} =~ /$authid,$title\-(\d);/) {
237                             my $weight=$1+1;
238                             $result{$key}->{"$_"} =~ s/$authid,$title\-(\d);//;
239                             $result{$key}->{"$_"} .= "$authid,$title-$weight;";
240                         # otherwise, create it, with weight=1
241                         } else {
242                             $result{$key}->{"$_"}.="$authid,$title-1;";
243                         }
244                     }
245                 }
246             }
247             # the subfield is not indexed, store it in __RAW__ index anyway
248             unless ($indexed) {
249                 my $line= lc $subfield->[1];
250                 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g;
251                 foreach (split / /,$line) {
252                         next unless $_;
253 #                     warn $record->as_formatted."$_ =>".$title;
254                         if ($result{__RAW__}->{"$_"} =~ /$authid,$title\-(\d);/) {
255                             my $weight=$1+1;
256 #                             $weight++;
257                             $result{__RAW__}->{"$_"} =~ s/$authid,$title\-(\d);//;
258                             $result{__RAW__}->{"$_"} .= "$authid,$title-$weight;";
259                         } else {
260                             $result{__RAW__}->{"$_"}.="$authid,$title-1;";
261                         }
262                 }
263             }
264         }
265     }
266 }
267 print "\nInserting...\n";
268 $i=0;
269 $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('authorityserver',?,?,?)");
270 foreach my $key (keys %result) {
271     foreach my $index (keys %{$result{$key}}) {
272         if (length($result{$key}->{$index}) > 1000000) {
273             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";
274         }
275         print "\r$i";
276         $i++;
277         $sth->execute($key,$index,$result{$key}->{$index});
278     }
279 }
280 print "\nauthorities done\n";