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