3 # This is a completely new Z3950 clients search using async ZOOM -TG 02/11/06
4 # Copyright 2000-2002 Katipo Communications
5 # Copyright 2010 Catalyst IT
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 use C4::Bookseller qw/ GetBookSellerFromId /;
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39 template_name => "acqui/z3950_search.tmpl",
43 flagsrequired => { acquisition => 'order_manage' },
49 my $dbh = C4::Context->dbh;
50 my $error = $input->param('error');
51 my $biblionumber = $input->param('biblionumber');
52 $biblionumber = 0 unless $biblionumber;
53 my $frameworkcode = $input->param('frameworkcode');
54 my $title = $input->param('title');
55 my $author = $input->param('author');
56 my $isbn = $input->param('isbn');
57 my $issn = $input->param('issn');
58 my $lccn = $input->param('lccn');
59 my $lccall = $input->param('lccall');
60 my $subject = $input->param('subject');
61 my $dewey = $input->param('dewey');
62 my $controlnumber = $input->param('controlnumber');
63 my $op = $input->param('op');
64 my $booksellerid = $input->param('booksellerid');
65 my $basketno = $input->param('basketno');
67 my $page = $input->param('current_page') || 1;
68 $page = $input->param('goto_page') if $input->param('changepage_goto');
89 my @breeding_loop = ();
90 my $random = $input->param('random');
92 { # this var is not useful anymore just kept to keep rel2_2 compatibility
93 $random = rand(1000000000);
96 my $DEBUG = $ENV{DEBUG} || 0; # if set to 1, many debug message are send on syslog.
99 my $frameworks = getframeworks;
100 my @frameworkcodeloop;
101 foreach my $thisframeworkcode ( keys %$frameworks ) {
103 value => $thisframeworkcode,
104 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
106 if ( $row{'value'} eq $frameworkcode){
107 $row{'active'} = 'true';
109 push @frameworkcodeloop, \%row;
112 my $vendor = GetBookSellerFromId($booksellerid);
113 $template->param( frameworkcode => $frameworkcode,
114 frameworkcodeloop => \@frameworkcodeloop,
115 booksellerid => $booksellerid,
116 basketno => $basketno,
117 name => $vendor->{'name'}
128 controlnumber=> $controlnumber,
129 biblionumber => $biblionumber,
134 if ( $op ne "do_search" ) {
135 my $sth = $dbh->prepare("select id,host,name,checked from z3950servers order by host");
137 my $serverloop = $sth->fetchall_arrayref( {} );
139 serverloop => $serverloop,
140 opsearch => "search",
142 output_html_with_http_headers $input, $cookie, $template->output;
146 my @id = $input->param('id');
149 # empty server list -> report and exit
150 $template->param( emptyserverlist => 1 );
151 output_html_with_http_headers $input, $cookie, $template->output;
163 $query .= " \@attr 1=7 \@attr 5=1 \"$term\" ";
168 $query .= " \@attr 1=8 \@attr 5=1 \"$term\" ";
172 utf8::decode($title);
173 $query .= " \@attr 1=4 \"$title\" ";
177 utf8::decode($author);
178 $query .= " \@attr 1=1003 \"$author\" ";
182 $query .= " \@attr 1=16 \"$dewey\" ";
186 utf8::decode($subject);
187 $query .= " \@attr 1=21 \"$subject\" ";
191 $query .= " \@attr 1=9 $lccn ";
195 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
198 if ($controlnumber) {
199 $query .= " \@attr 1=12 \"$controlnumber\" ";
202 for my $i (1..$nterms-1) {
203 $query = "\@and " . $query;
205 warn "query ".$query if $DEBUG;
207 foreach my $servid (@id) {
208 my $sth = $dbh->prepare("select * from z3950servers where id=?");
209 $sth->execute($servid);
210 while ( $server = $sth->fetchrow_hashref ) {
211 warn "serverinfo ".join(':',%$server) if $DEBUG;
212 my $option1 = new ZOOM::Options();
213 $option1->option( 'async' => 1 );
214 $option1->option( 'elementSetName', 'F' );
215 $option1->option( 'databaseName', $server->{db} );
216 $option1->option( 'user', $server->{userid} ) if $server->{userid};
217 $option1->option( 'password', $server->{password} )
218 if $server->{password};
219 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
220 $oConnection[$s] = create ZOOM::Connection($option1)
222 && warn( "" . $oConnection[$s]->errmsg() );
223 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
224 $oConnection[$s]->connect( $server->{host}, $server->{port} )
226 && warn( "" . $oConnection[$s]->errmsg() );
227 $serverhost[$s] = $server->{host};
228 $servername[$s] = $server->{name};
229 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
236 for ( my $z = 0 ; $z < $s ; $z++ ) {
237 warn "doing the search" if $DEBUG;
238 $oResult[$z] = $oConnection[$z]->search_pqf($query)
240 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
242 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
245 warn "# nremaining = $nremaining\n" if $DEBUG;
247 while ( $nremaining-- ) {
251 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
252 $event = $oConnection[ $k - 1 ]->last_event();
253 warn( "connection ", $k - 1, ": event $event (",
254 ZOOM::event_str($event), ")\n" )
256 last if $event == ZOOM::Event::ZEND;
261 warn "event from $k server = ",$serverhost[$k] if $DEBUG;
262 my ( $error, $errmsg, $addinfo, $diagset ) =
263 $oConnection[$k]->error_x();
265 if ($error =~ m/^(10000|10007)$/ ) {
266 push(@errconn, {'server' => $serverhost[$k]});
268 $DEBUG and warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
271 my $numresults = $oResult[$k]->size();
272 warn "numresults = $numresults" if $DEBUG;
275 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
276 $show_next = 1 if $numresults >= ($page*20);
277 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
278 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
279 my $rec = $oResult[$k]->record($i);
282 $marcdata = $rec->raw();
284 my ($charset_result, $charset_errors);
285 ($marcrecord, $charset_result, $charset_errors) =
286 MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
287 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
288 ## In HEAD i change everything to UTF-8
289 # In rel2_2 i am not sure what encoding is so no character conversion is done here
290 ##Add necessary encoding changes to here -TG
292 # Normalize the record so it doesn't have separated diacritics
293 SetUTF8Flag($marcrecord);
295 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
296 $oldbiblio->{isbn} =~ s/ |-|\.//g if $oldbiblio->{isbn};
297 # pad | and ( with spaces to allow line breaks in the HTML
298 $oldbiblio->{isbn} =~ s/\|/ \| /g if $oldbiblio->{isbn};
299 $oldbiblio->{isbn} =~ s/\(/ \(/g if $oldbiblio->{isbn};
301 $oldbiblio->{issn} =~ s/ |-|\.//g if $oldbiblio->{issn};
302 # pad | and ( with spaces to allow line breaks in the HTML
303 $oldbiblio->{issn} =~ s/\|/ \| /g if $oldbiblio->{issn};
304 $oldbiblio->{issn} =~ s/\(/ \(/g if $oldbiblio->{issn};
306 $notmarcrecord, $alreadyindb, $alreadyinfarm,
307 $imported, $breedingid
309 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
311 $row_data{server} = $servername[$k];
312 $row_data{isbn} = $oldbiblio->{isbn};
313 $row_data{lccn} = $oldbiblio->{lccn};
314 $row_data{title} = $oldbiblio->{title};
315 $row_data{author} = $oldbiblio->{author};
316 $row_data{breedingid} = $breedingid;
317 $row_data{biblionumber} = $biblionumber;
318 push( @breeding_loop, \%row_data );
321 push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
327 my $numberpending = $nremaining - 1;
330 foreach my $id (@id) {
331 push(@servers,{id => $id});
335 breeding_loop => \@breeding_loop,
336 server => $servername[$k],
337 numberpending => $numberpending,
338 current_page => $page,
339 servers => \@servers,
340 total_pages => $total_pages,
342 $template->param(show_nextbutton=>1) if $show_next;
343 $template->param(show_prevbutton=>1) if $page != 1;
345 # print $template->output if $firstresult !=1;
351 breeding_loop => \@breeding_loop,
352 #server => $servername[$k],
353 numberpending => $nremaining > 0 ? $nremaining : 0,
356 output_html_with_http_headers $input, $cookie, $template->output;