reverting to previous version
authorkados <kados>
Mon, 26 Feb 2007 13:34:22 +0000 (13:34 +0000)
committerkados <kados>
Mon, 26 Feb 2007 13:34:22 +0000 (13:34 +0000)
z3950/search.pl

index 8864da5..1edaef3 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl
-
+# This is a completely new Z3950 clients search using async ZOOM -TG 02/11/06
 # Copyright 2000-2002 Katipo Communications
 #
 # This file is part of Koha.
@@ -26,15 +26,15 @@ use C4::Interface::CGI::Output;
 use C4::Biblio;
 use C4::Context;
 use C4::Breeding;
-use MARC::File::USMARC;
+use C4::Koha;
 use ZOOM;
-use Encode;;
 
 my $input = new CGI;
 my $dbh = C4::Context->dbh;
 my $error = $input->param('error');
-my $oldbiblionumber=$input->param('oldbiblionumber');
-$oldbiblionumber=0 unless $oldbiblionumber;
+my $biblionumber=$input->param('oldbiblionumber');
+$biblionumber=0 unless $biblionumber;
+my $frameworkcode=$input->param('frameworkcode');
 my $title = $input->param('title');
 my $author = $input->param('author');
 my $isbn = $input->param('isbn');
@@ -50,179 +50,170 @@ my $server;
 my $database;
 my $port;
 my $marcdata;
-my $encoding=C4::Context->preference("marcflavour");
+my @encoding;
 my @results;
 my $count;
 my $toggle;
-my @breeding_loop = ();
 my $record;
 my $oldbiblio;
 my $dbh = C4::Context->dbh;
 my $errmsg;
 my @serverloop=();
 my @serverhost;
-unless ($random) { # if random is a parameter => we're just waiting for the search to end, it's a refresh.
-$random =rand(1000000000);
-}
-
+my @breeding_loop = ();
 
