Bug 19040: Refactor GetMarcBiblio parameters
[srvgit] / misc / migration_tools / rebuild_zebra.pl
index 5d04545..5e43cd6 100755 (executable)
@@ -26,6 +26,7 @@ use C4::Biblio;
 use C4::AuthoritiesMarc;
 use C4::Items;
 use Koha::RecordProcessor;
+use Koha::Caches;
 use XML::LibXML;
 
 use constant LOCK_FILENAME => 'rebuild..LCK';
@@ -62,6 +63,7 @@ my $run_user = (getpwuid($<))[0];
 my $wait_for_lock = 0;
 my $use_flock;
 my $table = 'biblioitems';
+my $is_memcached = Koha::Caches->get_instance->memcached_cache;
 
 my $verbose_logging = 0;
 my $zebraidx_log_opt = " -v none,fatal,warn ";
@@ -128,6 +130,9 @@ if ($daemon_mode) {
         $msg   .= "Please do '$0 --help' to see usage.\n";
         die $msg;
     }
+    unless ($is_memcached) {
+        warn "Warning: script running in daemon mode, without recommended caching system (memcached).\n";
+    }
     $authorities = 1;
     $biblios = 1;
     $process_zebraqueue = 1;
@@ -179,8 +184,8 @@ my $marcxml_close = q{
 };
 
 # Protect again simultaneous update of the zebra index by using a lock file.
-# Create our own lock directory if its missing.  This shouild be created
-# by koha-zebra-ctl.sh or at system installation.  If the desired directory
+# Create our own lock directory if it is missing. This should be created
+# by koha-zebra-ctl.sh or at system installation. If the desired directory
 # does not exist and cannot be created, we fall back on /tmp - which will
 # always work.
 
@@ -237,7 +242,10 @@ if ($daemon_mode) {
         if (_flock($LockFH, LOCK_EX|LOCK_NB)) {
             eval {
                 $dbh = C4::Context->dbh;
-                do_one_pass() if ( zebraqueue_not_empty() );
+                if( zebraqueue_not_empty() ) {
+                    Koha::Caches->flush_L1_caches() if $is_memcached;
+                    do_one_pass();
+                }
             };
             if ($@ && $verbose_logging) {
                 warn "Warning : $@\n";
@@ -635,19 +643,26 @@ sub generate_deleted_marc_records {
 }
 
 sub get_corrected_marc_record {
-    my ($record_type, $record_number) = @_;
+    my ( $record_type, $record_number ) = @_;
 
-    my $marc = get_raw_marc_record($record_type, $record_number);
+    my $marc = get_raw_marc_record( $record_type, $record_number );
 
-    if (defined $marc) {
+    if ( defined $marc ) {
         fix_leader($marc);
-        if ($record_type eq 'authority') {
-            fix_authority_id($marc, $record_number);
-        } elsif ($record_type eq 'biblio' && C4::Context->preference('IncludeSeeFromInSearches')) {
-            my $normalizer = Koha::RecordProcessor->new( { filters => 'EmbedSeeFromHeadings' } );
+        if ( $record_type eq 'authority' ) {
+            fix_authority_id( $marc, $record_number );
+        }
+        elsif ( $record_type eq 'biblio' ) {
+
+            my @filters;
+            push @filters, 'EmbedItemsAvailability';
+            push @filters, 'IncludeSeeFromInSearches'
+                if C4::Context->preference('IncludeSeeFromInSearches');
+
+            my $normalizer = Koha::RecordProcessor->new( { filters => \@filters } );
             $marc = $normalizer->process($marc);
         }
-        if (C4::Context->preference("marcflavour") eq "UNIMARC") {
+        if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
             fix_unimarc_100($marc);
         }
     }
@@ -660,7 +675,7 @@ sub get_raw_marc_record {
 
     my $marc;
     if ($record_type eq 'biblio') {
-        eval { $marc = GetMarcBiblio($record_number, 1); };
+        eval { $marc = GetMarcBiblio({ biblionumber => $record_number, embed_items => 1 }); };
         if ($@ || !$marc) {
             # here we do warn since catching an exception
             # means that the bib was found but failed