/[webpac]/branches/hidra/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

Annotation of /branches/hidra/WebPac.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 73 - (hide annotations)
Sat Jul 5 22:31:46 2003 UTC (20 years, 8 months ago) by dpavlin
Original Path: trunk/WebPac.pm
File size: 5642 byte(s)
- support multi-value fields (e.g. for checkboxes)
- remove accented characters before feeding swish

1 dpavlin 7 package WebPac;
2    
3     use base 'CGI::Application';
4     use strict;
5    
6     use HTML::Pager;
7     use HTML::FillInForm;
8     use SWISH;
9 dpavlin 14 use Text::Iconv;
10 dpavlin 9 use DBI;
11 dpavlin 53 use Config::IniFiles;
12 dpavlin 73 use Text::Unaccent;
13 dpavlin 7
14 dpavlin 11 use lib '..';
15     use index_DBI;
16 dpavlin 13 use back2html;
17 dpavlin 11
18 dpavlin 7 # configuration options
19 dpavlin 51 # FIXME they really should go in configuration file!
20 dpavlin 7 my $TEMPLATE_PATH = '/data/webpac/template_html';
21     my $CHARSET = 'ISO-8859-2';
22 dpavlin 13 my $SWISH = '/usr/bin/swish-e';
23 dpavlin 7 my $INDEX = '/data/webpac/index/isis.index';
24 dpavlin 63 my $MAX_HITS = 0;
25 dpavlin 7 my $ON_PAGE = 10;
26    
27 dpavlin 30 Text::Iconv->raise_error(0); # Conversion errors raise exceptions
28 dpavlin 7
29 dpavlin 14 my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
30    
31 dpavlin 53 # read global.conf configuration
32     my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'";
33    
34    
35 dpavlin 7 sub setup {
36     my $self = shift;
37     $self->tmpl_path($TEMPLATE_PATH);
38     $self->run_modes(
39     'search' => 'show_search_form',
40     'results' => 'show_results_list',
41 dpavlin 9 # 'user' => 'show_user_detail',
42     'index' => 'show_index',
43 dpavlin 7 );
44     $self->start_mode('search');
45     $self->mode_param('rm');
46    
47     $self->header_props(-charset=>$CHARSET);
48     }
49    
50     sub show_search_form {
51     my $self = shift;
52    
53     # Get the CGI.pm query object
54     my $q = $self->query();
55    
56     my $tmpl = $self->load_tmpl('search.html');
57     my $html = $tmpl->output;
58    
59     my $fif = new HTML::FillInForm;
60    
61     return $fif->fill(scalarref => \$html, fobject => $q,
62     target => 'search');
63     }
64    
65     sub show_results_list {
66     my $self = shift;
67    
68     my $q = $self->query();
69    
70     my @swish_results; # results from swish
71    
72     # load template for this page
73    
74     my @s_arr; # all queries are located here
75    
76 dpavlin 47 my @path_arr = $q->param('path');
77     my $full = $q->param('full');
78    
79 dpavlin 71 for(my $i = 1; $i <=30; $i++) {
80 dpavlin 7
81 dpavlin 9 return show_index($self, $i) if ($q->param("f".$i."_index"));
82 dpavlin 71 next if (! $q->param("v$i"));
83 dpavlin 9 next if (! $q->param("f$i"));
84 dpavlin 7
85     # re-write query from +/- to and/and not
86 dpavlin 73 my @param_vals = $q->param("v$i");
87     my @swish_q;
88     while (my $search = shift @param_vals) {
89     my $s;
90     # remove accents
91     $search = unac_string($CHARSET,$search);
92     while ($search =~ s/\s*("[^"]+")\s*/ /) {
93     $s .= "$1 ";
94     }
95     $search =~ s/^\s+//;
96     $search =~ s/\s+$//;
97 dpavlin 7
98 dpavlin 73 foreach (split(/\s+/,$search)) {
99     if (m/^([+-])(\S+)/) {
100     $s.= ($s) ? "and " : "";
101     $s.="not " if ($1 eq "-");
102     $s.="$2* ";
103     } elsif (m/(and|or|not)/i) {
104     $s.="$_ ";
105     } else {
106     $s.="$_* ";
107     }
108 dpavlin 7 }
109 dpavlin 73 $s =~ s/\*+/*/g;
110     push @swish_q,$s;
111 dpavlin 7 }
112 dpavlin 73 # FIXME default operator for multi-value fields is or. There is
113     # no way to change it, except here for now. Is there need?
114     push @s_arr, $q->param("f$i")."_swish=(".join(" or ",@swish_q).")";
115 dpavlin 7 }
116    
117 dpavlin 9 my $tmpl = $self->load_tmpl('results.html');
118    
119 dpavlin 7 # call swish
120     my $sh = SWISH->connect('Fork',
121     prog => $SWISH,
122     indexes => $INDEX,
123 dpavlin 13 properties => [qw/swishdocpath swishrank swishtitle headline html/],
124 dpavlin 7 results => sub {
125     my ($sh,$hit) = @_;
126    
127     push @swish_results, {
128     nr => ($#swish_results + 2),
129     path => $hit->swishdocpath,
130 dpavlin 41 headline => $from_utf8->convert($hit->headline),
131     html => back2html($from_utf8->convert($hit->html)),
132 dpavlin 7 rank => $hit->swishrank };
133    
134     },
135     #startnum => 0,
136 dpavlin 47 maxhits => $MAX_HITS
137 dpavlin 7 );
138    
139     die $SWISH::errstr unless $sh;
140 dpavlin 47 # construct swish query
141     my $sw_q = join(" and ",@s_arr);
142     if (@path_arr) {
143     $sw_q .= "and (swishdocpath=\"";
144     $sw_q .= join("\" or swishdocpath=\"",@path_arr);
145     $sw_q .= "\")";
146     $tmpl->param('full',1); # show full records
147     }
148 dpavlin 7
149 dpavlin 47 my $hits = $sh->query($sw_q);
150    
151 dpavlin 7 $tmpl->param('hits',$hits);
152 dpavlin 47 $tmpl->param('search',$sw_q);
153 dpavlin 7
154 dpavlin 73 $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 1);
155     $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 1);
156 dpavlin 51
157 dpavlin 7 # create a Pager object
158     my $pager = HTML::Pager->new(
159     # required parameters
160     query => $q,
161     get_data_callback => sub {
162     my ($offset, $rows) = @_;
163    
164     my @result;
165     for (my $i=0; $i<$rows; $i++) {
166     push @result, $swish_results[$offset+$i] if $swish_results[$offset+$i];
167     }
168     return \@result;
169     },
170     rows => $hits,
171     page_size => $ON_PAGE,
172     # some optional parameters
173     persist_vars => [
174     'rm',
175     'f1', 'v1',
176     'f2', 'v2',
177     'f3', 'v3',
178     'f4', 'v4',
179     'f5', 'v5',
180     'f6', 'v6',
181     'f7', 'v7',
182     'f8', 'v8',
183     'f9', 'v9',
184     ],
185     #cell_space_color => '#000000',
186     #cell_background_color => '#ffffff',
187     #nav_background_color => '#dddddd',
188     #javascript_presubmit => 'last_minute_javascript()',
189     debug => 1,
190     template => $tmpl,
191     );
192    
193     my $html = $pager->output;
194    
195     return $html;
196     }
197    
198 dpavlin 9 sub show_index {
199     my $self = shift;
200     my $i = shift; # field number
201    
202     my $q = $self->query();
203    
204 dpavlin 11 my $field = $q->param("f$i");
205     my $limit = $q->param("v$i");
206    
207 dpavlin 9 my $html;
208    
209 dpavlin 53 my $index = new index_DBI(
210     $cfg_global->val('global', 'dbi_dbd'),
211     $cfg_global->val('global', 'dbi_dsn'),
212     $cfg_global->val('global', 'dbi_user'),
213     $cfg_global->val('global', 'dbi_passwd') || ''
214     );
215 dpavlin 9
216 dpavlin 12 my $total = $index->check($field);
217     if (! $total) {
218 dpavlin 11 my $tmpl = $self->load_tmpl('no_index.html');
219     $tmpl->param('field',$field);
220     $html = $tmpl->output;
221     return $html;
222     }
223 dpavlin 9
224 dpavlin 12 my $tmpl = $self->load_tmpl('index_res.html');
225     $tmpl->param('field',$field);
226     $tmpl->param('limit',$limit);
227     $tmpl->param('total',$total);
228 dpavlin 11
229 dpavlin 51 # FIXME I should set offset and leave out limit from fetch!!
230 dpavlin 16 # if (! $q->param("PAGER_offset") {
231     # $q->param("Pager_offet)
232     # }
233    
234 dpavlin 12 my $pager = HTML::Pager->new(
235     query => $q,
236     get_data_callback => sub {
237     my ($offset, $rows) = @_;
238 dpavlin 11
239 dpavlin 12 my @result = $index->fetch($field,'item',$limit, $offset, $rows);
240     return \@result;
241     },
242     rows => $total,
243     page_size => $ON_PAGE,
244     persist_vars => [
245     'rm',
246     "f$i", "v$i", "f".$i."_index",
247     'offset',
248     ],
249     debug => 1,
250     template => $tmpl,
251     );
252 dpavlin 11
253 dpavlin 12 return $pager->output;
254 dpavlin 9 }
255    
256 dpavlin 7 1;

Properties

Name Value
cvs2svn:cvs-rev 1.17

  ViewVC Help
Powered by ViewVC 1.1.26