displaying the query to search
[koha-equinox.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 ZOOM;
31
32 my $input        = new CGI;
33 my $dbh          = C4::Context->dbh;
34 my $error        = $input->param('error');
35 my $biblionumber = $input->param('biblionumber');
36 $biblionumber = 0 unless $biblionumber;
37 my $frameworkcode = $input->param('frameworkcode');
38 my $title         = $input->param('title');
39 my $author        = $input->param('author');
40 my $isbn          = $input->param('isbn');
41 my $issn          = $input->param('issn');
42 my $random        = $input->param('random');
43 my $op            = $input->param('op');
44 my $noconnection;
45 my $numberpending;
46 my $attr = '';
47 my $term;
48 my $host;
49 my $server;
50 my $database;
51 my $port;
52 my $marcdata;
53 my @encoding;
54 my @results;
55 my $count;
56 my $toggle;
57 my $record;
58 my $oldbiblio;
59 my $dbh = C4::Context->dbh;
60 my $errmsg;
61 my @serverloop = ();
62 my @serverhost;
63 my @breeding_loop = ();
64
65 my $DEBUG = 1;    # if set to 1, many debug message are send on syslog.
66
67 unless ($random)
68 {    # this var is not useful anymore just kept to keep rel2_2 compatibility
69     $random = rand(1000000000);
70 }
71
72 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
73     {
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
83 $template->param( frameworkcode => $frameworkcode, );
84
85 if ( $op ne "do_search" ) {
86     my $sth = $dbh->prepare("select id,host,checked from z3950servers  order by host");
87     $sth->execute();
88     my $serverloop = $sth->fetchall_arrayref( {} );
89     $template->param(
90         isbn         => $isbn,
91         issn         => $issn,
92         title        => $title,
93         author       => $author,
94         serverloop   => $serverloop,
95         opsearch     => "search",
96         biblionumber => $biblionumber,
97     );
98     output_html_with_http_headers $input, $cookie, $template->output;
99 }
100 else {
101     my @id = $input->param('id');
102     my @oConnection;
103     my @oResult;
104     my $s = 0;
105
106     if ( $isbn || $issn ) {
107         $attr = '1=7';
108         $term = $isbn if ($isbn);
109         $term = $issn if ($issn);
110     }
111     elsif ($title) {
112         $attr = '1=4 ';
113         utf8::decode($title);
114         $term = $title;
115     }
116     elsif ($author) {
117         $attr = '1=1003';
118         utf8::decode($author);
119         $term = $author;
120     }
121
122     my $query = "\@attr $attr \"$term\"";
123     warn "query " . $query if $DEBUG;
124     foreach my $servid (@id) {
125         my $sth = $dbh->prepare("select * from z3950servers where id=?");
126         $sth->execute($servid);
127         while ( $server = $sth->fetchrow_hashref ) {
128             my $noconnection = 0;
129             my $option1      = new ZOOM::Options();
130             $option1->option( 'async' => 1 );
131             $option1->option( 'elementSetName', 'F' );
132             $option1->option( 'databaseName',   $server->{db} );
133             $option1->option( 'user', $server->{userid} ) if $server->{userid};
134             $option1->option( 'password', $server->{password} )
135               if $server->{password};
136             $option1->option( 'preferredRecordSyntax', $server->{syntax} );
137             $oConnection[$s] = create ZOOM::Connection($option1)
138               || $DEBUG
139               && warn( "something went wrong: " . $oConnection[$s]->errmsg() );
140             warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
141             $oConnection[$s]->connect( $server->{host}, $server->{port} )
142               || $DEBUG
143               && warn( "something went wrong: " . $oConnection[$s]->errmsg() );
144             $serverhost[$s] = $server->{host};
145             $encoding[$s]   = $server->{syntax};
146             $s++;
147         }    ## while fetch
148     }    # foreach
149     my $nremaining  = $s;
150     my $firstresult = 1;
151
152     for ( my $z = 0 ; $z < $s ; $z++ ) {
153         warn "doing the search" if $DEBUG;
154         $oResult[$z] = $oConnection[$z]->search_pqf($query)
155           || $DEBUG
156           && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
157
158         #$oResult[$z] = $oConnection[$z]->search_pqf($query);
159     }
160
161   AGAIN:
162     my $k;
163     my $event;
164     while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
165         $event = $oConnection[ $k - 1 ]->last_event();
166         warn( "connection ", $k - 1, ": event $event (",
167             ZOOM::event_str($event), ")\n" )
168           if $DEBUG;
169         last if $event == ZOOM::Event::ZEND;
170     }
171
172     if ( $k != 0 ) {
173         $k--;
174         warn $serverhost[$k] if $DEBUG;
175         my ( $error, $errmsg, $addinfo, $diagset ) =
176           $oConnection[$k]->error_x();
177         if ($error) {
178             warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n"
179               if $DEBUG;
180
181         }
182         else {
183             my $numresults = $oResult[$k]->size();
184             my $i;
185             my $result = '';
186             if ( $numresults > 0 ) {
187                 for (
188                     $i = 0 ;
189                     $i < ( ( $numresults < 20 ) ? ($numresults) : (20) ) ;
190                     $i++
191                   )
192                 {
193                     my $rec = $oResult[$k]->record($i);
194                     my $marcrecord;
195                     $marcdata   = $rec->raw();
196                     $marcrecord = FixEncoding($marcdata);
197 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
198 ## In HEAD i change everything to UTF-8
199 # In rel2_2 i am not sure what encoding is so no character conversion is done here
200 ##Add necessary encoding changes to here -TG
201                     my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
202                     $oldbiblio->{isbn}   =~ s/ |-|\.//g,
203                       $oldbiblio->{issn} =~ s/ |-|\.//g,
204                       my (
205                         $notmarcrecord, $alreadyindb, $alreadyinfarm,
206                         $imported,      $breedingid
207                       )
208                       = ImportBreeding( $marcdata, 1, $serverhost[$k],
209                         $encoding[$k], $random );
210                         
211                         $encoding[$k], $random ));
212                         
213                     my %row_data;
214                     if ( $i % 2 ) {
215                         $toggle = "#ffffcc";
216                     }
217                     else {
218                         $toggle = "white";
219                     }
220                     $row_data{toggle}       = $toggle;
221                     $row_data{server}       = $serverhost[$k];
222                     $row_data{isbn}         = $oldbiblio->{isbn};
223                     $row_data{title}        = $oldbiblio->{title};
224                     $row_data{author}       = $oldbiblio->{author};
225                     $row_data{breedingid}   = $breedingid;
226                     $row_data{biblionumber} = $biblionumber;
227                     push( @breeding_loop, \%row_data );
228                 }    # upto 5 results
229             }    #$numresults
230         }
231     }    # if $k !=0
232     $numberpending = $nremaining - 1;
233     $template->param(
234         breeding_loop => \@breeding_loop,
235         server        => $serverhost[$k],
236         numberpending => $numberpending,
237     );
238     
239     output_html_with_http_headers $input, $cookie, $template->output if $numberpending == 0;
240
241     #   print  $template->output  if $firstresult !=1;
242     $firstresult++;
243
244   MAYBE_AGAIN:
245     if ( --$nremaining > 0 ) {
246         goto AGAIN;
247     }
248 }    ## if op=search