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