More fixes for Bug 2704, 440 Display Issues
[koha.git] / cataloguing / z3950_search.pl
blobdf2fab199c9d311d2484f4ab096fd7a996ff5dae
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 $random = $input->param('random') || rand(1000000000); # this var is not useful anymore just kept for rel2_2 compatibility
46 my $op = $input->param('op');
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 $record;
59 my $oldbiblio;
60 my $errmsg;
61 my @serverloop = ();
62 my @serverhost;
63 my @servername;
64 my @breeding_loop = ();
66 my $DEBUG = 0; # if set to 1, many debug message are send on syslog.
68 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
69 template_name => "cataloguing/z3950_search.tmpl",
70 query => $input,
71 type => "intranet",
72 authnotrequired => 1,
73 flagsrequired => { catalogue => 1 },
74 debug => 1,
75 });
77 $template->param( frameworkcode => $frameworkcode, );
79 if ( $op ne "do_search" ) {
80 my $sth = $dbh->prepare("select id,host,name,checked from z3950servers order by host");
81 $sth->execute();
82 my $serverloop = $sth->fetchall_arrayref( {} );
83 $template->param(
84 isbn => $isbn,
85 issn => $issn,
86 lccn => $lccn,
87 title => $title,
88 author => $author,
89 serverloop => $serverloop,
90 opsearch => "search",
91 biblionumber => $biblionumber,
93 output_html_with_http_headers $input, $cookie, $template->output;
95 else {
96 my @id = $input->param('id');
97 my @oConnection;
98 my @oResult;
99 my @errconn;
100 my $s = 0;
101 my $query;
102 my $nterms;
103 if ($isbn || $issn) {
104 $term=$isbn if ($isbn);
105 $term=$issn if ($issn);
106 $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
107 $nterms++;
109 if ($title) {
110 utf8::decode($title);
111 $query .= " \@attr 1=4 \"$title\" ";
112 $nterms++;
114 if ($author) {
115 utf8::decode($author);
116 $query .= " \@attr 1=1003 \"$author\" ";
117 $nterms++;
119 if ($dewey) {
120 $query .= " \@attr 1=16 \"$dewey\" ";
121 $nterms++;
123 if ($subject) {
124 utf8::decode($subject);
125 $query .= " \@attr 1=21 \"$subject\" ";
126 $nterms++;
128 if ($lccn) {
129 $query .= " \@attr 1=9 $lccn ";
130 $nterms++;
132 for my $i (1..$nterms-1) {
133 $query = "\@and " . $query;
135 warn "query ".$query if $DEBUG;
137 foreach my $servid (@id) {
138 my $sth = $dbh->prepare("select * from z3950servers where id=?");
139 $sth->execute($servid);
140 while ( $server = $sth->fetchrow_hashref ) {
141 warn "serverinfo ".join(':',%$server) if $DEBUG;
142 my $option1 = new ZOOM::Options();
143 $option1->option('async' => 1);
144 $option1->option('elementSetName', 'F');
145 $option1->option('databaseName', $server->{db});
146 $option1->option('user', $server->{userid} ) if $server->{userid};
147 $option1->option('password', $server->{password}) if $server->{password};
148 $option1->option('preferredRecordSyntax', $server->{syntax});
149 $oConnection[$s] = create ZOOM::Connection($option1)
150 || $DEBUG
151 && warn( "" . $oConnection[$s]->errmsg() );
152 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
153 $oConnection[$s]->connect( $server->{host}, $server->{port} )
154 || $DEBUG
155 && warn( "" . $oConnection[$s]->errmsg() );
156 $serverhost[$s] = $server->{host};
157 $servername[$s] = $server->{name};
158 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
159 $s++;
160 } ## while fetch
161 } # foreach
162 my $nremaining = $s;
163 my $firstresult = 1;
165 for ( my $z = 0 ; $z < $s ; $z++ ) {
166 warn "doing the search" if $DEBUG;
167 $oResult[$z] = $oConnection[$z]->search_pqf($query)
168 || $DEBUG
169 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
171 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
174 AGAIN:
175 my $k;
176 my $event;
177 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
178 $event = $oConnection[ $k - 1 ]->last_event();
179 warn( "connection ", $k - 1, ": event $event (",
180 ZOOM::event_str($event), ")\n" )
181 if $DEBUG;
182 last if $event == ZOOM::Event::ZEND;
185 if ( $k != 0 ) {
186 $k--;
187 warn $serverhost[$k] if $DEBUG;
188 my ( $error, $errmsg, $addinfo, $diagset ) =
189 $oConnection[$k]->error_x();
190 if ($error) {
191 if ($error =~ m/^(10000|10007)$/ ) {
192 push(@errconn, {'server' => $serverhost[$k]});
194 $DEBUG and warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
196 else {
197 my $numresults = $oResult[$k]->size();
198 my $i;
199 my $result = '';
200 if ( $numresults > 0 ) {
201 for ($i = 0; $i < (($numresults < 20) ? $numresults : 20); $i++) {
202 my $rec = $oResult[$k]->record($i);
203 if ($rec) {
204 my $marcrecord;
205 $marcdata = $rec->raw();
207 my ($charset_result, $charset_errors);
208 ($marcrecord, $charset_result, $charset_errors) =
209 MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
210 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
211 ## In HEAD i change everything to UTF-8
212 # In rel2_2 i am not sure what encoding is so no character conversion is done here
213 ##Add necessary encoding changes to here -TG
214 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
215 $oldbiblio->{isbn} =~ s/ |-|\.//g,
216 $oldbiblio->{issn} =~ s/ |-|\.//g,
217 my (
218 $notmarcrecord, $alreadyindb, $alreadyinfarm,
219 $imported, $breedingid
221 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
222 my %row_data;
223 $row_data{toggle} = ($i % 2) ? 1 : 0;
224 $row_data{server} = $servername[$k];
225 $row_data{isbn} = $oldbiblio->{isbn};
226 $row_data{lccn} = $oldbiblio->{lccn};
227 $row_data{title} = $oldbiblio->{title};
228 $row_data{author} = $oldbiblio->{author};
229 $row_data{breedingid} = $breedingid;
230 $row_data{biblionumber} = $biblionumber;
231 push( @breeding_loop, \%row_data );
233 } else {
234 push(@breeding_loop,{'toggle'=>($i % 2)?1:0,'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
235 } # $rec
237 } #$numresults
239 } # if $k !=0
240 $numberpending = $nremaining - 1;
241 $template->param(
242 breeding_loop => \@breeding_loop,
243 server => $servername[$k],
244 numberpending => $numberpending,
245 biblionumber => $biblionumber,
246 errconn => \@errconn
249 output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
251 # print $template->output if $firstresult !=1;
252 $firstresult++;
254 MAYBE_AGAIN:
255 if ( --$nremaining > 0 ) {
256 goto AGAIN;
258 } ## if op=search