4e61df6f9a71d31352878facbb427661fb6f7784
[webpac] / WebPac.pm
1 package WebPac;
2
3 use base 'CGI::Application';
4 use strict;
5
6 use HTML::Pager;
7 use HTML::FillInForm;
8 use SWISH;
9 use Text::Iconv;
10 use DBI;
11 use Config::IniFiles;
12 use Text::Unaccent;
13
14 use lib '..';
15 use index_DBI;
16 use back2html;
17
18
19 # read global.conf configuration
20 my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'";
21
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
30
31 Text::Iconv->raise_error(0);     # Conversion errors raise exceptions
32
33 my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
34
35
36 sub setup {
37         my $self = shift;
38         $self->tmpl_path($TEMPLATE_PATH);
39         $self->run_modes(
40                 'search' => 'show_search_form',
41                 'results' => 'show_results_list',
42 #               'user' => 'show_user_detail',
43                 'index' => 'show_index',
44         );
45         $self->start_mode('search');
46         $self->mode_param('rm');
47
48         $self->header_props(-charset=>$CHARSET);
49 }
50
51 sub show_search_form {
52         my $self = shift;
53
54         # Get the CGI.pm query object
55         my $q = $self->query();
56
57         my $tmpl = $self->load_tmpl('search.html');
58         my $html = $tmpl->output;
59
60         my $fif = new HTML::FillInForm;
61
62         return $fif->fill(scalarref => \$html, fobject => $q,
63                 target => 'search');
64 }
65  
66 sub show_results_list {
67         my $self = shift;
68
69         my $q = $self->query();
70
71         my @swish_results;      # results from swish
72
73         # load template for this page
74
75         my @s_arr;      # all queries are located here
76
77         my @path_arr = $q->param('path');
78         my $full = $q->param('full');
79
80         my @persist_vars = ( 'rm' ); 
81
82         for(my $i = 1; $i <=30; $i++) {
83
84                 return show_index($self, $i) if ($q->param("f".$i."_index"));
85
86                 next if (! $q->param("v$i"));
87                 next if (! $q->param("f$i"));
88
89                 push @persist_vars, "f$i";
90                 push @persist_vars, "v$i";
91
92                 # re-write query from +/- to and/and not
93                 my @param_vals = $q->param("v$i");
94                 my @swish_q;
95                 while (my $search = shift @param_vals) {
96                         my $s;
97                         # remove accents
98                         $search = unac_string($CHARSET,$search);
99                         while ($search =~ s/\s*("[^"]+")\s*/ /) {
100                                 $s .= "$1 ";
101                         }
102                         $search =~ s/^\s+//;
103                         $search =~ s/\s+$//;
104
105                         foreach (split(/\s+/,$search)) {
106                                 if (m/^([+-])(\S+)/) {
107                                         $s.= ($s) ? "and " : "";
108                                         $s.="not " if ($1 eq "-");
109                                         $s.="$2* ";
110                                 } elsif (m/(and|or|not)/i) {
111                                         $s.="$_ ";
112                                 } else {
113                                         $s.="$_* ";
114                                 }
115                         }
116                         $s =~ s/\*+/*/g;
117                         push @swish_q,$s;
118                 }
119                 # FIXME default operator for multi-value fields is or. There is
120                 # no way to change it, except here for now. Is there need?
121                 push @s_arr, $q->param("f$i")."_swish=(".join(" or ",@swish_q).")";
122         }
123
124         my $tmpl = $self->load_tmpl('results.html');
125
126         sub esc_html {
127                 my $html = shift;
128                 $html =~ s/</&lt;/g;
129                 $html =~ s/>/&gt;/g;
130                 return $html;
131         }
132
133         # call swish
134         my $sh = SWISH->connect('Fork',
135                 prog     => $SWISH,
136                 indexes  => $INDEX,
137                 properties  => [qw/swishdocpath swishrank swishtitle headline html/],
138                 results  => sub {
139                         my ($sh,$hit) = @_;
140
141                         push @swish_results, {
142                                 nr => ($#swish_results + 2),
143                                 path => $hit->swishdocpath,
144                                 headline => esc_html($from_utf8->convert($hit->headline)),
145                                 html => back2html($from_utf8->convert($hit->html)),
146                                 rank => $hit->swishrank };
147
148                 },
149                 #startnum => 0,
150                 maxhits => $MAX_HITS
151         );
152
153         die $SWISH::errstr unless $sh;
154         # construct swish query
155         my $sw_q = join(" and ",@s_arr);
156         if (@path_arr && $q->param('show_full')) {
157                 $sw_q .= "and (swishdocpath=\"";
158                 $sw_q .= join("\" or swishdocpath=\"",@path_arr);
159                 $sw_q .= "\")";
160                 $tmpl->param('full',1); # show full records
161         }
162
163         my $hits = $sh->query($sw_q);
164
165         $tmpl->param('hits',$hits);
166         $tmpl->param('search',$sw_q);
167
168         $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);
169         $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);
170
171         # create a Pager object
172         my $pager = HTML::Pager->new(
173                 # required parameters
174                 query => $q,
175                 get_data_callback => sub {
176                         my ($offset, $rows) = @_;
177
178                         my @result;
179                         for (my $i=0; $i<$rows; $i++) {
180                                 my $r = $swish_results[$offset+$i];
181                                 if ($r && $q->param('show_full')) {
182                                         push @result, $r;
183                                 } elsif ($r) {
184                                         # if not full output, skip html
185                                         delete $r->{html};
186                                         push @result, $r;
187                                 }
188                         }
189                         return \@result;
190                 },
191                 rows => $hits,
192                 page_size => $ON_PAGE,
193                 # some optional parameters
194                 persist_vars => [ @persist_vars ],
195                 #cell_space_color => '#000000',
196                 #cell_background_color => '#ffffff',
197                 #nav_background_color => '#dddddd',
198                 #javascript_presubmit => 'last_minute_javascript()',
199                 debug => 1,
200                 template => $tmpl,
201         );
202
203         my $html = $pager->output;
204
205         return $html;
206 }
207  
208 sub show_index {
209         my $self = shift;
210         my $i = shift;          # field number
211
212         my $q = $self->query();
213
214         my $field = $q->param("f$i");
215         my $limit = $q->param("v$i");
216
217         my $html;
218
219         my $index = new index_DBI(
220                 $cfg_global->val('global', 'dbi_dbd'),
221                 $cfg_global->val('global', 'dbi_dsn'),
222                 $cfg_global->val('global', 'dbi_user'),
223                 $cfg_global->val('global', 'dbi_passwd') || ''
224         );
225
226         my $total = $index->check($field);
227         if (! $total) {
228                 my $tmpl = $self->load_tmpl('no_index.html');
229                 $tmpl->param('field',$field);
230                 $html = $tmpl->output;
231                 return $html;
232         }
233
234         my $tmpl = $self->load_tmpl('index_res.html');
235         $tmpl->param('field',$field);
236         $tmpl->param('limit',$limit);
237         $tmpl->param('total',$total);
238
239 # FIXME I should set offset and leave out limit from fetch!!
240 #       if (! $q->param("PAGER_offset") {
241 #               $q->param("Pager_offet)
242 #       }
243
244         my $pager = HTML::Pager->new(
245                 query => $q,
246                 get_data_callback => sub {
247                         my ($offset, $rows) = @_;
248
249                         my @result = $index->fetch($field,'item',$limit, $offset, $rows);
250                         return \@result;
251                 },
252                 rows => $total,
253                 page_size => $ON_PAGE,
254                 persist_vars => [
255                         'rm', 
256                         "f$i", "v$i", "f".$i."_index", 
257                         'offset',
258                         ],
259                 debug => 1,
260                 template => $tmpl,
261         );
262
263         return $pager->output;
264 }
265
266 1;