adding 'gettext' as this is requred by the translation utility
[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 @breeding_loop = ();
67
68 my $DEBUG = 0;    # if set to 1, many debug message are send on syslog.
69
70 unless ($random)
71 {    # this var is not useful anymore just kept to keep rel2_2 compatibility
72     $random = rand(1000000000);
73 }
74
75 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
76     {
77         template_name   => "cataloguing/z3950_search.tmpl",
78         query           => $input,
79         type            => "intranet",
80         authnotrequired => 1,
81         flagsrequired   => { catalogue => 1 },
82         debug           => 1,
83     }
84 );
85
86 $template->param( frameworkcode => $frameworkcode, );
87
88 if ( $op ne "do_search" ) {
89     my $sth = $dbh->prepare("select id,host,name,checked from z3950servers  order by host");
90     $sth->execute();
91     my $serverloop = $sth->fetchall_arrayref( {} );
92     $template->param(
93         isbn         => $isbn,
94         issn         => $issn,
95         lccn         => $lccn,
96         title        => $title,
97         author       => $author,
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 $s = 0;
109     my $query;
110     my $nterms;
111     if ($isbn || $issn) {
112         $term=$isbn if ($isbn);
113         $term=$issn if ($issn);
114         $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
115         $nterms++;
116     }
117     if ($title) {
118         utf8::decode($title);
119         $query .= " \@attr 1=4 \"$title\" ";
120         $nterms++;
121     }
122     if ($author) {
123         utf8::decode($author);
124         $query .= " \@attr 1=1003 \"$author\" ";
125         $nterms++;
126     }
127     if ($dewey) {
128         $query .= " \@attr 1=16 \"$dewey\" ";
129         $nterms++;
130     }
131     if ($subject) {
132         utf8::decode($subject);
133         $query .= " \@attr 1=21 \"$subject\" ";
134         $nterms++;
135     }
136         if ($lccn) {    
137         $query .= " \@attr 1=9 $lccn ";
138         $nterms++;
139     }
140 for my $i (1..$nterms-1) {
141     $query = "\@and " . $query;
142 }
143 warn "query ".$query  if $DEBUG;
144
145     foreach my $servid (@id) {
146         my $sth = $dbh->prepare("select * from z3950servers where id=?");
147         $sth->execute($servid);
148         while ( $server = $sth->fetchrow_hashref ) {
149             my $noconnection = 0;
150             my $option1      = new ZOOM::Options();
151             $option1->option( 'async' => 1 );
152             $option1->option( 'elementSetName', 'F' );
153             $option1->option( 'databaseName',   $server->{db} );
154             $option1->option( 'user', $server->{userid} ) if $server->{userid};
155             $option1->option( 'password', $server->{password} )
156               if $server->{password};
157             $option1->option( 'preferredRecordSyntax', $server->{syntax} );
158             $oConnection[$s] = create ZOOM::Connection($option1)
159               || $DEBUG
160               && warn( "" . $oConnection[$s]->errmsg() );
161             warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
162             $oConnection[$s]->connect( $server->{host}, $server->{port} )
163               || $DEBUG
164               && warn( "" . $oConnection[$s]->errmsg() );
165             $serverhost[$s] = $server->{host};
166             $encoding[$s]   = ($server->{encoding}?$server->{encoding}:"iso-5426");
167             $s++;
168         }    ## while fetch
169     }    # foreach
170     my $nremaining  = $s;
171     my $firstresult = 1;
172
173     for ( my $z = 0 ; $z < $s ; $z++ ) {
174         warn "doing the search" if $DEBUG;
175         $oResult[$z] = $oConnection[$z]->search_pqf($query)
176           || $DEBUG
177           && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
178
179         # $oResult[$z] = $oConnection[$z]->search_pqf($query);
180     }
181
182   AGAIN:
183     my $k;
184     my $event;
185     while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
186         $event = $oConnection[ $k - 1 ]->last_event();
187         warn( "connection ", $k - 1, ": event $event (",
188             ZOOM::event_str($event), ")\n" )
189           if $DEBUG;
190         last if $event == ZOOM::Event::ZEND;
191     }
192
193     if ( $k != 0 ) {
194         $k--;
195         warn $serverhost[$k] if $DEBUG;
196         my ( $error, $errmsg, $addinfo, $diagset ) =
197           $oConnection[$k]->error_x();
198         if ($error) {
199             warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
200               if $DEBUG;
201
202         }
203         else {
204             my $numresults = $oResult[$k]->size();
205             my $i;
206             my $result = '';
207             if ( $numresults > 0 ) {
208                 for (
209                     $i = 0 ;
210                     $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
211                     $i++
212                   )
213                 {
214                     my $rec = $oResult[$k]->record($i);
215                     my $marcrecord;
216                     $marcdata   = $rec->raw();
217
218                     my ($charset_result, $charset_errors);
219                     ($marcrecord, $charset_result, $charset_errors) = 
220                         MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
221 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
222 ## In HEAD i change everything to UTF-8
223 # In rel2_2 i am not sure what encoding is so no character conversion is done here
224 ##Add necessary encoding changes to here -TG
225                     my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
226                     $oldbiblio->{isbn}   =~ s/ |-|\.//g,
227                       $oldbiblio->{issn} =~ s/ |-|\.//g,
228                       my (
229                         $notmarcrecord, $alreadyindb, $alreadyinfarm,
230                         $imported,      $breedingid
231                       )
232                       = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
233                     my %row_data;
234                     if ( $i % 2 ) {
235                         $toggle = 1;
236                     }
237                     else {
238                         $toggle = 0;
239                     }
240                     $row_data{toggle}       = $toggle;
241                     $row_data{server}       = $serverhost[$k];
242                     $row_data{isbn}         = $oldbiblio->{isbn};
243                     $row_data{lccn}         = $oldbiblio->{lccn};
244                     $row_data{title}        = $oldbiblio->{title};
245                     $row_data{author}       = $oldbiblio->{author};
246                     $row_data{breedingid}   = $breedingid;
247                     $row_data{biblionumber} = $biblionumber;
248                     push( @breeding_loop, \%row_data );
249                 }    # upto 5 results
250             }    #$numresults
251         }
252     }    # if $k !=0
253     $numberpending = $nremaining - 1;
254     $template->param(
255         breeding_loop => \@breeding_loop,
256         server        => $serverhost[$k],
257         numberpending => $numberpending,
258     );
259     
260     output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
261
262     #   print  $template->output  if $firstresult !=1;
263     $firstresult++;
264
265   MAYBE_AGAIN:
266     if ( --$nremaining > 0 ) {
267         goto AGAIN;
268     }
269 }    ## if op=search