6 use C4::AuthoritiesMarc;
10 # script that fills the nozebra table
14 $|=1; # flushes output
16 # limit for database dumping
17 my $limit;# = "LIMIT 100";
33 $directory = "export" unless $directory;
34 my $dbh=C4::Context->dbh;
35 $dbh->do("update systempreferences set value=1 where variable='NoZebra'");
37 $dbh->do("truncate nozebra");
39 my %index = GetNoZebraIndexes();
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',
47 'biblionumber' =>'0909',
50 'publisher' => '210c',
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*',
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',
63 'biblionumber => '999c',
65 'publisher' => '260b',
68 'subject' => '600a, 650a',
71 'host-item' => '952a, 952c'");
72 %index = GetNoZebraIndexes();
77 print "***********************************\n";
78 print "***** building BIBLIO indexes *****\n";
79 print "***********************************\n";
81 $sth=$dbh->prepare("select biblionumber from biblioitems order by biblionumber $limit");
85 while (my ($biblionumber) = $sth->fetchrow) {
90 $record = GetMarcBiblio($biblionumber);
93 print " There was some pb getting biblionumber : ".$biblionumber."\n";
96 # get title of the record (to store the 10 first letters with the index)
97 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
98 my $title = lc($record->subfield($titletag,$titlesubfield));
100 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
101 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
102 $title = quotemeta $title;
103 # limit to 10 char, should be enough, and limit the DB size
104 $title = substr($title,0,10);
106 foreach my $field ($record->fields()) {
108 next if $field->tag <10;
109 foreach my $subfield ($field->subfields()) {
110 my $tag = $field->tag();
111 my $subfieldcode = $subfield->[0];
113 # check each index to see if the subfield is stored somewhere
114 # otherwise, store it in __RAW__ index
115 foreach my $key (keys %index) {
116 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
118 my $line= lc $subfield->[1];
119 # remove meaningless value in the field...
120 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
121 # ... and split in words
122 foreach (split / /,$line) {
123 next unless $_; # skip empty values (multiple spaces)
124 # remove any accented char
125 # if the entry is already here, improve weight
126 if ($result{$key}->{"$_"} =~ /$biblionumber,$title\-(\d);/) {
128 $result{$key}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
129 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
130 # otherwise, create it, with weight=1
132 $result{$key}->{"$_"}.="$biblionumber,$title-1;";
137 # the subfield is not indexed, store it in __RAW__ index anyway
139 my $line= lc $subfield->[1];
140 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g;
141 foreach (split / /,$line) {
143 # warn $record->as_formatted."$_ =>".$title;
144 if ($result{__RAW__}->{"$_"} =~ /$biblionumber,$title\-(\d);/) {
147 $result{__RAW__}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
148 $result{__RAW__}->{"$_"} .= "$biblionumber,$title-$weight;";
150 $result{__RAW__}->{"$_"}.="$biblionumber,$title-1;";
157 print "\nInserting records...\n";
159 my $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('biblioserver',?,?,?)");
160 foreach my $key (keys %result) {
161 foreach my $index (keys %{$result{$key}}) {
162 if (length($result{$key}->{$index}) > 1000000) {
163 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";
167 $sth->execute($key,$index,$result{$key}->{$index});
170 print "\nbiblios done\n";
172 print "\n***********************************\n";
173 print "***** building AUTHORITIES indexes *****\n";
174 print "***********************************\n";
177 $sth=$dbh->prepare("select authid from auth_header order by authid $limit");
181 while (my ($authid) = $sth->fetchrow) {
186 $record = GetAuthority($authid);
189 print " There was some pb getting authnumber : ".$authid."\n";
194 # for authorities, the "title" is the $a mainentry
195 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield(152,'b'));
197 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
198 my $title = $record->subfield($authref->{auth_tag_to_report},'a');
199 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
200 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
201 $index{'auth_type'} = '152b';
203 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
204 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
205 $title = quotemeta $title;
206 # limit to 10 char, should be enough, and limit the DB size
207 $title = substr($title,0,10);
209 foreach my $field ($record->fields()) {
211 next if $field->tag <10;
212 foreach my $subfield ($field->subfields()) {
213 my $tag = $field->tag();
214 my $subfieldcode = $subfield->[0];
216 # check each index to see if the subfield is stored somewhere
217 # otherwise, store it in __RAW__ index
218 foreach my $key (keys %index) {
219 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
221 my $line= lc $subfield->[1];
222 # remove meaningless value in the field...
223 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
224 # ... and split in words
225 foreach (split / /,$line) {
226 next unless $_; # skip empty values (multiple spaces)
227 # if the entry is already here, improve weight
228 if ($result{$key}->{"$_"} =~ /$authid,$title\-(\d);/) {
230 $result{$key}->{"$_"} =~ s/$authid,$title\-(\d);//;
231 $result{$key}->{"$_"} .= "$authid,$title-$weight;";
232 # otherwise, create it, with weight=1
234 $result{$key}->{"$_"}.="$authid,$title-1;";
239 # the subfield is not indexed, store it in __RAW__ index anyway
241 my $line= lc $subfield->[1];
242 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g;
243 foreach (split / /,$line) {
245 # warn $record->as_formatted."$_ =>".$title;
246 if ($result{__RAW__}->{"$_"} =~ /$authid,$title\-(\d);/) {
249 $result{__RAW__}->{"$_"} =~ s/$authid,$title\-(\d);//;
250 $result{__RAW__}->{"$_"} .= "$authid,$title-$weight;";
252 $result{__RAW__}->{"$_"}.="$authid,$title-1;";
259 print "\nInserting...\n";
261 my $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('authorityserver',?,?,?)");
262 foreach my $key (keys %result) {
263 foreach my $index (keys %{$result{$key}}) {
264 if (length($result{$key}->{$index}) > 1000000) {
265 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";
269 $sth->execute($key,$index,$result{$key}->{$index});
272 print "\nauthorities done\n";