Bug 3955: Fixes leading space error in search.
[koha.git] / cataloguing / z3950_search.pl
blobbe180191bec3e14cf7537d2002e51579ef12f814
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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
21 use strict;
22 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Biblio;
27 use C4::Context;
28 use C4::Breeding;
29 use C4::Koha;
30 use C4::Charset;
31 use ZOOM;
33 my $input = new CGI;
34 my $dbh = C4::Context->dbh;
35 my $error = $input->param('error');
36 my $biblionumber = $input->param('biblionumber') || 0;
37 my $frameworkcode = $input->param('frameworkcode');
38 my $title = $input->param('title');
39 my $author = $input->param('author');
40 my $isbn = $input->param('isbn');
41 my $issn = $input->param('issn');
42 my $lccn = $input->param('lccn');
43 my $subject = $input->param('subject');
44 my $dewey = $input->param('dewey');
45 my $controlnumber = $input->param('controlnumber');
46 my $stdid = $input->param('stdid');
47 my $srchany = $input->param('srchany');
48 my $random = $input->param('random') || rand(1000000000); # this var is not useful anymore just kept for rel2_2 compatibility
49 my $op = $input->param('op');
50 my $numberpending;
51 my $attr = '';
52 my $term;
53 my $host;
54 my $server;
55 my $database;
56 my $port;
57 my $marcdata;
58 my @encoding;
59 my @results;
60 my $count;
61 my $record;
62 my $oldbiblio;
63 my $errmsg;
64 my @serverloop = ();
65 my @serverhost;
66 my @servername;
67 my @breeding_loop = ();
69 my $DEBUG = 0; # if set to 1, many debug message are send on syslog.
71 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
72 template_name => "cataloguing/z3950_search.tmpl",
73 query => $input,
74 type => "intranet",
75 authnotrequired => 1,
76 flagsrequired => { catalogue => 1 },
77 debug => 1,
78 });
80 $template->param( frameworkcode => $frameworkcode, );
82 if ( $op ne "do_search" ) {
83 my $sth = $dbh->prepare("select id,host,name,checked from z3950servers order by host");
84 $sth->execute();
85 my $serverloop = $sth->fetchall_arrayref( {} );
86 $template->param(
87 isbn => $isbn,
88 issn => $issn,
89 lccn => $lccn,
90 title => $title,
91 author => $author,
92 controlnumber=> $controlnumber,
93 stdid => $stdid,
94 srchany => $srchany,
95 serverloop => $serverloop,
96 opsearch => "search",
97 biblionumber => $biblionumber,
99 output_html_with_http_headers $input, $cookie, $template->output;
101 else {
102 my @id = $input->param('id');
103 my @oConnection;
104 my @oResult;
105 my @errconn;
106 my $s = 0;
107 my $query;
108 my $nterms;
109 if ($isbn || $issn) {
110 $term=$isbn if ($isbn);
111 $term=$issn if ($issn);
112 $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
113 $nterms++;
115 if ($title) {
116 utf8::decode($title);
117 $query .= " \@attr 1=4 \"$title\" ";
118 $nterms++;
120 if ($author) {
121 utf8::decode($author);
122 $query .= " \@attr 1=1003 \"$author\" ";
123 $nterms++;
125 if ($dewey) {
126 $query .= " \@attr 1=16 \"$dewey\" ";
127 $nterms++;
129 if ($subject) {
130 utf8::decode($subject);
131 $query .= " \@attr 1=21 \"$subject\" ";
132 $nterms++;
134 if ($lccn) {
135 $query .= " \@attr 1=9 $lccn ";
136 $nterms++;
138 if ($controlnumber) {
139 $query .= " \@attr 1=12 \"$controlnumber\" ";
140 $nterms++;
142 if ($stdid) {
143 $query .= " \@attr 1=1007 \"$stdid\" ";
144 $nterms++;
146 if ($srchany) {
147 $query .= " \@attr 1=1016 \"$srchany\" ";
148 $nterms++;
150 for my $i (1..$nterms-1) {
151 $query = "\@and " . $query;
153 warn "query ".$query if $DEBUG;
155 foreach my $servid (@id) {
156 my $sth = $dbh->prepare("select * from z3950servers where id=?");
157 $sth->execute($servid);
158 while ( $server = $sth->fetchrow_hashref ) {
159 warn "serverinfo ".join(':',%$server) if $DEBUG;
160 my $option1 = new ZOOM::Options();
161 $option1->option('async' => 1);
162 $option1->option('elementSetName', 'F');
163 $option1->option('databaseName', $server->{db});
164 $option1->option('user', $server->{userid} ) if $server->{userid};
165 $option1->option('password', $server->{password}) if $server->{password};
166 $option1->option('preferredRecordSyntax', $server->{syntax});
167 $oConnection[$s] = create ZOOM::Connection($option1)
168 || $DEBUG
169 && warn( "" . $oConnection[$s]->errmsg() );
170 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
171 $oConnection[$s]->connect( $server->{host}, $server->{port} )
172 || $DEBUG
173 && warn( "" . $oConnection[$s]->errmsg() );
174 $serverhost[$s] = $server->{host};
175 $servername[$s] = $server->{name};
176 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
177 $s++;
178 } ## while fetch
179 } # foreach
180 my $nremaining = $s;
181 my $firstresult = 1;
183 for ( my $z = 0 ; $z < $s ; $z++ ) {
184 warn "doing the search" if $DEBUG;
185 $oResult[$z] = $oConnection[$z]->search_pqf($query)
186 || $DEBUG
187 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
189 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
192 AGAIN:
193 my $k;
194 my $event;
195 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
196 $event = $oConnection[ $k - 1 ]->last_event();
197 warn( "connection ", $k - 1, ": event $event (",
198 ZOOM::event_str($event), ")\n" )
199 if $DEBUG;
200 last if $event == ZOOM::Event::ZEND;
203 if ( $k != 0 ) {
204 $k--;
205 warn $serverhost[$k] if $DEBUG;
206 my ( $error, $errmsg, $addinfo, $diagset ) =
207 $oConnection[$k]->error_x();
208 if ($error) {
209 if ($error =~ m/^(10000|10007)$/ ) {
210 push(@errconn, {'server' => $serverhost[$k]});
212 $DEBUG and warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
214 else {
215 my $numresults = $oResult[$k]->size();
216 my $i;
217 my $result = '';
218 if ( $numresults > 0 ) {
219 for ($i = 0; $i < (($numresults < 20) ? $numresults : 20); $i++) {
220 my $rec = $oResult[$k]->record($i);
221 if ($rec) {
222 my $marcrecord;
223 $marcdata = $rec->raw();
225 my ($charset_result, $charset_errors);
226 ($marcrecord, $charset_result, $charset_errors) =
227 MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
228 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
229 ## In HEAD i change everything to UTF-8
230 # In rel2_2 i am not sure what encoding is so no character conversion is done here
231 ##Add necessary encoding changes to here -TG
232 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
233 $oldbiblio->{isbn} =~ s/ |-|\.//g,
234 $oldbiblio->{issn} =~ s/ |-|\.//g,
235 my (
236 $notmarcrecord, $alreadyindb, $alreadyinfarm,
237 $imported, $breedingid
239 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
240 my %row_data;
241 $row_data{server} = $servername[$k];
242 $row_data{isbn} = $oldbiblio->{isbn};
243 $row_data{lccn} = $oldbiblio->{lccn};
244 $row_data{title} = $oldbiblio->{title};
245 $row_data{author} = $oldbiblio->{author};
246 $row_data{date} = $oldbiblio->{copyrightdate};
247 $row_data{edition} = $oldbiblio->{editionstatement};
248 $row_data{breedingid} = $breedingid;
249 $row_data{biblionumber} = $biblionumber;
250 push( @breeding_loop, \%row_data );
252 } else {
253 push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
254 } # $rec
256 } #$numresults
258 } # if $k !=0
259 $numberpending = $nremaining - 1;
260 $template->param(
261 breeding_loop => \@breeding_loop,
262 server => $servername[$k],
263 numberpending => $numberpending,
264 biblionumber => $biblionumber,
265 errconn => \@errconn
268 output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
270 # print $template->output if $firstresult !=1;
271 $firstresult++;
273 MAYBE_AGAIN:
274 if ( --$nremaining > 0 ) {
275 goto AGAIN;
277 } ## if op=search