8 |
use SWISH; |
use SWISH; |
9 |
use Text::Iconv; |
use Text::Iconv; |
10 |
use DBI; |
use DBI; |
11 |
|
use Config::IniFiles; |
12 |
|
use Text::Unaccent; |
13 |
|
|
14 |
use lib '..'; |
use lib '..'; |
15 |
use index_DBI; |
use index_DBI_cache; |
16 |
use back2html; |
use back2html; |
17 |
|
|
18 |
# configuration options |
|
19 |
# FIX: they really should go in configuration file! |
# read global.conf configuration |
20 |
my $TEMPLATE_PATH = '/data/webpac/template_html'; |
my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'"; |
21 |
my $CHARSET = 'ISO-8859-2'; |
|
22 |
my $SWISH = '/usr/bin/swish-e'; |
# configuration options from global.conf |
23 |
my $INDEX = '/data/webpac/index/isis.index'; |
my $TEMPLATE_PATH = $cfg_global->val('webpac', 'template_html') || die "need template_html in global.conf, section webpac"; |
24 |
my $MAX_HITS = 500; |
my $CHARSET = $cfg_global->val('webpac', 'charset') || 'ISO-8859-1'; |
25 |
my $ON_PAGE = 10; |
my $SWISH = $cfg_global->val('webpac', 'swish') || '/usr/bin/swish-e'; |
26 |
|
my $INDEX = $cfg_global->val('webpac', 'index') || die "need index in global.conf, section webpac"; |
27 |
|
my $MAX_HITS = $cfg_global->val('webpac', 'max_hits') || 0; |
28 |
|
my $ON_PAGE =$cfg_global->val('webpac', 'on_page') || 10; |
29 |
|
my $MIN_WILDCARD =$cfg_global->val('webpac', 'min_wildcard') || 1; |
30 |
|
my $TEMPLATE =$cfg_global->val('webpac', 'template'); |
31 |
|
my $UNAC_FILTER =$cfg_global->val('global', 'unac_filter'); |
32 |
|
my $BASE_PATH =$cfg_global->val('webpac', 'base_path'); |
33 |
|
|
34 |
|
if ($UNAC_FILTER) { |
35 |
|
require $UNAC_FILTER; |
36 |
|
} |
37 |
|
|
38 |
Text::Iconv->raise_error(0); # Conversion errors raise exceptions |
Text::Iconv->raise_error(0); # Conversion errors raise exceptions |
39 |
|
|
40 |
my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET); |
my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET); |
41 |
|
|
42 |
|
# use path from cgi script to support templates in subdirs |
43 |
|
sub url_ex { |
44 |
|
my $q = shift || die "suff2file needs CGI object!"; |
45 |
|
my $tpl = shift || die "url_ex needs template name!"; |
46 |
|
return suff2file($BASE_PATH, $q->url(-path => 1),$TEMPLATE_PATH,$tpl); |
47 |
|
} |
48 |
|
|
49 |
|
sub suff2file($$$$) { |
50 |
|
my ($base_path, $p, $path, $tpl) = @_; |
51 |
|
|
52 |
|
return $tpl if (! $base_path); |
53 |
|
|
54 |
|
# strip everything to and including base path, leaving only |
55 |
|
# additional (virtual) path |
56 |
|
if ($p =~ s,^.*?$base_path,,) { |
57 |
|
$p =~ s,/*,,g; |
58 |
|
my ($name,$ext) = split(/\./,$tpl); |
59 |
|
$p = $name . $p . "." . $ext; |
60 |
|
} else { |
61 |
|
# if unable reset it! |
62 |
|
$p = $tpl; |
63 |
|
} |
64 |
|
|
65 |
|
if ( -e "$path/$p") { |
66 |
|
return $p; |
67 |
|
} else { |
68 |
|
return $tpl; |
69 |
|
} |
70 |
|
|
71 |
|
} |
72 |
|
|
73 |
sub setup { |
sub setup { |
74 |
my $self = shift; |
my $self = shift; |
75 |
$self->tmpl_path($TEMPLATE_PATH); |
$self->tmpl_path($TEMPLATE_PATH); |
85 |
$self->header_props(-charset=>$CHARSET); |
$self->header_props(-charset=>$CHARSET); |
86 |
} |
} |
87 |
|
|
88 |
|
sub in_template { |
89 |
|
my $q = shift || die "need CGI object!"; |
90 |
|
my $html = shift || die "This page is left unintentionally blank"; |
91 |
|
return $html if (! defined($TEMPLATE)); |
92 |
|
|
93 |
|
my ($dir,$tpl); |
94 |
|
if ($TEMPLATE =~ m,^(.*?/*)([^/]+)$,) { |
95 |
|
($dir,$tpl) = ($1,$2); |
96 |
|
} else { |
97 |
|
die "can't parse TEMPLATE path"; |
98 |
|
} |
99 |
|
|
100 |
|
my $master_tpl = suff2file($BASE_PATH, $q->url(-path => 1),$dir,$tpl); |
101 |
|
if (open(T, $master_tpl)) { |
102 |
|
my $template_html = join("\n",<T>); |
103 |
|
close(T); |
104 |
|
$template_html =~ s/##webpac##/$html/gsi; |
105 |
|
return $template_html; |
106 |
|
} else { |
107 |
|
return "Can't read template '$master_tpl'"; |
108 |
|
} |
109 |
|
} |
110 |
|
|
111 |
sub show_search_form { |
sub show_search_form { |
112 |
my $self = shift; |
my $self = shift; |
113 |
|
|
114 |
# Get the CGI.pm query object |
# Get the CGI.pm query object |
115 |
my $q = $self->query(); |
my $q = $self->query(); |
116 |
|
|
117 |
my $tmpl = $self->load_tmpl('search.html'); |
my $tmpl = $self->load_tmpl(url_ex($q,'search.html')); |
118 |
my $html = $tmpl->output; |
my $html = $tmpl->output; |
119 |
|
|
120 |
my $fif = new HTML::FillInForm; |
my $fif = new HTML::FillInForm; |
121 |
|
|
122 |
return $fif->fill(scalarref => \$html, fobject => $q, |
return in_template($q,$fif->fill(scalarref => \$html, fobject => $q, |
123 |
target => 'search'); |
target => 'search')); |
124 |
} |
} |
125 |
|
|
126 |
sub show_results_list { |
sub show_results_list { |
134 |
|
|
135 |
my @s_arr; # all queries are located here |
my @s_arr; # all queries are located here |
136 |
|
|
137 |
for(my $i = 1; $i <=10; $i++) { |
my @path_arr = $q->param('path'); |
138 |
|
my $full = $q->param('full'); |
139 |
|
|
140 |
|
my @persist_vars = ( 'rm' ); |
141 |
|
my @url_params = ( 'rm=results', 'show_full=1', 'last_PAGER_offset='.$q->param('PAGER_offset') || 0 ); |
142 |
|
|
143 |
|
# support parametars "f" and "v" for start |
144 |
|
for(my $i = ""; $i <=30; $i++) { |
145 |
|
|
146 |
return show_index($self, $i) if ($q->param("f".$i."_index")); |
return show_index($self, $i) if ($q->param("f".$i."_index")); |
147 |
next if (! $q->param("f$i")); |
|
148 |
next if (! $q->param("v$i")); |
next if (! $q->param("v$i")); |
149 |
|
next if (! $q->param("f$i")); |
150 |
|
|
151 |
# re-write query from +/- to and/and not |
push @persist_vars, "f$i"; |
152 |
my $s; |
push @persist_vars, "v$i"; |
153 |
my $search = $q->param("v$i"); |
push @persist_vars, "e$i" if ($q->param("e$i")); |
154 |
while ($search =~ s/\s*("[^"]+")\s*/ /) { |
|
155 |
$s .= "$1 "; |
push @url_params,"f$i=".$q->url_param("f$i"); |
156 |
|
foreach my $v ($q->url_param("v$i")) { |
157 |
|
push @url_params,"v$i=$v"; |
158 |
} |
} |
159 |
$search =~ s/^\s+//; |
push @url_params,"e$i=".$q->url_param("e$i"); |
160 |
$search =~ s/\s+$//; |
|
161 |
|
my $wc="*"; # swish wildcard |
162 |
|
$wc="" if ($i eq ""); # don't apply wildcard on field 0 |
163 |
|
|
164 |
foreach (split(/\s+/,$search)) { |
# re-write query from +/- to and/and not |
165 |
if (m/^([+-])(\S+)/) { |
my @param_vals = $q->param("v$i"); |
166 |
$s.= ($s) ? "and " : ""; |
my @swish_q; |
167 |
$s.="not " if ($1 eq "-"); |
my ($pre,$post,$exact) = ('','',''); |
168 |
$s.="$2* "; |
while (my $search = shift @param_vals) { |
169 |
} else { |
my $s; |
170 |
$s.="$_* "; |
# remove accents |
171 |
|
$search = unac_string($CHARSET,$search); |
172 |
|
while ($search =~ s/\s*("[^"]+")\s*/ /) { |
173 |
|
$s .= "$1 "; |
174 |
} |
} |
175 |
} |
$search =~ s/^\s+//; |
176 |
$s =~ s/\*+/*/g; |
$search =~ s/\s+$//; |
177 |
|
|
178 |
push @s_arr,$q->param("f$i")."_swish=($s)"; |
# filed e[nr] is exact match bitmask |
179 |
|
# 1 = beginning, 2=end, 3=both |
180 |
|
$pre = '"xxbxx ' if ($q->param("e$i") & 1); |
181 |
|
$post = ' xxexx"' if ($q->param("e$i") & 2); |
182 |
|
# add qotes on other side |
183 |
|
if ($q->param("e$i")) { |
184 |
|
$pre = '"' if (! $pre); |
185 |
|
$post = '"' if (! $post); |
186 |
|
# what about wildcards? |
187 |
|
$wc = ''; |
188 |
|
$wc = '*' if ($q->param("e$i") & 4); |
189 |
|
$exact = '_exact'; |
190 |
|
} |
191 |
|
|
192 |
|
foreach (split(/\s+/,$search)) { |
193 |
|
if (m/^([+-])(\S+)/) { |
194 |
|
$s.= ($s) ? "and " : ""; |
195 |
|
$s.="not " if ($1 eq "-"); |
196 |
|
$s.=$2.$wc." "; |
197 |
|
} elsif (m/^\s*(and|or|not)\s*$/i) { |
198 |
|
$s.=$_." "; |
199 |
|
# don't add * to words with less than x chars |
200 |
|
} elsif (length($_) <= $MIN_WILDCARD) { |
201 |
|
$s.=$_." "; |
202 |
|
} else { |
203 |
|
$s.=$_.$wc." "; |
204 |
|
} |
205 |
|
} |
206 |
|
$s =~ s/\*+/*/g; |
207 |
|
$s = $pre.$s.$post if ($q->param("e$i")); |
208 |
|
push @swish_q,$s; |
209 |
|
} |
210 |
|
# FIXME default operator for multi-value fields is or. There is |
211 |
|
# no way to change it, except here for now. Is there need? |
212 |
|
push @s_arr, $q->param("f$i")."_swish".$exact."=(".join(" or ",@swish_q).")"; |
213 |
} |
} |
214 |
|
|
215 |
my $tmpl = $self->load_tmpl('results.html'); |
my $tmpl = $self->load_tmpl(url_ex($q,'results.html'), global_vars => 1); |
216 |
|
|
217 |
|
sub esc_html { |
218 |
|
my $html = shift; |
219 |
|
$html =~ s/</</g; |
220 |
|
$html =~ s/>/>/g; |
221 |
|
return $html; |
222 |
|
} |
223 |
|
|
224 |
# call swish |
# call swish |
225 |
my $sh = SWISH->connect('Fork', |
my $sh = SWISH->connect('Fork', |
232 |
push @swish_results, { |
push @swish_results, { |
233 |
nr => ($#swish_results + 2), |
nr => ($#swish_results + 2), |
234 |
path => $hit->swishdocpath, |
path => $hit->swishdocpath, |
235 |
headline => $from_utf8->convert($hit->headline), |
headline => esc_html($from_utf8->convert($hit->headline)), |
236 |
html => back2html($from_utf8->convert($hit->html)), |
html => back2html($from_utf8->convert($hit->html)), |
237 |
rank => $hit->swishrank }; |
rank => $hit->swishrank }; |
238 |
|
|
239 |
}, |
}, |
240 |
#startnum => 0, |
#startnum => 0, |
241 |
maxhits => $MAX_HITS, |
maxhits => $MAX_HITS |
242 |
); |
); |
243 |
|
|
244 |
die $SWISH::errstr unless $sh; |
die $SWISH::errstr unless $sh; |
245 |
|
# construct swish query |
246 |
|
my $sw_q = join(" and ",@s_arr); |
247 |
|
if (@path_arr && $q->param('show_full')) { |
248 |
|
$sw_q .= "and (swishdocpath=\""; |
249 |
|
$sw_q .= join("\" or swishdocpath=\"",@path_arr); |
250 |
|
$sw_q .= "\")"; |
251 |
|
$tmpl->param('full',1); # show full records |
252 |
|
} elsif ($q->param('show_full')) { |
253 |
|
# just show full path, no path defined |
254 |
|
$tmpl->param('full',1); |
255 |
|
} else { |
256 |
|
$tmpl->param('full',0); |
257 |
|
} |
258 |
|
|
259 |
my $hits = $sh->query(join(" and ",@s_arr)) || 0; # FIX: and/or |
my $hits = $sh->query($sw_q); |
260 |
|
|
261 |
$tmpl->param('hits',$hits); |
$tmpl->param('hits',$hits); |
262 |
$tmpl->param('search',join(" and ",@s_arr)); |
$tmpl->param('search',$sw_q); |
263 |
|
|
264 |
|
$tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0); |
265 |
|
$tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0); |
266 |
|
|
267 |
|
$tmpl->param('url_params',"?".join("&",@url_params)); |
268 |
|
|
269 |
# create a Pager object |
# create a Pager object |
270 |
my $pager = HTML::Pager->new( |
my $pager = HTML::Pager->new( |
275 |
|
|
276 |
my @result; |
my @result; |
277 |
for (my $i=0; $i<$rows; $i++) { |
for (my $i=0; $i<$rows; $i++) { |
278 |
push @result, $swish_results[$offset+$i] if $swish_results[$offset+$i]; |
my $r = $swish_results[$offset+$i]; |
279 |
|
if ($r && $tmpl->param('full')) { |
280 |
|
push @result, $r; |
281 |
|
} elsif ($r) { |
282 |
|
# if not full output, skip html |
283 |
|
delete $r->{html}; |
284 |
|
push @result, $r; |
285 |
|
} |
286 |
} |
} |
287 |
return \@result; |
return \@result; |
288 |
}, |
}, |
289 |
rows => $hits, |
rows => $hits, |
290 |
page_size => $ON_PAGE, |
page_size => $ON_PAGE, |
291 |
# some optional parameters |
# some optional parameters |
292 |
persist_vars => [ |
persist_vars => [ @persist_vars ], |
|
'rm', |
|
|
'f1', 'v1', |
|
|
'f2', 'v2', |
|
|
'f3', 'v3', |
|
|
'f4', 'v4', |
|
|
'f5', 'v5', |
|
|
'f6', 'v6', |
|
|
'f7', 'v7', |
|
|
'f8', 'v8', |
|
|
'f9', 'v9', |
|
|
], |
|
293 |
#cell_space_color => '#000000', |
#cell_space_color => '#000000', |
294 |
#cell_background_color => '#ffffff', |
#cell_background_color => '#ffffff', |
295 |
#nav_background_color => '#dddddd', |
#nav_background_color => '#dddddd', |
300 |
|
|
301 |
my $html = $pager->output; |
my $html = $pager->output; |
302 |
|
|
303 |
return $html; |
return in_template($q,$html); |
304 |
} |
} |
305 |
|
|
306 |
sub show_index { |
sub show_index { |
314 |
|
|
315 |
my $html; |
my $html; |
316 |
|
|
317 |
my $index = new index_DBI(); |
my $index = new index_DBI( |
318 |
|
$cfg_global->val('global', 'dbi_dbd'), |
319 |
|
$cfg_global->val('global', 'dbi_dsn'), |
320 |
|
$cfg_global->val('global', 'dbi_user'), |
321 |
|
$cfg_global->val('global', 'dbi_passwd') || '' |
322 |
|
); |
323 |
|
|
324 |
my $total = $index->check($field); |
my $total = $index->count($field,$limit); |
325 |
if (! $total) { |
if (! $total) { |
326 |
my $tmpl = $self->load_tmpl('no_index.html'); |
my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html')); |
327 |
$tmpl->param('field',$field); |
$tmpl->param('field',$field); |
328 |
$html = $tmpl->output; |
$html = $tmpl->output; |
329 |
return $html; |
return $html; |
330 |
} |
} |
331 |
|
|
332 |
my $tmpl = $self->load_tmpl('index_res.html'); |
my $tmpl = $self->load_tmpl(url_ex($q,'index_res.html'), global_vars => 1); |
333 |
$tmpl->param('field',$field); |
$tmpl->param('field',$field); |
334 |
$tmpl->param('limit',$limit); |
$tmpl->param('limit',$limit); |
335 |
$tmpl->param('total',$total); |
$tmpl->param('total',$total); |
336 |
|
|
337 |
# FIX: I should set offset and leave out limit from fetch!! |
# FIXME I should set offset and leave out limit from fetch!! |
338 |
# if (! $q->param("PAGER_offset") { |
# if (! $q->param("PAGER_offset") { |
339 |
# $q->param("Pager_offet) |
# $q->param("Pager_offet) |
340 |
# } |
# } |
344 |
get_data_callback => sub { |
get_data_callback => sub { |
345 |
my ($offset, $rows) = @_; |
my ($offset, $rows) = @_; |
346 |
|
|
347 |
my @result = $index->fetch($field,'item',$limit, $offset, $rows); |
my @result = $index->fetch($field,$limit, $offset, $rows); |
348 |
return \@result; |
return \@result; |
349 |
}, |
}, |
350 |
rows => $total, |
rows => $total, |
358 |
template => $tmpl, |
template => $tmpl, |
359 |
); |
); |
360 |
|
|
361 |
return $pager->output; |
return in_template($q,$pager->output); |
362 |
} |
} |
363 |
|
|
364 |
1; |
1; |