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