BUGFIX NoZebra search (removal of adv zebra keyword bugguy)
[koha.git] / cataloguing / z3950_search.pl
blobb0edd7702b6f8d30b1b4a033a7a260f0c87eeb34
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');
37 $biblionumber = 0 unless $biblionumber;
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 $random = $input->param('random');
45 my $op = $input->param('op');
46 my $noconnection;
47 my $numberpending;
48 my $attr = '';
49 my $term;
50 my $host;
51 my $server;
52 my $database;
53 my $port;
54 my $marcdata;
55 my @encoding;
56 my @results;
57 my $count;
58 my $toggle;
59 my $record;
60 my $oldbiblio;
61 my $errmsg;
62 my @serverloop = ();
63 my @serverhost;
64 my @breeding_loop = ();
66 my $DEBUG = 0; # if set to 1, many debug message are send on syslog.
68 unless ($random)
69 { # this var is not useful anymore just kept to keep rel2_2 compatibility
70 $random = rand(1000000000);
73 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
75 template_name => "cataloguing/z3950_search.tmpl",
76 query => $input,
77 type => "intranet",
78 authnotrequired => 1,
79 flagsrequired => { catalogue => 1 },
80 debug => 1,
84 $template->param( frameworkcode => $frameworkcode, );
86 if ( $op ne "do_search" ) {
87 my $sth = $dbh->prepare("select id,host,name,checked from z3950servers order by host");
88 $sth->execute();
89 my $serverloop = $sth->fetchall_arrayref( {} );
90 $template->param(
91 isbn => $isbn,
92 issn => $issn,
93 lccn => $lccn,
94 title => $title,
95 author => $author,
96 serverloop => $serverloop,
97 opsearch => "search",
98 biblionumber => $biblionumber,
100 output_html_with_http_headers $input, $cookie, $template->output;
102 else {
103 my @id = $input->param('id');
104 my @oConnection;
105 my @oResult;
106 my $s = 0;
108 if ( $isbn || $issn ) {
109 $attr = '1=7';
110 $term = $isbn if ($isbn);
111 $term = $issn if ($issn);
113 elsif ($lccn) {
114 $attr = '1=9';
115 $term = $lccn;
117 elsif ($title) {
118 $attr = '1=4 ';
119 utf8::decode($title);
120 $term = $title;
122 elsif ($author) {
123 $attr = '1=1003';
124 utf8::decode($author);
125 $term = $author;
128 my $query = "\@attr $attr \"$term\"";
129 warn "query " . $query if $DEBUG;
130 foreach my $servid (@id) {
131 my $sth = $dbh->prepare("select * from z3950servers where id=?");
132 $sth->execute($servid);
133 while ( $server = $sth->fetchrow_hashref ) {
134 my $noconnection = 0;
135 my $option1 = new ZOOM::Options();
136 $option1->option( 'async' => 1 );
137 $option1->option( 'elementSetName', 'F' );
138 $option1->option( 'databaseName', $server->{db} );
139 $option1->option( 'user', $server->{userid} ) if $server->{userid};
140 $option1->option( 'password', $server->{password} )
141 if $server->{password};
142 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
143 $oConnection[$s] = create ZOOM::Connection($option1)
144 || $DEBUG
145 && warn( "" . $oConnection[$s]->errmsg() );
146 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
147 $oConnection[$s]->connect( $server->{host}, $server->{port} )
148 || $DEBUG
149 && warn( "" . $oConnection[$s]->errmsg() );
150 $serverhost[$s] = $server->{host};
151 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
152 $s++;
153 } ## while fetch
154 } # foreach
155 my $nremaining = $s;
156 my $firstresult = 1;
158 for ( my $z = 0 ; $z < $s ; $z++ ) {
159 warn "doing the search" if $DEBUG;
160 $oResult[$z] = $oConnection[$z]->search_pqf($query)
161 || $DEBUG
162 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
164 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
167 AGAIN:
168 my $k;
169 my $event;
170 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
171 $event = $oConnection[ $k - 1 ]->last_event();
172 warn( "connection ", $k - 1, ": event $event (",
173 ZOOM::event_str($event), ")\n" )
174 if $DEBUG;
175 last if $event == ZOOM::Event::ZEND;
178 if ( $k != 0 ) {
179 $k--;
180 warn $serverhost[$k] if $DEBUG;
181 my ( $error, $errmsg, $addinfo, $diagset ) =
182 $oConnection[$k]->error_x();
183 if ($error) {
184 warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
185 if $DEBUG;
188 else {
189 my $numresults = $oResult[$k]->size();
190 my $i;
191 my $result = '';
192 if ( $numresults > 0 ) {
193 for (
194 $i = 0 ;
195 $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
196 $i++
199 my $rec = $oResult[$k]->record($i);
200 my $marcrecord;
201 $marcdata = $rec->raw();
203 my ($charset_result, $charset_errors);
204 ($marcrecord, $charset_result, $charset_errors) =
205 MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
206 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
207 ## In HEAD i change everything to UTF-8
208 # In rel2_2 i am not sure what encoding is so no character conversion is done here
209 ##Add necessary encoding changes to here -TG
210 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
211 $oldbiblio->{isbn} =~ s/ |-|\.//g,
212 $oldbiblio->{issn} =~ s/ |-|\.//g,
213 my (
214 $notmarcrecord, $alreadyindb, $alreadyinfarm,
215 $imported, $breedingid
217 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
218 my %row_data;
219 if ( $i % 2 ) {
220 $toggle = 1;
222 else {
223 $toggle = 0;
225 $row_data{toggle} = $toggle;
226 $row_data{server} = $serverhost[$k];
227 $row_data{isbn} = $oldbiblio->{isbn};
228 $row_data{lccn} = $oldbiblio->{lccn};
229 $row_data{title} = $oldbiblio->{title};
230 $row_data{author} = $oldbiblio->{author};
231 $row_data{breedingid} = $breedingid;
232 $row_data{biblionumber} = $biblionumber;
233 push( @breeding_loop, \%row_data );
234 } # upto 5 results
235 } #$numresults
237 } # if $k !=0
238 $numberpending = $nremaining - 1;
239 $template->param(
240 breeding_loop => \@breeding_loop,
241 server => $serverhost[$k],
242 numberpending => $numberpending,
245 output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
247 # print $template->output if $firstresult !=1;
248 $firstresult++;
250 MAYBE_AGAIN:
251 if ( --$nremaining > 0 ) {
252 goto AGAIN;
254 } ## if op=search