various bugfixes on parameters modules + adding default NoZebraIndexes systempreferen...
[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                 `indexname` varchar(40) character set latin1 NOT NULL,
38                 `value` varchar(250) character set latin1 NOT NULL,
39                 `biblionumbers` longtext character set latin1 NOT NULL,
40                 KEY `indexname` (`indexname`),
41                 KEY `value` (`value`))
42                 ENGINE=InnoDB DEFAULT CHARSET=utf8");
43 $dbh->do("truncate nozebra");
44 my $sth;
45 $sth=$dbh->prepare("select biblionumber from biblioitems order by biblionumber $limit");
46 $sth->execute();
47 my $i=0;
48 my %result;
49
50 my %index = GetNoZebraIndexes();
51
52 unless (%index) {
53     if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
54         $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',
55         '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',
56         'isbn' => '010a',
57         'issn' => '011a',
58         'biblionumber' =>'0909',
59         'itemtype' => '200b',
60         'language' => '010a',
61         'publisher' => '210x',
62         'date' => '210d',
63         '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',
64         'Koha-Auth-Number' => '6009,6019,6029,6039,6049,6059,6069,6109',
65         'subject' => '600*,601*,606*,610*',
66         'dewey' => '676a',
67         'host-item' => '995a,995c',\" where variable='NoZebraIndexes'");
68         %index = GetNoZebraIndexes();
69     } else {
70         # build a MARC21 default index file
71     }
72 }
73 $|=1;
74 while (my ($biblionumber) = $sth->fetchrow) {
75     $i++;
76     print "\r$i";
77     my $record = GetMarcBiblio($biblionumber);
78
79     # get title of the record (to store the 10 first letters with the index)
80     my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
81     my $title = lc($record->subfield($titletag,$titlesubfield));
82
83     # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
84     $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
85     # limit to 10 char, should be enough, and limit the DB size
86     $title = substr($title,0,10);
87     #parse each field
88     foreach my $field ($record->fields()) {
89         #parse each subfield
90         next if $field->tag <10;
91         foreach my $subfield ($field->subfields()) {
92             my $tag = $field->tag();
93             my $subfieldcode = $subfield->[0];
94             my $indexed=0;
95             # check each index to see if the subfield is stored somewhere
96             # otherwise, store it in __RAW__ index
97             foreach my $key (keys %index) {
98                 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
99                     $indexed=1;
100                     my $line= lc $subfield->[1];
101                     # remove meaningless value in the field...
102                     $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g;
103                     # ... and split in words
104                     foreach (split / /,$line) {
105                         next unless $_; # skip  empty values (multiple spaces)
106                         # if the entry is already here, improve weight
107                         if ($result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) {
108                             my $weight=$1+1;
109                             $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
110                             $result{$key}->{$_} .= "$biblionumber,$title-$weight;";
111                         # otherwise, create it, with weight=1
112                         } else {
113                             $result{$key}->{$_}.="$biblionumber,$title-1;";
114                         }
115                     }
116                 }
117             }
118             # the subfield is not indexed, store it in __RAW__ index anyway
119             unless ($indexed) {
120                 my $line= lc $subfield->[1];
121                 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=/ /g;
122                 foreach (split / /,$line) {
123                         next unless $_;
124 #                     warn $record->as_formatted."$_ =>".$title;
125                         if ($result{__RAW__}->{$_} =~ /$biblionumber,$title\-(\d);/) {
126                             my $weight=$1+1;
127 #                             $weight++;
128                             $result{__RAW__}->{$_} =~ s/$biblionumber,$title\-(\d);//;
129                             $result{__RAW__}->{$_} .= "$biblionumber,$title-$weight;";
130                         } else {
131                             $result{__RAW__}->{$_}.="$biblionumber,$title-1;";
132                         }
133                 }
134             }
135         }
136     }
137 }
138 my $sth = $dbh->prepare("INSERT INTO nozebra (indexname,value,biblionumbers) VALUES (?,?,?)");
139 foreach my $key (keys %result) {
140     foreach my $index (keys %{$result{$key}}) {
141         if (length($result{$key}->{$index}) > 1000000) {
142             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";
143         }
144         $sth->execute($key,$index,$result{$key}->{$index});
145     }
146 }