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