functions that were in C4::Interface::CGI::Output are now in C4::Output.
[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::Biblio;
26 use C4::Context;
27 use C4::Breeding;
28 use C4::Koha;
29 use ZOOM;
30
31 my $input = new CGI;
32 my $dbh = C4::Context->dbh;
33 my $error = $input->param('error');
34 my $biblionumber=$input->param('biblionumber');
35 $biblionumber=0 unless $biblionumber;
36 my $frameworkcode=$input->param('frameworkcode');
37 my $title = $input->param('title');
38 my $author = $input->param('author');
39 my $isbn = $input->param('isbn');
40 my $issn = $input->param('issn');
41 my $random = $input->param('random');
42 my $op=$input->param('op');
43 my $noconnection;
44 my $numberpending;
45 my $attr='';
46 my $term;
47 my $host;
48 my $server;
49 my $database;
50 my $port;
51 my $marcdata;
52 my @encoding;
53 my @results;
54 my $count;
55 my $toggle;
56 my $record;
57 my $oldbiblio;
58 my $dbh = C4::Context->dbh;
59 my $errmsg;
60 my @serverloop=();
61 my @serverhost;
62 my @breeding_loop = ();
63
64 my $DEBUG = 1; # if set to 1, many debug message are send on syslog.
65
66 unless ($random) { # this var is not useful anymore just kept to keep rel2_2 compatibility
67     $random =rand(1000000000);
68 }
69
70 my ($template, $loggedinuser, $cookie)= get_template_and_user({
71                 template_name => "cataloguing/z3950_search.tmpl",
72                 query => $input,
73                 type => "intranet",
74                 authnotrequired => 1,
75                 flagsrequired => {catalogue => 1},
76                 debug => 1,
77                 });
78
79 $template->param(
80         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
81         intranetstylesheet => C4::Context->preference("intranetstylesheet"),
82         IntranetNav => C4::Context->preference("IntranetNav"),
83         frameworkcode => $frameworkcode,
84         );
85
86 if ($op ne "do_search"){
87     my $sth=$dbh->prepare("select id,host,checked from z3950servers  order by host");
88     $sth->execute();
89     my $serverloop=$sth->fetchall_arrayref({});
90     $template->param(isbn=>$isbn, issn=>$issn,title=>$title,author=>$author,
91                         serverloop => $serverloop,
92                         opsearch => "search",
93                         biblionumber => $biblionumber,
94                         );
95     output_html_with_http_headers $input, $cookie, $template->output;
96 }else{
97     my @id=$input->param('id');
98     my @oConnection;
99     my @oResult;
100     my $s=0;
101                             
102     if ($isbn || $issn) {
103                 $attr='1=7';
104 #          warn "isbn : $isbn";
105                 $term=$isbn if ($isbn);
106                 $term=$issn if ($issn);
107         } elsif ($title) {
108                 $attr='1=4 ';
109         utf8::decode($title);
110         $title=~tr/������/aaaeeeeiioouu/;
111                 $term=$title;
112         } elsif ($author) {
113                 $attr='1=1003';
114         utf8::decode($author);
115         $author=~tr/������/aaaeeeeiioouu/;
116                 $term=$author;
117         }
118
119     my $query="\@attr $attr \"$term\""; 
120     warn "query ".$query if $DEBUG;
121     foreach my $servid (@id){
122         my $sth=$dbh->prepare("select * from z3950servers where id=?");
123         $sth->execute($servid);
124         while ($server=$sth->fetchrow_hashref) {
125             my $noconnection=0;                                                 
126             my $option1=new ZOOM::Options();
127             $option1->option('async'=>1);
128             $option1->option('elementSetName', 'F');
129             $option1->option('databaseName',$server->{db})  ;
130             $option1->option('user',$server->{userid}) if $server->{userid};
131             $option1->option('password',$server->{password})  if $server->{password};
132             $option1->option('preferredRecordSyntax', $server->{syntax});
133             $oConnection[$s]=create ZOOM::Connection($option1) || $DEBUG && warn ("something went wrong: ".$oConnection[$s]->errmsg());
134             warn ("server data",$server->{name}, $server->{port}) if $DEBUG;
135             $oConnection[$s]->connect($server->{host}, $server->{port}) || $DEBUG && warn ("something went wrong: ".$oConnection[$s]->errmsg());
136             $serverhost[$s]=$server->{host};
137             $encoding[$s]=$server->{syntax};
138             $s++;
139         }## while fetch
140     }# foreach
141     my $nremaining = $s;
142     my $firstresult=1;
143
144     for (my $z=0 ;$z<$s;$z++){
145         warn "doing the search" if $DEBUG;
146         $oResult[$z] = $oConnection[$z]->search_pqf($query) || $DEBUG && warn ("somthing went wrong: " . $oConnection[$s]->errmsg());
147 #$oResult[$z] = $oConnection[$z]->search_pqf($query);
148     }
149
150 AGAIN:
151     my $k;
152     my $event;
153     while (($k = ZOOM::event(\@oConnection)) != 0) {
154         $event = $oConnection[$k-1]->last_event();
155         warn ("connection ", $k-1, ": event $event (", ZOOM::event_str($event), ")\n") if $DEBUG;
156         last if $event == ZOOM::Event::ZEND;
157     }
158
159     if ($k != 0) {
160         $k--;
161         warn $serverhost[$k] if $DEBUG;
162         my($error, $errmsg, $addinfo, $diagset) = $oConnection[$k]->error_x();
163         if ($error) {
164             warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n" if $DEBUG;
165             
166         } else {
167           my $numresults=$oResult[$k]->size() ;
168           my $i;
169           my $result='';
170           if ($numresults>0){
171               for ($i=0; $i<(($numresults<20) ? ($numresults) : (20)) ; $i++) {
172                   my $rec=$oResult[$k]->record($i);
173                   my $marcrecord;
174                   $marcdata = $rec->raw();
175                   $marcrecord= FixEncoding($marcdata);
176   ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
177   ## In HEAD i change everything to UTF-8
178   # In rel2_2 i am not sure what encoding is so no character conversion is done here
179   ##Add necessary encoding changes to here -TG
180                   my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,"");
181                   $oldbiblio->{isbn} =~ s/ |-|\.//g,
182                   $oldbiblio->{issn} =~ s/ |-|\.//g,
183                   my ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid)=ImportBreeding($marcdata,1,$serverhost[$k],$encoding[$k],$random);
184                   my %row_data;
185                   if ($i % 2) {
186                       $toggle="#ffffcc";
187                   } else {
188                       $toggle="white";
189                   }
190                   $row_data{toggle} = $toggle;
191                   $row_data{server} = $serverhost[$k];
192                   $row_data{isbn} = $oldbiblio->{isbn};
193                   $row_data{title} =$oldbiblio->{title};
194                   $row_data{author} = $oldbiblio->{author};
195                   $row_data{breedingid} = $breedingid;
196                   $row_data{biblionumber}=$biblionumber;
197                   push (@breeding_loop, \%row_data);
198               }# upto 5 results
199           }#$numresults
200         }
201     }# if $k !=0
202     $numberpending=$nremaining-1;
203     $template->param(breeding_loop => \@breeding_loop, server=>$serverhost[$k],
204                 numberpending => $numberpending,
205     );
206     output_html_with_http_headers $input, $cookie, $template->output if $numberpending==0;
207
208 #       print  $template->output  if $firstresult !=1;
209     $firstresult++;
210
211     MAYBE_AGAIN:
212         if (--$nremaining > 0) {
213             goto AGAIN;
214         }
215 } ## if op=search