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');
83 my @breeding_loop = ();
84 my $random = $input->param('random');
86 { # this var is not useful anymore just kept to keep rel2_2 compatibility
87 $random = rand(1000000000);
90 my $DEBUG = $ENV{DEBUG
} || 0; # if set to 1, many debug message are send on syslog.
93 my $frameworks = getframeworks
;
94 my @frameworkcodeloop;
95 foreach my $thisframeworkcode ( keys %$frameworks ) {
97 value
=> $thisframeworkcode,
98 frameworktext
=> $frameworks->{$thisframeworkcode}->{'frameworktext'},
100 if ( $row{'value'} eq $frameworkcode){
101 $row{'active'} = 'true';
103 push @frameworkcodeloop, \
%row;
106 my $vendor = GetBookSellerFromId
($booksellerid);
107 $template->param( frameworkcode
=> $frameworkcode,
108 frameworkcodeloop
=> \
@frameworkcodeloop,
109 booksellerid
=> $booksellerid,
110 basketno
=> $basketno,
111 name
=> $vendor->{'name'}
116 if ( $op ne "do_search" ) {
117 my $sth = $dbh->prepare("select id,host,name,checked from z3950servers order by host");
119 my $serverloop = $sth->fetchall_arrayref( {} );
127 controlnumber
=> $controlnumber,
128 serverloop
=> $serverloop,
129 opsearch
=> "search",
130 biblionumber
=> $biblionumber,
132 output_html_with_http_headers
$input, $cookie, $template->output;
136 my @id = $input->param('id');
139 # empty server list -> report and exit
140 $template->param( emptyserverlist
=> 1 );
141 output_html_with_http_headers
$input, $cookie, $template->output;
151 if ($isbn || $issn) {
152 $term=$isbn if ($isbn);
153 $term=$issn if ($issn);
154 $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
158 utf8
::decode
($title);
159 $query .= " \@attr 1=4 \"$title\" ";
163 utf8
::decode
($author);
164 $query .= " \@attr 1=1003 \"$author\" ";
168 $query .= " \@attr 1=16 \"$dewey\" ";
172 utf8
::decode
($subject);
173 $query .= " \@attr 1=21 \"$subject\" ";
177 $query .= " \@attr 1=9 $lccn ";
181 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
184 if ($controlnumber) {
185 $query .= " \@attr 1=12 \"$controlnumber\" ";
188 for my $i (1..$nterms-1) {
189 $query = "\@and " . $query;
191 warn "query ".$query if $DEBUG;
193 foreach my $servid (@id) {
194 my $sth = $dbh->prepare("select * from z3950servers where id=?");
195 $sth->execute($servid);
196 while ( $server = $sth->fetchrow_hashref ) {
197 warn "serverinfo ".join(':',%$server) if $DEBUG;
198 my $option1 = new ZOOM
::Options
();
199 $option1->option( 'async' => 1 );
200 $option1->option( 'elementSetName', 'F' );
201 $option1->option( 'databaseName', $server->{db
} );
202 $option1->option( 'user', $server->{userid
} ) if $server->{userid
};
203 $option1->option( 'password', $server->{password
} )
204 if $server->{password
};
205 $option1->option( 'preferredRecordSyntax', $server->{syntax
} );
206 $oConnection[$s] = create ZOOM
::Connection
($option1)
208 && warn( "" . $oConnection[$s]->errmsg() );
209 warn( "server data", $server->{name
}, $server->{port
} ) if $DEBUG;
210 $oConnection[$s]->connect( $server->{host
}, $server->{port
} )
212 && warn( "" . $oConnection[$s]->errmsg() );
213 $serverhost[$s] = $server->{host
};
214 $servername[$s] = $server->{name
};
215 $encoding[$s] = ($server->{encoding
}?
$server->{encoding
}:"iso-5426");
222 for ( my $z = 0 ; $z < $s ; $z++ ) {
223 warn "doing the search" if $DEBUG;
224 $oResult[$z] = $oConnection[$z]->search_pqf($query)
226 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
228 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
231 warn "# nremaining = $nremaining\n" if $DEBUG;
233 while ( $nremaining-- ) {
237 while ( ( $k = ZOOM
::event
( \
@oConnection ) ) != 0 ) {
238 $event = $oConnection[ $k - 1 ]->last_event();
239 warn( "connection ", $k - 1, ": event $event (",
240 ZOOM
::event_str
($event), ")\n" )
242 last if $event == ZOOM
::Event
::ZEND
;
247 warn "event from $k server = ",$serverhost[$k] if $DEBUG;
248 my ( $error, $errmsg, $addinfo, $diagset ) =
249 $oConnection[$k]->error_x();
251 if ($error =~ m/^(10000|10007)$/ ) {
252 push(@errconn, {'server' => $serverhost[$k]});
254 $DEBUG and warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
257 my $numresults = $oResult[$k]->size();
258 warn "numresults = $numresults" if $DEBUG;
261 if ( $numresults > 0 ) {
264 $i < ( ( $numresults < 20 ) ?
($numresults) : (20) ) ;
268 my $rec = $oResult[$k]->record($i);
271 $marcdata = $rec->raw();
273 my ($charset_result, $charset_errors);
274 ($marcrecord, $charset_result, $charset_errors) =
275 MarcToUTF8Record
($marcdata, C4
::Context
->preference('marcflavour'), $encoding[$k]);
276 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
277 ## In HEAD i change everything to UTF-8
278 # In rel2_2 i am not sure what encoding is so no character conversion is done here
279 ##Add necessary encoding changes to here -TG
280 my $oldbiblio = TransformMarcToKoha
( $dbh, $marcrecord, "" );
281 $oldbiblio->{isbn
} =~ s/ |-|\.//g if $oldbiblio->{isbn
};
282 # pad | and ( with spaces to allow line breaks in the HTML
283 $oldbiblio->{isbn
} =~ s/\|/ \| /g if $oldbiblio->{isbn
};
284 $oldbiblio->{isbn
} =~ s/\(/ \(/g if $oldbiblio->{isbn
};
286 $oldbiblio->{issn
} =~ s/ |-|\.//g if $oldbiblio->{issn
};
287 # pad | and ( with spaces to allow line breaks in the HTML
288 $oldbiblio->{issn
} =~ s/\|/ \| /g if $oldbiblio->{issn
};
289 $oldbiblio->{issn
} =~ s/\(/ \(/g if $oldbiblio->{issn
};
291 $notmarcrecord, $alreadyindb, $alreadyinfarm,
292 $imported, $breedingid
294 = ImportBreeding
( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
296 $row_data{server
} = $servername[$k];
297 $row_data{isbn
} = $oldbiblio->{isbn
};
298 $row_data{lccn
} = $oldbiblio->{lccn
};
299 $row_data{title
} = $oldbiblio->{title
};
300 $row_data{author
} = $oldbiblio->{author
};
301 $row_data{breedingid
} = $breedingid;
302 $row_data{biblionumber
} = $biblionumber;
303 push( @breeding_loop, \
%row_data );
306 push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
312 # print $template->output if $firstresult !=1;
318 breeding_loop
=> \
@breeding_loop,
319 #server => $servername[$k],
320 numberpending
=> $nremaining > 0 ?
$nremaining : 0,
323 output_html_with_http_headers
$input, $cookie, $template->output;