3 use base 'CGI::Application';
16 # configuration options
17 # FIXME they really should go in configuration file!
18 my $TEMPLATE_PATH = '/data/webpac/template_html';
19 my $CHARSET = 'ISO-8859-2';
20 my $SWISH = '/usr/bin/swish-e';
21 my $INDEX = '/data/webpac/index/isis.index';
25 Text::Iconv->raise_error(0); # Conversion errors raise exceptions
27 my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
31 $self->tmpl_path($TEMPLATE_PATH);
33 'search' => 'show_search_form',
34 'results' => 'show_results_list',
35 # 'user' => 'show_user_detail',
36 'index' => 'show_index',
38 $self->start_mode('search');
39 $self->mode_param('rm');
41 $self->header_props(-charset=>$CHARSET);
44 sub show_search_form {
47 # Get the CGI.pm query object
48 my $q = $self->query();
50 my $tmpl = $self->load_tmpl('search.html');
51 my $html = $tmpl->output;
53 my $fif = new HTML::FillInForm;
55 return $fif->fill(scalarref => \$html, fobject => $q,
59 sub show_results_list {
62 my $q = $self->query();
64 my @swish_results; # results from swish
66 # load template for this page
68 my @s_arr; # all queries are located here
70 my @path_arr = $q->param('path');
71 my $full = $q->param('full');
73 for(my $i = 1; $i <=10; $i++) {
75 return show_index($self, $i) if ($q->param("f".$i."_index"));
76 next if (! $q->param("f$i"));
77 next if (! $q->param("v$i"));
79 # re-write query from +/- to and/and not
81 my $search = $q->param("v$i");
82 while ($search =~ s/\s*("[^"]+")\s*/ /) {
88 foreach (split(/\s+/,$search)) {
89 if (m/^([+-])(\S+)/) {
90 $s.= ($s) ? "and " : "";
91 $s.="not " if ($1 eq "-");
99 push @s_arr,$q->param("f$i")."_swish=($s)";
102 my $tmpl = $self->load_tmpl('results.html');
105 my $sh = SWISH->connect('Fork',
108 properties => [qw/swishdocpath swishrank swishtitle headline html/],
112 push @swish_results, {
113 nr => ($#swish_results + 2),
114 path => $hit->swishdocpath,
115 headline => $from_utf8->convert($hit->headline),
116 html => back2html($from_utf8->convert($hit->html)),
117 rank => $hit->swishrank };
124 die $SWISH::errstr unless $sh;
126 # construct swish query
127 my $sw_q = join(" and ",@s_arr);
129 $sw_q .= "and (swishdocpath=\"";
130 $sw_q .= join("\" or swishdocpath=\"",@path_arr);
132 $tmpl->param('full',1); # show full records
135 my $hits = $sh->query($sw_q);
137 $tmpl->param('hits',$hits);
138 $tmpl->param('search',$sw_q);
140 $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);
141 $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);
143 # create a Pager object
144 my $pager = HTML::Pager->new(
145 # required parameters
147 get_data_callback => sub {
148 my ($offset, $rows) = @_;
151 for (my $i=0; $i<$rows; $i++) {
152 push @result, $swish_results[$offset+$i] if $swish_results[$offset+$i];
157 page_size => $ON_PAGE,
158 # some optional parameters
171 #cell_space_color => '#000000',
172 #cell_background_color => '#ffffff',
173 #nav_background_color => '#dddddd',
174 #javascript_presubmit => 'last_minute_javascript()',
179 my $html = $pager->output;
186 my $i = shift; # field number
188 my $q = $self->query();
190 my $field = $q->param("f$i");
191 my $limit = $q->param("v$i");
195 my $index = new index_DBI();
197 my $total = $index->check($field);
199 my $tmpl = $self->load_tmpl('no_index.html');
200 $tmpl->param('field',$field);
201 $html = $tmpl->output;
205 my $tmpl = $self->load_tmpl('index_res.html');
206 $tmpl->param('field',$field);
207 $tmpl->param('limit',$limit);
208 $tmpl->param('total',$total);
210 # FIXME I should set offset and leave out limit from fetch!!
211 # if (! $q->param("PAGER_offset") {
212 # $q->param("Pager_offet)
215 my $pager = HTML::Pager->new(
217 get_data_callback => sub {
218 my ($offset, $rows) = @_;
220 my @result = $index->fetch($field,'item',$limit, $offset, $rows);
224 page_size => $ON_PAGE,
227 "f$i", "v$i", "f".$i."_index",
234 return $pager->output;