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