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, |
38 |
my @spellings; |
my @spellings; |
39 |
# FIX: doesn't work very well |
# FIX: doesn't work very well |
40 |
if ($config->{findaffix}) { |
if ($config->{findaffix}) { |
41 |
my $spelling_alt = new Lingua::Spelling::Alternative; |
foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) { |
42 |
$spelling_alt->load_findaffix(x($config->{findaffix})); |
my $spelling_alt = new Lingua::Spelling::Alternative; |
43 |
push @spellings,$spelling_alt; |
$spelling_alt->load_findaffix($findaffix); |
44 |
|
push @spellings,$spelling_alt; |
45 |
|
} |
46 |
} |
} |
47 |
if ($config->{affix}) { |
if ($config->{affix}) { |
48 |
my $spelling_alt = new Lingua::Spelling::Alternative; |
foreach my $affix (split(/[, ]+/,x($config->{affix}))) { |
49 |
$spelling_alt->load_affix(x($config->{affix})); |
my $spelling_alt = new Lingua::Spelling::Alternative; |
50 |
push @spellings,$spelling_alt; |
$spelling_alt->load_affix($affix); |
51 |
|
push @spellings,$spelling_alt; |
52 |
|
} |
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 |
|
|
105 |
$search =~ s/^\s+//; |
$search =~ s/^\s+//; |
106 |
$search =~ s/\s+$//; |
$search =~ s/\s+$//; |
107 |
|
|
108 |
|
my %words; |
109 |
|
|
110 |
foreach (split(/\s+/,$search)) { |
foreach (split(/\s+/,$search)) { |
111 |
if (m/^([+-])(\S+)/) { |
if (m/^([+-])(\S+)/) { |
112 |
$s.= ($s) ? "and " : ""; |
$s.= ($s) ? "and " : ""; |
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 |
my $sh = SWISH->connect('Fork', |
# my $sh = SWISH->connect('Fork', |
173 |
prog => x($config->{prog}), |
# prog => x($config->{prog}), |
174 |
indexes => x($config->{index}), |
# indexes => x($config->{index}), |
175 |
results => sub { |
# results => sub { |
176 |
my ($sh,$hit) = @_; |
# my ($sh,$hit) = @_; |
177 |
|
# |
178 |
|
# 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 |
|
|
244 |
|
|
245 |
if ($config->{url}) { |
for(my $i=$pager->first; $i<=$pager->last; $i++) { |
|
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) ); |
|
246 |
|
|
247 |
|
my $result = $results->NextResult; |
248 |
|
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 |
die $SWISH::errstr unless $sh; |
} |
276 |
|
|
277 |
|
# pager navigation |
278 |
|
my $nav_html; |
279 |
|
|
280 |
$hits = $sh->query($s); |
my $nav_fmt=qq{ <a href="%s">%s</a> }; |
281 |
|
|
282 |
if ($hits && $hits > 0) { |
if ($pager->previous_set) { |
283 |
print p,hr; |
param('page', $pager->previous_set); |
284 |
printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); |
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'<<'); |
285 |
} else { |
} |
286 |
print p; |
|
287 |
printf (x($config->{text}->{no_hits}),$s,$sh->errstr); |
|
288 |
|
foreach my $p (@{$pager->pages_in_set()}) { |
289 |
|
# for (my $p=$pager->previous_set; $p <= $pager->next_set; $p++) { |
290 |
|
if($p == $pager->current_page()) { |
291 |
|
$nav_html .= "<b>$p</b> "; |
292 |
|
} 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 |
} |
} |