-my ($template, $loggedinuser, $cookie);
-if ($op ne "do_search"){
+my $DEBUG = 0; # if set to 1, many debug message are send on syslog.
 
-my $sth=$dbh->prepare("select id,host,checked from z3950servers  order by host");
-$sth->execute();
-while ($server=$sth->fetchrow_hashref) {
-my %temploop;
-$temploop{server}=$server->{host};
-$temploop{id}=$server->{id};
-$temploop{checked}=$server->{checked};
-push (@serverloop, \%temploop);
+unless ($random) { # this var is not useful anymore just kept to keep rel2_2 compatibility
+    $random =rand(1000000000);
 }
-($template, $loggedinuser, $cookie)
-= get_template_and_user({template_name => "z3950/searchresult.tmpl",
-                               query => $input,
-                               type => "intranet",
-                               authnotrequired => 1,
-                               flagsrequired => {catalogue => 1},
-                               debug => 1,
-                               });
-$template->param(isbn=>$isbn, issn=>$issn,title=>$title,author=>$author,
-                                               serverloop => \@serverloop,
-                                               opsearch => "search",
-                                               oldbiblionumber => $oldbiblionumber,
-                                               );
-output_html_with_http_headers $input, $cookie, $template->output;
 
-}else{
-
-my @id=$input->param('id');
-my @oConnection;
-my @oResult;
-my $s=0;
-                                                       if ($isbn ne "/" || $issn ne "/") {
-                                                               $attr='1=7';
-                                                       $term=$isbn if ($isbn ne"/");
-                                                       $term=$issn if ($issn ne"/");
-                                                       } elsif ($title ne"/") {
-                                                               $attr='1=4 @attr 4=1  ';
-                                                       $term=$title;
-                                                       } elsif ($author ne "/") {
-                                                               $attr='1=1003';
-                                                       $term=$author;
-                                                       } 
+my ($template, $loggedinuser, $cookie)= get_template_and_user({
+                template_name => "z3950/searchresult.tmpl",
+                query => $input,
+                type => "intranet",
+                authnotrequired => 1,
+                flagsrequired => {catalogue => 1},
+                debug => 1,
+                });
 
-                                                       
-my $query="\@attr $attr \"$term\"";    
-                       
-  foreach my $servid ( @id){
-  my $sth=$dbh->prepare("select name, port, db, host from z3950servers where id=?");
-  $sth->execute($servid);
+$template->param(
+        intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
+        intranetstylesheet => C4::Context->preference("intranetstylesheet"),
+        IntranetNav => C4::Context->preference("IntranetNav"),
+        frameworkcode => $frameworkcode,
+        );
 
+if ($op ne "do_search"){
+    my $sth=$dbh->prepare("select id,host,checked from z3950servers  order by host");
+    $sth->execute();
     while ($server=$sth->fetchrow_hashref) {
-       my $noconnection=0;
-       #$numberpending=1;
-                                                       
-       my $option1=new ZOOM::Options();
-       $option1->option(async=>1);
-       $option1->option('elementSetName', 'F');
-       $option1->option('databaseName',$server->{db})  ;
-       $option1->option('preferredRecordSyntax', 'USMARC');
-        $oConnection[$s]=create ZOOM::Connection($option1);
-       $oConnection[$s]->connect($server->{name}, $server->{port});
-       $serverhost[$s]=$server->{host};
-       $s++;
-    }## while fetch
-
-  }# foreach
-my $nremaining = $s;
-my $firstresult=1;
- for (my $z=0 ;$z<$s;$z++){
-$oResult[$z] = $oConnection[$z]->search_pqf($query);
+        my %temploop;
+        $temploop{server}=$server->{host};
+        $temploop{id}=$server->{id};
+        $temploop{checked}=$server->{checked};
+        push (@serverloop, \%temploop);
+    }
+    $template->param(isbn=>$isbn, issn=>$issn,title=>$title,author=>$author,
+                        serverloop => \@serverloop,
+                        opsearch => "search",
+                        oldbiblionumber => $biblionumber,
+                        );
+    output_html_with_http_headers $input, $cookie, $template->output;
+}else{
+    my @id=$input->param('id');
+    my @oConnection;
+    my @oResult;
+    my $s=0;
+                            
+    if ($isbn ne "/" || $issn ne "/") {
+        $attr='1=7';
+#         warn "isbn : $isbn";
+        $term=$isbn if ($isbn ne "/");
+        $term=$issn if ($issn ne "/");
+    } elsif ($title ne "/") {
+        $attr='1=4 @attr 4=1  ';
+        $term=$title;
+    } elsif ($author ne "/") {
+        $attr='1=1003';
+        $term=$author;
+    } 
+
+    my $query="\@attr $attr \"$term\"";        
+    warn "query ".$query if $DEBUG;
+    foreach my $servid (@id){
+        my $sth=$dbh->prepare("select * from z3950servers where id=?");
+        $sth->execute($servid);
+        while ($server=$sth->fetchrow_hashref) {
+            my $noconnection=0;                                                        
+            my $option1=new ZOOM::Options();
+            $option1->option('async'=>1);
+            $option1->option('elementSetName', 'F');
+            $option1->option('databaseName',$server->{db})  ;
+            $option1->option('user',$server->{userid}) if $server->{userid};
+            $option1->option('password',$server->{password})  if $server->{password};
+            $option1->option('preferredRecordSyntax', $server->{syntax});
+            $oConnection[$s]=create ZOOM::Connection($option1) || $DEBUG && warn ("something went wrong: ".$oConnection[$s]->errmsg());
+            warn ("server data",$server->{name}, $server->{port}) if $DEBUG;
+            $oConnection[$s]->connect($server->{host}, $server->{port}) || $DEBUG && warn ("something went wrong: ".$oConnection[$s]->errmsg());
+            $serverhost[$s]=$server->{host};
+            $encoding[$s]=$server->{syntax};
+            $s++;
+        }## while fetch
+    }# foreach
+    my $nremaining = $s;
+    my $firstresult=1;
+
+    for (my $z=0 ;$z<$s;$z++){
+        warn "doing the search" if $DEBUG;
+        $oResult[$z] = $oConnection[$z]->search_pqf($query) || $DEBUG && warn ("somthing went wrong: " . $oConnection[$s]->errmsg());
+#$oResult[$z] = $oConnection[$z]->search_pqf($query);
+    }
 
-}
 AGAIN:
- my $k;
-my $event;
-  while (($k = ZOOM::event(\@oConnection)) != 0) {
-       $event = $oConnection[$k-1]->last_event();
-# warn ("connection ", $k-1, ": event $event (", ZOOM::event_str($event), ")\n");
-       last if $event == ZOOM::Event::ZEND;
-   }
-if ($k != 0) {
-       $k--;
-#warn $serverhost[$k];
-        my($error, $errmsg, $addinfo, $diagset) = $oConnection[$k]->error_x();
-       if ($error) {
-
-       warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
-       goto MAYBE_AGAIN;
-       }
-       
-       my $numresults=$oResult[$k]->size() ;                                                           
-                                                                       
-
-       my $i;
-       my $result='';
-       if ($numresults>0){
-               for ($i=0; $i<(($numresults<5) ? ($numresults) : (5)) ; $i++) {
-                       my $rec=$oResult[$k]->record($i);                                                                               
-                       my $marcrecord;
-                       $marcdata = $rec->raw();                                                                                        
-                       $marcrecord = MARC::File::USMARC::decode($marcdata);
-                       
-                       my $marcxml=$marcrecord->as_xml_record($marcrecord);
-                       $marcxml=Encode::encode('utf8',$marcxml);
-                       #$marcxml=Encode::decode('utf8',$marcxml);
-                       my $xmlhash=XML_xml2hash_onerecord($marcxml);                                           
-               my $oldbiblio = XMLmarc2koha_onerecord($dbh,$xmlhash,'biblios');
-                               $oldbiblio->{isbn} =~ s/ |-|\.//g,
-                       $oldbiblio->{isbn} = substr($oldbiblio->{isbn},0,10);
-                       $oldbiblio->{issn} =~ s/ |-|\.//g,
-                       $oldbiblio->{issn} = substr($oldbiblio->{issn},0,10);
-
-my ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$bid)=ImportBreeding($marcdata,1,$serverhost[$k],$encoding,$random);
-                       my %row_data;
- @breeding_loop = ();
-       if ($i % 2) {
-               $toggle="#ffffcc";
-       } else {
-               $toggle="white";
-       }
-       $row_data{toggle} = $toggle;
-       $row_data{server} = $serverhost[$k];
-       $row_data{isbn} = $oldbiblio->{isbn};
-
-       $row_data{title} =$oldbiblio->{title};
-       $row_data{author} = $oldbiblio->{author};
-       $row_data{id} = $bid;
-       $row_data{oldbiblionumber}=$oldbiblionumber;
-       push (@breeding_loop, \%row_data);
-
-                                               
-}# $numresults
-}#for up to 5 results
-}# if $k !=0
-
-my ($template, $loggedinuser, $cookie)
-= get_template_and_user({template_name => "z3950/searchresult.tmpl",
-                               query => $input,
-                               type => "intranet",
-                               authnotrequired => 1,
-                               flagsrequired => {catalogue => 1},
-                               debug => 1,
-                               });
-$numberpending=$nremaining-1;
-$template->param(
-                                               breeding_loop => \@breeding_loop,
-#                                               refresh=>($numberpending eq 0 ? "" : "search.pl?random=$random"),
-                                               numberpending => $numberpending,
-#                                              oldbiblionumber => $oldbiblionumber,
-                                               );
-output_html_with_http_headers $input, "", $template->output if $firstresult==1;
-$firstresult++;
-print  $template->output if $firstresult !=1;
-MAYBE_AGAIN:
-if (--$nremaining > 0) {
-    goto AGAIN;
-}
+    my $k;
+    my $event;
+    while (($k = ZOOM::event(\@oConnection)) != 0) {
+        $event = $oConnection[$k-1]->last_event();
+        warn ("connection ", $k-1, ": event $event (", ZOOM::event_str($event), ")\n") if $DEBUG;
+        last if $event == ZOOM::Event::ZEND;
+    }
+
+    if ($k != 0) {
+        $k--;
+        warn $serverhost[$k] if $DEBUG;
+        my($error, $errmsg, $addinfo, $diagset) = $oConnection[$k]->error_x();
+        if ($error) {
+            warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n" if $DEBUG;
+            goto MAYBE_AGAIN;
+        }
+        my $numresults=$oResult[$k]->size() ;
+        my $i;
+        my $result='';
+        if ($numresults>0){
+            for ($i=0; $i<(($numresults<5) ? ($numresults) : (5)) ; $i++) {
+                my $rec=$oResult[$k]->record($i);
+                my $marcrecord;
+                $marcdata = $rec->raw();
+                               $marcrecord = MARC::Record->new_from_usmarc($marcdata);#rec->raw();
+                #$marcrecord= FixEncoding($marcdata);
+####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
+## In HEAD i change everything to UTF-8
+# In rel2_2 i am not sure what encoding is so no character conversion is done here
+##Add necessary encoding changes to here -TG
+                my $oldbiblio = MARCmarc2koha($dbh,$marcrecord,"");
+                $oldbiblio->{isbn} =~ s/ |-|\.//g,
+                $oldbiblio->{issn} =~ s/ |-|\.//g,
+                my ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid)=ImportBreeding($marcdata,1,$serverhost[$k],$encoding[$k],$random);
+                my %row_data;
+                if ($i % 2) {
+                    $toggle="#ffffcc";
+                } else {
+                    $toggle="white";
+                }
+                $row_data{toggle} = $toggle;
+                $row_data{server} = $serverhost[$k];
+                $row_data{isbn} = $oldbiblio->{isbn};
+                $row_data{title} =$oldbiblio->{title};
+                $row_data{author} = $oldbiblio->{author};
+                $row_data{id} = $breedingid;
+                $row_data{oldbiblionumber}=$biblionumber;
+                push (@breeding_loop, \%row_data);
+            }# upto 5 results
+    
+        }#$numresults
+    }# if $k !=0
+    $numberpending=$nremaining-1;
+    $template->param(breeding_loop => \@breeding_loop, server=>$serverhost[$k],
+                numberpending => $numberpending,
+    );
+    output_html_with_http_headers $input, $cookie, $template->output if $numberpending==0;
+
+#      print  $template->output  if $firstresult !=1;
+    $firstresult++;
+
+    MAYBE_AGAIN:
+        if (--$nremaining > 0) {
+            goto AGAIN;
+        }
 } ## if op=search