Merge branch 'master' into new/bug_7164
[koha.git] / acqui / z3950_search.pl
blob47dbe5265ef0b979639002d646d98f3055355cc3
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
5 # Copyright 2010 Catalyst IT
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 use warnings;
23 use strict;
24 use CGI;
26 use C4::Auth;
27 use C4::Output;
28 use C4::Biblio;
29 use C4::Context;
30 use C4::Breeding;
31 use C4::Koha;
32 use C4::Charset;
33 use C4::Bookseller qw/ GetBookSellerFromId /;
34 use ZOOM;
36 my $input = new CGI;
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39 template_name => "acqui/z3950_search.tmpl",
40 query => $input,
41 type => "intranet",
42 authnotrequired => 1,
43 flagsrequired => { acquisition => 'order_manage' },
44 debug => 1,
49 my $dbh = C4::Context->dbh;
50 my $error = $input->param('error');
51 my $biblionumber = $input->param('biblionumber');
52 $biblionumber = 0 unless $biblionumber;
53 my $frameworkcode = $input->param('frameworkcode');
54 my $title = $input->param('title');
55 my $author = $input->param('author');
56 my $isbn = $input->param('isbn');
57 my $issn = $input->param('issn');
58 my $lccn = $input->param('lccn');
59 my $lccall = $input->param('lccall');
60 my $subject= $input->param('subject');
61 my $dewey = $input->param('dewey');
62 my $controlnumber = $input->param('controlnumber');
63 my $op = $input->param('op');
64 my $booksellerid = $input->param('booksellerid');
65 my $basketno = $input->param('basketno');
66 my $noconnection;
67 my $numberpending;
68 my $attr = '';
69 my $term;
70 my $host;
71 my $server;
72 my $database;
73 my $port;
74 my $marcdata;
75 my @encoding;
76 my @results;
77 my $count;
78 my $toggle;
79 my $record;
80 my $oldbiblio;
81 my $errmsg;
82 my @serverhost;
83 my @servername;
84 my @breeding_loop = ();
85 my $random = $input->param('random');
86 unless ($random)
87 { # this var is not useful anymore just kept to keep rel2_2 compatibility
88 $random = rand(1000000000);
91 my $DEBUG = 0; # if set to 1, many debug message are send on syslog.
93 # get framework list
94 my $frameworks = getframeworks;
95 my @frameworkcodeloop;
96 foreach my $thisframeworkcode ( keys %$frameworks ) {
97 my %row = (
98 value => $thisframeworkcode,
99 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
101 if ( $row{'value'} eq $frameworkcode){
102 $row{'active'} = 'true';
104 push @frameworkcodeloop, \%row;
107 my $vendor = GetBookSellerFromId($booksellerid);
108 $template->param( frameworkcode => $frameworkcode,
109 frameworkcodeloop => \@frameworkcodeloop,
110 booksellerid => $booksellerid,
111 basketno => $basketno,
112 name => $vendor->{'name'}
117 if ( $op ne "do_search" ) {
118 my $sth = $dbh->prepare("select id,host,name,checked from z3950servers order by host");
119 $sth->execute();
120 my $serverloop = $sth->fetchall_arrayref( {} );
121 $template->param(
122 isbn => $isbn,
123 issn => $issn,
124 lccn => $lccn,
125 lccall => $lccall,
126 title => $title,
127 author => $author,
128 controlnumber=> $controlnumber,
129 serverloop => $serverloop,
130 opsearch => "search",
131 biblionumber => $biblionumber,
133 output_html_with_http_headers $input, $cookie, $template->output;
135 else {
136 my @id = $input->param('id');
137 my @oConnection;
138 my @oResult;
139 my $s = 0;
140 my $query;
141 my $nterms;
142 if ($isbn || $issn) {
143 $term=$isbn if ($isbn);
144 $term=$issn if ($issn);
145 $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
146 $nterms++;
148 if ($title) {
149 utf8::decode($title);
150 $query .= " \@attr 1=4 \"$title\" ";
151 $nterms++;
153 if ($author) {
154 utf8::decode($author);
155 $query .= " \@attr 1=1003 \"$author\" ";
156 $nterms++;
158 if ($dewey) {
159 $query .= " \@attr 1=16 \"$dewey\" ";
160 $nterms++;
162 if ($subject) {
163 utf8::decode($subject);
164 $query .= " \@attr 1=21 \"$subject\" ";
165 $nterms++;
167 if ($lccn) {
168 $query .= " \@attr 1=9 $lccn ";
169 $nterms++;
171 if ($lccall) {
172 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
173 $nterms++;
175 if ($controlnumber) {
176 $query .= " \@attr 1=12 \"$controlnumber\" ";
177 $nterms++;
179 for my $i (1..$nterms-1) {
180 $query = "\@and " . $query;
182 warn "query ".$query if $DEBUG;
184 foreach my $servid (@id) {
185 my $sth = $dbh->prepare("select * from z3950servers where id=?");
186 $sth->execute($servid);
187 while ( $server = $sth->fetchrow_hashref ) {
188 warn "serverinfo ".join(':',%$server) if $DEBUG;
189 my $noconnection = 0;
190 my $option1 = new ZOOM::Options();
191 $option1->option( 'async' => 1 );
192 $option1->option( 'elementSetName', 'F' );
193 $option1->option( 'databaseName', $server->{db} );
194 $option1->option( 'user', $server->{userid} ) if $server->{userid};
195 $option1->option( 'password', $server->{password} )
196 if $server->{password};
197 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
198 $oConnection[$s] = create ZOOM::Connection($option1)
199 || $DEBUG
200 && warn( "" . $oConnection[$s]->errmsg() );
201 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
202 $oConnection[$s]->connect( $server->{host}, $server->{port} )
203 || $DEBUG
204 && warn( "" . $oConnection[$s]->errmsg() );
205 $serverhost[$s] = $server->{host};
206 $servername[$s] = $server->{name};
207 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
208 $s++;
209 } ## while fetch
210 } # foreach
211 my $nremaining = $s;
212 my $firstresult = 1;
214 for ( my $z = 0 ; $z < $s ; $z++ ) {
215 warn "doing the search" if $DEBUG;
216 $oResult[$z] = $oConnection[$z]->search_pqf($query)
217 || $DEBUG
218 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
220 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
223 sub displayresults {
224 my $k;
225 my $event;
226 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
227 $event = $oConnection[ $k - 1 ]->last_event();
228 warn( "connection ", $k - 1, ": event $event (",
229 ZOOM::event_str($event), ")\n" )
230 if $DEBUG;
231 last if $event == ZOOM::Event::ZEND;
234 if ( $k != 0 ) {
235 $k--;
236 warn $serverhost[$k] if $DEBUG;
237 my ( $error, $errmsg, $addinfo, $diagset ) =
238 $oConnection[$k]->error_x();
239 if ($error) {
240 warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
241 if $DEBUG;
244 else {
245 my $numresults = $oResult[$k]->size();
246 my $i;
247 my $result = '';
248 if ( $numresults > 0 ) {
249 for (
250 $i = 0 ;
251 $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
252 $i++
255 my $rec = $oResult[$k]->record($i);
256 if ($rec) {
257 my $marcrecord;
258 $marcdata = $rec->raw();
260 my ($charset_result, $charset_errors);
261 ($marcrecord, $charset_result, $charset_errors) =
262 MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
263 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
264 ## In HEAD i change everything to UTF-8
265 # In rel2_2 i am not sure what encoding is so no character conversion is done here
266 ##Add necessary encoding changes to here -TG
267 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
268 $oldbiblio->{isbn} =~ s/ |-|\.//g if $oldbiblio->{isbn};
269 # pad | and ( with spaces to allow line breaks in the HTML
270 $oldbiblio->{isbn} =~ s/\|/ \| /g if $oldbiblio->{isbn};
271 $oldbiblio->{isbn} =~ s/\(/ \(/g if $oldbiblio->{isbn};
273 $oldbiblio->{issn} =~ s/ |-|\.//g if $oldbiblio->{issn};
274 # pad | and ( with spaces to allow line breaks in the HTML
275 $oldbiblio->{issn} =~ s/\|/ \| /g if $oldbiblio->{issn};
276 $oldbiblio->{issn} =~ s/\(/ \(/g if $oldbiblio->{issn};
277 my (
278 $notmarcrecord, $alreadyindb, $alreadyinfarm,
279 $imported, $breedingid
281 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
282 my %row_data;
283 if ( $i % 2 ) {
284 $toggle = 1;
286 else {
287 $toggle = 0;
289 $row_data{toggle} = $toggle;
290 $row_data{server} = $servername[$k];
291 $row_data{isbn} = $oldbiblio->{isbn};
292 $row_data{lccn} = $oldbiblio->{lccn};
293 $row_data{title} = $oldbiblio->{title};
294 $row_data{author} = $oldbiblio->{author};
295 $row_data{breedingid} = $breedingid;
296 $row_data{biblionumber} = $biblionumber;
297 push( @breeding_loop, \%row_data );
299 } else {
300 push(@breeding_loop,{'toggle'=>($i % 2)?1:0,'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
301 } # $rec
302 } # upto 5 results
303 } #$numresults
305 } # if $k !=0
306 $numberpending = $nremaining - 1;
307 $template->param(
308 breeding_loop => \@breeding_loop,
309 server => $servername[$k],
310 numberpending => $numberpending,
312 output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
314 # print $template->output if $firstresult !=1;
315 $firstresult++;
317 displayresults();
318 while ( --$nremaining > 0 ) {
319 displayresults();
321 } ## if op=search