Bug 18098: Add an index with the count of not onloan items
[koha.git] / misc / migration_tools / bulkmarcimport.pl
index fc5ab84..dc2cae4 100755 (executable)
@@ -31,6 +31,9 @@ use Getopt::Long;
 use IO::File;
 use Pod::Usage;
 
+use Koha::SearchEngine;
+use Koha::SearchEngine::Search;
+
 use open qw( :std :encoding(UTF-8) );
 binmode( STDOUT, ":encoding(UTF-8)" );
 my ( $input_marc_file, $number, $offset) = ('',0,0);
@@ -81,6 +84,8 @@ $biblios ||= !$authorities;
 $insert  ||= !$update;
 my $writemode = ($append) ? "a" : "w";
 
+pod2usage( -msg => "\nYou must specify either --biblios or --authorities, not both.\n", -exitval ) if $biblios && $authorities;
+
 if ($all) {
     $insert = 1;
     $update = 1;
@@ -121,16 +126,18 @@ if ((not defined $sourcesubfield) && (not defined $sourcetag)){
   $sourcesubfield="a";
 }
 
-#Disable the syspref cache or else the following UPDATE statements will have no effect
-C4::Context->disable_syspref_cache();
 
-# save the CataloguingLog and AuthoritiesLog properties : we don't want to log a bulkmarcimport. It will slow the import &
-# will create problems in the action_logs table, that can't handle more than 1 entry per second per user.
-my $CataloguingLog = C4::Context->preference('CataloguingLog');
-$dbh->do("UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'");
+# Disable logging for the biblios and authorities import operation. It would unnecessarily
+# slow the import
 
-my $AuthoritiesLog = C4::Context->preference('AuthoritiesLog');
-$dbh->do("UPDATE systempreferences SET value=0 WHERE variable='AuthoritiesLog'");
+# Disable the syspref cache so we can change logging settings
+C4::Context->disable_syspref_cache();
+# Save current CataloguingLog and AuthoritiesLog sysprefs values
+my $CataloguingLog = C4::Context->preference( 'CataloguingLog' );
+my $AuthoritiesLog = C4::Context->preference( 'AuthoritiesLog' );
+# Disable logging for both
+C4::Context->set_preference( 'CataloguingLog', 0 );
+C4::Context->set_preference( 'AuthoritiesLog', 0 );
 
 if ($fk_off) {
        $dbh->do("SET FOREIGN_KEY_CHECKS = 0");
@@ -213,6 +220,17 @@ if ($logfile){
    $loghandle= IO::File->new($logfile, $writemode) ;
    print $loghandle "id;operation;status\n";
 }
+
+my $searcher = Koha::SearchEngine::Search->new(
+    {
+        index => (
+              $authorities
+            ? $Koha::SearchEngine::AUTHORITIES_INDEX
+            : $Koha::SearchEngine::BIBLIOS_INDEX
+        )
+    }
+);
+
 RECORD: while (  ) {
     my $record;
     # get records
@@ -266,8 +284,13 @@ RECORD: while (  ) {
         my $query = build_query( $match, $record );
         my $server = ( $authorities ? 'authorityserver' : 'biblioserver' );
         $debug && warn $query;
-        my ( $error, $results, $totalhits ) = C4::Search::SimpleSearch( $query, 0, 3, [$server] );
-        die "unable to search the database for duplicates : $error" if ( defined $error );
+        my ( $error, $results, $totalhits ) = $searcher->simple_search_compat( $query, 0, 3, [$server] );
+        # changed to warn so able to continue with one broken record
+        if ( defined $error ) {
+            warn "unable to search the database for duplicates : $error";
+            printlog( { id => $id || $originalid || $match, op => "match", status => "ERROR" } ) if ($logfile);
+            next RECORD;
+        }
         $debug && warn "$query $server : $totalhits";
         if ( $results && scalar(@$results) == 1 ) {
             my $marcrecord = C4::Search::new_record_from_zebra( $server, $results->[0] );
@@ -396,7 +419,7 @@ RECORD: while (  ) {
             if ($biblionumber) {
                 eval{$biblioitemnumber=GetBiblioData($biblionumber)->{biblioitemnumber};};
                 if ($update) {
-                    eval { ( $biblionumber, $biblioitemnumber ) = ModBiblio( $record, $biblionumber, GetFrameworkcode($biblionumber) ) };
+                    eval { ( $biblionumber, $biblioitemnumber ) = ModBiblio( $record, $biblionumber, GetFrameworkCode($biblionumber) ) };
                     if ($@) {
                         warn "ERROR: Edit biblio $biblionumber failed: $@\n";
                         printlog( { id => $id || $originalid || $biblionumber, op => "update", status => "ERROR" } ) if ($logfile);
@@ -500,10 +523,10 @@ if ($fk_off) {
        $dbh->do("SET FOREIGN_KEY_CHECKS = 1");
 }
 
-# restore CataloguingLog
-$dbh->do("UPDATE systempreferences SET value=$CataloguingLog WHERE variable='CataloguingLog'");
-# restore AuthoritiesLog
-$dbh->do("UPDATE systempreferences SET value=$AuthoritiesLog WHERE variable='AuthoritiesLog'");
+# Restore CataloguingLog
+C4::Context->set_preference( 'CataloguingLog', $CataloguingLog );
+# Restore AuthoritiesLog
+C4::Context->set_preference( 'AuthoritiesLog', $AuthoritiesLog );
 
 my $timeneeded = gettimeofday - $starttime;
 print "\n$i MARC records done in $timeneeded seconds\n";
@@ -671,7 +694,7 @@ If specified, data will be appended to the logfile. If not, the logfile will be
 
 =item B<-t, -test>
 
-Test mode: parses the file, saying what he would do, but doing nothing.
+Test mode: parses the file, saying what it would do, but doing nothing.
 
 =item B<-s>
 
@@ -720,7 +743,7 @@ if set, do whatever is required
 
 =item B<-k, -keepids>=<FIELD>
 
-Field store ids in I<FIELD> (usefull for authorities, where 001 contains the
+Field store ids in I<FIELD> (useful for authorities, where 001 contains the
 authid for Koha, that can contain a very valuable info for authorities coming
 from LOC or BNF. useless for biblios probably)
 
@@ -754,7 +777,7 @@ I<FILE> for the koha bib and source id
 
 =item B<-keepids>
 
-Store ids in 009 (usefull for authorities, where 001 contains the authid for
+Store ids in 009 (useful for authorities, where 001 contains the authid for
 Koha, that can contain a very valuable info for authorities coming from LOC or
 BNF. useless for biblios probably)