Add the faceted search facility
[koha.git] / z3950 / search.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use CGI;
22
23 use C4::Auth;
24 use C4::Output;
25 use C4::Interface::CGI::Output;
26 use C4::Biblio;
27 use C4::Context;
28 use C4::Breeding;
29 use MARC::File::USMARC;
30 use ZOOM;
31 use Encode;;
32
33 my $input = new CGI;
34 my $dbh = C4::Context->dbh;
35 my $error = $input->param('error');
36 my $oldbiblionumber=$input->param('oldbiblionumber');
37 $oldbiblionumber=0 unless $oldbiblionumber;
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=C4::Context->preference("marcflavour");
54 my @results;
55 my $count;
56 my $toggle;
57 my @breeding_loop = ();
58 my $record;
59 my $oldbiblio;
60 my $dbh = C4::Context->dbh;
61 my $errmsg;
62 my @serverloop=();
63 my @serverhost;
64 unless ($random) { # if random is a parameter => we're just waiting for the search to end, it's a refresh.
65 $random =rand(1000000000);
66 }
67
68
69 my ($template, $loggedinuser, $cookie);
70 if ($op ne "do_search"){
71
72 my $sth=$dbh->prepare("select id,host,checked from z3950servers  order by host");
73 $sth->execute();
74 while ($server=$sth->fetchrow_hashref) {
75 my %temploop;
76 $temploop{server}=$server->{host};
77 $temploop{id}=$server->{id};
78 $temploop{checked}=$server->{checked};
79 push (@serverloop, \%temploop);
80 }
81 ($template, $loggedinuser, $cookie)
82 = get_template_and_user({template_name => "z3950/searchresult.tmpl",
83                                 query => $input,
84                                 type => "intranet",
85                                 authnotrequired => 1,
86                                 flagsrequired => {catalogue => 1},
87                                 debug => 1,
88                                 });
89 $template->param(isbn=>$isbn, issn=>$issn,title=>$title,author=>$author,
90                                                 serverloop => \@serverloop,
91                                                 opsearch => "search",
92                                                 oldbiblionumber => $oldbiblionumber,
93                                                 );
94 output_html_with_http_headers $input, $cookie, $template->output;
95
96 }else{
97
98 my @id=$input->param('id');
99 my @oConnection;
100 my @oResult;
101 my $s=0;
102                                                         if ($isbn ne "/" || $issn ne "/") {
103                                                                 $attr='1=7';
104                                                         $term=$isbn if ($isbn ne"/");
105                                                         $term=$issn if ($issn ne"/");
106                                                         } elsif ($title ne"/") {
107                                                                 $attr='1=4 @attr 4=1  ';
108                                                         $term=$title;
109                                                         } elsif ($author ne "/") {
110                                                                 $attr='1=1003';
111                                                         $term=$author;
112                                                         } 
113
114                                                         
115 my $query="\@attr $attr \"$term\"";     
116                         
117   foreach my $servid ( @id){
118   my $sth=$dbh->prepare("select name, port, db, host from z3950servers where id=?");
119   $sth->execute($servid);
120
121     while ($server=$sth->fetchrow_hashref) {
122         my $noconnection=0;
123         #$numberpending=1;
124                                                         
125         my $option1=new ZOOM::Options();
126         $option1->option(async=>1);
127         $option1->option('elementSetName', 'F');
128         $option1->option('databaseName',$server->{db})  ;
129         $option1->option('preferredRecordSyntax', 'USMARC');
130          $oConnection[$s]=create ZOOM::Connection($option1);
131         $oConnection[$s]->connect($server->{name}, $server->{port});
132         $serverhost[$s]=$server->{host};
133         $s++;
134     }## while fetch
135
136   }# foreach
137 my $nremaining = $s;
138 my $firstresult=1;
139  for (my $z=0 ;$z<$s;$z++){
140 $oResult[$z] = $oConnection[$z]->search_pqf($query);
141
142 }
143 AGAIN:
144  my $k;
145 my $event;
146   while (($k = ZOOM::event(\@oConnection)) != 0) {
147         $event = $oConnection[$k-1]->last_event();
148 # warn ("connection ", $k-1, ": event $event (", ZOOM::event_str($event), ")\n");
149         last if $event == ZOOM::Event::ZEND;
150    }
151 if ($k != 0) {
152         $k--;
153 #warn $serverhost[$k];
154          my($error, $errmsg, $addinfo, $diagset) = $oConnection[$k]->error_x();
155         if ($error) {
156
157         warn "$k $serverhost[$k] error $query: $errmsg ($error) $addinfo\n";
158         goto MAYBE_AGAIN;
159         }
160         
161         my $numresults=$oResult[$k]->size() ;                                                           
162                                                                         
163
164         my $i;
165         my $result='';
166         if ($numresults>0){
167                 for ($i=0; $i<(($numresults<5) ? ($numresults) : (5)) ; $i++) {
168                         my $rec=$oResult[$k]->record($i);                                                                               
169                         my $marcrecord;
170                         $marcdata = $rec->raw();                                                                                        
171                         $marcrecord = MARC::File::USMARC::decode($marcdata);
172                         
173                         my $marcxml=$marcrecord->as_xml_record($marcrecord);
174                         $marcxml=Encode::encode('utf8',$marcxml);
175                         #$marcxml=Encode::decode('utf8',$marcxml);
176                         my $xmlhash=XML_xml2hash_onerecord($marcxml);                                           
177                 my $oldbiblio = XMLmarc2koha_onerecord($dbh,$xmlhash,'biblios');
178                                 $oldbiblio->{isbn} =~ s/ |-|\.//g,
179                         $oldbiblio->{isbn} = substr($oldbiblio->{isbn},0,10);
180                         $oldbiblio->{issn} =~ s/ |-|\.//g,
181                         $oldbiblio->{issn} = substr($oldbiblio->{issn},0,10);
182
183 my ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$bid)=ImportBreeding($marcdata,1,$serverhost[$k],$encoding,$random);
184                         my %row_data;
185  @breeding_loop = ();
186         if ($i % 2) {
187                 $toggle="#ffffcc";
188         } else {
189                 $toggle="white";
190         }
191         $row_data{toggle} = $toggle;
192         $row_data{server} = $serverhost[$k];
193         $row_data{isbn} = $oldbiblio->{isbn};
194
195         $row_data{title} =$oldbiblio->{title};
196         $row_data{author} = $oldbiblio->{author};
197         $row_data{id} = $bid;
198         $row_data{oldbiblionumber}=$oldbiblionumber;
199         push (@breeding_loop, \%row_data);
200
201                                                 
202 }# $numresults
203 }#for up to 5 results
204 }# if $k !=0
205
206 my ($template, $loggedinuser, $cookie)
207 = get_template_and_user({template_name => "z3950/searchresult.tmpl",
208                                 query => $input,
209                                 type => "intranet",
210                                 authnotrequired => 1,
211                                 flagsrequired => {catalogue => 1},
212                                 debug => 1,
213                                 });
214 $numberpending=$nremaining-1;
215 $template->param(
216                                                 breeding_loop => \@breeding_loop,
217 #                                                refresh=>($numberpending eq 0 ? "" : "search.pl?random=$random"),
218                                                 numberpending => $numberpending,
219 #                                               oldbiblionumber => $oldbiblionumber,
220                                                 );
221 output_html_with_http_headers $input, "", $template->output if $firstresult==1;
222 $firstresult++;
223 print  $template->output if $firstresult !=1;
224 MAYBE_AGAIN:
225 if (--$nremaining > 0) {
226     goto AGAIN;
227 }
228 } ## if op=search