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.
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38 template_name => "acqui/z3950_search.tmpl",
42 flagsrequired => { acquisition => 'order_manage' },
47 my $dbh = C4::Context->dbh;
48 my $error = $input->param('error');
49 my $biblionumber = $input->param('biblionumber');
50 $biblionumber = 0 unless $biblionumber;
51 my $frameworkcode = $input->param('frameworkcode');
52 my $title = $input->param('title');
53 my $author = $input->param('author');
54 my $isbn = $input->param('isbn');
55 my $issn = $input->param('issn');
56 my $lccn = $input->param('lccn');
57 my $lccall = $input->param('lccall');
58 my $subject= $input->param('subject');
59 my $dewey = $input->param('dewey');
60 my $op = $input->param('op');
61 my $booksellerid = $input->param('booksellerid');
62 my $basketno = $input->param('basketno');
81 my @breeding_loop = ();
82 my $random = $input->param('random');
84 { # this var is not useful anymore just kept to keep rel2_2 compatibility
85 $random = rand(1000000000);
88 my $DEBUG = 0; # if set to 1, many debug message are send on syslog.
91 my $frameworks = getframeworks;
92 my @frameworkcodeloop;
93 foreach my $thisframeworkcode ( keys %$frameworks ) {
95 value => $thisframeworkcode,
96 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
98 if ( $row{'value'} eq $frameworkcode){
99 $row{'active'} = 'true';
101 push @frameworkcodeloop, \%row;
104 $template->param( frameworkcode => $frameworkcode,
105 frameworkcodeloop => \@frameworkcodeloop,
106 booksellerid => $booksellerid,
107 basketno => $basketno);
110 if ( $op ne "do_search" ) {
111 my $sth = $dbh->prepare("select id,host,name,checked from z3950servers order by host");
113 my $serverloop = $sth->fetchall_arrayref( {} );
121 serverloop => $serverloop,
122 opsearch => "search",
123 biblionumber => $biblionumber,
125 output_html_with_http_headers $input, $cookie, $template->output;
128 my @id = $input->param('id');
134 if ($isbn || $issn) {
135 $term=$isbn if ($isbn);
136 $term=$issn if ($issn);
137 $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
141 utf8::decode($title);
142 $query .= " \@attr 1=4 \"$title\" ";
146 utf8::decode($author);
147 $query .= " \@attr 1=1003 \"$author\" ";
151 $query .= " \@attr 1=16 \"$dewey\" ";
155 utf8::decode($subject);
156 $query .= " \@attr 1=21 \"$subject\" ";
160 $query .= " \@attr 1=9 $lccn ";
164 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
167 for my $i (1..$nterms-1) {
168 $query = "\@and " . $query;
170 warn "query ".$query if $DEBUG;
172 foreach my $servid (@id) {
173 my $sth = $dbh->prepare("select * from z3950servers where id=?");
174 $sth->execute($servid);
175 while ( $server = $sth->fetchrow_hashref ) {
176 warn "serverinfo ".join(':',%$server) if $DEBUG;
177 my $noconnection = 0;
178 my $option1 = new ZOOM::Options();
179 $option1->option( 'async' => 1 );
180 $option1->option( 'elementSetName', 'F' );
181 $option1->option( 'databaseName', $server->{db} );
182 $option1->option( 'user', $server->{userid} ) if $server->{userid};
183 $option1->option( 'password', $server->{password} )
184 if $server->{password};
185 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
186 $oConnection[$s] = create ZOOM::Connection($option1)
188 && warn( "" . $oConnection[$s]->errmsg() );
189 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
190 $oConnection[$s]->connect( $server->{host}, $server->{port} )
192 && warn( "" . $oConnection[$s]->errmsg() );
193 $serverhost[$s] = $server->{host};
194 $servername[$s] = $server->{name};
195 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
202 for ( my $z = 0 ; $z < $s ; $z++ ) {
203 warn "doing the search" if $DEBUG;
204 $oResult[$z] = $oConnection[$z]->search_pqf($query)
206 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
208 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
214 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
215 $event = $oConnection[ $k - 1 ]->last_event();
216 warn( "connection ", $k - 1, ": event $event (",
217 ZOOM::event_str($event), ")\n" )
219 last if $event == ZOOM::Event::ZEND;
224 warn $serverhost[$k] if $DEBUG;
225 my ( $error, $errmsg, $addinfo, $diagset ) =
226 $oConnection[$k]->error_x();
228 warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
233 my $numresults = $oResult[$k]->size();
236 if ( $numresults > 0 ) {
239 $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
243 my $rec = $oResult[$k]->record($i);
246 $marcdata = $rec->raw();
248 my ($charset_result, $charset_errors);
249 ($marcrecord, $charset_result, $charset_errors) =
250 MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
251 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
252 ## In HEAD i change everything to UTF-8
253 # In rel2_2 i am not sure what encoding is so no character conversion is done here
254 ##Add necessary encoding changes to here -TG
255 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
256 $oldbiblio->{isbn} =~ s/ |-|\.//g if $oldbiblio->{isbn};
257 # pad | and ( with spaces to allow line breaks in the HTML
258 $oldbiblio->{isbn} =~ s/\|/ \| /g if $oldbiblio->{isbn};
259 $oldbiblio->{isbn} =~ s/\(/ \(/g if $oldbiblio->{isbn};
261 $oldbiblio->{issn} =~ s/ |-|\.//g if $oldbiblio->{issn};
262 # pad | and ( with spaces to allow line breaks in the HTML
263 $oldbiblio->{issn} =~ s/\|/ \| /g if $oldbiblio->{issn};
264 $oldbiblio->{issn} =~ s/\(/ \(/g if $oldbiblio->{issn};
266 $notmarcrecord, $alreadyindb, $alreadyinfarm,
267 $imported, $breedingid
269 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
277 $row_data{toggle} = $toggle;
278 $row_data{server} = $servername[$k];
279 $row_data{isbn} = $oldbiblio->{isbn};
280 $row_data{lccn} = $oldbiblio->{lccn};
281 $row_data{title} = $oldbiblio->{title};
282 $row_data{author} = $oldbiblio->{author};
283 $row_data{breedingid} = $breedingid;
284 $row_data{biblionumber} = $biblionumber;
285 push( @breeding_loop, \%row_data );
288 push(@breeding_loop,{'toggle'=>($i % 2)?1:0,'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
294 $numberpending = $nremaining - 1;
296 breeding_loop => \@breeding_loop,
297 server => $servername[$k],
298 numberpending => $numberpending,
300 output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
302 # print $template->output if $firstresult !=1;
306 while ( --$nremaining > 0 ) {