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