Bug 8945: Update help files for 3.10
[koha.git] / acqui / z3950_search.pl
blob68cd0d6a5dfad7ca328218952ea09906bf59885f
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');
67 my $page = $input->param('current_page') || 1;
68 $page = $input->param('goto_page') if $input->param('changepage_goto');
69 my $show_next = 0;
70 my $total_pages = 0;
72 my $noconnection;
73 my $attr = '';
74 my $term;
75 my $host;
76 my $server;
77 my $database;
78 my $port;
79 my $marcdata;
80 my @encoding;
81 my @results;
82 my $count;
83 my $toggle;
84 my $record;
85 my $oldbiblio;
86 my $errmsg;
87 my @serverhost;
88 my @servername;
89 my @breeding_loop = ();
90 my $random = $input->param('random');
91 unless ($random)
92 { # this var is not useful anymore just kept to keep rel2_2 compatibility
93 $random = rand(1000000000);
96 my $DEBUG = $ENV{DEBUG} || 0; # if set to 1, many debug message are send on syslog.
98 # get framework list
99 my $frameworks = getframeworks;
100 my @frameworkcodeloop;
101 foreach my $thisframeworkcode ( keys %$frameworks ) {
102 my %row = (
103 value => $thisframeworkcode,
104 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
106 if ( $row{'value'} eq $frameworkcode){
107 $row{'active'} = 'true';
109 push @frameworkcodeloop, \%row;
112 my $vendor = GetBookSellerFromId($booksellerid);
113 $template->param( frameworkcode => $frameworkcode,
114 frameworkcodeloop => \@frameworkcodeloop,
115 booksellerid => $booksellerid,
116 basketno => $basketno,
117 name => $vendor->{'name'}
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 biblionumber => $biblionumber,
130 dewey => $dewey,
131 subject => $subject,
134 if ( $op ne "do_search" ) {
135 my $sth = $dbh->prepare("select id,host,name,checked from z3950servers order by host");
136 $sth->execute();
137 my $serverloop = $sth->fetchall_arrayref( {} );
138 $template->param(
139 serverloop => $serverloop,
140 opsearch => "search",
142 output_html_with_http_headers $input, $cookie, $template->output;
143 exit;
146 my @id = $input->param('id');
148 if ( not @id ) {
149 # empty server list -> report and exit
150 $template->param( emptyserverlist => 1 );
151 output_html_with_http_headers $input, $cookie, $template->output;
152 exit;
155 my @oConnection;
156 my @oResult;
157 my @errconn;
158 my $s = 0;
159 my $query;
160 my $nterms;
161 if ($isbn) {
162 $term=$isbn;
163 $query .= " \@attr 1=7 \@attr 5=1 \"$term\" ";
164 $nterms++;
166 if ($issn) {
167 $term=$issn;
168 $query .= " \@attr 1=8 \@attr 5=1 \"$term\" ";
169 $nterms++;
171 if ($title) {
172 utf8::decode($title);
173 $query .= " \@attr 1=4 \"$title\" ";
174 $nterms++;
176 if ($author) {
177 utf8::decode($author);
178 $query .= " \@attr 1=1003 \"$author\" ";
179 $nterms++;
181 if ($dewey) {
182 $query .= " \@attr 1=16 \"$dewey\" ";
183 $nterms++;
185 if ($subject) {
186 utf8::decode($subject);
187 $query .= " \@attr 1=21 \"$subject\" ";
188 $nterms++;
190 if ($lccn) {
191 $query .= " \@attr 1=9 $lccn ";
192 $nterms++;
194 if ($lccall) {
195 $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" ";
196 $nterms++;
198 if ($controlnumber) {
199 $query .= " \@attr 1=12 \"$controlnumber\" ";
200 $nterms++;
202 for my $i (1..$nterms-1) {
203 $query = "\@and " . $query;
205 warn "query ".$query if $DEBUG;
207 foreach my $servid (@id) {
208 my $sth = $dbh->prepare("select * from z3950servers where id=?");
209 $sth->execute($servid);
210 while ( $server = $sth->fetchrow_hashref ) {
211 warn "serverinfo ".join(':',%$server) if $DEBUG;
212 my $option1 = new ZOOM::Options();
213 $option1->option( 'async' => 1 );
214 $option1->option( 'elementSetName', 'F' );
215 $option1->option( 'databaseName', $server->{db} );
216 $option1->option( 'user', $server->{userid} ) if $server->{userid};
217 $option1->option( 'password', $server->{password} )
218 if $server->{password};
219 $option1->option( 'preferredRecordSyntax', $server->{syntax} );
220 $oConnection[$s] = create ZOOM::Connection($option1)
221 || $DEBUG
222 && warn( "" . $oConnection[$s]->errmsg() );
223 warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
224 $oConnection[$s]->connect( $server->{host}, $server->{port} )
225 || $DEBUG
226 && warn( "" . $oConnection[$s]->errmsg() );
227 $serverhost[$s] = $server->{host};
228 $servername[$s] = $server->{name};
229 $encoding[$s] = ($server->{encoding}?$server->{encoding}:"iso-5426");
230 $s++;
231 } ## while fetch
232 } # foreach
233 my $nremaining = $s;
234 my $firstresult = 1;
236 for ( my $z = 0 ; $z < $s ; $z++ ) {
237 warn "doing the search" if $DEBUG;
238 $oResult[$z] = $oConnection[$z]->search_pqf($query)
239 || $DEBUG
240 && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
242 # $oResult[$z] = $oConnection[$z]->search_pqf($query);
245 warn "# nremaining = $nremaining\n" if $DEBUG;
247 while ( $nremaining-- ) {
249 my $k;
250 my $event;
251 while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
252 $event = $oConnection[ $k - 1 ]->last_event();
253 warn( "connection ", $k - 1, ": event $event (",
254 ZOOM::event_str($event), ")\n" )
255 if $DEBUG;
256 last if $event == ZOOM::Event::ZEND;
259 if ( $k != 0 ) {
260 $k--;
261 warn "event from $k server = ",$serverhost[$k] if $DEBUG;
262 my ( $error, $errmsg, $addinfo, $diagset ) =
263 $oConnection[$k]->error_x();
264 if ($error) {
265 if ($error =~ m/^(10000|10007)$/ ) {
266 push(@errconn, {'server' => $serverhost[$k]});
268 $DEBUG and warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
270 else {
271 my $numresults = $oResult[$k]->size();
272 warn "numresults = $numresults" if $DEBUG;
273 my $i;
274 my $result = '';
275 if ( $numresults > 0 and $numresults >= (($page-1)*20)) {
276 $show_next = 1 if $numresults >= ($page*20);
277 $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
278 for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
279 my $rec = $oResult[$k]->record($i);
280 if ($rec) {
281 my $marcrecord;
282 $marcdata = $rec->raw();
284 my ($charset_result, $charset_errors);
285 ($marcrecord, $charset_result, $charset_errors) =
286 MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
287 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
288 ## In HEAD i change everything to UTF-8
289 # In rel2_2 i am not sure what encoding is so no character conversion is done here
290 ##Add necessary encoding changes to here -TG
292 # Normalize the record so it doesn't have separated diacritics
293 SetUTF8Flag($marcrecord);
295 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
296 $oldbiblio->{isbn} =~ s/ |-|\.//g if $oldbiblio->{isbn};
297 # pad | and ( with spaces to allow line breaks in the HTML
298 $oldbiblio->{isbn} =~ s/\|/ \| /g if $oldbiblio->{isbn};
299 $oldbiblio->{isbn} =~ s/\(/ \(/g if $oldbiblio->{isbn};
301 $oldbiblio->{issn} =~ s/ |-|\.//g if $oldbiblio->{issn};
302 # pad | and ( with spaces to allow line breaks in the HTML
303 $oldbiblio->{issn} =~ s/\|/ \| /g if $oldbiblio->{issn};
304 $oldbiblio->{issn} =~ s/\(/ \(/g if $oldbiblio->{issn};
305 my (
306 $notmarcrecord, $alreadyindb, $alreadyinfarm,
307 $imported, $breedingid
309 = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
310 my %row_data;
311 $row_data{server} = $servername[$k];
312 $row_data{isbn} = $oldbiblio->{isbn};
313 $row_data{lccn} = $oldbiblio->{lccn};
314 $row_data{title} = $oldbiblio->{title};
315 $row_data{author} = $oldbiblio->{author};
316 $row_data{breedingid} = $breedingid;
317 $row_data{biblionumber} = $biblionumber;
318 push( @breeding_loop, \%row_data );
320 } else {
321 push(@breeding_loop,{'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
322 } # $rec
323 } # upto 5 results
324 } #$numresults
326 } # if $k !=0
327 my $numberpending = $nremaining - 1;
329 my @servers = ();
330 foreach my $id (@id) {
331 push(@servers,{id => $id});
334 $template->param(
335 breeding_loop => \@breeding_loop,
336 server => $servername[$k],
337 numberpending => $numberpending,
338 current_page => $page,
339 servers => \@servers,
340 total_pages => $total_pages,
342 $template->param(show_nextbutton=>1) if $show_next;
343 $template->param(show_prevbutton=>1) if $page != 1;
345 # print $template->output if $firstresult !=1;
346 $firstresult++;
348 } # while nremaining
350 $template->param(
351 breeding_loop => \@breeding_loop,
352 #server => $servername[$k],
353 numberpending => $nremaining > 0 ? $nremaining : 0,
354 errconn => \@errconn
356 output_html_with_http_headers $input, $cookie, $template->output;