Add larger values to numerical limit.
[koha.git] / cataloguing / z3950_search.pl
1 #!/usr/bin/perl
2
3 # This is a completely new Z3950 clients search using async ZOOM -TG 02/11/06
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
16 #
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
20
21 use strict;
22 use CGI;
23
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;
32
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 $subject= $input->param('subject');
45 my $dewey = $input->param('dewey');
46 my $random        = $input->param('random');
47 my $op            = $input->param('op');
48 my $noconnection;
49 my $numberpending;
50 my $attr = '';
51 my $term;
52 my $host;
53 my $server;
54 my $database;
55 my $port;
56 my $marcdata;
57 my @encoding;
58 my @results;
59 my $count;
60 my $toggle;
61 my $record;
62 my $oldbiblio;
63 my $errmsg;
64 my @serverloop = ();
65 my @serverhost;
66 my @servername;
67 my @breeding_loop = ();
68
69 my $DEBUG = 0;    # if set to 1, many debug message are send on syslog.
70
71 unless ($random)
72 {    # this var is not useful anymore just kept to keep rel2_2 compatibility
73     $random = rand(1000000000);
74 }
75
76 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
77     {
78         template_name   => "cataloguing/z3950_search.tmpl",
79         query           => $input,
80         type            => "intranet",
81         authnotrequired => 1,
82         flagsrequired   => { catalogue => 1 },
83         debug           => 1,
84     }
85 );
86
87 $template->param( frameworkcode => $frameworkcode, );
88
89 if ( $op ne "do_search" ) {
90     my $sth = $dbh->prepare("select id,host,name,checked from z3950servers  order by host");
91     $sth->execute();
92     my $serverloop = $sth->fetchall_arrayref( {} );
93     $template->param(
94         isbn         => $isbn,
95         issn         => $issn,
96         lccn         => $lccn,
97         title        => $title,
98         author       => $author,
99         serverloop   => $serverloop,
100         opsearch     => "search",
101         biblionumber => $biblionumber,
102     );
103     output_html_with_http_headers $input, $cookie, $template->output;
104 }
105 else {
106     my @id = $input->param('id');
107     my @oConnection;
108     my @oResult;
109     my @errconn;
110     my $s = 0;
111     my $query;
112     my $nterms;
113     if ($isbn || $issn) {
114         $term=$isbn if ($isbn);
115         $term=$issn if ($issn);
116         $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
117         $nterms++;
118     }
119     if ($title) {
120         utf8::decode($title);
121         $query .= " \@attr 1=4 \"$title\" ";
122         $nterms++;
123     }
124     if ($author) {
125         utf8::decode($author);
126         $query .= " \@attr 1=1003 \"$author\" ";
127         $nterms++;
128     }
129     if ($dewey) {
130         $query .= " \@attr 1=16 \"$dewey\" ";
131         $nterms++;
132     }
133     if ($subject) {
134         utf8::decode($subject);
135         $query .= " \@attr 1=21 \"$subject\" ";
136         $nterms++;
137     }
138         if ($lccn) {    
139         $query .= " \@attr 1=9 $lccn ";
140         $nterms++;
141     }
142 for my $i (1..$nterms-1) {
143     $query = "\@and " . $query;
144 }
145 warn "query ".$query  if $DEBUG;
146
147     foreach my $servid (@id) {
148         my $sth = $dbh->prepare("select * from z3950servers where id=?");
149         $sth->execute($servid);
150         while ( $server = $sth->fetchrow_hashref ) {
151             warn "serverinfo ".join(':',%$server) if $DEBUG;
152             my $noconnection = 0;
153             my $option1      = new ZOOM::Options();
154             $option1->option( 'async' => 1 );
155             $option1->option( 'elementSetName', 'F' );
156             $option1->option( 'databaseName',   $server->{db} );
157             $option1->option( 'user', $server->{userid} ) if $server->{userid};
158             $option1->option( 'password', $server->{password} )
159               if $server->{password};
160             $option1->option( 'preferredRecordSyntax', $server->{syntax} );
161             $oConnection[$s] = create ZOOM::Connection($option1)
162               || $DEBUG
163               && warn( "" . $oConnection[$s]->errmsg() );
164             warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
165             $oConnection[$s]->connect( $server->{host}, $server->{port} )
166               || $DEBUG
167               && warn( "" . $oConnection[$s]->errmsg() );
168             $serverhost[$s] = $server->{host};
169             $servername[$s] = $server->{name};
170             $encoding[$s]   = ($server->{encoding}?$server->{encoding}:"iso-5426");
171             $s++;
172         }    ## while fetch
173     }    # foreach
174     my $nremaining  = $s;
175     my $firstresult = 1;
176
177     for ( my $z = 0 ; $z < $s ; $z++ ) {
178         warn "doing the search" if $DEBUG;
179         $oResult[$z] = $oConnection[$z]->search_pqf($query)
180           || $DEBUG
181           && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
182
183         # $oResult[$z] = $oConnection[$z]->search_pqf($query);
184     }
185
186   AGAIN:
187     my $k;
188     my $event;
189     while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
190         $event = $oConnection[ $k - 1 ]->last_event();
191         warn( "connection ", $k - 1, ": event $event (",
192             ZOOM::event_str($event), ")\n" )
193           if $DEBUG;
194         last if $event == ZOOM::Event::ZEND;
195     }
196
197     if ( $k != 0 ) {
198         $k--;
199         warn $serverhost[$k] if $DEBUG;
200         my ( $error, $errmsg, $addinfo, $diagset ) =
201           $oConnection[$k]->error_x();
202         if ($error) {
203             if ($error =~ m/^(10000|10007)$/ ) {
204                 push(@errconn, {'server' => $serverhost[$k]});
205             }
206             warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
207               if $DEBUG;
208
209         }
210         else {
211             my $numresults = $oResult[$k]->size();
212             my $i;
213             my $result = '';
214             if ( $numresults > 0 ) {
215                 for (
216                     $i = 0 ;
217                     $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
218                     $i++
219                   )
220                 {
221                     my $rec = $oResult[$k]->record($i);
222                     if ($rec) {
223                         my $marcrecord;
224                         $marcdata   = $rec->raw();
225
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
239                           )
240                           = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
241                         my %row_data;
242                         if ( $i % 2 ) {
243                             $toggle = 1;
244                         }
245                         else {
246                             $toggle = 0;
247                         }
248                         $row_data{toggle}       = $toggle;
249                         $row_data{server}       = $servername[$k];
250                         $row_data{isbn}         = $oldbiblio->{isbn};
251                         $row_data{lccn}         = $oldbiblio->{lccn};
252                         $row_data{title}        = $oldbiblio->{title};
253                         $row_data{author}       = $oldbiblio->{author};
254                         $row_data{breedingid}   = $breedingid;
255                         $row_data{biblionumber} = $biblionumber;
256                         push( @breeding_loop, \%row_data );
257                             
258                     } else {
259                         push(@breeding_loop,{'toggle'=>($i % 2)?1:0,'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
260                     } # $rec
261                 }    # upto 5 results
262             }    #$numresults
263         }
264     }    # if $k !=0
265     $numberpending = $nremaining - 1;
266     $template->param(
267         breeding_loop => \@breeding_loop,
268         server        => $servername[$k],
269         numberpending => $numberpending,
270         biblionumber  => $biblionumber,
271         errconn       => \@errconn
272     );
273     
274     output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
275
276     #   print  $template->output  if $firstresult !=1;
277     $firstresult++;
278
279   MAYBE_AGAIN:
280     if ( --$nremaining > 0 ) {
281         goto AGAIN;
282     }
283 }    ## if op=search