misc updates
[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
12 use lib '..';
13 use index_DBI;
14 use back2html;
15
16 # configuration options
17 # FIX: 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';
22 my $MAX_HITS = 500;
23 my $ON_PAGE = 10;
24
25 Text::Iconv->raise_error(0);     # Conversion errors raise exceptions
26
27 my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
28
29 sub setup {
30         my $self = shift;
31         $self->tmpl_path($TEMPLATE_PATH);
32         $self->run_modes(
33                 'search' => 'show_search_form',
34                 'results' => 'show_results_list',
35 #               'user' => 'show_user_detail',
36                 'index' => 'show_index',
37         );
38         $self->start_mode('search');
39         $self->mode_param('rm');
40
41         $self->header_props(-charset=>$CHARSET);
42 }
43
44 sub show_search_form {
45         my $self = shift;
46
47         # Get the CGI.pm query object
48         my $q = $self->query();
49
50         my $tmpl = $self->load_tmpl('search.html');
51         my $html = $tmpl->output;
52
53         my $fif = new HTML::FillInForm;
54
55         return $fif->fill(scalarref => \$html, fobject => $q,
56                 target => 'search');
57 }
58  
59 sub show_results_list {
60         my $self = shift;
61
62         my $q = $self->query();
63
64         my @swish_results;      # results from swish
65
66         # load template for this page
67
68         my @s_arr;      # all queries are located here
69
70         for(my $i = 1; $i <=10; $i++) {
71
72                 return show_index($self, $i) if ($q->param("f".$i."_index"));
73                 next if (! $q->param("f$i"));
74                 next if (! $q->param("v$i"));
75
76                 # re-write query from +/- to and/and not
77                 my $s;
78                 my $search = $q->param("v$i");
79                 while ($search =~ s/\s*("[^"]+")\s*/ /) {
80                         $s .= "$1 ";
81                 }
82                 $search =~ s/^\s+//;
83                 $search =~ s/\s+$//;
84
85                 foreach (split(/\s+/,$search)) {
86                         if (m/^([+-])(\S+)/) {
87                                 $s.= ($s) ? "and " : "";
88                                 $s.="not " if ($1 eq "-");
89                                 $s.="$2* ";
90                         } else {
91                                 $s.="$_* ";
92                         }
93                 }
94                 $s =~ s/\*+/*/g;
95
96                 push @s_arr,$q->param("f$i")."_swish=($s)";
97         }
98
99         my $tmpl = $self->load_tmpl('results.html');
100
101         # call swish
102         my $sh = SWISH->connect('Fork',
103                 prog     => $SWISH,
104                 indexes  => $INDEX,
105                 properties  => [qw/swishdocpath swishrank swishtitle headline html/],
106                 results  => sub {
107                         my ($sh,$hit) = @_;
108
109                         push @swish_results, {
110                                 nr => ($#swish_results + 2),
111                                 path => $hit->swishdocpath,
112                                 headline => $from_utf8->convert($hit->headline),
113                                 html => back2html($from_utf8->convert($hit->html)),
114                                 rank => $hit->swishrank };
115
116                 },
117                 #startnum => 0,
118                 maxhits => $MAX_HITS,
119         );
120
121         die $SWISH::errstr unless $sh;
122
123         my $hits = $sh->query(join(" and ",@s_arr)) || 0;       # FIX: and/or
124
125         $tmpl->param('hits',$hits);
126         $tmpl->param('search',join(" and ",@s_arr));
127
128         # create a Pager object
129         my $pager = HTML::Pager->new(
130                 # required parameters
131                 query => $q,
132                 get_data_callback => sub {
133                         my ($offset, $rows) = @_;
134
135                         my @result;
136                         for (my $i=0; $i<$rows; $i++) {
137                                 push @result, $swish_results[$offset+$i] if $swish_results[$offset+$i];
138                         }
139                         return \@result;
140                 },
141                 rows => $hits,
142                 page_size => $ON_PAGE,
143                 # some optional parameters
144                 persist_vars => [
145                         'rm',
146                         'f1', 'v1',
147                         'f2', 'v2',
148                         'f3', 'v3',
149                         'f4', 'v4',
150                         'f5', 'v5',
151                         'f6', 'v6',
152                         'f7', 'v7',
153                         'f8', 'v8',
154                         'f9', 'v9',
155                         ],
156                 #cell_space_color => '#000000',
157                 #cell_background_color => '#ffffff',
158                 #nav_background_color => '#dddddd',
159                 #javascript_presubmit => 'last_minute_javascript()',
160                 debug => 1,
161                 template => $tmpl,
162         );
163
164         my $html = $pager->output;
165
166         return $html;
167 }
168  
169 sub show_index {
170         my $self = shift;
171         my $i = shift;          # field number
172
173         my $q = $self->query();
174
175         my $field = $q->param("f$i");
176         my $limit = $q->param("v$i");
177
178         my $html;
179
180         my $index = new index_DBI();
181
182         my $total = $index->check($field);
183         if (! $total) {
184                 my $tmpl = $self->load_tmpl('no_index.html');
185                 $tmpl->param('field',$field);
186                 $html = $tmpl->output;
187                 return $html;
188         }
189
190         my $tmpl = $self->load_tmpl('index_res.html');
191         $tmpl->param('field',$field);
192         $tmpl->param('limit',$limit);
193         $tmpl->param('total',$total);
194
195 # FIX: I should set offset and leave out limit from fetch!!
196 #       if (! $q->param("PAGER_offset") {
197 #               $q->param("Pager_offet)
198 #       }
199
200         my $pager = HTML::Pager->new(
201                 query => $q,
202                 get_data_callback => sub {
203                         my ($offset, $rows) = @_;
204
205                         my @result = $index->fetch($field,'item',$limit, $offset, $rows);
206                         return \@result;
207                 },
208                 rows => $total,
209                 page_size => $ON_PAGE,
210                 persist_vars => [
211                         'rm', 
212                         "f$i", "v$i", "f".$i."_index", 
213                         'offset',
214                         ],
215                 debug => 1,
216                 template => $tmpl,
217         );
218
219         return $pager->output;
220 }
221
222 1;