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, |
20 |
|
|
21 |
my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset}); |
my $from_utf8 = Text::Iconv->new('UTF8', $config->{charset}); |
22 |
sub x { |
sub x { |
23 |
|
return if (! defined $_[0]); |
24 |
return $from_utf8->convert($_[0]); |
return $from_utf8->convert($_[0]); |
25 |
} |
} |
26 |
|
|
27 |
use Data::Dumper; |
# Escape <, >, & and ", and to produce valid XML |
28 |
#print Dumper($config); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
29 |
|
my $escape_re = join '|' => keys %escape; |
30 |
|
sub e { |
31 |
|
my $out; |
32 |
|
foreach my $v (@_) { |
33 |
|
$v =~ s/($escape_re)/$escape{$1}/g; |
34 |
|
$out .= $v; |
35 |
|
} |
36 |
|
return $out; |
37 |
|
} |
38 |
|
|
39 |
my $spelling_alt; |
my @spellings; |
40 |
# FIX: doesn't work very well |
# FIX: doesn't work very well |
41 |
if ($config->{findaffix}) { |
if ($config->{findaffix}) { |
42 |
$spelling_alt = new Lingua::Spelling::Alternative; |
foreach my $findaffix (split(/[, ]+/,x($config->{findaffix}))) { |
43 |
$spelling_alt->load_findaffix(x($config->{findaffix})); |
my $spelling_alt = new Lingua::Spelling::Alternative; |
44 |
|
$spelling_alt->load_findaffix($findaffix); |
45 |
|
push @spellings,$spelling_alt; |
46 |
|
} |
47 |
} |
} |
48 |
if ($config->{affix}) { |
if ($config->{affix}) { |
49 |
$spelling_alt = new Lingua::Spelling::Alternative; |
foreach my $affix (split(/[, ]+/,x($config->{affix}))) { |
50 |
$spelling_alt->load_affix(x($config->{affix})); |
my $spelling_alt = new Lingua::Spelling::Alternative; |
51 |
|
$spelling_alt->load_affix($affix); |
52 |
|
push @spellings,$spelling_alt; |
53 |
|
} |
54 |
} |
} |
55 |
|
|
56 |
my $hits=0; |
my $hits=0; |
57 |
my $max_hits=x($config->{max_hits}); |
my $max_hits=param('max_hits') || x($config->{max_hits}); |
58 |
|
|
59 |
my %labels; |
my %labels; |
60 |
foreach (@{$config->{labels}->{label}}) { |
foreach (@{$config->{labels}->{label}}) { |
61 |
|
next if (! $_->{value}); # skip unlimited (0) |
62 |
$labels{$_->{value}} = x($_->{content}); |
$labels{$_->{value}} = x($_->{content}); |
63 |
} |
} |
64 |
|
|
65 |
|
my $path = param('path'); # limit to this path |
66 |
|
my %path_label; |
67 |
|
my @path_name; |
68 |
|
foreach (@{$config->{paths}->{path}}) { |
69 |
|
|
70 |
|
print STDERR "##: $_->{limit}",x($_->{content}),"\n"; |
71 |
|
push @path_name,x($_->{limit}); |
72 |
|
$path_label{$_->{limit}} = x($_->{content}); |
73 |
|
} |
74 |
|
|
75 |
|
my @properties = split(/\s+/,x($config->{properties})) if ($config->{properties}); |
76 |
|
|
77 |
if ($config->{charset}) { |
if ($config->{charset}) { |
78 |
print header(-charset=>x($config->{charset})); |
print header(-charset=>x($config->{charset})); |
79 |
} else { |
} else { |
85 |
print x($config->{text}->{documents}); |
print x($config->{text}->{documents}); |
86 |
print textfield('search'); |
print textfield('search'); |
87 |
print submit(-value=> x($config->{text}->{submit})); |
print submit(-value=> x($config->{text}->{submit})); |
88 |
print checkbox(-name=>'no_affix', -checked=>0, -label=>x($config->{text}->{no_spell})) if ($spelling_alt); |
print br,checkbox(-name=>'no_affix', -checked=>0, -label=>x($config->{text}->{no_spell})) if (@spellings); |
89 |
|
print checkbox(-name=>'no_properties', -checked=>0, -label=>($config->{text}->{no_properties})) if (@properties); |
90 |
|
if (@path_name) { |
91 |
|
print br,x($config->{text}->{limit}); |
92 |
|
print popup_menu(-name=>'path',-values=>\@path_name,-labels=>\%path_label,-default=>$path); |
93 |
|
} |
94 |
print end_form,hr; |
print end_form,hr; |
95 |
|
|
96 |
if (param('search')) { |
if (param('search')) { |
106 |
$search =~ s/^\s+//; |
$search =~ s/^\s+//; |
107 |
$search =~ s/\s+$//; |
$search =~ s/\s+$//; |
108 |
|
|
109 |
|
my %words; |
110 |
|
|
111 |
foreach (split(/\s+/,$search)) { |
foreach (split(/\s+/,$search)) { |
112 |
if (m/^([+-])(\S+)/) { |
if (m/^([+-])(\S+)/) { |
113 |
$s.= ($s) ? "and " : ""; |
$s.= ($s) ? "and " : ""; |
114 |
$s.="not " if ($1 eq "-"); |
$s.="not " if ($1 eq "-"); |
115 |
if ($spelling_alt && !param('no_affix')) { |
if (@spellings && !param('no_affix')) { |
116 |
my $w = $2; $w =~ s/[\*\s]+//g; |
my $w = $2; $w =~ s/[\*\s]+//g; |
117 |
$w =~ s/^(['"]*)([^'"]+)(['"]*)/$2/; |
$w =~ s/^(['"]*)([^'"]+)(['"]*)/$2/; |
118 |
$s.="$1(".join("* or ",$spelling_alt->alternatives($w))."*)$3 "; |
my $or=""; |
119 |
|
foreach my $spelling_alt (@spellings) { |
120 |
|
$s.="$or$1(".join("* or ",$spelling_alt->alternatives($w))."*)$3 "; |
121 |
|
$or = "or "; |
122 |
|
} |
123 |
} else { |
} else { |
124 |
$s.="$2* "; |
$s.="$2* "; |
125 |
} |
} |
126 |
} else { |
} else { |
127 |
if ($spelling_alt && !param('no_affix')) { |
if (@spellings && !param('no_affix')) { |
128 |
my $w = $_; $w =~ s/[\*\s]+//g; |
my $w = $_; $w =~ s/[\*\s]+//g; |
129 |
#$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) "; |
#$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) "; |
130 |
$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) "; |
my $or=""; |
131 |
|
foreach my $spelling_alt (@spellings) { |
132 |
|
$s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) "; |
133 |
|
$or = "or "; |
134 |
|
} |
135 |
} else { |
} else { |
136 |
$s.="$_* "; |
$s.="$_* "; |
137 |
} |
} |
143 |
$s=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/; |
$s=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/; |
144 |
$s=~s/\*\*+/*/g; |
$s=~s/\*\*+/*/g; |
145 |
|
|
146 |
my $sh = SWISH->connect('Fork', |
# limit to some path |
147 |
prog => x($config->{prog}), |
$s = "swishdocpath=(\"*$path*\") and $s" if ($path); |
148 |
indexes => x($config->{index}), |
|
149 |
# properties => [qw/god br nr/], |
my %params; # optional parametars for swish |
150 |
results => sub { |
|
151 |
my ($sh,$hit) = @_; |
# default format for output |
152 |
|
my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n"; |
153 |
|
|
154 |
|
# output start of table |
155 |
|
print qq{ |
156 |
|
<table border="0"> |
157 |
|
}; |
158 |
|
# html before and after each hit |
159 |
|
my $tr_pre = qq{ |
160 |
|
<tr><td> |
161 |
|
}; |
162 |
|
my $tr_post = qq{ |
163 |
|
</td></tr> |
164 |
|
}; |
165 |
|
|
166 |
|
if (@properties) { |
167 |
|
$hit_fmt = x($config->{hit}) if (! param('no_properties')); |
168 |
|
$params{properties} = \@properties; |
169 |
|
} else { |
170 |
|
$hit_fmt = x($config->{hit}) if (x($config->{hit})); |
171 |
|
} |
172 |
|
|
173 |
|
# my $sh = SWISH->connect('Fork', |
174 |
|
# prog => x($config->{prog}), |
175 |
|
# indexes => x($config->{index}), |
176 |
|
# results => sub { |
177 |
|
# my ($sh,$hit) = @_; |
178 |
|
# |
179 |
|
# if ($config->{url}) { |
180 |
|
# printf ($hit_fmt ,"http://".virtual_host().x($config->{url}).$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties)); |
181 |
|
# } else { |
182 |
|
# printf ($hit_fmt ,$hit->swishdocpath,e($hit->swishtitle) || 'untitled',$hit->swishrank, map($hit->$_, @properties) ); |
183 |
|
# |
184 |
|
# } |
185 |
|
# |
186 |
|
## print $_[1]->as_string,"<br>\n"; |
187 |
|
## my @fields = $hit->field_names; |
188 |
|
## print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields; |
189 |
|
# }, |
190 |
|
# maxhits => param('max_hits') || $max_hits, |
191 |
|
# \%params, |
192 |
|
# ); |
193 |
|
# |
194 |
|
# die $SWISH::errstr unless $sh; |
195 |
|
# |
196 |
|
# $hits = $sh->query($s); |
197 |
|
# |
198 |
|
# if ($hits && $hits > 0) { |
199 |
|
# print p,hr; |
200 |
|
# printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); |
201 |
|
# } else { |
202 |
|
# print p; |
203 |
|
# printf (x($config->{text}->{no_hits}),$s,$sh->errstr); |
204 |
|
# } |
205 |
|
# if ($hits && $hits > 0) { |
206 |
|
# print p,hr; |
207 |
|
# printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); |
208 |
|
# } else { |
209 |
|
# print p; |
210 |
|
# printf (x($config->{text}->{no_hits}),$s,$sh->errstr); |
211 |
|
# } |
212 |
|
|
213 |
|
my $swish = SWISH::API->new($config->{index}); |
214 |
|
|
215 |
|
$swish->AbortLastError if $swish->Error; |
216 |
|
|
217 |
|
my $results = $swish->Query($s); |
218 |
|
|
219 |
|
my $hits = $results->Hits; |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
# build pager |
224 |
|
my $current_page = param('page') || 1; |
225 |
|
|
226 |
|
my $pager = Data::Pageset->new({ |
227 |
|
'total_entries' => $hits, |
228 |
|
'entries_per_page' => $max_hits, |
229 |
|
'current_page' => $current_page, |
230 |
|
'pages_per_set' => $pages_per_set, |
231 |
|
}); |
232 |
|
|
233 |
|
$results->SeekResult( $pager->first - 1 ); |
234 |
|
|
235 |
|
# get number of entries on this page |
236 |
|
my $i = $pager->entries_on_this_page; |
237 |
|
|
238 |
|
# print number of hits or error message |
239 |
|
if ( !$hits ) { |
240 |
|
printf (x($config->{text}->{no_hits}),$s,$swish->ErrorString); |
241 |
|
} else { |
242 |
|
printf (x($config->{text}->{hits}),$i,$results->Hits,$s); |
243 |
|
} |
244 |
|
|
|
if ($config->{url}) { |
|
|
printf ("<a href=\"%s\">%s</a> [%s]<br>\n","http://".virtual_host().x($config->{url}).$hit->swishdocpath,$hit->swishtitle || 'untitled',$hit->swishrank); |
|
|
} else { |
|
|
printf ("<a href=\"%s\">%s</a> [%s]<br>\n",$hit->swishdocpath,$hit->swishtitle || 'untitled',$hit->swishrank); |
|
245 |
|
|
246 |
|
for(my $i=$pager->first; $i<=$pager->last; $i++) { |
247 |
|
|
248 |
|
my $result = $results->NextResult; |
249 |
|
last if (! $result); |
250 |
|
|
251 |
|
my @arr; |
252 |
|
foreach my $prop (@properties) { |
253 |
|
if ($prop =~ m/swishdescription/) { |
254 |
|
my $tmp = $result->Property($prop); |
255 |
|
$tmp =~ s/<[^>]+>//g; |
256 |
|
push @arr, $tmp; |
257 |
|
} else { |
258 |
|
push @arr, $result->Property($prop); |
259 |
} |
} |
260 |
|
} |
261 |
|
|
262 |
# print $_[1]->as_string,"<br>\n"; |
my $title = e($result->Property("swishtitle")) || 'untitled'; |
263 |
# my @fields = $hit->field_names; |
my $rank = $result->Property("swishrank"); |
264 |
# print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields; |
my $host = $result->Property("swishdocpath"); |
265 |
}, |
$host = "http://".virtual_host().x($config->{url}).$result->Property("swishdocpath") if ($config->{url}); |
266 |
maxhits => param('max_hits') || $max_hits, |
print $tr_pre,$i,". "; |
267 |
); |
# print collection name which is not link |
268 |
|
if ($title =~ s/^(.+? :: )//) { |
269 |
|
print $1; |
270 |
|
} |
271 |
|
printf($hit_fmt, $host, $title || 'untitled', $rank, @arr); |
272 |
|
print $tr_post; |
273 |
|
|
274 |
die $SWISH::errstr unless $sh; |
} |
275 |
|
|
276 |
|
# pager navigation |
277 |
|
my $nav_html; |
278 |
|
|
279 |
$hits = $sh->query($s); |
my $nav_fmt=qq{ <a href="%s">%s</a> }; |
280 |
|
|
281 |
if ($hits > 0) { |
if ($pager->previous_set) { |
282 |
print p,hr; |
param('page', $pager->previous_set); |
283 |
printf (x($config->{text}->{hits}),$hits,param('max_hits') || $max_hits,$s); |
$nav_html .= sprintf($nav_fmt,url(-relative=>1, -query=>1),'<<'); |
|
} else { |
|
|
print p; |
|
|
printf (x($config->{text}->{no_hits}),$s,$sh->errstr); |
|
284 |
} |
} |
285 |
|
|
286 |
|
|
287 |
|
foreach my $p (@{$pager->pages_in_set()}) { |
288 |
|
next if ($p < 0); |
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 |
} |
} |