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";
28 my $result = GetOptions
(
31 # 's' => \$skip_export, # Not used and conflicts with 's' option some lines below for sysprefs!!!
32 # 'k' => \$keep_export,
34 # 'a' => \$authorities,
35 's' => \
$sysprefs, # rebuild 'NoZebraIndexes' syspref
36 'h|help' => \
$want_help,
37 'commit:f' => \
$commit,
40 if (not $result or $want_help) {
48 $0: reindex MARC bibs
and authorities
if NOT using Zebra
("NoZebra").
50 Use this batch job to reindex all biblio
and authority
51 records
in your Koha database
. This job is useful
52 only
if you are NOT using Zebra
('NoZebra'); if you are
53 using the
'Zebra'mode
, this job should NOT be used
.
56 -d Temporary directory
for indexing
.
57 If
not specified
, one is automatically
58 created
. The export directory
59 is automatically deleted
unless
60 you supply the
-k switch
.
62 -s Rebuild
"NoZebraIndexes" System Preference
64 --help
or -h show this message
.
66 } # END of print_usage sub
70 $commitnum = $commit if ($commit) ;
72 $directory = "export" unless $directory;
73 my $dbh=C4
::Context
->dbh;
74 $dbh->do("update systempreferences set value=1 where variable='NoZebra'");
76 $dbh->do("truncate nozebra");
78 my %index = GetNoZebraIndexes
();
80 if (!%index || $sysprefs ) {
81 if (C4
::Context
->preference('marcflavour') eq 'UNIMARC') {
82 $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',
83 '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',
86 'biblionumber' =>'0909',
89 'publisher' => '210c',
91 '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',
92 'Koha-Auth-Number' => '6009,6019,6029,6039,6049,6059,6069,6109,7009,7019,7029,7109,7119,7129',
93 'subject' => '600*,601*,606*,610*',
95 'host-item' => '995a,995c',\" where variable='NoZebraIndexes'");
96 %index = GetNoZebraIndexes
();
97 } elsif (C4
::Context
->preference('marcflavour') eq 'MARC21') {
98 $dbh->do("UPDATE systempreferences SET value=\"
99 'title' => '130a,210a,222a,240a,243a,245a,245b,246a,246b,247a,247b,250a,250b,440a,830a',
100 'author' => '100a,100b,100c,100d,110a,111a,111b,111c,111d,245c,700a,710a,711a,800a,810a,811a',
104 'biblionumber => '999c',
105 'itemtype' => '942c',
106 'publisher' => '260b',
108 'note' => '500a, 501a,504a,505a,508a,511a,518a,520a,521a,522a,524a,526a,530a,533a,538a,541a,546a,555a,556a,562a,563a,583a,585a,582a',
109 'subject' => '600*,610*,611*,630*,650*,651*,653*,654*,655*,662*,690*',
113 'an' => '6009,6109,6119',
114 'series' => 440*,490*,
117 'collection' => '9528',
118 \"WHERE variable='NoZebraIndexes'");
120 %index = GetNoZebraIndexes
();
125 $dbh->{AutoCommit
} = 0;
127 print "***********************************\n";
128 print "***** building BIBLIO indexes *****\n";
129 print "***********************************\n";
132 $sth=$dbh->prepare("select biblionumber from biblioitems order by biblionumber $limit");
136 while (my ($biblionumber) = $sth->fetchrow) {
141 $record = GetMarcBiblio
($biblionumber);
144 print " There was some pb getting biblionumber : ".$biblionumber."\n";
148 # get title of the record (to store the 10 first letters with the index)
149 my ($titletag,$titlesubfield) = GetMarcFromKohaField
('biblio.title', '');
150 my $title = lc($record->subfield($titletag,$titlesubfield));
152 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
153 $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|=|://g;
154 # limit to 10 char, should be enough, and limit the DB size
155 $title = substr($title,0,10);
157 foreach my $field ($record->fields()) {
159 next if $field->tag <10;
160 foreach my $subfield ($field->subfields()) {
161 my $tag = $field->tag();
162 my $subfieldcode = $subfield->[0];
164 # check each index to see if the subfield is stored somewhere
165 # otherwise, store it in __RAW__ index
166 foreach my $key (keys %index) {
167 if ($index{$key} =~ /\Q$tag\E\*/ or $index{$key} =~ /\Q$tag$subfieldcode\E/) {
169 my $line= lc $subfield->[1];
170 # remove meaningless value in the field...
171 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g
;
172 # ... and split in words
173 foreach (split / /,$line) {
174 next unless $_; # skip empty values (multiple spaces)
175 # remove any accented char
176 # if the entry is already here, improve weight
177 if ($result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d);/) {
179 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d);//;
180 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
181 # otherwise, create it, with weight=1
183 $result{$key}->{"$_"}.="$biblionumber,$title-1;";
188 # the subfield is not indexed, store it in __RAW__ index anyway
190 my $line= lc $subfield->[1];
191 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g
;
192 foreach (split / /,$line) {
194 # warn $record->as_formatted."$_ =>".$title;
195 if ($result{__RAW__
}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d);/) {
198 $result{__RAW__
}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d);//;
199 $result{__RAW__
}->{"$_"} .= "$biblionumber,$title-$weight;";
201 $result{__RAW__
}->{"$_"}.="$biblionumber,$title-1;";
210 print "\nInserting records...\n";
214 $dbh->{AutoCommit
} = 0;
216 $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('biblioserver',?,?,?)");
217 foreach my $key (keys %result) {
218 foreach my $index (keys %{$result{$key}}) {
219 if (length($result{$key}->{$index}) > 1000000) {
220 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";
224 $sth->execute($key,$index,$result{$key}->{$index});
225 $dbh->commit() if (0 == $i % $commitnum);
227 $dbh->commit() if (0 == $i % $commitnum);
232 print "\nbiblios done\n";
234 print "\n***********************************\n";
235 print "***** building AUTHORITIES indexes *****\n";
236 print "***********************************\n";
238 $sth=$dbh->prepare("select authid from auth_header order by authid $limit");
242 while (my ($authid) = $sth->fetchrow) {
247 $record = GetAuthority
($authid);
250 print " There was some pb getting authnumber : ".$authid."\n";
255 # for authorities, the "title" is the $a mainentry
256 my $authref = C4
::AuthoritiesMarc
::GetAuthType
(C4
::AuthoritiesMarc
::GetAuthTypeCode
($authid));
258 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
259 my $title = $record->subfield($authref->{auth_tag_to_report
},'a');
260 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
261 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
262 $index{'auth_type'} = '152b';
264 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
265 $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=//g;
266 $title = quotemeta $title;
267 # limit to 10 char, should be enough, and limit the DB size
268 $title = substr($title,0,10);
270 foreach my $field ($record->fields()) {
272 next if $field->tag <10;
273 foreach my $subfield ($field->subfields()) {
274 my $tag = $field->tag();
275 my $subfieldcode = $subfield->[0];
277 # check each index to see if the subfield is stored somewhere
278 # otherwise, store it in __RAW__ index
279 foreach my $key (keys %index) {
280 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
282 my $line= lc $subfield->[1];
283 # remove meaningless value in the field...
284 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g
;
285 # ... and split in words
286 foreach (split / /,$line) {
287 next unless $_; # skip empty values (multiple spaces)
288 # if the entry is already here, improve weight
289 if ($result{$key}->{"$_"} =~ /$authid,$title\-(\d);/) {
291 $result{$key}->{"$_"} =~ s/$authid,$title\-(\d);//;
292 $result{$key}->{"$_"} .= "$authid,$title-$weight;";
293 # otherwise, create it, with weight=1
295 $result{$key}->{"$_"}.="$authid,$title-1;";
300 # the subfield is not indexed, store it in __RAW__ index anyway
302 my $line= lc $subfield->[1];
303 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g
;
304 foreach (split / /,$line) {
306 # warn $record->as_formatted."$_ =>".$title;
307 if ($result{__RAW__
}->{"$_"} =~ /$authid,$title\-(\d);/) {
310 $result{__RAW__
}->{"$_"} =~ s/$authid,$title\-(\d);//;
311 $result{__RAW__
}->{"$_"} .= "$authid,$title-$weight;";
313 $result{__RAW__
}->{"$_"}.="$authid,$title-1;";
323 print "\nInserting...\n";
327 $dbh->{AutoCommit
} = 0;
328 $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('authorityserver',?,?,?)");
329 foreach my $key (keys %result) {
330 foreach my $index (keys %{$result{$key}}) {
331 if (length($result{$key}->{$index}) > 1000000) {
332 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";
336 $sth->execute($key,$index,$result{$key}->{$index});
337 $dbh->commit() if (0 == $i % $commitnum);
339 $dbh->commit() if (0 == $i % $commitnum);
342 print "\nauthorities done\n";