3 use base 'CGI::Application';
19 # read global.conf configuration
20 my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'";
22 # configuration options from global.conf
23 my $TEMPLATE_PATH = $cfg_global->val('webpac', 'template_html') || die "need template_html in global.conf, section webpac";
24 my $CHARSET = $cfg_global->val('webpac', 'charset') || 'ISO-8859-1';
25 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');
38 Text::Iconv->raise_error(0); # Conversion errors raise exceptions
40 my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
42 # use path from cgi script to support templates in subdirs
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);
50 my ($base_path, $p, $path, $tpl) = @_;
52 return $tpl if (! $base_path);
54 # strip everything to and including base path, leaving only
55 # additional (virtual) path
56 if ($p =~ s,^.*?$base_path,,) {
58 my ($name,$ext) = split(/\./,$tpl);
59 $p = $name . $p . "." . $ext;
75 $self->tmpl_path($TEMPLATE_PATH);
77 'search' => 'show_search_form',
78 'results' => 'show_results_list',
79 # 'user' => 'show_user_detail',
80 'index' => 'show_index',
82 $self->start_mode('search');
83 $self->mode_param('rm');
85 $self->header_props(-charset=>$CHARSET);
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));
94 if ($TEMPLATE =~ m,^(.*?/*)([^/]+)$,) {
95 ($dir,$tpl) = ($1,$2);
97 die "can't parse TEMPLATE path";
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>);
104 $template_html =~ s/##webpac##/$html/gsi;
105 return $template_html;
107 return "Can't read template '$master_tpl'";
111 sub show_search_form {
114 # Get the CGI.pm query object
115 my $q = $self->query();
117 my $tmpl = $self->load_tmpl(url_ex($q,'search.html'));
118 my $html = $tmpl->output;
120 my $fif = new HTML::FillInForm;
122 return in_template($q,$fif->fill(scalarref => \$html, fobject => $q,
123 target => 'search'));
126 sub show_results_list {
129 my $q = $self->query();
131 my @swish_results; # results from swish
133 # load template for this page
135 my @s_arr; # all queries are located here
137 my @path_arr = $q->param('path');
138 my $full = $q->param('full');
140 my @persist_vars = ( 'rm' );
141 my @url_params = ( 'rm=results', 'show_full=1', 'last_PAGER_offset='.$q->param('PAGER_offset') || 0 );
143 # support parametars "f" and "v" for start
144 for(my $i = ""; $i <=30; $i++) {
146 return show_index($self, $i) if ($q->param("f".$i."_index"));
148 next if (! $q->param("v$i"));
149 next if (! $q->param("f$i"));
151 push @persist_vars, "f$i";
152 push @persist_vars, "v$i";
153 push @persist_vars, "e$i" if ($q->param("e$i"));
155 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";
159 push @url_params,"e$i=".$q->url_param("e$i");
161 my $wc="*"; # swish wildcard
162 $wc="" if ($i eq ""); # don't apply wildcard on field 0
164 # re-write query from +/- to and/and not
165 my @param_vals = $q->param("v$i");
167 my ($pre,$post,$exact) = ('','','');
168 while (my $search = shift @param_vals) {
171 $search = unac_string($CHARSET,$search);
172 while ($search =~ s/\s*("[^"]+")\s*/ /) {
178 # 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?
188 $wc = '*' if ($q->param("e$i") & 4);
192 foreach (split(/\s+/,$search)) {
193 if (m/^([+-])(\S+)/) {
194 $s.= ($s) ? "and " : "";
195 $s.="not " if ($1 eq "-");
197 } elsif (m/^\s*(and|or|not)\s*$/i) {
199 # don't add * to words with less than x chars
200 } elsif (length($_) <= $MIN_WILDCARD) {
207 $s = $pre.$s.$post if ($q->param("e$i"));
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).")";
215 my $tmpl = $self->load_tmpl(url_ex($q,'results.html'), global_vars => 1);
224 my $sort = 'swishrank';
225 if ($q->param("sort")) {
227 push @persist_vars, "sort";
231 my $sh = SWISH->connect('Fork',
234 properties => [qw/swishdocpath swishrank swishtitle headline html/],
238 push @swish_results, {
239 nr => ($#swish_results + 2),
240 path => $hit->swishdocpath,
241 headline => esc_html($from_utf8->convert($hit->headline)),
242 html => back2html($from_utf8->convert($hit->html)),
243 rank => $hit->swishrank };
247 maxhits => $MAX_HITS,
251 die $SWISH::errstr unless $sh;
252 # construct swish query
253 my $sw_q = join(" and ",@s_arr);
254 if (@path_arr && $q->param('show_full')) {
255 $sw_q .= "and (swishdocpath=\"";
256 $sw_q .= join("\" or swishdocpath=\"",@path_arr);
258 $tmpl->param('full',1); # show full records
259 } elsif ($q->param('show_full')) {
260 # just show full path, no path defined
261 $tmpl->param('full',1);
263 $tmpl->param('full',0);
266 my $hits = $sh->query($sw_q);
268 $tmpl->param('hits',$hits);
269 $tmpl->param('search',$sw_q);
271 $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);
272 $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);
274 $tmpl->param('url_params',"?".join("&",@url_params));
276 # create a Pager object
277 my $pager = HTML::Pager->new(
278 # required parameters
280 get_data_callback => sub {
281 my ($offset, $rows) = @_;
284 for (my $i=0; $i<$rows; $i++) {
285 my $r = $swish_results[$offset+$i];
286 if ($r && $tmpl->param('full')) {
289 # if not full output, skip html
297 page_size => $ON_PAGE,
298 # some optional parameters
299 persist_vars => [ @persist_vars ],
300 #cell_space_color => '#000000',
301 #cell_background_color => '#ffffff',
302 #nav_background_color => '#dddddd',
303 #javascript_presubmit => 'last_minute_javascript()',
308 my $html = $pager->output;
310 return in_template($q,$html);
315 my $i = shift; # field number
317 my $q = $self->query();
319 my $field = $q->param("f$i");
320 my $limit = $q->param("v$i");
324 my $index = new index_DBI(
325 $cfg_global->val('global', 'dbi_dbd'),
326 $cfg_global->val('global', 'dbi_dsn'),
327 $cfg_global->val('global', 'dbi_user'),
328 $cfg_global->val('global', 'dbi_passwd') || ''
331 my $total = $index->count($field,$limit);
333 my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html'));
334 $tmpl->param('field',$field);
335 $html = $tmpl->output;
339 my $tmpl = $self->load_tmpl(url_ex($q,'index_res.html'), global_vars => 1);
340 $tmpl->param('field',$field);
341 $tmpl->param('limit',$limit);
342 $tmpl->param('total',$total);
344 # FIXME I should set offset and leave out limit from fetch!!
345 # if (! $q->param("PAGER_offset") {
346 # $q->param("Pager_offet)
349 my $pager = HTML::Pager->new(
351 get_data_callback => sub {
352 my ($offset, $rows) = @_;
354 my @result = $index->fetch($field,$limit, $offset, $rows);
358 page_size => $ON_PAGE,
361 "f$i", "v$i", "f".$i."_index",
368 return in_template($q,$pager->output);