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