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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37 template_name => "acqui/z3950_search.tmpl",
41 flagsrequired => { acquisition => 'order_manage' },
46 my $dbh = C4::Context->dbh;
47 my $error = $input->param('error');
48 my $biblionumber = $input->param('biblionumber');
49 $biblionumber = 0 unless $biblionumber;
50 my $frameworkcode = $input->param('frameworkcode');
51 my $title = $input->param('title');
52 my $author = $input->param('author');
53 my $isbn = $input->param('isbn');
54 my $issn = $input->param('issn');
55 my $lccn = $input->param('lccn');
56 my $subject= $input->param('subject');
57 my $dewey = $input->param('dewey');
58 my $op = $input->param('op');
59 my $booksellerid = $input->param('booksellerid');
60 my $basketno = $input->param('basketno');
79 my @breeding_loop = ();
80 my $random = $input->param('random');
82 { # this var is not useful anymore just kept to keep rel2_2 compatibility
83 $random = rand(1000000000);
86 my $DEBUG = 0; # if set to 1, many debug message are send on syslog.
89 my $frameworks = getframeworks;
90 my @frameworkcodeloop;
91 foreach my $thisframeworkcode ( keys %$frameworks ) {
93 value => $thisframeworkcode,
94 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
96 if ( $row->{'value'} eq $frameworkcode){
97 $row->{'active'} = 'true';
99 push @frameworkcodeloop, $row;
102 $template->param( frameworkcode => $frameworkcode,
103 frameworkcodeloop => \@frameworkcodeloop,
104 booksellerid => $booksellerid,
105 basketno => $basketno);
108 if ( $op ne "do_search" ) {
109 my $sth = $dbh->prepare("select id,host,name,checked from z3950servers order by host");
111 my $serverloop = $sth->fetchall_arrayref( {} );
118 serverloop => $serverloop,
119 opsearch => "search",
120 biblionumber => $biblionumber,
122 output_html_with_http_headers $input, $cookie, $template->output;
125 my @id = $input->param('id');
131 if ($isbn || $issn) {
132 $term=$isbn if ($isbn);
133 $term=$issn if ($issn);
134 $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
138 utf8::decode($title);
139 $query .= " \@attr 1=4 \"$title\" ";
143 utf8::decode($author);
144 $query .= " \@attr 1=1003 \"$author\" ";
148 $query .= " \@attr 1=16 \"$dewey\" ";
152 utf8::decode($subject);
153 $query .= " \@attr 1=21 \"$subject\" ";
157 $query .= " \@attr 1=9 $lccn ";
160 for my $i (1..$nterms-1) {
161 $query = "\@and " . $query;
163 warn "query ".$query if $DEBUG;
165 foreach my $servid (@id) {
166 my $sth = $dbh->prepare("select * from z3950servers where id=?");
167 $sth->execute($servid);
168 while ( $server = $sth->fetchrow_hashref ) {
169 warn "serverinfo ".join(':',%$server) if $DEBUG;
170 my $noconnection = 0;
171 my $option1 = new ZOOM::Options();
172 $option1->option( 'async' => 1 );
173 $option1->option( 'elementSetName', 'F' );
174 $option1->option( 'databaseName', $server->{db} );
175 $option1->option( 'user', $server->{userid} ) if $server->{userid};
176 $option1->option( 'password', $server->{password} )
177 if $server->{password};
178 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
179 $oConnection[$s] = create ZOOM::Connection($option1)
181 && warn( "" . $oConnection[$s]->errmsg() );
182 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
183 $oConnection[$s]->connect( $server->{host}, $server->{port} )
185 && warn( "" . $oConnection[$s]->errmsg() );
186 $serverhost[$s] = $server->{host};
187 $servername[$s] = $server->{name};
188 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
195 for ( my $z = 0 ; $z < $s ; $z++ ) {
196 warn "doing the search" if $DEBUG;
197 $oResult[$z] = $oConnection[$z]->search_pqf($query)
199 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
201 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
207 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
208 $event = $oConnection[ $k - 1 ]->last_event();
209 warn( "connection ", $k - 1, ": event $event (",
210 ZOOM::event_str($event), ")\n" )
212 last if $event == ZOOM::Event::ZEND;
217 warn $serverhost[$k] if $DEBUG;
218 my ( $error, $errmsg, $addinfo, $diagset ) =
219 $oConnection[$k]->error_x();
221 warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
226 my $numresults = $oResult[$k]->size();
229 if ( $numresults > 0 ) {
232 $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
236 my $rec = $oResult[$k]->record($i);
239 $marcdata = $rec->raw();
241 my ($charset_result, $charset_errors);
242 ($marcrecord, $charset_result, $charset_errors) =
243 MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
244 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
245 ## In HEAD i change everything to UTF-8
246 # In rel2_2 i am not sure what encoding is so no character conversion is done here
247 ##Add necessary encoding changes to here -TG
248 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
249 $oldbiblio->{isbn} =~ s/ |-|\.//g,
250 $oldbiblio->{issn} =~ s/ |-|\.//g,
252 $notmarcrecord, $alreadyindb, $alreadyinfarm,
253 $imported, $breedingid
255 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
263 $row_data{toggle} = $toggle;
264 $row_data{server} = $servername[$k];
265 $row_data{isbn} = $oldbiblio->{isbn};
266 $row_data{lccn} = $oldbiblio->{lccn};
267 $row_data{title} = $oldbiblio->{title};
268 $row_data{author} = $oldbiblio->{author};
269 $row_data{breedingid} = $breedingid;
270 $row_data{biblionumber} = $biblionumber;
271 push( @breeding_loop, \%row_data );
274 push(@breeding_loop,{'toggle'=>($i % 2)?1:0,'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
280 $numberpending = $nremaining - 1;
282 breeding_loop => \@breeding_loop,
283 server => $servername[$k],
284 numberpending => $numberpending,
287 output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
289 # print $template->output if $firstresult !=1;
293 if ( --$nremaining > 0 ) {