3 |
use strict; |
use strict; |
4 |
use CGI qw/:standard -no_xhtml/; |
use CGI qw/:standard -no_xhtml/; |
5 |
use CGI::Carp qw(fatalsToBrowser); |
use CGI::Carp qw(fatalsToBrowser); |
6 |
use SWISH; |
use SWISH::API; |
7 |
use XML::Simple; |
use XML::Simple; |
8 |
use Lingua::Spelling::Alternative; |
use Lingua::Spelling::Alternative; |
9 |
use Text::Iconv; |
use Text::Iconv; |
10 |
|
use Data::Pageset; |
11 |
|
|
12 |
|
# for pager |
13 |
|
my $pages_per_set = 20; |
14 |
|
|
15 |
Text::Iconv->raise_error(0); # Conversion errors raise exceptions |
Text::Iconv->raise_error(0); # Conversion errors raise exceptions |
16 |
my $config=XMLin(undef, |
my $config=XMLin(undef, |
53 |
} |
} |
54 |
|
|
55 |
my $hits=0; |
my $hits=0; |
56 |
my $max_hits=x($config->{max_hits}); |
my $max_hits=param('max_hits') || x($config->{max_hits}); |
57 |
|
|
58 |
my %labels; |
my %labels; |
59 |
foreach (@{$config->{labels}->{label}}) { |
foreach (@{$config->{labels}->{label}}) { |
60 |
|
next if (! $_->{value}); # skip unlimited (0) |
61 |
$labels{$_->{value}} = x($_->{content}); |
$labels{$_->{value}} = x($_->{content}); |
62 |
} |
} |
63 |
|
|
150 |
# default format for output |
# default format for output |
151 |
my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n"; |
my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n"; |
152 |
|
|
153 |
|
# output start of table |
154 |
|
print qq{ |
155 |
|
<table border="0"> |
156 |
|
}; |
157 |
|
# html before and after each hit |
158 |
|
my $tr_pre = qq{ |
159 |
|
<tr><td> |
160 |
|
}; |
161 |
|
my $tr_post = qq{ |
162 |
|
</td></tr> |
163 |
|
}; |
164 |
|
|
165 |
if (@properties) { |
if (@properties) { |
166 |
$hit_fmt = x($config->{hit}) if (! param('no_properties')); |
$hit_fmt = x($config->{hit}) if (! param('no_properties')); |
167 |
$params{properties} = \@properties; |
$params{properties} = \@properties; |
169 |
$hit_fmt = x($config->{hit}) if (x($config->{hit})); |
$hit_fmt = x($config->{hit}) if (x($config->{hit})); |
170 |
} |
} |
171 |
|
|
172 |
sub kill_html { |
# my $sh = SWISH->connect('Fork', |
173 |
my @out; |
# prog => x($config->{prog}), |
174 |
foreach (@_) { |
# indexes => x($config->{index}), |
175 |
s/<[^>]+>//g; |
# results => sub { |
176 |
push @out,$_; |
# my ($sh,$hit) = @_; |
177 |
} |
# |
178 |
return @out; |
# if ($config->{url}) { |
179 |
|
# printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties)); |
180 |
|
# } else { |
181 |
|
# printf ($hit_fmt ,$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties) ); |
182 |
|
# |
183 |
|
# } |
184 |
|
# |
185 |
|
## print $_[1]->as_string,"<br>\n"; |
186 |
|
## my @fields = $hit->field_names; |
187 |
|
## print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields; |
188 |
|
# }, |
189 |
|
# maxhits => param('max_hits') || $max_hits, |
190 |
|
# \%params, |
191 |
|
# ); |
192 |
|
# |
193 |
|
# die $SWISH::errstr unless $sh; |
194 |
|
# |
195 |
|
# $hits = $sh->query($s); |
196 |
|
# |
197 |
|
# if ($hits && $hits > 0) { |
198 |
|
# print p,hr; |
199 |
|
# printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); |
200 |
|
# } else { |
201 |
|
# print p; |
202 |
|
# printf (x($config->{text}->{no_hits}),$s,$sh->errstr); |
203 |
|
# } |
204 |
|
# if ($hits && $hits > 0) { |
205 |
|
# print p,hr; |
206 |
|
# printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); |
207 |
|
# } else { |
208 |
|
# print p; |
209 |
|
# printf (x($config->{text}->{no_hits}),$s,$sh->errstr); |
210 |
|
# } |
211 |
|
|
212 |
|
my $swish = SWISH::API->new($config->{index}); |
213 |
|
|
214 |
|
$swish->AbortLastError if $swish->Error; |
215 |
|
|
216 |
|
my $results = $swish->Query($s); |
217 |
|
|
218 |
|
my $hits = $results->Hits; |
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
# build pager |
223 |
|
my $current_page = param('page') || 1; |
224 |
|
|
225 |
|
my $pager = Data::Pageset->new({ |
226 |
|
'total_entries' => $hits, |
227 |
|
'entries_per_page' => $max_hits, |
228 |
|
'current_page' => $current_page, |
229 |
|
'pages_per_set' => $pages_per_set, |
230 |
|
}); |
231 |
|
|
232 |
|
$results->SeekResult( $pager->first - 1 ); |
233 |
|
|
234 |
|
# get number of entries on this page |
235 |
|
my $i = $pager->entries_on_this_page; |
236 |
|
|
237 |
|
# print number of hits or error message |
238 |
|
if ( !$hits ) { |
239 |
|
printf (x($config->{text}->{no_hits}),$s,$swish->ErrorString); |
240 |
|
} else { |
241 |
|
printf (x($config->{text}->{hits}),$i,$results->Hits,$s); |
242 |
} |
} |
243 |
|
|
|
my $sh = SWISH->connect('Fork', |
|
|
prog => x($config->{prog}), |
|
|
indexes => x($config->{index}), |
|
|
results => sub { |
|
|
my ($sh,$hit) = @_; |
|
244 |
|
|
245 |
if ($config->{url}) { |
for(my $i=$pager->first; $i<=$pager->last; $i++) { |
246 |
printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties)); |
|
247 |
} else { |
my $result = $results->NextResult; |
248 |
printf ($hit_fmt ,$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties) ); |
last if (! $result); |
249 |
|
|
250 |
|
my @arr; |
251 |
|
foreach my $prop (@properties) { |
252 |
|
if ($prop =~ m/swishdescription/) { |
253 |
|
my $tmp = $result->Property($prop); |
254 |
|
$tmp =~ s/<[^>]+>//g; |
255 |
|
push @arr, $tmp; |
256 |
|
} else { |
257 |
|
push @arr, $result->Property($prop); |
258 |
} |
} |
259 |
|
} |
260 |
|
|
261 |
# print $_[1]->as_string,"<br>\n"; |
print $tr_pre,$i,". "; |
262 |
# my @fields = $hit->field_names; |
if ($config->{url}) { |
263 |
# print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields; |
printf($hit_fmt, "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath"), |
264 |
}, |
e($result->Property("swishtitle")) || 'untitled', |
265 |
maxhits => param('max_hits') || $max_hits, |
$result->Property("swishrank"), |
266 |
\%params, |
@arr); |
267 |
); |
} else { |
268 |
|
printf($hit_fmt, $result->Property("swishdocpath"), |
269 |
|
e($result->Property("swishtitle")) || 'untitled', |
270 |
|
$result->Property("swishrank"), |
271 |
|
@arr); |
272 |
|
} |
273 |
|
print $tr_post; |
274 |
|
|
275 |
|
} |
276 |
|
|
277 |
die $SWISH::errstr unless $sh; |
# pager navigation |
278 |
|
my $nav_html; |
279 |
|
|
280 |
|
my $nav_fmt=qq{ <a href="%s">%s</a> }; |
281 |
|
|
282 |
$hits = $sh->query($s); |
if ($pager->previous_set) { |
283 |
|
param('page', $pager->previous_set); |
284 |
|
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'<<'); |
285 |
|
} |
286 |
|
|
287 |
if ($hits && $hits > 0) { |
|
288 |
print p,hr; |
foreach my $p (@{$pager->pages_in_set()}) { |
289 |
printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); |
# for (my $p=$pager->previous_set; $p <= $pager->next_set; $p++) { |
290 |
} else { |
if($p == $pager->current_page()) { |
291 |
print p; |
$nav_html .= "<b>$p</b> "; |
292 |
printf (x($config->{text}->{no_hits}),$s,$sh->errstr); |
} else { |
293 |
|
param('page', $p); |
294 |
|
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),$p); |
295 |
|
} |
296 |
} |
} |
297 |
|
|
298 |
|
if ($pager->next_set) { |
299 |
|
param('page', $pager->next_set); |
300 |
|
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'>>'); |
301 |
|
} |
302 |
|
|
303 |
|
# end html table |
304 |
|
print qq{ |
305 |
|
<tr><td> |
306 |
|
Pages: $nav_html |
307 |
|
</td></tr> |
308 |
|
</table> |
309 |
|
}; |
310 |
|
|
311 |
|
|
312 |
|
|
313 |
} else { |
} else { |
314 |
print p(x($config->{text}->{footer})); |
print p(x($config->{text}->{footer})); |
315 |
} |
} |