3 use base 'CGI::Application';
18 # configuration options
19 # FIXME they really should go in configuration file!
20 my $TEMPLATE_PATH = '/data/webpac/template_html';
21 my $CHARSET = 'ISO-8859-2';
22 my $SWISH = '/usr/bin/swish-e';
23 my $INDEX = '/data/webpac/index/isis.index';
27 Text::Iconv->raise_error(0); # Conversion errors raise exceptions
29 my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
31 # read global.conf configuration
32 my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'";
37 $self->tmpl_path($TEMPLATE_PATH);
39 'search' => 'show_search_form',
40 'results' => 'show_results_list',
41 # 'user' => 'show_user_detail',
42 'index' => 'show_index',
44 $self->start_mode('search');
45 $self->mode_param('rm');
47 $self->header_props(-charset=>$CHARSET);
50 sub show_search_form {
53 # Get the CGI.pm query object
54 my $q = $self->query();
56 my $tmpl = $self->load_tmpl('search.html');
57 my $html = $tmpl->output;
59 my $fif = new HTML::FillInForm;
61 return $fif->fill(scalarref => \$html, fobject => $q,
65 sub show_results_list {
68 my $q = $self->query();
70 my @swish_results; # results from swish
72 # load template for this page
74 my @s_arr; # all queries are located here
76 my @path_arr = $q->param('path');
77 my $full = $q->param('full');
79 for(my $i = 1; $i <=30; $i++) {
81 return show_index($self, $i) if ($q->param("f".$i."_index"));
82 next if (! $q->param("v$i"));
83 next if (! $q->param("f$i"));
85 # re-write query from +/- to and/and not
86 my @param_vals = $q->param("v$i");
88 while (my $search = shift @param_vals) {
91 $search = unac_string($CHARSET,$search);
92 while ($search =~ s/\s*("[^"]+")\s*/ /) {
98 foreach (split(/\s+/,$search)) {
99 if (m/^([+-])(\S+)/) {
100 $s.= ($s) ? "and " : "";
101 $s.="not " if ($1 eq "-");
103 } elsif (m/(and|or|not)/i) {
112 # FIXME default operator for multi-value fields is or. There is
113 # no way to change it, except here for now. Is there need?
114 push @s_arr, $q->param("f$i")."_swish=(".join(" or ",@swish_q).")";
117 my $tmpl = $self->load_tmpl('results.html');
120 my $sh = SWISH->connect('Fork',
123 properties => [qw/swishdocpath swishrank swishtitle headline html/],
127 push @swish_results, {
128 nr => ($#swish_results + 2),
129 path => $hit->swishdocpath,
130 headline => $from_utf8->convert($hit->headline),
131 html => back2html($from_utf8->convert($hit->html)),
132 rank => $hit->swishrank };
139 die $SWISH::errstr unless $sh;
140 # construct swish query
141 my $sw_q = join(" and ",@s_arr);
143 $sw_q .= "and (swishdocpath=\"";
144 $sw_q .= join("\" or swishdocpath=\"",@path_arr);
146 $tmpl->param('full',1); # show full records
149 my $hits = $sh->query($sw_q);
151 $tmpl->param('hits',$hits);
152 $tmpl->param('search',$sw_q);
154 $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 1);
155 $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 1);
157 # create a Pager object
158 my $pager = HTML::Pager->new(
159 # required parameters
161 get_data_callback => sub {
162 my ($offset, $rows) = @_;
165 for (my $i=0; $i<$rows; $i++) {
166 push @result, $swish_results[$offset+$i] if $swish_results[$offset+$i];
171 page_size => $ON_PAGE,
172 # some optional parameters
185 #cell_space_color => '#000000',
186 #cell_background_color => '#ffffff',
187 #nav_background_color => '#dddddd',
188 #javascript_presubmit => 'last_minute_javascript()',
193 my $html = $pager->output;
200 my $i = shift; # field number
202 my $q = $self->query();
204 my $field = $q->param("f$i");
205 my $limit = $q->param("v$i");
209 my $index = new index_DBI(
210 $cfg_global->val('global', 'dbi_dbd'),
211 $cfg_global->val('global', 'dbi_dsn'),
212 $cfg_global->val('global', 'dbi_user'),
213 $cfg_global->val('global', 'dbi_passwd') || ''
216 my $total = $index->check($field);
218 my $tmpl = $self->load_tmpl('no_index.html');
219 $tmpl->param('field',$field);
220 $html = $tmpl->output;
224 my $tmpl = $self->load_tmpl('index_res.html');
225 $tmpl->param('field',$field);
226 $tmpl->param('limit',$limit);
227 $tmpl->param('total',$total);
229 # FIXME I should set offset and leave out limit from fetch!!
230 # if (! $q->param("PAGER_offset") {
231 # $q->param("Pager_offet)
234 my $pager = HTML::Pager->new(
236 get_data_callback => sub {
237 my ($offset, $rows) = @_;
239 my @result = $index->fetch($field,'item',$limit, $offset, $rows);
243 page_size => $ON_PAGE,
246 "f$i", "v$i", "f".$i."_index",
253 return $pager->output;