2 # This is a completely new Z3950 clients search using async ZOOM -TG 02/11/06
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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
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.
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
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');
58 my $dbh = C4::Context->dbh;
62 my @breeding_loop = ();
64 my $DEBUG = 1; # if set to 1, many debug message are send on syslog.
66 unless ($random) { # this var is not useful anymore just kept to keep rel2_2 compatibility
67 $random =rand(1000000000);
70 my ($template, $loggedinuser, $cookie)= get_template_and_user({
71 template_name => "cataloguing/z3950_search.tmpl",
75 flagsrequired => {catalogue => 1},
80 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
81 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
82 IntranetNav => C4::Context->preference("IntranetNav"),
83 frameworkcode => $frameworkcode,
86 if ($op ne "do_search"){
87 my $sth=$dbh->prepare("select id,host,checked from z3950servers order by host");
89 my $serverloop=$sth->fetchall_arrayref({});
90 $template->param(isbn=>$isbn, issn=>$issn,title=>$title,author=>$author,
91 serverloop => $serverloop,
93 biblionumber => $biblionumber,
95 output_html_with_http_headers $input, $cookie, $template->output;
97 my @id=$input->param('id');
102 if ($isbn || $issn) {
104 # warn "isbn : $isbn";
105 $term=$isbn if ($isbn);
106 $term=$issn if ($issn);
109 utf8::decode($title);
110 $title=~tr/������/aaaeeeeiioouu/;
114 utf8::decode($author);
115 $author=~tr/������/aaaeeeeiioouu/;
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) {
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};
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);
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;
161 warn $serverhost[$k] if $DEBUG;
162 my($error, $errmsg, $addinfo, $diagset) = $oConnection[$k]->error_x();
164 warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n" if $DEBUG;
167 my $numresults=$oResult[$k]->size() ;
171 for ($i=0; $i<(($numresults<20) ? ($numresults) : (20)) ; $i++) {
172 my $rec=$oResult[$k]->record($i);
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);
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);
202 $numberpending=$nremaining-1;
203 $template->param(breeding_loop => \@breeding_loop, server=>$serverhost[$k],
204 numberpending => $numberpending,
206 output_html_with_http_headers $input, $cookie, $template->output if $numberpending==0;
208 # print $template->output if $firstresult !=1;
212 if (--$nremaining > 0) {