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