Bug 6679 - [SIGNED-OFF] fix 3 perlcritic violations in C4/Message.pm
[koha.git] / cataloguing / z3950_search.pl
blob5e22636b5a463ea99c23031250ad8373011e493a
1 #!/usr/bin/perl
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
11 # version.
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.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Biblio;
28 use C4::Context;
29 use C4::Breeding;
30 use C4::Koha;
31 use C4::Charset;
32 use ZOOM;
34 my $input = new CGI;
35 my $dbh = C4::Context->dbh;
36 my $error = $input->param('error');
37 my $biblionumber = $input->param('biblionumber') || 0;
38 my $frameworkcode = $input->param('frameworkcode');
39 my $title = $input->param('title');
40 my $author = $input->param('author');
41 my $isbn = $input->param('isbn');
42 my $issn = $input->param('issn');
43 my $lccn = $input->param('lccn');
44 my $lccall = $input->param('lccall');
45 my $subject = $input->param('subject');
46 my $dewey = $input->param('dewey');
47 my $controlnumber = $input->param('controlnumber');
48 my $stdid = $input->param('stdid');
49 my $srchany = $input->param('srchany');
50 my $random = $input->param('random') || rand(1000000000); # this var is not useful anymore just kept for rel2_2 compatibility
51 my $op = $input->param('op');
52 my $numberpending;
53 my $attr = '';
54 my $term;
55 my $host;
56 my $server;
57 my $database;
58 my $port;
59 my $marcdata;
60 my @encoding;
61 my @results;
62 my $count;
63 my $record;
64 my $oldbiblio;
65 my $errmsg;
66 my @serverloop = ();
67 my @serverhost;
68 my @servername;
69 my @breeding_loop = ();
71 my $DEBUG = 0; # if set to 1, many debug message are send on syslog.
73 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
74 template_name => "cataloguing/z3950_search.tmpl",
75 query => $input,
76 type => "intranet",
77 authnotrequired => 1,
78 flagsrequired => { catalogue => 1 },
79 debug => 1,
80 });
82 $template->param( frameworkcode => $frameworkcode, );
84 if ( $op ne "do_search" ) {
85 my $sth = $dbh->prepare("SELECT id,host,name,checked FROM z3950servers ORDER BY rank, name");
86 $sth->execute();
87 my $serverloop = $sth->fetchall_arrayref( {} );
88 $template->param(
89 isbn => $isbn,
90 issn => $issn,
91 lccn => $lccn,
92 lccall => $lccall,
93 title => $title,
94 author => $author,
95 controlnumber=> $controlnumber,
96 stdid => $stdid,
97 srchany => $srchany,
98 serverloop => $serverloop,
99 opsearch => "search",
100 biblionumber => $biblionumber,
102 output_html_with_http_headers $input, $cookie, $template->output;
104 else {
105 my @id = $input->param('id');
107 if ( not defined @id ) {
108 # empty server list -> report and exit
109 $template->param( emptyserverlist => 1 );
110 output_html_with_http_headers $input, $cookie, $template->output;
111 exit;
114 my @oConnection;
115 my @oResult;
116 my @errconn;
117 my $s = 0;
118 my $query = '';
119 my $nterms;
120 if ($isbn) {
121 $term=$isbn;
122 $query .= " \@attr 1=7 \@attr 5=1 \"$term\" ";
123 $nterms++;
125 if ($issn) {
126 $term=$issn;
127 $query .= " \@attr 1=8 \@attr 5=1 \"$term\" ";
128 $nterms++;
130 if ($title) {
131 utf8::decode($title);
132 $query .= " \@attr 1=4 \"$title\" ";
133 $nterms++;
135 if ($author) {
136 utf8::decode($author);
137 $query .= " \@attr 1=1003 \"$author\" ";
138 $nterms++;
140 if ($dewey) {
141 $query .= " \@attr 1=16 \"$dewey\" ";
142 $nterms++;
144 if ($subject) {
145 utf8::decode($subject);
146 $query .= " \@attr 1=21 \"$subject\" ";
147 $nterms++;
149 if ($lccn) {
150 $query .= " \@attr 1=9 $lccn ";
151 $nterms++;
153 if ($lccall) {
154 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
155 $nterms++;
157 if ($controlnumber) {
158 $query .= " \@attr 1=12 \"$controlnumber\" ";
159 $nterms++;
161 if ($stdid) {
162 $query .= " \@attr 1=1007 \"$stdid\" ";
163 $nterms++;
165 if ($srchany) {
166 $query .= " \@attr 1=1016 \"$srchany\" ";
167 $nterms++;
169 for my $i (1..$nterms-1) {
170 $query = "\@and " . $query;
172 warn "query ".$query if $DEBUG;
174 foreach my $servid (@id) {
175 my $sth = $dbh->prepare("SELECT * FROM z3950servers WHERE id=? ORDER BY rank, name");
176 $sth->execute($servid);
177 while ( $server = $sth->fetchrow_hashref ) {
178 warn "serverinfo ".join(':',%$server) if $DEBUG;
179 my $option1 = new ZOOM::Options();
180 $option1->option('async' => 1);
181 $option1->option('elementSetName', 'F');
182 $option1->option('databaseName', $server->{db});
183 $option1->option('user', $server->{userid} ) if $server->{userid};
184 $option1->option('password', $server->{password}) if $server->{password};
185 $option1->option('preferredRecordSyntax', $server->{syntax});
186 $option1->option( 'timeout', $server->{timeout} ) if ($server->{timeout});
187 $oConnection[$s] = create ZOOM::Connection($option1)
188 || $DEBUG
189 && warn( "" . $oConnection[$s]->errmsg() );
190 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
191 $oConnection[$s]->connect( $server->{host}, $server->{port} )
192 || $DEBUG
193 && warn( "" . $oConnection[$s]->errmsg() );
194 $serverhost[$s] = $server->{host};
195 $servername[$s] = $server->{name};
196 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
197 $s++;
198 } ## while fetch
199 } # foreach
200 my $nremaining = $s;
201 my $firstresult = 1;
203 for ( my $z = 0 ; $z < $s ; $z++ ) {
204 warn "doing the search" if $DEBUG;
205 $oResult[$z] = $oConnection[$z]->search_pqf($query)
206 || $DEBUG
207 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
209 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
212 AGAIN:
213 my $k;
214 my $event;
215 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
216 $event = $oConnection[ $k - 1 ]->last_event();
217 warn( "connection ", $k - 1, ": event $event (",
218 ZOOM::event_str($event), ")\n" )
219 if $DEBUG;
220 last if $event == ZOOM::Event::ZEND;
223 if ( $k != 0 ) {
224 $k--;
225 warn $serverhost[$k] if $DEBUG;
226 my ( $error, $errmsg, $addinfo, $diagset ) =
227 $oConnection[$k]->error_x();
228 if ($error) {
229 if ($error =~ m/^(10000|10007)$/ ) {
230 push(@errconn, {'server' => $serverhost[$k], 'error' => $error});
232 $DEBUG and warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
234 else {
235 my $numresults = $oResult[$k]->size();
236 my $i;
237 my $result = '';
238 if ( $numresults > 0 ) {
239 for ($i = 0; $i < (($numresults < 20) ? $numresults : 20); $i++) {
240 my $rec = $oResult[$k]->record($i);
241 if ($rec) {
242 my $marcrecord;
243 $marcdata = $rec->raw();
245 my ($charset_result, $charset_errors);
246 ($marcrecord, $charset_result, $charset_errors) =
247 MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
248 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
249 ## In HEAD i change everything to UTF-8
250 # In rel2_2 i am not sure what encoding is so no character conversion is done here
251 ##Add necessary encoding changes to here -TG
252 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
253 $oldbiblio->{isbn} =~ s/ |-|\.//g if $oldbiblio->{isbn};
254 # pad | and ( with spaces to allow line breaks in the HTML
255 $oldbiblio->{isbn} =~ s/\|/ \| /g if $oldbiblio->{isbn};
256 $oldbiblio->{isbn} =~ s/\(/ \(/g if $oldbiblio->{isbn};
258 $oldbiblio->{issn} =~ s/ |-|\.//g if $oldbiblio->{issn};
259 # pad | and ( with spaces to allow line breaks in the HTML
260 $oldbiblio->{issn} =~ s/\|/ \| /g if $oldbiblio->{issn};
261 $oldbiblio->{issn} =~ s/\(/ \(/g if $oldbiblio->{issn};
262 my (
263 $notmarcrecord, $alreadyindb, $alreadyinfarm,
264 $imported, $breedingid
266 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
267 my %row_data;
268 $row_data{server} = $servername[$k];
269 $row_data{isbn} = $oldbiblio->{isbn};
270 $row_data{lccn} = $oldbiblio->{lccn};
271 $row_data{title} = $oldbiblio->{title};
272 $row_data{author} = $oldbiblio->{author};
273 $row_data{date} = $oldbiblio->{copyrightdate};
274 $row_data{edition} = $oldbiblio->{editionstatement};
275 $row_data{breedingid} = $breedingid;
276 $row_data{biblionumber} = $biblionumber;
277 push( @breeding_loop, \%row_data );
279 } else {
280 push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
281 } # $rec
283 } #$numresults
285 } # if $k !=0
286 $numberpending = $nremaining - 1;
287 $template->param(
288 breeding_loop => \@breeding_loop,
289 server => $servername[$k],
290 numberpending => $numberpending,
291 biblionumber => $biblionumber,
292 errconn => \@errconn
295 output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
297 # print $template->output if $firstresult !=1;
298 $firstresult++;
300 MAYBE_AGAIN:
301 if ( --$nremaining > 0 ) {
302 goto AGAIN;
304 } ## if op=search