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
25 use C4::Interface::CGI::Output;
29 use MARC::File::USMARC;
34 my $dbh = C4::Context->dbh;
35 my $error = $input->param('error');
36 my $oldbiblionumber=$input->param('oldbiblionumber');
37 $oldbiblionumber=0 unless $oldbiblionumber;
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');
53 my $encoding=C4::Context->preference("marcflavour");
57 my @breeding_loop = ();
60 my $dbh = C4::Context->dbh;
64 unless ($random) { # if random is a parameter => we're just waiting for the search to end, it's a refresh.
65 $random =rand(1000000000);
69 my ($template, $loggedinuser, $cookie);
70 if ($op ne "do_search"){
72 my $sth=$dbh->prepare("select id,host,checked from z3950servers order by host");
74 while ($server=$sth->fetchrow_hashref) {
76 $temploop{server}=$server->{host};
77 $temploop{id}=$server->{id};
78 $temploop{checked}=$server->{checked};
79 push (@serverloop, \%temploop);
81 ($template, $loggedinuser, $cookie)
82 = get_template_and_user({template_name => "z3950/searchresult.tmpl",
86 flagsrequired => {catalogue => 1},
89 $template->param(isbn=>$isbn, issn=>$issn,title=>$title,author=>$author,
90 serverloop => \@serverloop,
92 oldbiblionumber => $oldbiblionumber,
94 output_html_with_http_headers $input, $cookie, $template->output;
98 my @id=$input->param('id');
102 if ($isbn ne "/" || $issn ne "/") {
104 $term=$isbn if ($isbn ne"/");
105 $term=$issn if ($issn ne"/");
106 } elsif ($title ne"/") {
107 $attr='1=4 @attr 4=1 ';
109 } elsif ($author ne "/") {
115 my $query="\@attr $attr \"$term\"";
117 foreach my $servid ( @id){
118 my $sth=$dbh->prepare("select name, port, db, host from z3950servers where id=?");
119 $sth->execute($servid);
121 while ($server=$sth->fetchrow_hashref) {
125 my $option1=new ZOOM::Options();
126 $option1->option(async=>1);
127 $option1->option('elementSetName', 'F');
128 $option1->option('databaseName',$server->{db}) ;
129 $option1->option('preferredRecordSyntax', 'USMARC');
130 $oConnection[$s]=create ZOOM::Connection($option1);
131 $oConnection[$s]->connect($server->{name}, $server->{port});
132 $serverhost[$s]=$server->{host};
139 for (my $z=0 ;$z<$s;$z++){
140 $oResult[$z] = $oConnection[$z]->search_pqf($query);
146 while (($k = ZOOM::event(\@oConnection)) != 0) {
147 $event = $oConnection[$k-1]->last_event();
148 # warn ("connection ", $k-1, ": event $event (", ZOOM::event_str($event), ")\n");
149 last if $event == ZOOM::Event::ZEND;
153 #warn $serverhost[$k];
154 my($error, $errmsg, $addinfo, $diagset) = $oConnection[$k]->error_x();
157 warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
161 my $numresults=$oResult[$k]->size() ;
167 for ($i=0; $i<(($numresults<5) ? ($numresults) : (5)) ; $i++) {
168 my $rec=$oResult[$k]->record($i);
170 $marcdata = $rec->raw();
171 $marcrecord = MARC::File::USMARC::decode($marcdata);
173 my $marcxml=$marcrecord->as_xml_record($marcrecord);
174 $marcxml=Encode::encode('utf8',$marcxml);
175 #$marcxml=Encode::decode('utf8',$marcxml);
176 my $xmlhash=XML_xml2hash_onerecord($marcxml);
177 my $oldbiblio = XMLmarc2koha_onerecord($dbh,$xmlhash,'biblios');
178 $oldbiblio->{isbn} =~ s/ |-|\.//g,
179 $oldbiblio->{isbn} = substr($oldbiblio->{isbn},0,10);
180 $oldbiblio->{issn} =~ s/ |-|\.//g,
181 $oldbiblio->{issn} = substr($oldbiblio->{issn},0,10);
183 my ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$bid)=ImportBreeding($marcdata,1,$serverhost[$k],$encoding,$random);
191 $row_data{toggle} = $toggle;
192 $row_data{server} = $serverhost[$k];
193 $row_data{isbn} = $oldbiblio->{isbn};
195 $row_data{title} =$oldbiblio->{title};
196 $row_data{author} = $oldbiblio->{author};
197 $row_data{id} = $bid;
198 $row_data{oldbiblionumber}=$oldbiblionumber;
199 push (@breeding_loop, \%row_data);
203 }#for up to 5 results
206 my ($template, $loggedinuser, $cookie)
207 = get_template_and_user({template_name => "z3950/searchresult.tmpl",
210 authnotrequired => 1,
211 flagsrequired => {catalogue => 1},
214 $numberpending=$nremaining-1;
216 breeding_loop => \@breeding_loop,
217 # refresh=>($numberpending eq 0 ? "" : "search.pl?random=$random"),
218 numberpending => $numberpending,
219 # oldbiblionumber => $oldbiblionumber,
221 output_html_with_http_headers $input, "", $template->output if $firstresult==1;
223 print $template->output if $firstresult !=1;
225 if (--$nremaining > 0) {