/[webpac]/trunk2/WebPac.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk2/WebPac.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (show annotations)
Sat Feb 22 13:22:09 2003 UTC (21 years, 1 month ago) by dpavlin
Original Path: trunk/WebPac.pm
File size: 4392 byte(s)
use Text::Iconv for conversions

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

Properties

Name Value
cvs2svn:cvs-rev 1.7

  ViewVC Help
Powered by ViewVC 1.1.26