3 # This is a completely new Z3950 clients search using async ZOOM -TG 02/11/06
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
34 my $dbh = C4::Context->dbh;
35 my $error = $input->param('error');
36 my $biblionumber = $input->param('biblionumber');
37 $biblionumber = 0 unless $biblionumber;
38 my $frameworkcode = $input->param('frameworkcode');
39 my $title = $input->param('title');
40 my $author = $input->param('author');
41 my $isbn = $input->param('isbn');
42 my $issn = $input->param('issn');
43 my $lccn = $input->param('lccn');
44 my $subject= $input->param('subject');
45 my $dewey = $input->param('dewey');
46 my $random = $input->param('random');
47 my $op = $input->param('op');
67 my @breeding_loop = ();
69 my $DEBUG = 0; # if set to 1, many debug message are send on syslog.
72 { # this var is not useful anymore just kept to keep rel2_2 compatibility
73 $random = rand(1000000000);
76 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
78 template_name => "cataloguing/z3950_search.tmpl",
82 flagsrequired => { catalogue => 1 },
87 $template->param( frameworkcode => $frameworkcode, );
89 if ( $op ne "do_search" ) {
90 my $sth = $dbh->prepare("select id,host,name,checked from z3950servers order by host");
92 my $serverloop = $sth->fetchall_arrayref( {} );
99 serverloop => $serverloop,
100 opsearch => "search",
101 biblionumber => $biblionumber,
103 output_html_with_http_headers $input, $cookie, $template->output;
106 my @id = $input->param('id');
112 if ($isbn || $issn) {
113 $term=$isbn if ($isbn);
114 $term=$issn if ($issn);
115 $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
119 utf8::decode($title);
120 $query .= " \@attr 1=4 \"$title\" ";
124 utf8::decode($author);
125 $query .= " \@attr 1=1003 \"$author\" ";
129 $query .= " \@attr 1=16 \"$dewey\" ";
133 utf8::decode($subject);
134 $query .= " \@attr 1=21 \"$subject\" ";
138 $query .= " \@attr 1=9 $lccn ";
141 for my $i (1..$nterms-1) {
142 $query = "\@and " . $query;
144 warn "query ".$query if $DEBUG;
146 foreach my $servid (@id) {
147 my $sth = $dbh->prepare("select * from z3950servers where id=?");
148 $sth->execute($servid);
149 while ( $server = $sth->fetchrow_hashref ) {
150 warn "serverinfo ".join(':',%$server) if $DEBUG;
151 my $noconnection = 0;
152 my $option1 = new ZOOM::Options();
153 $option1->option( 'async' => 1 );
154 $option1->option( 'elementSetName', 'F' );
155 $option1->option( 'databaseName', $server->{db} );
156 $option1->option( 'user', $server->{userid} ) if $server->{userid};
157 $option1->option( 'password', $server->{password} )
158 if $server->{password};
159 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
160 $oConnection[$s] = create ZOOM::Connection($option1)
162 && warn( "" . $oConnection[$s]->errmsg() );
163 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
164 $oConnection[$s]->connect( $server->{host}, $server->{port} )
166 && warn( "" . $oConnection[$s]->errmsg() );
167 $serverhost[$s] = $server->{host};
168 $servername[$s] = $server->{name};
169 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
176 for ( my $z = 0 ; $z < $s ; $z++ ) {
177 warn "doing the search" if $DEBUG;
178 $oResult[$z] = $oConnection[$z]->search_pqf($query)
180 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
182 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
188 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
189 $event = $oConnection[ $k - 1 ]->last_event();
190 warn( "connection ", $k - 1, ": event $event (",
191 ZOOM::event_str($event), ")\n" )
193 last if $event == ZOOM::Event::ZEND;
198 warn $serverhost[$k] if $DEBUG;
199 my ( $error, $errmsg, $addinfo, $diagset ) =
200 $oConnection[$k]->error_x();
202 warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
207 my $numresults = $oResult[$k]->size();
210 if ( $numresults > 0 ) {
213 $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
217 my $rec = $oResult[$k]->record($i);
220 $marcdata = $rec->raw();
222 my ($charset_result, $charset_errors);
223 ($marcrecord, $charset_result, $charset_errors) =
224 MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
225 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
226 ## In HEAD i change everything to UTF-8
227 # In rel2_2 i am not sure what encoding is so no character conversion is done here
228 ##Add necessary encoding changes to here -TG
229 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
230 $oldbiblio->{isbn} =~ s/ |-|\.//g,
231 $oldbiblio->{issn} =~ s/ |-|\.//g,
233 $notmarcrecord, $alreadyindb, $alreadyinfarm,
234 $imported, $breedingid
236 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
244 $row_data{toggle} = $toggle;
245 $row_data{server} = $servername[$k];
246 $row_data{isbn} = $oldbiblio->{isbn};
247 $row_data{lccn} = $oldbiblio->{lccn};
248 $row_data{title} = $oldbiblio->{title};
249 $row_data{author} = $oldbiblio->{author};
250 $row_data{breedingid} = $breedingid;
251 $row_data{biblionumber} = $biblionumber;
252 push( @breeding_loop, \%row_data );
255 push(@breeding_loop,{'toggle'=>($i % 2)?1:0,'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
261 $numberpending = $nremaining - 1;
263 breeding_loop => \@breeding_loop,
264 server => $servername[$k],
265 numberpending => $numberpending,
266 biblionumber => $biblionumber,
269 output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
271 # print $template->output if $firstresult !=1;
275 if ( --$nremaining > 0 ) {