Code cleaning :
[koha.git] / cataloguing / z3950_search.pl
1 #!/usr/bin/perl
2 # This is a completely new Z3950 clients search using async ZOOM -TG 02/11/06
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use CGI;
22
23 use C4::Auth;
24 use C4::Output;
25 use C4::Interface::CGI::Output;
26 use C4::Biblio;
27 use C4::Context;
28 use C4::Breeding;
29 use C4::Koha;
30 use ZOOM;
31
32 my $input = new CGI;
33 my $dbh = C4::Context->dbh;
34 my $error = $input->param('error');
35 my $biblionumber=$input->param('biblionumber');
36 $biblionumber=0 unless $biblionumber;
37 my $frameworkcode=$input->param('frameworkcode');
38 my $title = $input->param('title');
39 my $author = $input->param('author');
40 my $isbn = $input->param('isbn');
41 my $issn = $input->param('issn');
42 my $random = $input->param('random');
43 my $op=$input->param('op');
44 my $noconnection;
45 my $numberpending;
46 my $attr='';
47 my $term;
48 my $host;
49 my $server;
50 my $database;
51 my $port;
52 my $marcdata;
53 my @encoding;
54 my @results;
55 my $count;
56 my $toggle;
57 my $record;
58 my $oldbiblio;
59 my $dbh = C4::Context->dbh;
60 my $errmsg;
61 my @serverloop=();
62 my @serverhost;
63 my @breeding_loop = ();
64
65 my $DEBUG = 1; # if set to 1, many debug message are send on syslog.
66
67 unless ($random) { # this var is not useful anymore just kept to keep rel2_2 compatibility
68     $random =rand(1000000000);
69 }
70
71 my ($template, $loggedinuser, $cookie)= get_template_and_user({
72                 template_name => "cataloguing/z3950_search.tmpl",
73                 query => $input,
74                 type => "intranet",
75                 authnotrequired => 1,
76                 flagsrequired => {catalogue => 1},
77                 debug => 1,
78                 });
79
80 $template->param(
81         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
82         intranetstylesheet => C4::Context->preference("intranetstylesheet"),
83         IntranetNav => C4::Context->preference("IntranetNav"),
84         frameworkcode => $frameworkcode,
85         );
86
87 if ($op ne "do_search"){
88     my $sth=$dbh->prepare("select id,host,checked from z3950servers  order by host");
89     $sth->execute();
90     my $serverloop=$sth->fetchall_arrayref({});
91     $template->param(isbn=>$isbn, issn=>$issn,title=>$title,author=>$author,
92                         serverloop => $serverloop,
93                         opsearch => "search",
94                         biblionumber => $biblionumber,
95                         );
96     output_html_with_http_headers $input, $cookie, $template->output;
97 }else{
98     my @id=$input->param('id');
99     my @oConnection;
100     my @oResult;
101     my $s=0;
102                             
103     if ($isbn || $issn) {
104                 $attr='1=7';
105 #          warn "isbn : $isbn";
106                 $term=$isbn if ($isbn);
107                 $term=$issn if ($issn);
108         } elsif ($title) {
109                 $attr='1=4 ';
110         utf8::decode($title);
111         $title=~tr/������/aaaeeeeiioouu/;
112                 $term=$title;
113         } elsif ($author) {
114                 $attr='1=1003';
115         utf8::decode($author);
116         $author=~tr/������/aaaeeeeiioouu/;
117                 $term=$author;
118         }
119
120     my $query="\@attr $attr \"$term\""; 
121     warn "query ".$query if $DEBUG;
122     foreach my $servid (@id){
123         my $sth=$dbh->prepare("select * from z3950servers where id=?");
124         $sth->execute($servid);
125         while ($server=$sth->fetchrow_hashref) {
126             my $noconnection=0;                                                 
127             my $option1=new ZOOM::Options();
128             $option1->option('async'=>1);
129             $option1->option('elementSetName', 'F');
130             $option1->option('databaseName',$server->{db})  ;
131             $option1->option('user',$server->{userid}) if $server->{userid};
132             $option1->option('password',$server->{password})  if $server->{password};
133             $option1->option('preferredRecordSyntax', $server->{syntax});
134             $oConnection[$s]=create ZOOM::Connection($option1) || $DEBUG && warn ("something went wrong: ".$oConnection[$s]->errmsg());
135             warn ("server data",$server->{name}, $server->{port}) if $DEBUG;
136             $oConnection[$s]->connect($server->{host}, $server->{port}) || $DEBUG && warn ("something went wrong: ".$oConnection[$s]->errmsg());
137             $serverhost[$s]=$server->{host};
138             $encoding[$s]=$server->{syntax};
139             $s++;
140         }## while fetch
141     }# foreach
142     my $nremaining = $s;
143     my $firstresult=1;
144
145     for (my $z=0 ;$z<$s;$z++){
146         warn "doing the search" if $DEBUG;
147         $oResult[$z] = $oConnection[$z]->search_pqf($query) || $DEBUG && warn ("somthing went wrong: " . $oConnection[$s]->errmsg());
148 #$oResult[$z] = $oConnection[$z]->search_pqf($query);
149     }
150
151 AGAIN:
152     my $k;
153     my $event;
154     while (($k = ZOOM::event(\@oConnection)) != 0) {
155         $event = $oConnection[$k-1]->last_event();
156         warn ("connection ", $k-1, ": event $event (", ZOOM::event_str($event), ")\n") if $DEBUG;
157         last if $event == ZOOM::Event::ZEND;
158     }
159
160     if ($k != 0) {
161         $k--;
162         warn $serverhost[$k] if $DEBUG;
163         my($error, $errmsg, $addinfo, $diagset) = $oConnection[$k]->error_x();
164         if ($error) {
165             warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n" if $DEBUG;
166             
167         } else {
168           my $numresults=$oResult[$k]->size() ;
169           my $i;
170           my $result='';
171           if ($numresults>0){
172               for ($i=0; $i<(($numresults<20) ? ($numresults) : (20)) ; $i++) {
173                   my $rec=$oResult[$k]->record($i);
174                   my $marcrecord;
175                   $marcdata = $rec->raw();
176                   $marcrecord= FixEncoding($marcdata);
177   ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
178   ## In HEAD i change everything to UTF-8
179   # In rel2_2 i am not sure what encoding is so no character conversion is done here
180   ##Add necessary encoding changes to here -TG
181                   my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,"");
182                   $oldbiblio->{isbn} =~ s/ |-|\.//g,
183                   $oldbiblio->{issn} =~ s/ |-|\.//g,
184                   my ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid)=ImportBreeding($marcdata,1,$serverhost[$k],$encoding[$k],$random);
185                   my %row_data;
186                   if ($i % 2) {
187                       $toggle="#ffffcc";
188                   } else {
189                       $toggle="white";
190                   }
191                   $row_data{toggle} = $toggle;
192                   $row_data{server} = $serverhost[$k];
193                   $row_data{isbn} = $oldbiblio->{isbn};
194                   $row_data{title} =$oldbiblio->{title};
195                   $row_data{author} = $oldbiblio->{author};
196                   $row_data{breedingid} = $breedingid;
197                   $row_data{biblionumber}=$biblionumber;
198                   push (@breeding_loop, \%row_data);
199               }# upto 5 results
200           }#$numresults
201         }
202     }# if $k !=0
203     $numberpending=$nremaining-1;
204     $template->param(breeding_loop => \@breeding_loop, server=>$serverhost[$k],
205                 numberpending => $numberpending,
206     );
207     output_html_with_http_headers $input, $cookie, $template->output if $numberpending==0;
208
209 #       print  $template->output  if $firstresult !=1;
210     $firstresult++;
211
212     MAYBE_AGAIN:
213         if (--$nremaining > 0) {
214             goto AGAIN;
215         }
216 } ## if op=search