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'");
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");
45 $dbh->do("truncate nozebra");
47 my %index = GetNoZebraIndexes();
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',
55 'biblionumber' =>'0909',
58 'publisher' => '210x',
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*',
64 'host-item' => '995a,995c',\" where variable='NoZebraIndexes'");
65 %index = GetNoZebraIndexes();
67 # build a MARC21 default index file
72 print "***********************************\n";
73 print "***** building BIBLIO indexes *****\n";
74 print "***********************************\n";
76 $sth=$dbh->prepare("select biblionumber from biblioitems order by biblionumber $limit");
80 while (my ($biblionumber) = $sth->fetchrow) {
85 $record = GetMarcBiblio($biblionumber);
88 print " There was some pb getting biblionumber : ".$biblionumber."\n";
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));
95 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
96 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
97 # limit to 10 char, should be enough, and limit the DB size
98 $title = substr($title,0,10);
100 foreach my $field ($record->fields()) {
102 next if $field->tag <10;
103 foreach my $subfield ($field->subfields()) {
104 my $tag = $field->tag();
105 my $subfieldcode = $subfield->[0];
107 # check each index to see if the subfield is stored somewhere
108 # otherwise, store it in __RAW__ index
109 foreach my $key (keys %index) {
110 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
112 my $line= lc $subfield->[1];
113 # remove meaningless value in the field...
114 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
115 # ... and split in words
116 foreach (split / /,$line) {
117 next unless $_; # skip empty values (multiple spaces)
118 # if the entry is already here, improve weight
119 if ($result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) {
121 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
122 $result{$key}->{$_} .= "$biblionumber,$title-$weight;";
123 # otherwise, create it, with weight=1
125 $result{$key}->{$_}.="$biblionumber,$title-1;";
130 # the subfield is not indexed, store it in __RAW__ index anyway
132 my $line= lc $subfield->[1];
133 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g;
134 foreach (split / /,$line) {
136 # warn $record->as_formatted."$_ =>".$title;
137 if ($result{__RAW__}->{$_} =~ /$biblionumber,$title\-(\d);/) {
140 $result{__RAW__}->{$_} =~ s/$biblionumber,$title\-(\d);//;
141 $result{__RAW__}->{$_} .= "$biblionumber,$title-$weight;";
143 $result{__RAW__}->{$_}.="$biblionumber,$title-1;";
150 my $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('biblioserver',?,?,?)");
151 foreach my $key (keys %result) {
152 foreach my $index (keys %{$result{$key}}) {
153 if (length($result{$key}->{$index}) > 1000000) {
154 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";
156 $sth->execute($key,$index,$result{$key}->{$index});
160 print "\n***********************************\n";
161 print "***** building AUTHORITIES indexes *****\n";
162 print "***********************************\n";
165 $sth=$dbh->prepare("select authid from auth_header order by authid $limit");
169 while (my ($authid) = $sth->fetchrow) {
172 my $record = GetAuthority($authid);
175 # for authorities, the "title" is the $a mainentry
176 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield(152,'b'));
178 # warn "for $authid / ".$record->as_formatted. "Dumper : ".Dumper($authref);
179 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
180 my $title = $record->subfield($authref->{auth_tag_to_report},'a');
181 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
182 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
183 $index{'auth_type'} = '152b';
185 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
186 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
187 # limit to 10 char, should be enough, and limit the DB size
188 $title = substr($title,0,10);
190 foreach my $field ($record->fields()) {
192 next if $field->tag <10;
193 foreach my $subfield ($field->subfields()) {
194 my $tag = $field->tag();
195 my $subfieldcode = $subfield->[0];
197 # check each index to see if the subfield is stored somewhere
198 # otherwise, store it in __RAW__ index
199 foreach my $key (keys %index) {
200 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
202 my $line= lc $subfield->[1];
203 # remove meaningless value in the field...
204 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
205 # ... and split in words
206 foreach (split / /,$line) {
207 next unless $_; # skip empty values (multiple spaces)
208 # if the entry is already here, improve weight
209 if ($result{$key}->{$_} =~ /$authid,$title\-(\d);/) {
211 $result{$key}->{$_} =~ s/$authid,$title\-(\d);//;
212 $result{$key}->{$_} .= "$authid,$title-$weight;";
213 # otherwise, create it, with weight=1
215 $result{$key}->{$_}.="$authid,$title-1;";
220 # the subfield is not indexed, store it in __RAW__ index anyway
222 my $line= lc $subfield->[1];
223 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g;
224 foreach (split / /,$line) {
226 # warn $record->as_formatted."$_ =>".$title;
227 if ($result{__RAW__}->{$_} =~ /$authid,$title\-(\d);/) {
230 $result{__RAW__}->{$_} =~ s/$authid,$title\-(\d);//;
231 $result{__RAW__}->{$_} .= "$authid,$title-$weight;";
233 $result{__RAW__}->{$_}.="$authid,$title-1;";
240 my $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('authorityserver',?,?,?)");
241 foreach my $key (keys %result) {
242 foreach my $index (keys %{$result{$key}}) {
243 if (length($result{$key}->{$index}) > 1000000) {
244 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";
246 $sth->execute($key,$index,$result{$key}->{$index});