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
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');
62 my @breeding_loop = ();
64 my $DEBUG = 1; # if set to 1, many debug message are send on syslog.
67 { # this var is not useful anymore just kept to keep rel2_2 compatibility
68 $random = rand(1000000000);
71 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
73 template_name => "cataloguing/z3950_search.tmpl",
77 flagsrequired => { catalogue => 1 },
82 $template->param( frameworkcode => $frameworkcode, );
84 if ( $op ne "do_search" ) {
85 my $sth = $dbh->prepare("select id,host,checked from z3950servers order by host");
87 my $serverloop = $sth->fetchall_arrayref( {} );
93 serverloop => $serverloop,
95 biblionumber => $biblionumber,
97 output_html_with_http_headers $input, $cookie, $template->output;
100 my @id = $input->param('id');
105 if ( $isbn || $issn ) {
107 $term = $isbn if ($isbn);
108 $term = $issn if ($issn);
112 utf8::decode($title);
117 utf8::decode($author);
121 my $query = "\@attr $attr \"$term\"";
122 warn "query " . $query if $DEBUG;
123 foreach my $servid (@id) {
124 my $sth = $dbh->prepare("select * from z3950servers where id=?");
125 $sth->execute($servid);
126 while ( $server = $sth->fetchrow_hashref ) {
127 my $noconnection = 0;
128 my $option1 = new ZOOM::Options();
129 $option1->option( 'async' => 1 );
130 $option1->option( 'elementSetName', 'F' );
131 $option1->option( 'databaseName', $server->{db} );
132 $option1->option( 'user', $server->{userid} ) if $server->{userid};
133 $option1->option( 'password', $server->{password} )
134 if $server->{password};
135 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
136 $oConnection[$s] = create ZOOM::Connection($option1)
138 && warn( "something went wrong: " . $oConnection[$s]->errmsg() );
139 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
140 $oConnection[$s]->connect( $server->{host}, $server->{port} )
142 && warn( "something went wrong: " . $oConnection[$s]->errmsg() );
143 $serverhost[$s] = $server->{host};
144 $encoding[$s] = $server->{syntax};
151 for ( my $z = 0 ; $z < $s ; $z++ ) {
152 warn "doing the search" if $DEBUG;
153 $oResult[$z] = $oConnection[$z]->search_pqf($query)
155 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
157 #$oResult[$z] = $oConnection[$z]->search_pqf($query);
163 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
164 $event = $oConnection[ $k - 1 ]->last_event();
165 warn( "connection ", $k - 1, ": event $event (",
166 ZOOM::event_str($event), ")\n" )
168 last if $event == ZOOM::Event::ZEND;
173 warn $serverhost[$k] if $DEBUG;
174 my ( $error, $errmsg, $addinfo, $diagset ) =
175 $oConnection[$k]->error_x();
177 warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
182 my $numresults = $oResult[$k]->size();
185 if ( $numresults > 0 ) {
188 $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
192 my $rec = $oResult[$k]->record($i);
194 $marcdata = $rec->raw();
195 $marcrecord = FixEncoding($marcdata);
196 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
197 ## In HEAD i change everything to UTF-8
198 # In rel2_2 i am not sure what encoding is so no character conversion is done here
199 ##Add necessary encoding changes to here -TG
200 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
201 $oldbiblio->{isbn} =~ s/ |-|\.//g,
202 $oldbiblio->{issn} =~ s/ |-|\.//g,
204 $notmarcrecord, $alreadyindb, $alreadyinfarm,
205 $imported, $breedingid
207 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
216 $row_data{toggle} = $toggle;
217 $row_data{server} = $serverhost[$k];
218 $row_data{isbn} = $oldbiblio->{isbn};
219 $row_data{title} = $oldbiblio->{title};
220 $row_data{author} = $oldbiblio->{author};
221 $row_data{breedingid} = $breedingid;
222 $row_data{biblionumber} = $biblionumber;
223 push( @breeding_loop, \%row_data );
228 $numberpending = $nremaining - 1;
230 breeding_loop => \@breeding_loop,
231 server => $serverhost[$k],
232 numberpending => $numberpending,
235 output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
237 # print $template->output if $firstresult !=1;
241 if ( --$nremaining > 0 ) {