Bug 2505 : Switch on warnings in circ/returns.pl
[koha_fer] / misc / cronjobs / zebraqueue_start.pl
index ddc98c1..ea84156 100755 (executable)
@@ -2,7 +2,13 @@
 # script that starts the zebraquee
 #  Written by TG on 01/08/2006
 use strict;
-
+#use warnings; FIXME - Bug 2505
+BEGIN {
+    # find Koha's Perl modules
+    # test carefully before changing this
+    use FindBin;
+    eval { require "$FindBin::Bin/../kohalib.pl" };
+}
 
 use C4::Context;
 use C4::Biblio;
@@ -13,14 +19,19 @@ use utf8;
 ### ZEBRA SERVER UPDATER
 ##Uses its own database handle
 my $dbh=C4::Context->dbh;
-my $readsth=$dbh->prepare("select id,biblio_auth_number,operation,server from zebraqueue");
+my $readsth=$dbh->prepare("SELECT id,biblio_auth_number,operation,server FROM zebraqueue WHERE done=0 
+                           ORDER BY id DESC"); # NOTE - going in reverse order to catch deletes that
+                                               # occur after a string of updates (e.g., user deletes
+                                               # the items attached to a bib, then the items.
+                                               # Having a specialUpdate occur after a recordDelete
+                                               # should not occur.
 #my $delsth=$dbh->prepare("delete from zebraqueue where id =?");
 
 
 #AGAIN:
 
 #my $wait=C4::Context->preference('zebrawait') || 120;
-my $verbose = 1;
+my $verbose = 0;
 print "starting with verbose=$verbose\n" if $verbose;
 
 my ($id,$biblionumber,$operation,$server,$marcxml);
@@ -33,7 +44,10 @@ while (($id,$biblionumber,$operation,$server)=$readsth->fetchrow){
         # if the operation is a deletion, zebra requires that we give it the xml.
         # as it is no more in the SQL db, retrieve it from zebra itself.
         # may sound silly, but that's the way zebra works ;-)
-           if ($operation =~ /delete/) {
+           if ($operation =~ /delete/i) { # NOTE depending on version, delete operation
+                                       #      was coded 'delete_record' or 'recordDelete'.
+                                       #      'recordDelete' is the preferred one, as that's
+                                       #      what the ZOOM API wants.
               # 1st read the record in zebra
             my $Zconn=C4::Context->Zconn($server, 0, 1,'','xml');
             my $query = $Zconn->search_pqf( '@attr 1=Local-Number '.$biblionumber);
@@ -46,7 +60,7 @@ while (($id,$biblionumber,$operation,$server)=$readsth->fetchrow){
                 my $marc = GetMarcBiblio($biblionumber);
                 $marcxml = $marc->as_xml_record() if $marc;
             } elsif ($server eq "authorityserver") {
-                $marcxml =C4::AuthoritiesMarc::XMLgetauthority($dbh,$biblionumber);
+                $marcxml =C4::AuthoritiesMarc::GetAuthorityXML($biblionumber);
             }
             if ($verbose) {
                 if ($marcxml) {
@@ -64,7 +78,7 @@ while (($id,$biblionumber,$operation,$server)=$readsth->fetchrow){
             ## it's Broken XML-- Should not reach here-- but if it does -lets protect ZEBRA
             if ($@){
                 warn $@;
-                my $delsth=$dbh->prepare("delete from zebraqueue where id =?");
+                my $delsth=$dbh->prepare("UPDATE zebraqueue SET done=1 WHERE id =?");
                 $delsth->execute($id);
                 next;
             }
@@ -80,17 +94,26 @@ while (($id,$biblionumber,$operation,$server)=$readsth->fetchrow){
         # did a modif (or item deletion) just before biblio deletion, there are some specialUpdage
         # that are pending and can't succeed, as we don't have the XML anymore
         # so, delete everything for this biblionumber
-        if ($operation eq 'delete_record') {
+        my $reset_readsth = 0;
+        if ($operation eq 'recordDelete') {
             print "deleting biblio deletion $biblionumber\n" if $verbose;
-            $delsth =$dbh->prepare("delete from zebraqueue where biblio_auth_number =?");
+            $delsth =$dbh->prepare("UPDATE zebraqueue SET done=1 WHERE biblio_auth_number =?");
             $delsth->execute($biblionumber);
+            $reset_readsth = 1 if $delsth->rows() > 0;
         # if it's not a deletion, delete every pending specialUpdate for this biblionumber
         # in case the user add biblio, then X items, before this script runs
         # this avoid indexing X+1 times where just 1 is enough.
         } else {
             print "deleting special date for $biblionumber\n" if $verbose;
-            $delsth =$dbh->prepare("delete from zebraqueue where biblio_auth_number =? and operation='specialUpdate'");
+            $delsth =$dbh->prepare("UPDATE zebraqueue SET done=1 WHERE biblio_auth_number =? and operation='specialUpdate'");
             $delsth->execute($biblionumber);
+            $reset_readsth = 1 if $delsth->rows() > 0;
+        }
+        if ($reset_readsth) {
+            # if we can ignore rows in zebraqueue because we've already
+            # touched a record, reset the query. 
+            $readsth->finish();
+            $readsth->execute();
         }
     }
 }