9 |
use Text::Iconv; |
use Text::Iconv; |
10 |
use Data::Pageset; |
use Data::Pageset; |
11 |
|
|
12 |
|
|
13 |
|
sub get_snippet { |
14 |
|
my $context_chars = 100; |
15 |
|
|
16 |
|
my $desc = shift || return ''; |
17 |
|
# test if $desc contains any of our query words |
18 |
|
my @snips; |
19 |
|
|
20 |
|
my @colors = qw{#ffff66 #a0ffff #99ff99 #ff9999 #ff66ff}; |
21 |
|
|
22 |
|
my $i = 0; |
23 |
|
|
24 |
|
for my $q (@_) { |
25 |
|
if ($desc =~ m/(.*?)(\Q$q\E)(.*)/si) { |
26 |
|
my $bef = $1; |
27 |
|
my $qm = $2; |
28 |
|
my $af = $3; |
29 |
|
$bef = substr $bef, -$context_chars; |
30 |
|
$af = substr $af, 0, $context_chars; |
31 |
|
|
32 |
|
# no partial words... |
33 |
|
$af =~ s,^\S+\s+|\s+\S+$,,gs; |
34 |
|
$bef =~ s,^\S+\s+|\s+\S+$,,gs; |
35 |
|
|
36 |
|
push(@snips, "$bef <span style=\"background:".$colors[$i]."\">$qm</span> $af"); |
37 |
|
$i++; |
38 |
|
$i = 0 if ($i > $#colors); |
39 |
|
} |
40 |
|
} |
41 |
|
my $ellip = ' ... '; |
42 |
|
my $snippet = $ellip. join($ellip, @snips) . $ellip; |
43 |
|
|
44 |
|
return $snippet; |
45 |
|
} |
46 |
|
|
47 |
# for pager |
# for pager |
48 |
my $pages_per_set = 20; |
my $pages_per_set = 20; |
49 |
|
|
51 |
my $config=XMLin(undef, |
my $config=XMLin(undef, |
52 |
# keyattr => { label => "value" }, |
# keyattr => { label => "value" }, |
53 |
forcecontent => 0, |
forcecontent => 0, |
54 |
|
ForceArray => [ 'path' ], |
55 |
); |
); |
56 |
|
|
57 |
my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset}); |
my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset}); |
76 |
# FIX: doesn't work very well |
# FIX: doesn't work very well |
77 |
if ($config->{findaffix}) { |
if ($config->{findaffix}) { |
78 |
foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) { |
foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) { |
79 |
|
next if (! -f $findaffix); |
80 |
my $spelling_alt = new Lingua::Spelling::Alternative; |
my $spelling_alt = new Lingua::Spelling::Alternative; |
81 |
$spelling_alt->load_findaffix($findaffix); |
$spelling_alt->load_findaffix($findaffix); |
82 |
push @spellings,$spelling_alt; |
push @spellings,$spelling_alt; |
84 |
} |
} |
85 |
if ($config->{affix}) { |
if ($config->{affix}) { |
86 |
foreach my $affix (split(/[, ]+/,x($config->{affix}))) { |
foreach my $affix (split(/[, ]+/,x($config->{affix}))) { |
87 |
|
next if (! -f $affix); |
88 |
my $spelling_alt = new Lingua::Spelling::Alternative; |
my $spelling_alt = new Lingua::Spelling::Alternative; |
89 |
$spelling_alt->load_affix($affix); |
$spelling_alt->load_affix($affix); |
90 |
push @spellings,$spelling_alt; |
push @spellings,$spelling_alt; |
100 |
$labels{$_->{value}} = x($_->{content}); |
$labels{$_->{value}} = x($_->{content}); |
101 |
} |
} |
102 |
|
|
103 |
my $path = param('path'); # limit to this path |
my $path; |
104 |
|
# limit to this path |
105 |
|
$path .= '"'.join('*" or "',param('path')).'*"' if (param('path')); |
106 |
my %path_label; |
my %path_label; |
107 |
my @path_name; |
my @path_name; |
108 |
foreach (@{$config->{paths}->{path}}) { |
foreach (@{$config->{paths}->{path}}) { |
130 |
if (@path_name) { |
if (@path_name) { |
131 |
print br,x($config->{text}->{limit}); |
print br,x($config->{text}->{limit}); |
132 |
print popup_menu(-name=>'path',-values=>\@path_name,-labels=>\%path_label,-default=>$path); |
print popup_menu(-name=>'path',-values=>\@path_name,-labels=>\%path_label,-default=>$path); |
133 |
|
} elsif (param('path')) { |
134 |
|
print hidden(-name=>'path',-values=>param('path')); |
135 |
} |
} |
136 |
print end_form,hr; |
print end_form,hr; |
137 |
|
|
186 |
$s=~s/\*\*+/*/g; |
$s=~s/\*\*+/*/g; |
187 |
|
|
188 |
# limit to some path |
# limit to some path |
189 |
$s = "swishdocpath=(\"*$path*\") and $s" if ($path); |
$s = "swishdocpath=($path) and $s" if ($path); |
190 |
|
|
191 |
my %params; # optional parametars for swish |
my %params; # optional parametars for swish |
192 |
|
|
212 |
$hit_fmt = x($config->{hit}) if (x($config->{hit})); |
$hit_fmt = x($config->{hit}) if (x($config->{hit})); |
213 |
} |
} |
214 |
|
|
|
# my $sh = SWISH->connect('Fork', |
|
|
# prog => x($config->{prog}), |
|
|
# indexes => x($config->{index}), |
|
|
# results => sub { |
|
|
# my ($sh,$hit) = @_; |
|
|
# |
|
|
# if ($config->{url}) { |
|
|
# printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties)); |
|
|
# } else { |
|
|
# printf ($hit_fmt ,$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties) ); |
|
|
# |
|
|
# } |
|
|
# |
|
|
## print $_[1]->as_string,"<br>\n"; |
|
|
## my @fields = $hit->field_names; |
|
|
## print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields; |
|
|
# }, |
|
|
# maxhits => param('max_hits') || $max_hits, |
|
|
# \%params, |
|
|
# ); |
|
|
# |
|
|
# die $SWISH::errstr unless $sh; |
|
|
# |
|
|
# $hits = $sh->query($s); |
|
|
# |
|
|
# if ($hits && $hits > 0) { |
|
|
# print p,hr; |
|
|
# printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); |
|
|
# } else { |
|
|
# print p; |
|
|
# printf (x($config->{text}->{no_hits}),$s,$sh->errstr); |
|
|
# } |
|
|
# if ($hits && $hits > 0) { |
|
|
# print p,hr; |
|
|
# printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); |
|
|
# } else { |
|
|
# print p; |
|
|
# printf (x($config->{text}->{no_hits}),$s,$sh->errstr); |
|
|
# } |
|
|
|
|
215 |
my $swish = SWISH::API->new($config->{index}); |
my $swish = SWISH::API->new($config->{index}); |
|
|
|
216 |
$swish->AbortLastError if $swish->Error; |
$swish->AbortLastError if $swish->Error; |
|
|
|
217 |
my $results = $swish->Query($s); |
my $results = $swish->Query($s); |
|
|
|
218 |
my $hits = $results->Hits; |
my $hits = $results->Hits; |
219 |
|
|
220 |
|
|
|
|
|
221 |
# build pager |
# build pager |
222 |
my $current_page = param('page') || 1; |
my $current_page = param('page') || 1; |
223 |
|
|
240 |
printf (x($config->{text}->{hits}),$i,$results->Hits,$s); |
printf (x($config->{text}->{hits}),$i,$results->Hits,$s); |
241 |
} |
} |
242 |
|
|
243 |
|
my %path2title; |
244 |
|
use Data::Dumper; |
245 |
|
foreach my $p (@{$config->{path2title}->{path}}) { |
246 |
|
$path2title{$p->{dir}} = $p->{content}; |
247 |
|
} |
248 |
|
|
249 |
for(my $i=$pager->first; $i<=$pager->last; $i++) { |
for(my $i=$pager->first; $i<=$pager->last; $i++) { |
250 |
|
|
252 |
last if (! $result); |
last if (! $result); |
253 |
|
|
254 |
my @arr; |
my @arr; |
255 |
|
|
256 |
foreach my $prop (@properties) { |
foreach my $prop (@properties) { |
257 |
if ($prop =~ m/swishdescription/) { |
if ($prop =~ m/swishdescription/) { |
258 |
my $tmp = $result->Property($prop); |
my $tmp = get_snippet( |
259 |
$tmp =~ s/<[^>]+>//g; |
$result->Property($prop), |
260 |
|
split(/\s+/,$search) |
261 |
|
); |
262 |
|
|
263 |
push @arr, $tmp; |
push @arr, $tmp; |
264 |
} else { |
} else { |
265 |
push @arr, $result->Property($prop); |
push @arr, $result->Property($prop); |
270 |
my $rank = $result->Property("swishrank"); |
my $rank = $result->Property("swishrank"); |
271 |
my $host = $result->Property("swishdocpath"); |
my $host = $result->Property("swishdocpath"); |
272 |
$host = "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath") if ($config->{url}); |
$host = "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath") if ($config->{url}); |
273 |
|
|
274 |
|
foreach my $p (keys %path2title) { |
275 |
|
if ($host =~ m/$p/i) { |
276 |
|
$title =~ s/$path2title{$p}\s*[:-]+\s*//; |
277 |
|
$title = $path2title{$p}." :: ".$title; |
278 |
|
last; |
279 |
|
} |
280 |
|
} |
281 |
|
|
282 |
print $tr_pre,$i,". "; |
print $tr_pre,$i,". "; |
283 |
# print collection name which is not link |
# print collection name which is not link |
284 |
if ($title =~ s/^(.+? :: )//) { |
if ($title =~ s/^(.+? :: )//) { |
285 |
print $1; |
print $1; |
286 |
} |
} |
287 |
|
|
288 |
printf($hit_fmt, $host, $title || 'untitled', $rank, @arr); |
printf($hit_fmt, $host, $title || 'untitled', $rank, @arr); |
289 |
print $tr_post; |
print $tr_post; |
290 |
|
|
295 |
|
|
296 |
my $nav_fmt=qq{ <a href="%s">%s</a> }; |
my $nav_fmt=qq{ <a href="%s">%s</a> }; |
297 |
|
|
298 |
|
if ($pager->current_page() > $pager->first_page) { |
299 |
|
param('page', $pager->current_page - 1); |
300 |
|
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'<<'); |
301 |
|
} |
302 |
|
|
303 |
if ($pager->previous_set) { |
if ($pager->previous_set) { |
304 |
param('page', $pager->previous_set); |
param('page', $pager->previous_set); |
305 |
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'<<'); |
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..'); |
306 |
} |
} |
307 |
|
|
308 |
|
|
319 |
|
|
320 |
if ($pager->next_set) { |
if ($pager->next_set) { |
321 |
param('page', $pager->next_set); |
param('page', $pager->next_set); |
322 |
|
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..'); |
323 |
|
} |
324 |
|
|
325 |
|
if ($pager->current_page() < $pager->last_page) { |
326 |
|
param('page', $pager->current_page + 1); |
327 |
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'>>'); |
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'>>'); |
328 |
} |
} |
329 |
|
|
330 |
|
if ($config->{text}->{pages}) { |
331 |
|
$nav_html = x($config->{text}->{pages})." ".$nav_html; |
332 |
|
} |
333 |
|
|
334 |
# end html table |
# end html table |
335 |
print qq{ |
print qq{ |
336 |
<tr><td> |
<tr><td> |
337 |
Pages: $nav_html |
$nav_html |
338 |
</td></tr> |
</td></tr> |
339 |
</table> |
</table> |
340 |
}; |
}; |