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