Add support for a remote mysql server.
[koha.git] / cataloguing / z3950_search.pl
blobcf9e7b2ff4c6fda33e36a606bb819157d409aa26
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 $subject = $input->param('subject');
45 my $dewey = $input->param('dewey');
46 my $controlnumber = $input->param('controlnumber');
47 my $stdid = $input->param('stdid');
48 my $srchany = $input->param('srchany');
49 my $random = $input->param('random') || rand(1000000000); # this var is not useful anymore just kept for rel2_2 compatibility
50 my $op = $input->param('op');
51 my $numberpending;
52 my $attr = '';
53 my $term;
54 my $host;
55 my $server;
56 my $database;
57 my $port;
58 my $marcdata;
59 my @encoding;
60 my @results;
61 my $count;
62 my $record;
63 my $oldbiblio;
64 my $errmsg;
65 my @serverloop = ();
66 my @serverhost;
67 my @servername;
68 my @breeding_loop = ();
70 my $DEBUG = 0; # if set to 1, many debug message are send on syslog.
72 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
73 template_name => "cataloguing/z3950_search.tmpl",
74 query => $input,
75 type => "intranet",
76 authnotrequired => 1,
77 flagsrequired => { catalogue => 1 },
78 debug => 1,
79 });
81 $template->param( frameworkcode => $frameworkcode, );
83 if ( $op ne "do_search" ) {
84 my $sth = $dbh->prepare("select id,host,name,checked from z3950servers order by host");
85 $sth->execute();
86 my $serverloop = $sth->fetchall_arrayref( {} );
87 $template->param(
88 isbn => $isbn,
89 issn => $issn,
90 lccn => $lccn,
91 title => $title,
92 author => $author,
93 controlnumber=> $controlnumber,
94 stdid => $stdid,
95 srchany => $srchany,
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 @errconn;
107 my $s = 0;
108 my $query;
109 my $nterms;
110 if ($isbn || $issn) {
111 $term=$isbn if ($isbn);
112 $term=$issn if ($issn);
113 $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
114 $nterms++;
116 if ($title) {
117 utf8::decode($title);
118 $query .= " \@attr 1=4 \"$title\" ";
119 $nterms++;
121 if ($author) {
122 utf8::decode($author);
123 $query .= " \@attr 1=1003 \"$author\" ";
124 $nterms++;
126 if ($dewey) {
127 $query .= " \@attr 1=16 \"$dewey\" ";
128 $nterms++;
130 if ($subject) {
131 utf8::decode($subject);
132 $query .= " \@attr 1=21 \"$subject\" ";
133 $nterms++;
135 if ($lccn) {
136 $query .= " \@attr 1=9 $lccn ";
137 $nterms++;
139 if ($controlnumber) {
140 $query .= " \@attr 1=12 \"$controlnumber\" ";
141 $nterms++;
143 if ($stdid) {
144 $query .= " \@attr 1=1007 \"$stdid\" ";
145 $nterms++;
147 if ($srchany) {
148 $query .= " \@attr 1=1016 \"$srchany\" ";
149 $nterms++;
151 for my $i (1..$nterms-1) {
152 $query = "\@and " . $query;
154 warn "query ".$query if $DEBUG;
156 foreach my $servid (@id) {
157 my $sth = $dbh->prepare("select * from z3950servers where id=?");
158 $sth->execute($servid);
159 while ( $server = $sth->fetchrow_hashref ) {
160 warn "serverinfo ".join(':',%$server) if $DEBUG;
161 my $option1 = new ZOOM::Options();
162 $option1->option('async' => 1);
163 $option1->option('elementSetName', 'F');
164 $option1->option('databaseName', $server->{db});
165 $option1->option('user', $server->{userid} ) if $server->{userid};
166 $option1->option('password', $server->{password}) if $server->{password};
167 $option1->option('preferredRecordSyntax', $server->{syntax});
168 $oConnection[$s] = create ZOOM::Connection($option1)
169 || $DEBUG
170 && warn( "" . $oConnection[$s]->errmsg() );
171 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
172 $oConnection[$s]->connect( $server->{host}, $server->{port} )
173 || $DEBUG
174 && warn( "" . $oConnection[$s]->errmsg() );
175 $serverhost[$s] = $server->{host};
176 $servername[$s] = $server->{name};
177 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
178 $s++;
179 } ## while fetch
180 } # foreach
181 my $nremaining = $s;
182 my $firstresult = 1;
184 for ( my $z = 0 ; $z < $s ; $z++ ) {
185 warn "doing the search" if $DEBUG;
186 $oResult[$z] = $oConnection[$z]->search_pqf($query)
187 || $DEBUG
188 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
190 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
193 AGAIN:
194 my $k;
195 my $event;
196 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
197 $event = $oConnection[ $k - 1 ]->last_event();
198 warn( "connection ", $k - 1, ": event $event (",
199 ZOOM::event_str($event), ")\n" )
200 if $DEBUG;
201 last if $event == ZOOM::Event::ZEND;
204 if ( $k != 0 ) {
205 $k--;
206 warn $serverhost[$k] if $DEBUG;
207 my ( $error, $errmsg, $addinfo, $diagset ) =
208 $oConnection[$k]->error_x();
209 if ($error) {
210 if ($error =~ m/^(10000|10007)$/ ) {
211 push(@errconn, {'server' => $serverhost[$k]});
213 $DEBUG and warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
215 else {
216 my $numresults = $oResult[$k]->size();
217 my $i;
218 my $result = '';
219 if ( $numresults > 0 ) {
220 for ($i = 0; $i < (($numresults < 20) ? $numresults : 20); $i++) {
221 my $rec = $oResult[$k]->record($i);
222 if ($rec) {
223 my $marcrecord;
224 $marcdata = $rec->raw();
226 my ($charset_result, $charset_errors);
227 ($marcrecord, $charset_result, $charset_errors) =
228 MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
229 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
230 ## In HEAD i change everything to UTF-8
231 # In rel2_2 i am not sure what encoding is so no character conversion is done here
232 ##Add necessary encoding changes to here -TG
233 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
234 $oldbiblio->{isbn} =~ s/ |-|\.//g,
235 $oldbiblio->{issn} =~ s/ |-|\.//g,
236 my (
237 $notmarcrecord, $alreadyindb, $alreadyinfarm,
238 $imported, $breedingid
240 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
241 my %row_data;
242 $row_data{server} = $servername[$k];
243 $row_data{isbn} = $oldbiblio->{isbn};
244 $row_data{lccn} = $oldbiblio->{lccn};
245 $row_data{title} = $oldbiblio->{title};
246 $row_data{author} = $oldbiblio->{author};
247 $row_data{date} = $oldbiblio->{copyrightdate};
248 $row_data{edition} = $oldbiblio->{editionstatement};
249 $row_data{breedingid} = $breedingid;
250 $row_data{biblionumber} = $biblionumber;
251 push( @breeding_loop, \%row_data );
253 } else {
254 push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
255 } # $rec
257 } #$numresults
259 } # if $k !=0
260 $numberpending = $nremaining - 1;
261 $template->param(
262 breeding_loop => \@breeding_loop,
263 server => $servername[$k],
264 numberpending => $numberpending,
265 biblionumber => $biblionumber,
266 errconn => \@errconn
269 output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
271 # print $template->output if $firstresult !=1;
272 $firstresult++;
274 MAYBE_AGAIN:
275 if ( --$nremaining > 0 ) {
276 goto AGAIN;
278 } ## if op=search