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 |
headline => $hit->headline, |
115 |
html => back2html($hit->html), |
116 |
rank => $hit->swishrank }; |
117 |
|
118 |
}, |
119 |
#startnum => 0, |
120 |
maxhits => $MAX_HITS, |
121 |
); |
122 |
|
123 |
die $SWISH::errstr unless $sh; |
124 |
|
125 |
my $hits = $sh->query(join(" and ",@s_arr)) || 0; # FIX: and/or |
126 |
|
127 |
$tmpl->param('hits',$hits); |
128 |
$tmpl->param('search',join(" and ",@s_arr)); |
129 |
|
130 |
# create a Pager object |
131 |
my $pager = HTML::Pager->new( |
132 |
# required parameters |
133 |
query => $q, |
134 |
get_data_callback => sub { |
135 |
my ($offset, $rows) = @_; |
136 |
|
137 |
my @result; |
138 |
for (my $i=0; $i<$rows; $i++) { |
139 |
push @result, $swish_results[$offset+$i] if $swish_results[$offset+$i]; |
140 |
} |
141 |
return \@result; |
142 |
}, |
143 |
rows => $hits, |
144 |
page_size => $ON_PAGE, |
145 |
# some optional parameters |
146 |
persist_vars => [ |
147 |
'rm', |
148 |
'f1', 'v1', |
149 |
'f2', 'v2', |
150 |
'f3', 'v3', |
151 |
'f4', 'v4', |
152 |
'f5', 'v5', |
153 |
'f6', 'v6', |
154 |
'f7', 'v7', |
155 |
'f8', 'v8', |
156 |
'f9', 'v9', |
157 |
], |
158 |
#cell_space_color => '#000000', |
159 |
#cell_background_color => '#ffffff', |
160 |
#nav_background_color => '#dddddd', |
161 |
#javascript_presubmit => 'last_minute_javascript()', |
162 |
debug => 1, |
163 |
template => $tmpl, |
164 |
); |
165 |
|
166 |
my $html = $pager->output; |
167 |
|
168 |
return $html; |
169 |
} |
170 |
|
171 |
sub show_index { |
172 |
my $self = shift; |
173 |
my $i = shift; # field number |
174 |
|
175 |
my $q = $self->query(); |
176 |
|
177 |
my $field = $q->param("f$i"); |
178 |
my $limit = $q->param("v$i"); |
179 |
|
180 |
my $html; |
181 |
|
182 |
my $index = new index_DBI(); |
183 |
|
184 |
my $total = $index->check($field); |
185 |
if (! $total) { |
186 |
my $tmpl = $self->load_tmpl('no_index.html'); |
187 |
$tmpl->param('field',$field); |
188 |
$html = $tmpl->output; |
189 |
return $html; |
190 |
} |
191 |
|
192 |
my $tmpl = $self->load_tmpl('index_res.html'); |
193 |
$tmpl->param('field',$field); |
194 |
$tmpl->param('limit',$limit); |
195 |
$tmpl->param('total',$total); |
196 |
|
197 |
# FIX: I should set offset and leave out limit from fetch!! |
198 |
# if (! $q->param("PAGER_offset") { |
199 |
# $q->param("Pager_offet) |
200 |
# } |
201 |
|
202 |
my $pager = HTML::Pager->new( |
203 |
query => $q, |
204 |
get_data_callback => sub { |
205 |
my ($offset, $rows) = @_; |
206 |
|
207 |
my @result = $index->fetch($field,'item',$limit, $offset, $rows); |
208 |
return \@result; |
209 |
}, |
210 |
rows => $total, |
211 |
page_size => $ON_PAGE, |
212 |
persist_vars => [ |
213 |
'rm', |
214 |
"f$i", "v$i", "f".$i."_index", |
215 |
'offset', |
216 |
], |
217 |
debug => 1, |
218 |
template => $tmpl, |
219 |
); |
220 |
|
221 |
return $pager->output; |
222 |
} |
223 |
|
224 |
1; |