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 |
|
my $max_desc = 16384; # 16k to filter |
16 |
|
|
17 |
|
my $desc = shift || return ''; |
18 |
|
$desc = substr($desc,0,$max_desc) if (length($desc) > $max_desc); |
19 |
|
$desc = e($desc); |
20 |
|
# test if $desc contains any of our query words |
21 |
|
my @snips; |
22 |
|
|
23 |
|
my @colors = qw{#ffff66 #a0ffff #99ff99 #ff9999 #ff66ff}; |
24 |
|
|
25 |
|
my $i = 0; |
26 |
|
|
27 |
|
for my $q (@_) { |
28 |
|
if ($desc =~ m/(.*?)(\Q$q\E)(.*)/si) { |
29 |
|
my $bef = $1; |
30 |
|
my $qm = $2; |
31 |
|
my $af = $3; |
32 |
|
$bef = substr $bef, -$context_chars; |
33 |
|
$af = substr $af, 0, $context_chars; |
34 |
|
|
35 |
|
# no partial words... |
36 |
|
$af =~ s,^\S+\s+|\s+\S+$,,gs; |
37 |
|
$bef =~ s,^\S+\s+|\s+\S+$,,gs; |
38 |
|
|
39 |
|
push(@snips, "$bef <span style=\"background:".$colors[$i]."; color:black;\">$qm</span> $af"); |
40 |
|
$i++; |
41 |
|
$i = 0 if ($i > $#colors); |
42 |
|
} |
43 |
|
} |
44 |
|
my $ellip = ' ... '; |
45 |
|
my $snippet = $ellip. join($ellip, @snips) . $ellip if (@snips); |
46 |
|
|
47 |
|
return $snippet; |
48 |
|
} |
49 |
|
|
50 |
# for pager |
# for pager |
51 |
my $pages_per_set = 20; |
my $pages_per_set = 20; |
52 |
|
|
54 |
my $config=XMLin(undef, |
my $config=XMLin(undef, |
55 |
# keyattr => { label => "value" }, |
# keyattr => { label => "value" }, |
56 |
forcecontent => 0, |
forcecontent => 0, |
57 |
|
ForceArray => [ 'path' ], |
58 |
); |
); |
59 |
|
|
60 |
my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset}); |
my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset}); |
79 |
# FIX: doesn't work very well |
# FIX: doesn't work very well |
80 |
if ($config->{findaffix}) { |
if ($config->{findaffix}) { |
81 |
foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) { |
foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) { |
82 |
|
next if (! -f $findaffix); |
83 |
my $spelling_alt = new Lingua::Spelling::Alternative; |
my $spelling_alt = new Lingua::Spelling::Alternative; |
84 |
$spelling_alt->load_findaffix($findaffix); |
$spelling_alt->load_findaffix($findaffix); |
85 |
push @spellings,$spelling_alt; |
push @spellings,$spelling_alt; |
87 |
} |
} |
88 |
if ($config->{affix}) { |
if ($config->{affix}) { |
89 |
foreach my $affix (split(/[, ]+/,x($config->{affix}))) { |
foreach my $affix (split(/[, ]+/,x($config->{affix}))) { |
90 |
|
next if (! -f $affix); |
91 |
my $spelling_alt = new Lingua::Spelling::Alternative; |
my $spelling_alt = new Lingua::Spelling::Alternative; |
92 |
$spelling_alt->load_affix($affix); |
$spelling_alt->load_affix($affix); |
93 |
push @spellings,$spelling_alt; |
push @spellings,$spelling_alt; |
103 |
$labels{$_->{value}} = x($_->{content}); |
$labels{$_->{value}} = x($_->{content}); |
104 |
} |
} |
105 |
|
|
106 |
my $path = param('path'); # limit to this path |
my $path; |
107 |
|
# limit to this path |
108 |
|
$path .= '"'.join('*" or "',param('path')).'*"' if (param('path')); |
109 |
my %path_label; |
my %path_label; |
110 |
my @path_name; |
my @path_name; |
111 |
foreach (@{$config->{paths}->{path}}) { |
foreach (@{$config->{paths}->{path}}) { |
133 |
if (@path_name) { |
if (@path_name) { |
134 |
print br,x($config->{text}->{limit}); |
print br,x($config->{text}->{limit}); |
135 |
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); |
136 |
|
} elsif (param('path')) { |
137 |
|
print hidden(-name=>'path',-values=>param('path')); |
138 |
} |
} |
139 |
print end_form,hr; |
print end_form,hr; |
140 |
|
|
189 |
$s=~s/\*\*+/*/g; |
$s=~s/\*\*+/*/g; |
190 |
|
|
191 |
# limit to some path |
# limit to some path |
192 |
$s = "swishdocpath=(\"*$path*\") and $s" if ($path); |
$s = "swishdocpath=($path) and $s" if ($path); |
193 |
|
|
194 |
my %params; # optional parametars for swish |
my %params; # optional parametars for swish |
195 |
|
|
196 |
# default format for output |
# default format for output |
197 |
my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n"; |
my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n"; |
198 |
|
|
|
# output start of table |
|
|
print qq{ |
|
|
<table border="0"> |
|
|
}; |
|
|
# html before and after each hit |
|
|
my $tr_pre = qq{ |
|
|
<tr><td> |
|
|
}; |
|
|
my $tr_post = qq{ |
|
|
</td></tr> |
|
|
}; |
|
|
|
|
199 |
if (@properties) { |
if (@properties) { |
200 |
$hit_fmt = x($config->{hit}) if (! param('no_properties')); |
$hit_fmt = x($config->{hit}) if (! param('no_properties')); |
201 |
$params{properties} = \@properties; |
$params{properties} = \@properties; |
203 |
$hit_fmt = x($config->{hit}) if (x($config->{hit})); |
$hit_fmt = x($config->{hit}) if (x($config->{hit})); |
204 |
} |
} |
205 |
|
|
|
# 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); |
|
|
# } |
|
|
|
|
206 |
my $swish = SWISH::API->new($config->{index}); |
my $swish = SWISH::API->new($config->{index}); |
|
|
|
207 |
$swish->AbortLastError if $swish->Error; |
$swish->AbortLastError if $swish->Error; |
|
|
|
208 |
my $results = $swish->Query($s); |
my $results = $swish->Query($s); |
|
|
|
209 |
my $hits = $results->Hits; |
my $hits = $results->Hits; |
210 |
|
|
211 |
|
|
|
|
|
212 |
# build pager |
# build pager |
213 |
my $current_page = param('page') || 1; |
my $current_page = param('page') || 1; |
214 |
|
|
231 |
printf (x($config->{text}->{hits}),$i,$results->Hits,$s); |
printf (x($config->{text}->{hits}),$i,$results->Hits,$s); |
232 |
} |
} |
233 |
|
|
234 |
|
my %path2title; |
235 |
|
foreach my $p (@{$config->{path2title}->{path}}) { |
236 |
|
$path2title{$p->{dir}} = $p->{content}; |
237 |
|
} |
238 |
|
|
239 |
|
# output start of table |
240 |
|
print qq{ |
241 |
|
<table border="0"> |
242 |
|
}; |
243 |
|
# html before and after each hit |
244 |
|
my $tr_pre = qq{ |
245 |
|
<tr><td> |
246 |
|
}; |
247 |
|
my $tr_post = qq{ |
248 |
|
</td></tr> |
249 |
|
}; |
250 |
|
|
251 |
for(my $i=$pager->first; $i<=$pager->last; $i++) { |
for(my $i=$pager->first; $i<=$pager->last; $i++) { |
252 |
|
|
254 |
last if (! $result); |
last if (! $result); |
255 |
|
|
256 |
my @arr; |
my @arr; |
257 |
|
|
258 |
foreach my $prop (@properties) { |
foreach my $prop (@properties) { |
259 |
if ($prop =~ m/swishdescription/) { |
if ($prop =~ m/swishdescription/) { |
260 |
my $tmp = $result->Property($prop); |
my $tmp = get_snippet( |
261 |
$tmp =~ s/<[^>]+>//g; |
$result->Property($prop), |
262 |
|
split(/\s+/,$search) |
263 |
|
); |
264 |
|
|
265 |
push @arr, $tmp; |
push @arr, $tmp; |
266 |
} else { |
} else { |
267 |
push @arr, $result->Property($prop); |
push @arr, $result->Property($prop); |
272 |
my $rank = $result->Property("swishrank"); |
my $rank = $result->Property("swishrank"); |
273 |
my $host = $result->Property("swishdocpath"); |
my $host = $result->Property("swishdocpath"); |
274 |
$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}); |
275 |
|
|
276 |
|
foreach my $p (keys %path2title) { |
277 |
|
if ($host =~ m/$p/i) { |
278 |
|
$title =~ s/$path2title{$p}\s*[:-]+\s*//; |
279 |
|
$title = $path2title{$p}." :: ".$title; |
280 |
|
last; |
281 |
|
} |
282 |
|
} |
283 |
|
|
284 |
print $tr_pre,$i,". "; |
print $tr_pre,$i,". "; |
285 |
# print collection name which is not link |
# print collection name which is not link |
286 |
if ($title =~ s/^(.+? :: )//) { |
if ($title =~ s/^(.+? :: )//) { |
287 |
print $1; |
print $1; |
288 |
} |
} |
289 |
|
|
290 |
printf($hit_fmt, $host, $title || 'untitled', $rank, @arr); |
printf($hit_fmt, $host, $title || 'untitled', $rank, @arr); |
291 |
print $tr_post; |
print $tr_post; |
292 |
|
|
297 |
|
|
298 |
my $nav_fmt=qq{ <a href="%s">%s</a> }; |
my $nav_fmt=qq{ <a href="%s">%s</a> }; |
299 |
|
|
300 |
|
if ($pager->current_page() > $pager->first_page) { |
301 |
|
param('page', $pager->current_page - 1); |
302 |
|
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'<<'); |
303 |
|
} |
304 |
|
|
305 |
if ($pager->previous_set) { |
if ($pager->previous_set) { |
306 |
param('page', $pager->previous_set); |
param('page', $pager->previous_set); |
307 |
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'<<'); |
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..'); |
308 |
} |
} |
309 |
|
|
310 |
|
|
321 |
|
|
322 |
if ($pager->next_set) { |
if ($pager->next_set) { |
323 |
param('page', $pager->next_set); |
param('page', $pager->next_set); |
324 |
|
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'..'); |
325 |
|
} |
326 |
|
|
327 |
|
if ($pager->current_page() < $pager->last_page) { |
328 |
|
param('page', $pager->current_page + 1); |
329 |
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'>>'); |
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'>>'); |
330 |
} |
} |
331 |
|
|
332 |
|
if ($config->{text}->{pages}) { |
333 |
|
$nav_html = x($config->{text}->{pages})." ".$nav_html; |
334 |
|
} |
335 |
|
|
336 |
# end html table |
# end html table |
337 |
print qq{ |
print qq{ |
338 |
<tr><td> |
<tr><td> |
339 |
Pages: $nav_html |
$nav_html |
340 |
</td></tr> |
</td></tr> |
341 |
</table> |
</table> |
342 |
}; |
}; |