3 use base 'CGI::Application';
17 # configuration options
18 # FIXME they really should go in configuration file!
19 my $TEMPLATE_PATH = '/data/webpac/template_html';
20 my $CHARSET = 'ISO-8859-2';
21 my $SWISH = '/usr/bin/swish-e';
22 my $INDEX = '/data/webpac/index/isis.index';
26 Text::Iconv->raise_error(0); # Conversion errors raise exceptions
28 my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
30 # read global.conf configuration
31 my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'";
36 $self->tmpl_path($TEMPLATE_PATH);
38 'search' => 'show_search_form',
39 'results' => 'show_results_list',
40 # 'user' => 'show_user_detail',
41 'index' => 'show_index',
43 $self->start_mode('search');
44 $self->mode_param('rm');
46 $self->header_props(-charset=>$CHARSET);
49 sub show_search_form {
52 # Get the CGI.pm query object
53 my $q = $self->query();
55 my $tmpl = $self->load_tmpl('search.html');
56 my $html = $tmpl->output;
58 my $fif = new HTML::FillInForm;
60 return $fif->fill(scalarref => \$html, fobject => $q,
64 sub show_results_list {
67 my $q = $self->query();
69 my @swish_results; # results from swish
71 # load template for this page
73 my @s_arr; # all queries are located here
75 my @path_arr = $q->param('path');
76 my $full = $q->param('full');
78 for(my $i = 1; $i <=10; $i++) {
80 return show_index($self, $i) if ($q->param("f".$i."_index"));
81 next if (! $q->param("f$i"));
82 next if (! $q->param("v$i"));
84 # re-write query from +/- to and/and not
86 my $search = $q->param("v$i");
87 while ($search =~ s/\s*("[^"]+")\s*/ /) {
93 foreach (split(/\s+/,$search)) {
94 if (m/^([+-])(\S+)/) {
95 $s.= ($s) ? "and " : "";
96 $s.="not " if ($1 eq "-");
98 } elsif (m/(and|or|not)/i) {
106 push @s_arr,$q->param("f$i")."_swish=($s)";
109 my $tmpl = $self->load_tmpl('results.html');
112 my $sh = SWISH->connect('Fork',
115 properties => [qw/swishdocpath swishrank swishtitle headline html/],
119 push @swish_results, {
120 nr => ($#swish_results + 2),
121 path => $hit->swishdocpath,
122 headline => $from_utf8->convert($hit->headline),
123 html => back2html($from_utf8->convert($hit->html)),
124 rank => $hit->swishrank };
131 die $SWISH::errstr unless $sh;
133 # construct swish query
134 my $sw_q = join(" and ",@s_arr);
136 $sw_q .= "and (swishdocpath=\"";
137 $sw_q .= join("\" or swishdocpath=\"",@path_arr);
139 $tmpl->param('full',1); # show full records
142 my $hits = $sh->query($sw_q);
144 $tmpl->param('hits',$hits);
145 $tmpl->param('search',$sw_q);
147 $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);
148 $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);
150 # create a Pager object
151 my $pager = HTML::Pager->new(
152 # required parameters
154 get_data_callback => sub {
155 my ($offset, $rows) = @_;
158 for (my $i=0; $i<$rows; $i++) {
159 push @result, $swish_results[$offset+$i] if $swish_results[$offset+$i];
164 page_size => $ON_PAGE,
165 # some optional parameters
178 #cell_space_color => '#000000',
179 #cell_background_color => '#ffffff',
180 #nav_background_color => '#dddddd',
181 #javascript_presubmit => 'last_minute_javascript()',
186 my $html = $pager->output;
193 my $i = shift; # field number
195 my $q = $self->query();
197 my $field = $q->param("f$i");
198 my $limit = $q->param("v$i");
202 my $index = new index_DBI(
203 $cfg_global->val('global', 'dbi_dbd'),
204 $cfg_global->val('global', 'dbi_dsn'),
205 $cfg_global->val('global', 'dbi_user'),
206 $cfg_global->val('global', 'dbi_passwd') || ''
209 my $total = $index->check($field);
211 my $tmpl = $self->load_tmpl('no_index.html');
212 $tmpl->param('field',$field);
213 $html = $tmpl->output;
217 my $tmpl = $self->load_tmpl('index_res.html');
218 $tmpl->param('field',$field);
219 $tmpl->param('limit',$limit);
220 $tmpl->param('total',$total);
222 # FIXME I should set offset and leave out limit from fetch!!
223 # if (! $q->param("PAGER_offset") {
224 # $q->param("Pager_offet)
227 my $pager = HTML::Pager->new(
229 get_data_callback => sub {
230 my ($offset, $rows) = @_;
232 my @result = $index->fetch($field,'item',$limit, $offset, $rows);
236 page_size => $ON_PAGE,
239 "f$i", "v$i", "f".$i."_index",
246 return $pager->output;