Bug 5549 : Refactor fines.pl
[koha_fer] / misc / migration_tools / rebuild_nozebra.pl
index cd55650..b6b82c5 100755 (executable)
@@ -6,6 +6,7 @@ use C4::Biblio;
 use C4::AuthoritiesMarc;
 
 use strict;
+#use warnings; FIXME - Bug 2505
 # 
 # script that fills the nozebra table
 #
@@ -16,22 +17,59 @@ $|=1; # flushes output
 # limit for database dumping
 my $limit;# = "LIMIT 100";
 my $directory;
-my $skip_export;
-my $keep_export;
-my $reset;
-my $biblios;
+#my $skip_export;
+#my $keep_export;
+#my $reset;
+#my $biblios;
 my $authorities;
 my $sysprefs;
-GetOptions(
+my $commit;
+my $want_help;
+
+my $result = GetOptions(
     'd:s'      => \$directory,
-    'reset'      => \$reset,
-    's'        => \$skip_export,
-    'k'        => \$keep_export,
-    'b'        => \$biblios,
-    'a'        => \$authorities,
+#    'reset'      => \$reset,
+#    's'        => \$skip_export,    # Not used and conflicts with 's' option some lines below for sysprefs!!!
+#    'k'        => \$keep_export,
+#    'b'        => \$biblios,
+#    'a'        => \$authorities,
     's'        => \$sysprefs,  # rebuild 'NoZebraIndexes' syspref
+    'h|help'        => \$want_help,
+   'commit:f'    => \$commit,
     );
 
+if (not $result or $want_help) {
+    print_usage();
+    exit 0;
+}
+
+
+sub print_usage {
+    print <<_USAGE_;
+$0: reindex MARC bibs and authorities if NOT using Zebra ("NoZebra").
+
+Use this batch job to reindex all biblio and authority
+records in your Koha database.  This job is useful
+only if you are NOT using Zebra ('NoZebra'); if you are 
+using the 'Zebra'mode, this job should NOT be used.
+
+Parameters:
+    -d                      Temporary directory for indexing.
+                            If not specified, one is automatically
+                            created.  The export directory
+                            is automatically deleted unless
+                            you supply the -k switch.
+
+    -s                      Rebuild "NoZebraIndexes" System Preference 
+
+    --help or -h            show this message.
+_USAGE_
+}   # END of print_usage sub
+
+
+my $commitnum = 1000; 
+$commitnum = $commit if ($commit) ;
+
 $directory = "export" unless $directory;
 my $dbh=C4::Context->dbh;
 $dbh->do("update systempreferences set value=1 where variable='NoZebra'");
@@ -74,7 +112,10 @@ if  (!%index || $sysprefs ) {
 'bc' => '952p',
 'callnum' => '952o',
 'an' => '6009,6109,6119',
-'host-item' => '952a,952c',
+'series' => 440*,490*,
+'host-item' => '9529
+'shelf' => '952c',
+'collection' => '9528',
 \"WHERE variable='NoZebraIndexes'");
 
         %index = GetNoZebraIndexes();
@@ -82,9 +123,12 @@ if  (!%index || $sysprefs ) {
 }
 $|=1;
 
+$dbh->{AutoCommit} = 0;
+
 print "***********************************\n";
 print "***** building BIBLIO indexes *****\n";
 print "***********************************\n";
+
 my $sth;
 $sth=$dbh->prepare("select biblionumber from biblioitems order by biblionumber $limit");
 $sth->execute();
@@ -103,11 +147,11 @@ while (my ($biblionumber) = $sth->fetchrow) {
     }
     next unless $record;
     # get title of the record (to store the 10 first letters with the index)
-    my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
+    my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title', '');
     my $title = lc($record->subfield($titletag,$titlesubfield));
 
     # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
-    $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
+    $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|=|://g;
     # limit to 10 char, should be enough, and limit the DB size
     $title = substr($title,0,10);
     #parse each field
@@ -121,7 +165,7 @@ while (my ($biblionumber) = $sth->fetchrow) {
             # check each index to see if the subfield is stored somewhere
             # otherwise, store it in __RAW__ index
             foreach my $key (keys %index) {
-                if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
+                if ($index{$key} =~ /\Q$tag\E\*/ or $index{$key} =~ /\Q$tag$subfieldcode\E/) {
                     $indexed=1;
                     my $line= lc $subfield->[1];
                     # remove meaningless value in the field...
@@ -162,8 +206,14 @@ while (my ($biblionumber) = $sth->fetchrow) {
         }
     }
 }
+
+
 print "\nInserting records...\n";
 $i=0;
+
+my $commitnum = 100;
+$dbh->{AutoCommit} = 0;
+
 $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('biblioserver',?,?,?)");
 foreach my $key (keys %result) {
     foreach my $index (keys %{$result{$key}}) {
@@ -173,8 +223,13 @@ foreach my $key (keys %result) {
         print "\r$i";
         $i++;
         $sth->execute($key,$index,$result{$key}->{$index});
+        $dbh->commit() if (0 == $i % $commitnum);
     }
+   $dbh->commit() if (0 == $i % $commitnum);
 }
+$dbh->commit;
+
+
 print "\nbiblios done\n";
 
 print "\n***********************************\n";
@@ -208,7 +263,7 @@ while (my ($authid) = $sth->fetchrow) {
     $index{'auth_type'}    = '152b';
 
     # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
-    $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
+    $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=//g;
     $title = quotemeta $title;
     # limit to 10 char, should be enough, and limit the DB size
     $title = substr($title,0,10);
@@ -263,8 +318,14 @@ while (my ($authid) = $sth->fetchrow) {
         }
     }
 }
+
+
+
 print "\nInserting...\n";
 $i=0;
+
+my $commitnum = 100;
+$dbh->{AutoCommit} = 0;
 $sth = $dbh->prepare("INSERT INTO nozebra (server,indexname,value,biblionumbers) VALUES ('authorityserver',?,?,?)");
 foreach my $key (keys %result) {
     foreach my $index (keys %{$result{$key}}) {
@@ -274,6 +335,9 @@ foreach my $key (keys %result) {
         print "\r$i";
         $i++;
         $sth->execute($key,$index,$result{$key}->{$index});
+        $dbh->commit() if (0 == $i % $commitnum);
     }
+   $dbh->commit() if (0 == $i % $commitnum);
 }
+$dbh->commit;
 print "\nauthorities done\n";