3 use base 'CGI::Application';
13 use POSIX qw(locale_h);
20 # read global.conf configuration
21 my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'";
23 # configuration options from global.conf
24 my $TEMPLATE_PATH = $cfg_global->val('webpac', 'template_html') || die "need template_html in global.conf, section webpac";
25 my $CHARSET = $cfg_global->val('webpac', 'charset') || 'ISO-8859-1';
26 my $SWISH = $cfg_global->val('webpac', 'swish') || '/usr/bin/swish-e';
27 my $INDEX = $cfg_global->val('webpac', 'index') || die "need index in global.conf, section webpac";
28 my $MAX_HITS = $cfg_global->val('webpac', 'max_hits') || 0;
29 my $ON_PAGE =$cfg_global->val('webpac', 'on_page') || 10;
30 my $MIN_WILDCARD =$cfg_global->val('webpac', 'min_wildcard') || 1;
31 my $TEMPLATE =$cfg_global->val('webpac', 'template');
32 my $UNAC_FILTER =$cfg_global->val('global', 'my_unac_filter');
33 my $BASE_PATH =$cfg_global->val('webpac', 'base_path');
35 my $pages_per_set = $cfg_global->val('webpac', 'pages_per_set') || 10;
36 my $locale = $cfg_global->val('locale') || 'hr_HR';
38 Text::Iconv->raise_error(0); # Conversion errors raise exceptions
40 my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
42 setlocale(LC_CTYPE, $locale);
43 setlocale(LC_COLLATE, $locale);
48 sub WebPac::my_unac_string {
49 my ($charset, $string) = (@_);
54 # use path from cgi script to support templates in subdirs
56 my $q = shift || die "suff2file needs CGI object!";
57 my $tpl = shift || die "url_ex needs template name!";
58 return suff2file($BASE_PATH, $q->url(-absolute => 1,-path => 1),$TEMPLATE_PATH,$tpl);
62 my ($base_path, $p, $path, $tpl) = @_;
64 return $tpl if (! $base_path);
66 # strip everything to and including base path, leaving only
67 # additional (virtual) path
68 if ($base_path eq "/") {
70 my ($name,$ext) = split(/\./,$tpl);
71 $p = $name . "-" . $p . "." . $ext;
72 } elsif ($p =~ s,^.*?$base_path,,) {
74 my ($name,$ext) = split(/\./,$tpl);
75 $p = $name . $p . "." . $ext;
91 $self->tmpl_path($TEMPLATE_PATH);
93 'search' => 'show_search_form',
94 'results' => 'show_results_list',
95 # 'user' => 'show_user_detail',
96 'index' => 'show_index',
98 $self->start_mode('search');
99 $self->mode_param('rm');
101 $self->header_props(-charset=>$CHARSET);
105 my $q = shift || die "need CGI object!";
106 my $html = shift || die "This page is left unintentionally blank";
107 return $html if (! defined($TEMPLATE));
110 if ($TEMPLATE =~ m,^(.*?/*)([^/]+)$,) {
111 ($dir,$tpl) = ($1,$2);
113 die "can't parse TEMPLATE path";
116 my $master_tpl = suff2file($BASE_PATH, $q->url(-absolute => 1, -path => 1),$dir,$tpl);
117 if (open(T, $master_tpl)) {
118 my $template_html = join("\n",<T>);
120 $template_html =~ s/##webpac##/$html/gsi;
121 return $template_html;
123 return "Can't read template '$master_tpl'";
127 #--------------------------------------------------------------------------
130 # make pager navigation and fill template variables
131 # compatibile with HTML::Pager
134 sub make_pager($$$) {
135 my ($q,$tmpl,$pager) = @_;
140 my ($pager_prev,$pager_next, $pager_jump) = ('','','');
142 my $nav_fmt=qq{ <a href="%s">%s</a> };
144 if ($pager->current_page() > $pager->first_page) {
145 $q->param('PAGER_offset', $pager->current_page - 1);
146 $pager_prev .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'<<');
149 if ($pager->previous_set) {
150 $q->param('PAGER_offset', $pager->previous_set);
151 $pager_prev .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'..');
155 foreach my $p (@{$pager->pages_in_set()}) {
157 if($p == $pager->current_page()) {
158 $pager_jump .= "<b>$p</b> ";
160 $q->param('PAGER_offset', $p);
161 $pager_jump .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),$p);
165 if ($pager->next_set) {
166 $q->param('PAGER_offset', $pager->next_set);
167 $pager_next .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'..');
170 if ($pager->current_page() < $pager->last_page) {
171 $q->param('PAGER_offset', $pager->current_page + 1);
172 $pager_next .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'>>');
175 $tmpl->param('PAGER_PREV', $pager_prev);
176 $tmpl->param('PAGER_JUMP', $pager_jump);
177 $tmpl->param('PAGER_NEXT', $pager_next);
182 # put persisten variables in template
185 sub make_pager_vars {
188 my @persist_vars = @_;
189 my $hidden_vars = '';
190 my $hidden_search = '';
191 foreach my $v (@persist_vars) {
192 foreach my $val ($q->param($v)) {
193 next if (! $val || $val eq '');
194 $val =~ s/"/"/g;
195 $hidden_vars .= '<input type="hidden" name="'.$v.'" value="'.$val.'"/>'."\n";
196 $hidden_search .= '<input type="hidden" name="'.$v.'" value="'.$val.'"/>'."\n" if ($v ne "rm");
200 $tmpl->param('PAGER_HIDDEN', $hidden_vars);
201 $tmpl->param('SEARCH_HIDDEN', $hidden_search);
202 $tmpl->param('PAGER_JAVASCRIPT', qq#
203 <SCRIPT LANGUAGE="Javascript">
205 // dummy emulator for HTML::Pager templates
206 function PAGER_set_offset_and_submit() {
214 #--------------------------------------------------------------------------
216 sub show_search_form {
219 # Get the CGI.pm query object
220 my $q = $self->query();
222 my $tmpl = $self->load_tmpl(url_ex($q,'search.html'));
223 my $html = $tmpl->output;
225 my $fif = new HTML::FillInForm;
227 return in_template($q,$fif->fill(scalarref => \$html, fobject => $q,
228 target => 'search'));
231 sub show_results_list {
234 my $q = $self->query();
237 if ($q->param('reset')) {
239 return $self->show_search_form();
242 # load template for this page
244 my @s_arr; # all queries are located here
246 my @path_arr = $q->param('path');
247 my $full = $q->param('full');
249 my @persist_vars = ( 'rm', 'persist_search' );
250 my @url_params = ( 'rm=results', 'show_full=1', 'last_PAGER_offset='.($q->param('PAGER_offset') || 0) );
252 my @persist_search_vars;
253 my @url_params_persist;
254 if ($q->param("persist_search")) {
255 @persist_search_vars = split(/\s*,\s*/, $q->param("persist_search"));
256 push @url_params_persist, "persist_search=".$q->url_param("persist_search");
257 push @url_params,"persist_search=".$q->url_param("persist_search");
260 # support parametars "f" and "v" for start
261 for(my $i = 0; $i <=30; $i++) {
263 $i = '' if ($i == 0);
265 return show_index($self, $i) if ($q->param("f".$i."_index"));
267 next if (! $q->param("v$i") || $q->param("v$i") eq '');
268 next if (! $q->param("f$i"));
270 my $persist = grep(/^$i$/,@persist_search_vars);
272 push @persist_vars, "f$i";
273 push @persist_vars, "v$i";
274 push @persist_vars, "e$i" if ($q->param("e$i"));
276 # create url parametars (and persistent ones)
278 push @url_params,"f$i=".$q->url_param("f$i");
279 push @url_params_persist,"f$i=".$q->url_param("f$i") if ($persist);
281 foreach my $v ($q->url_param("v$i")) {
282 # escape quotes so that phrase search work
284 push @url_params,"v$i=$v";
285 push @url_params_persist,"v$i=$v" if ($persist);
288 if ($q->param("e$i")) {
289 push @url_params,"e$i=".$q->url_param("e$i");
290 # push @url_params_persist,"e$i=".$q->url_param("e$i");
293 my $wc="*"; # swish wildcard
294 $wc="" if ($i eq ""); # don't apply wildcard on field 0
296 # re-write query from +/- to and/and not
297 my @param_vals = $q->param("v$i");
299 my ($pre,$post,$exact) = ('','','');
300 while (my $search = shift @param_vals) {
303 $search = my_unac_string($CHARSET,$search);
304 while ($search =~ s/\s*("[^"]+")\s*/ /) {
310 # filed e[nr] is exact match bitmask
311 # 1 = beginning, 2=end, 3=both
312 my $exact_flag = $q->param("e$i") || 0;
313 $pre = '"xxbxx ' if ($exact_flag & 1);
314 $post = ' xxexx"' if ($exact_flag & 2);
315 # add qotes on other side
316 if ($q->param("e$i")) {
317 $pre = '"' if (! $pre);
318 $post = '"' if (! $post);
319 # what about wildcards?
321 $wc = '*' if ($q->param("e$i") & 4);
325 foreach (split(/\s+/,$search)) {
326 if (m/^([+-])(\S+)/) {
327 $s.= ($s) ? "and " : "";
328 $s.="not " if ($1 eq "-");
330 } elsif (m/^\s*(and|or|not)\s*$/i) {
332 # don't add * to words with less than x chars
333 } elsif (length($_) <= $MIN_WILDCARD) {
340 $s =~ s/[()]//g; # () are used in query language
341 $s = $pre.$s.$post if ($q->param("e$i"));
344 # FIXME default operator for multi-value fields is or. There is
345 # no way to change it, except here for now. Is there need?
346 push @s_arr, $q->param("f$i")."_swish".$exact."=(".join(" or ",@swish_q).")";
349 my $tmpl = $self->load_tmpl(url_ex($q,'results.html'), global_vars => 1, die_on_bad_params => 0);
358 my $sort = 'swishrank';
359 if ($q->param("sort")) {
361 push @persist_vars, "sort";
364 my $sortby = $q->param("sortby");
367 push @persist_vars, "sortby";
369 # used to filter entries in index and swish
370 my $filter = $q->param("filter");
372 # construct swish query
373 my $sw_q = join(" and ",@s_arr);
374 if (@path_arr && $q->param('show_full')) {
375 $sw_q .= " and (swishdocpath=\"";
376 $sw_q .= join("\" or swishdocpath=\"",@path_arr);
378 $tmpl->param('full',1); # show full records
379 # } elsif (@path_arr && $#path_arr == 0) {
380 # # I will assume that it's a filter since there isn't show_full
381 # $filter = shift @path_arr;
382 } elsif ($q->param('show_full')) {
383 # just show full path, no path defined
384 $tmpl->param('full',1);
386 $tmpl->param('full',0);
390 $sw_q .= " and (swishdocpath=\"$filter\")" unless (@path_arr);
391 push @persist_vars, "filter";
392 push @url_params, "filter=$filter";
393 push @url_params_persist, "filter=$filter";
398 # create new swish instance
399 my $swish = SWISH::API->new($INDEX);
400 $swish_msg .= $swish->ErrorString." ".$swish->LastErrorMsg if $swish->Error;
402 # execute query and get number of results from SWISH-E
403 my $search = $swish->New_Search_Object;
405 $search->SetSort($sort);
407 my $results = $search->Execute($sw_q);
408 $swish_msg .= $swish->ErrorString." ".$swish->LastErrorMsg if $swish->Error;
410 my $hits = $results->Hits;
412 $tmpl->param('hits',$hits);
413 my $search_msg = $sw_q;
414 $search_msg .= '<em>'.$swish_msg.'</em>' if ($swish_msg);
415 $tmpl->param('search', $search_msg);
417 $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);
418 $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);
420 # URL parametars for search results
421 $tmpl->param('url_params',"?".join("&",@url_params));
422 $tmpl->param('url_params_paths',"?".join("&",@url_params).'&'.join("&",map { my $t = $_; $t =~ s/\#/%23/g; "path=$t"; } @path_arr));
430 my $current_page = $q->param('PAGER_offset') || 1;
432 my $pager = Data::Pageset->new({
433 'total_entries' => $hits,
434 'entries_per_page' => $ON_PAGE,
435 'current_page' => $current_page,
436 'pages_per_set' => $pages_per_set,
439 $results->SeekResult( $pager->first - 1 );
441 # get number of entries on this page
442 my $i = $pager->entries_on_this_page;
444 # results from swish for template
447 for(my $i=$pager->first; $i<=$pager->last; $i++) {
449 my $result = $results->NextResult;
454 path => $result->Property('swishdocpath'),
455 headline => esc_html($from_utf8->convert($result->Property('headline'))),
456 rank => $result->Property('swishrank')
459 $r->{html} = back2html($from_utf8->convert($result->Property('html')), join("&",@url_params_persist)) if ($q->param('show_full'));
461 push @pager_data_list, $r;
466 # put something in template
467 make_pager($q, $tmpl, $pager);
468 make_pager_vars($q, $tmpl, @persist_vars);
469 $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
471 my $html = $tmpl->output;
473 return in_template($q,$html);
478 my $i = shift; # field number
480 my $q = $self->query();
482 my $field = $q->param("f$i");
483 my $limit = $q->param("v$i");
485 my $filter = $q->param("filter");
489 my $index = new index_DBI(
490 $cfg_global->val('global', 'dbi_dbd'),
491 $cfg_global->val('global', 'dbi_dsn'),
492 $cfg_global->val('global', 'dbi_user'),
493 $cfg_global->val('global', 'dbi_passwd') || ''
496 my $total = $index->count($field,$limit,$filter);
498 if (! defined($total)) {
499 my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html'));
500 $tmpl->param('field',$field);
501 $html = $tmpl->output;
505 my $tmpl = $self->load_tmpl(url_ex($q,'index_res.html'), global_vars => 1, die_on_bad_params => 0);
506 $tmpl->param('field',$field);
507 $tmpl->param('limit',$limit);
508 $tmpl->param('total',$total);
509 $tmpl->param('filter',$filter);
511 # FIXME I should set offset and leave out limit from fetch!!
512 # if (! $q->param("PAGER_offset") {
513 # $q->param("Pager_offet)
520 my $pager = Data::Pageset->new({
521 'total_entries' => $total,
522 'entries_per_page' => $ON_PAGE,
523 'current_page' => $q->param('PAGER_offset') || 1,
524 'pages_per_set' => $pages_per_set
527 my @persist_vars = qw{rm f$i v$i f$i_index offset};
529 make_pager($q, $tmpl, $pager);
530 make_pager_vars($q, $tmpl, @persist_vars);
532 my @pager_data_list = $index->fetch($field,$limit, $pager->first - 1, $pager->entries_on_this_page, $filter);
533 $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
535 return in_template($q,$tmpl->output);