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', 'my_unac_filter');
32 my $BASE_PATH =$cfg_global->val('webpac', 'base_path');
34 my $pages_per_set = $cfg_global->val('webpac', 'pages_per_set') || 10;
36 Text::Iconv->raise_error(0); # Conversion errors raise exceptions
38 my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
43 sub WebPac::my_unac_string {
44 my ($charset, $string) = (@_);
49 # use path from cgi script to support templates in subdirs
51 my $q = shift || die "suff2file needs CGI object!";
52 my $tpl = shift || die "url_ex needs template name!";
53 return suff2file($BASE_PATH, $q->url(-absolute => 1,-path => 1),$TEMPLATE_PATH,$tpl);
57 my ($base_path, $p, $path, $tpl) = @_;
59 return $tpl if (! $base_path);
61 # strip everything to and including base path, leaving only
62 # additional (virtual) path
63 if ($base_path eq "/") {
65 my ($name,$ext) = split(/\./,$tpl);
66 $p = $name . "-" . $p . "." . $ext;
67 } elsif ($p =~ s,^.*?$base_path,,) {
69 my ($name,$ext) = split(/\./,$tpl);
70 $p = $name . $p . "." . $ext;
86 $self->tmpl_path($TEMPLATE_PATH);
88 'search' => 'show_search_form',
89 'results' => 'show_results_list',
90 # 'user' => 'show_user_detail',
91 'index' => 'show_index',
93 $self->start_mode('search');
94 $self->mode_param('rm');
96 $self->header_props(-charset=>$CHARSET);
100 my $q = shift || die "need CGI object!";
101 my $html = shift || die "This page is left unintentionally blank";
102 return $html if (! defined($TEMPLATE));
105 if ($TEMPLATE =~ m,^(.*?/*)([^/]+)$,) {
106 ($dir,$tpl) = ($1,$2);
108 die "can't parse TEMPLATE path";
111 my $master_tpl = suff2file($BASE_PATH, $q->url(-absolute => 1, -path => 1),$dir,$tpl);
112 if (open(T, $master_tpl)) {
113 my $template_html = join("\n",<T>);
115 $template_html =~ s/##webpac##/$html/gsi;
116 return $template_html;
118 return "Can't read template '$master_tpl'";
122 #--------------------------------------------------------------------------
125 # make pager navigation and fill template variables
126 # compatibile with HTML::Pager
129 sub make_pager($$$) {
130 my ($q,$tmpl,$pager) = @_;
135 my ($pager_prev,$pager_next, $pager_jump) = ('','','');
137 my $nav_fmt=qq{ <a href="%s">%s</a> };
139 if ($pager->current_page() > $pager->first_page) {
140 $q->param('PAGER_offset', $pager->current_page - 1);
141 $pager_prev .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'<<');
144 if ($pager->previous_set) {
145 $q->param('PAGER_offset', $pager->previous_set);
146 $pager_prev .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'..');
150 foreach my $p (@{$pager->pages_in_set()}) {
152 if($p == $pager->current_page()) {
153 $pager_jump .= "<b>$p</b> ";
155 $q->param('PAGER_offset', $p);
156 $pager_jump .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),$p);
160 if ($pager->next_set) {
161 $q->param('PAGER_offset', $pager->next_set);
162 $pager_next .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'..');
165 if ($pager->current_page() < $pager->last_page) {
166 $q->param('PAGER_offset', $pager->current_page + 1);
167 $pager_next .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'>>');
170 $tmpl->param('PAGER_PREV', $pager_prev);
171 $tmpl->param('PAGER_JUMP', $pager_jump);
172 $tmpl->param('PAGER_NEXT', $pager_next);
177 # put persisten variables in template
180 sub make_pager_vars {
183 my @persist_vars = @_;
184 my $hidden_vars = '';
185 my $hidden_search = '';
186 foreach my $v (@persist_vars) {
187 foreach my $val ($q->param($v)) {
188 next if (! $val || $val eq '');
189 $hidden_vars .= '<input type="hidden" name="'.$v.'" value="'.$val.'"/>'."\n";
190 $hidden_search .= '<input type="hidden" name="'.$v.'" value="'.$val.'"/>'."\n" if ($v ne "rm");
194 $tmpl->param('PAGER_HIDDEN', $hidden_vars);
195 $tmpl->param('SEARCH_HIDDEN', $hidden_search);
196 $tmpl->param('PAGER_JAVASCRIPT', qq#
197 <SCRIPT LANGUAGE="Javascript">
199 // dummy emulator for HTML::Pager templates
200 function PAGER_set_offset_and_submit() {
208 #--------------------------------------------------------------------------
210 sub show_search_form {
213 # Get the CGI.pm query object
214 my $q = $self->query();
216 my $tmpl = $self->load_tmpl(url_ex($q,'search.html'));
217 my $html = $tmpl->output;
219 my $fif = new HTML::FillInForm;
221 return in_template($q,$fif->fill(scalarref => \$html, fobject => $q,
222 target => 'search'));
225 sub show_results_list {
228 my $q = $self->query();
230 # load template for this page
232 my @s_arr; # all queries are located here
234 my @path_arr = $q->param('path');
235 my $full = $q->param('full');
237 my @persist_vars = ( 'rm', 'persist_search' );
238 my @url_params = ( 'rm=results', 'show_full=1', 'last_PAGER_offset='.($q->param('PAGER_offset') || 0) );
240 my @persist_search_vars;
241 my @url_params_persist;
242 if ($q->param("persist_search")) {
243 @persist_search_vars = split(/\s*,\s*/, $q->param("persist_search"));
244 push @url_params_persist, "persist_search=".$q->url_param("persist_search");
245 push @url_params,"persist_search=".$q->url_param("persist_search");
248 # support parametars "f" and "v" for start
249 for(my $i = 0; $i <=30; $i++) {
251 $i = '' if ($i == 0);
253 return show_index($self, $i) if ($q->param("f".$i."_index"));
255 next if (! $q->param("v$i") || $q->param("v$i") eq '');
256 next if (! $q->param("f$i"));
258 my $persist = grep(/^$i$/,@persist_search_vars);
260 push @persist_vars, "f$i";
261 push @persist_vars, "v$i";
262 push @persist_vars, "e$i" if ($q->param("e$i"));
264 # create url parametars (and persistent ones)
266 push @url_params,"f$i=".$q->url_param("f$i");
267 push @url_params_persist,"f$i=".$q->url_param("f$i") if ($persist);
269 foreach my $v ($q->url_param("v$i")) {
270 push @url_params,"v$i=$v";
271 push @url_params_persist,"v$i=$v" if ($persist);
274 if ($q->param("e$i")) {
275 push @url_params,"e$i=".$q->url_param("e$i");
276 # push @url_params_persist,"e$i=".$q->url_param("e$i");
279 my $wc="*"; # swish wildcard
280 $wc="" if ($i eq ""); # don't apply wildcard on field 0
282 # re-write query from +/- to and/and not
283 my @param_vals = $q->param("v$i");
285 my ($pre,$post,$exact) = ('','','');
286 while (my $search = shift @param_vals) {
289 $search = my_unac_string($CHARSET,$search);
290 while ($search =~ s/\s*("[^"]+")\s*/ /) {
296 # filed e[nr] is exact match bitmask
297 # 1 = beginning, 2=end, 3=both
298 my $exact_flag = $q->param("e$i") || 0;
299 $pre = '"xxbxx ' if ($exact_flag & 1);
300 $post = ' xxexx"' if ($exact_flag & 2);
301 # add qotes on other side
302 if ($q->param("e$i")) {
303 $pre = '"' if (! $pre);
304 $post = '"' if (! $post);
305 # what about wildcards?
307 $wc = '*' if ($q->param("e$i") & 4);
311 foreach (split(/\s+/,$search)) {
312 if (m/^([+-])(\S+)/) {
313 $s.= ($s) ? "and " : "";
314 $s.="not " if ($1 eq "-");
316 } elsif (m/^\s*(and|or|not)\s*$/i) {
318 # don't add * to words with less than x chars
319 } elsif (length($_) <= $MIN_WILDCARD) {
326 $s = $pre.$s.$post if ($q->param("e$i"));
329 # FIXME default operator for multi-value fields is or. There is
330 # no way to change it, except here for now. Is there need?
331 push @s_arr, $q->param("f$i")."_swish".$exact."=(".join(" or ",@swish_q).")";
334 my $tmpl = $self->load_tmpl(url_ex($q,'results.html'), global_vars => 1, die_on_bad_params => 0);
336 $tmpl->param('url_params',"?".join("&",@url_params));
345 my $sort = 'swishrank';
346 if ($q->param("sort")) {
348 push @persist_vars, "sort";
351 my $sortby = $q->param("sortby");
354 push @persist_vars, "sortby";
356 $tmpl->param('url_params_paths',"?".join("&",@url_params).'&'.join("&",map { my $t = $_; $t =~ s/\#/%23/g; "path=$t"; } @path_arr));
358 # construct swish query
359 my $sw_q = join(" and ",@s_arr);
360 if (@path_arr && $q->param('show_full')) {
361 $sw_q .= "and (swishdocpath=\"";
362 $sw_q .= join("\" or swishdocpath=\"",@path_arr);
364 $tmpl->param('full',1); # show full records
365 } elsif ($q->param('show_full')) {
366 # just show full path, no path defined
367 $tmpl->param('full',1);
369 $tmpl->param('full',0);
374 # create new swish instance
375 my $swish = SWISH::API->new($INDEX);
376 $swish_msg .= $swish->ErrorString." ".$swish->LastErrorMsg if $swish->Error;
378 # execute query and get number of results from SWISH-E
379 my $search = $swish->New_Search_Object;
381 $search->SetSort($sort);
383 my $results = $search->Execute($sw_q);
384 $swish_msg .= $swish->ErrorString." ".$swish->LastErrorMsg if $swish->Error;
386 my $hits = $results->Hits;
388 $tmpl->param('hits',$hits);
389 my $search_msg = $sw_q;
390 $search_msg .= '<em>'.$swish_msg.'</em>' if ($swish_msg);
391 $tmpl->param('search', $search_msg);
393 $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);
394 $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);
400 my $current_page = $q->param('PAGER_offset') || 1;
402 my $pager = Data::Pageset->new({
403 'total_entries' => $hits,
404 'entries_per_page' => $ON_PAGE,
405 'current_page' => $current_page,
406 'pages_per_set' => $pages_per_set,
409 $results->SeekResult( $pager->first - 1 );
411 # get number of entries on this page
412 my $i = $pager->entries_on_this_page;
414 # results from swish for template
417 for(my $i=$pager->first; $i<=$pager->last; $i++) {
419 my $result = $results->NextResult;
424 path => $result->Property('swishdocpath'),
425 headline => esc_html($from_utf8->convert($result->Property('headline'))),
426 rank => $result->Property('swishrank')
429 $r->{html} = back2html($from_utf8->convert($result->Property('html')), join("&",@url_params_persist)) if ($q->param('show_full'));
431 push @pager_data_list, $r;
436 # put something in template
437 make_pager($q, $tmpl, $pager);
438 make_pager_vars($q, $tmpl, @persist_vars);
439 $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
441 my $html = $tmpl->output;
443 return in_template($q,$html);
448 my $i = shift; # field number
450 my $q = $self->query();
452 my $field = $q->param("f$i");
453 my $limit = $q->param("v$i");
455 my $filter = $q->param("filter");
459 my $index = new index_DBI(
460 $cfg_global->val('global', 'dbi_dbd'),
461 $cfg_global->val('global', 'dbi_dsn'),
462 $cfg_global->val('global', 'dbi_user'),
463 $cfg_global->val('global', 'dbi_passwd') || ''
466 my $total = $index->count($field,$limit,$filter);
468 if (! defined($total)) {
469 my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html'));
470 $tmpl->param('field',$field);
471 $html = $tmpl->output;
475 my $tmpl = $self->load_tmpl(url_ex($q,'index_res.html'), global_vars => 1, die_on_bad_params => 0);
476 $tmpl->param('field',$field);
477 $tmpl->param('limit',$limit);
478 $tmpl->param('total',$total);
480 # FIXME I should set offset and leave out limit from fetch!!
481 # if (! $q->param("PAGER_offset") {
482 # $q->param("Pager_offet)
489 my $pager = Data::Pageset->new({
490 'total_entries' => $total,
491 'entries_per_page' => $ON_PAGE,
492 'current_page' => $q->param('PAGER_offset') || 1,
493 'pages_per_set' => $pages_per_set
496 my @persist_vars = qw{rm f$i v$i f$i_index offset};
498 make_pager($q, $tmpl, $pager);
499 make_pager_vars($q, $tmpl, @persist_vars);
501 my @pager_data_list = $index->fetch($field,$limit, $pager->first - 1, $pager->entries_on_this_page, $filter);
502 $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
504 return in_template($q,$tmpl->output);