Koha NoZebra :
[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 $dbh->do("CREATE TABLE `nozebra` (
37                 `server` varchar(20)     NOT NULL,
38                 `indexname` varchar(40)  NOT NULL,
39                 `value` varchar(250)     NOT NULL,
40                 `biblionumbers` longtext NOT NULL,
41                 KEY `indexname` (`server`,`indexname`),
42                 KEY `value` (`server`,`value`))
43                 ENGINE=InnoDB DEFAULT CHARSET=utf8");
44
45 $dbh->do("truncate nozebra");
46
47 my %index = GetNoZebraIndexes();
48
49 unless (%index) {
50     if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
51         $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',
52         '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',
53         'isbn' => '010a',
54         'issn' => '011a',
55         'biblionumber' =>'0909',
56         'itemtype' => '200b',
57         'language' => '101a',
58         'publisher' => '210x',
59         'date' => '210d',
60         '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',
61         'Koha-Auth-Number' => '6009,6019,6029,6039,6049,6059,6069,6109,7009,7019,7029,7109,7119,7129',
62         'subject' => '600*,601*,606*,610*',
63         'dewey' => '676a',
64         'host-item' => '995a,995c',\" where variable='NoZebraIndexes'");
65         %index = GetNoZebraIndexes();
66     } else {
67         # build a MARC21 default index file
68     }
69 }
70 $|=1;
71
72 print "***********************************\n";
73 print "***** building BIBLIO indexes *****\n";
74 print "***********************************\n";
75 my $sth;
76 $sth=$dbh->prepare("select biblionumber from biblioitems order by biblionumber $limit");
77 $sth->execute();
78 my $i=0;
79 my %result;
80 while (my ($biblionumber) = $sth->fetchrow) {
81     $i++;
82     print "\r$i";
83     my $record = GetMarcBiblio($biblionumber);
84
85     # get title of the record (to store the 10 first letters with the index)
86     my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
87     my $title = lc($record->subfield($titletag,$titlesubfield));
88
89     # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
90     $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
91     # limit to 10 char, should be enough, and limit the DB size
92     $title = substr($title,0,10);
93     #parse each field
94     foreach my $field ($record->fields()) {
95         #parse each subfield
96         next if $field->tag <10;
97         foreach my $subfield ($field->subfields()) {
98             my $tag = $field->tag();
99             my $subfieldcode = $subfield->[0];
100             my $indexed=0;
101             # check each index to see if the subfield is stored somewhere
102             # otherwise, store it in __RAW__ index
103             foreach my $key (keys %index) {
104                 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
105                     $indexed=1;
106                     my $line= lc $subfield->[1];
107                     # remove meaningless value in the field...
108                     $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
109                     # ... and split in words
110                     foreach (split / /,$line) {
111                         next unless $_; # skip  empty values (multiple spaces)
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 my $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('biblioserver',?,?,?)");
145 foreach my $key (keys %result) {
146     foreach my $index (keys %{$result{$key}}) {
147         if (length($result{$key}->{$index}) > 1000000) {
148             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";
149         }
150         $sth->execute($key,$index,$result{$key}->{$index});
151     }
152 }
153
154 print "\n***********************************\n";
155 print "***** building AUTHORITIES indexes *****\n";
156 print "***********************************\n";
157
158 my $sth;
159 $sth=$dbh->prepare("select authid from auth_header order by authid $limit");
160 $sth->execute();
161 my $i=0;
162 my %result;
163 while (my ($authid) = $sth->fetchrow) {
164     $i++;
165     print "\r$i";
166     my $record = GetAuthority($authid);
167
168     my %index;
169     # for authorities, the "title" is the $a mainentry
170     my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield(152,'b'));
171     use Data::Dumper;
172 #     warn "for $authid / ".$record->as_formatted. "Dumper : ".Dumper($authref);
173     warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
174     my $title = $record->subfield($authref->{auth_tag_to_report},'a');
175     $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
176     $index{'mainentry'}    = $authref->{'auth_tag_to_report'}.'*';
177     $index{'auth_type'}    = '152b';
178
179     # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
180     $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
181     # limit to 10 char, should be enough, and limit the DB size
182     $title = substr($title,0,10);
183     #parse each field
184     foreach my $field ($record->fields()) {
185         #parse each subfield
186         next if $field->tag <10;
187         foreach my $subfield ($field->subfields()) {
188             my $tag = $field->tag();
189             my $subfieldcode = $subfield->[0];
190             my $indexed=0;
191             # check each index to see if the subfield is stored somewhere
192             # otherwise, store it in __RAW__ index
193             foreach my $key (keys %index) {
194                 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
195                     $indexed=1;
196                     my $line= lc $subfield->[1];
197                     # remove meaningless value in the field...
198                     $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
199                     # ... and split in words
200                     foreach (split / /,$line) {
201                         next unless $_; # skip  empty values (multiple spaces)
202                         # if the entry is already here, improve weight
203                         if ($result{$key}->{$_} =~ /$authid,$title\-(\d);/) {
204                             my $weight=$1+1;
205                             $result{$key}->{$_} =~ s/$authid,$title\-(\d);//;
206                             $result{$key}->{$_} .= "$authid,$title-$weight;";
207                         # otherwise, create it, with weight=1
208                         } else {
209                             $result{$key}->{$_}.="$authid,$title-1;";
210                         }
211                     }
212                 }
213             }
214             # the subfield is not indexed, store it in __RAW__ index anyway
215             unless ($indexed) {
216                 my $line= lc $subfield->[1];
217                 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g;
218                 foreach (split / /,$line) {
219                         next unless $_;
220 #                     warn $record->as_formatted."$_ =>".$title;
221                         if ($result{__RAW__}->{$_} =~ /$authid,$title\-(\d);/) {
222                             my $weight=$1+1;
223 #                             $weight++;
224                             $result{__RAW__}->{$_} =~ s/$authid,$title\-(\d);//;
225                             $result{__RAW__}->{$_} .= "$authid,$title-$weight;";
226                         } else {
227                             $result{__RAW__}->{$_}.="$authid,$title-1;";
228                         }
229                 }
230             }
231         }
232     }
233 }
234 my $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('authorityserver',?,?,?)");
235 foreach my $key (keys %result) {
236     foreach my $index (keys %{$result{$key}}) {
237         if (length($result{$key}->{$index}) > 1000000) {
238             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";
239         }
240         $sth->execute($key,$index,$result{$key}->{$index});
241     }
242 }