/[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

Annotation of /trunk2/WebPac.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 337 - (hide annotations)
Thu Jun 10 19:22:40 2004 UTC (15 years, 3 months ago) by dpavlin
File size: 11733 byte(s)
new trunk for webpac v2

1 dpavlin 7 package WebPac;
2    
3     use base 'CGI::Application';
4     use strict;
5    
6     use HTML::FillInForm;
7 dpavlin 304 use SWISH::API;
8 dpavlin 14 use Text::Iconv;
9 dpavlin 9 use DBI;
10 dpavlin 53 use Config::IniFiles;
11 dpavlin 73 use Text::Unaccent;
12 dpavlin 304 use Data::Pageset;
13 dpavlin 7
14 dpavlin 11 use lib '..';
15 dpavlin 128 use index_DBI_cache;
16 dpavlin 13 use back2html;
17 dpavlin 11
18 dpavlin 7
19 dpavlin 76 # read global.conf configuration
20     my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'";
21    
22     # configuration options from global.conf
23     my $TEMPLATE_PATH = $cfg_global->val('webpac', 'template_html') || die "need template_html in global.conf, section webpac";
24     my $CHARSET = $cfg_global->val('webpac', 'charset') || 'ISO-8859-1';
25     my $SWISH = $cfg_global->val('webpac', 'swish') || '/usr/bin/swish-e';
26     my $INDEX = $cfg_global->val('webpac', 'index') || die "need index in global.conf, section webpac";
27     my $MAX_HITS = $cfg_global->val('webpac', 'max_hits') || 0;
28     my $ON_PAGE =$cfg_global->val('webpac', 'on_page') || 10;
29 dpavlin 120 my $MIN_WILDCARD =$cfg_global->val('webpac', 'min_wildcard') || 1;
30 dpavlin 147 my $TEMPLATE =$cfg_global->val('webpac', 'template');
31 dpavlin 320 my $UNAC_FILTER =$cfg_global->val('global', 'my_unac_filter');
32 dpavlin 198 my $BASE_PATH =$cfg_global->val('webpac', 'base_path');
33 dpavlin 304 # for pager
34     my $pages_per_set = $cfg_global->val('webpac', 'pages_per_set') || 10;
35 dpavlin 76
36 dpavlin 320 Text::Iconv->raise_error(0); # Conversion errors raise exceptions
37 dpavlin 304
38 dpavlin 320 my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
39    
40 dpavlin 164 if ($UNAC_FILTER) {
41     require $UNAC_FILTER;
42 dpavlin 320 } else {
43     sub WebPac::my_unac_string {
44     my ($charset, $string) = (@_);
45     return $string;
46     }
47 dpavlin 164 }
48 dpavlin 76
49 dpavlin 198 # use path from cgi script to support templates in subdirs
50     sub url_ex {
51     my $q = shift || die "suff2file needs CGI object!";
52     my $tpl = shift || die "url_ex needs template name!";
53 dpavlin 302 return suff2file($BASE_PATH, $q->url(-absolute => 1,-path => 1),$TEMPLATE_PATH,$tpl);
54 dpavlin 198 }
55 dpavlin 53
56 dpavlin 198 sub suff2file($$$$) {
57     my ($base_path, $p, $path, $tpl) = @_;
58    
59     return $tpl if (! $base_path);
60    
61     # strip everything to and including base path, leaving only
62     # additional (virtual) path
63 dpavlin 302 if ($base_path eq "/") {
64 dpavlin 198 $p =~ s,/*,,g;
65     my ($name,$ext) = split(/\./,$tpl);
66 dpavlin 302 $p = $name . "-" . $p . "." . $ext;
67     } elsif ($p =~ s,^.*?$base_path,,) {
68     $p =~ s,/*,,g;
69     my ($name,$ext) = split(/\./,$tpl);
70 dpavlin 198 $p = $name . $p . "." . $ext;
71     } else {
72     # if unable reset it!
73     $p = $tpl;
74     }
75    
76     if ( -e "$path/$p") {
77     return $p;
78     } else {
79     return $tpl;
80     }
81    
82     }
83    
84 dpavlin 7 sub setup {
85     my $self = shift;
86     $self->tmpl_path($TEMPLATE_PATH);
87     $self->run_modes(
88     'search' => 'show_search_form',
89     'results' => 'show_results_list',
90 dpavlin 9 # 'user' => 'show_user_detail',
91     'index' => 'show_index',
92 dpavlin 7 );
93     $self->start_mode('search');
94     $self->mode_param('rm');
95    
96     $self->header_props(-charset=>$CHARSET);
97     }
98    
99 dpavlin 147 sub in_template {
100 dpavlin 198 my $q = shift || die "need CGI object!";
101     my $html = shift || die "This page is left unintentionally blank";
102 dpavlin 147 return $html if (! defined($TEMPLATE));
103 dpavlin 198
104     my ($dir,$tpl);
105     if ($TEMPLATE =~ m,^(.*?/*)([^/]+)$,) {
106     ($dir,$tpl) = ($1,$2);
107     } else {
108     die "can't parse TEMPLATE path";
109     }
110    
111 dpavlin 302 my $master_tpl = suff2file($BASE_PATH, $q->url(-absolute => 1, -path => 1),$dir,$tpl);
112 dpavlin 198 if (open(T, $master_tpl)) {
113 dpavlin 147 my $template_html = join("\n",<T>);
114     close(T);
115     $template_html =~ s/##webpac##/$html/gsi;
116     return $template_html;
117     } else {
118 dpavlin 198 return "Can't read template '$master_tpl'";
119 dpavlin 147 }
120     }
121    
122 dpavlin 304 #--------------------------------------------------------------------------
123    
124     #
125     # make pager navigation and fill template variables
126     # compatibile with HTML::Pager
127     #
128    
129     sub make_pager($$$) {
130     my ($q,$tmpl,$pager) = @_;
131    
132     #
133     # pager navigation
134     #
135     my ($pager_prev,$pager_next, $pager_jump) = ('','','');
136    
137     my $nav_fmt=qq{ <a href="%s">%s</a> };
138    
139     if ($pager->current_page() > $pager->first_page) {
140     $q->param('PAGER_offset', $pager->current_page - 1);
141     $pager_prev .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'&lt;&lt;');
142     }
143    
144     if ($pager->previous_set) {
145     $q->param('PAGER_offset', $pager->previous_set);
146     $pager_prev .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'..');
147     }
148    
149    
150     foreach my $p (@{$pager->pages_in_set()}) {
151     next if ($p < 0);
152     if($p == $pager->current_page()) {
153     $pager_jump .= "<b>$p</b> ";
154     } else {
155     $q->param('PAGER_offset', $p);
156     $pager_jump .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),$p);
157     }
158     }
159    
160     if ($pager->next_set) {
161     $q->param('PAGER_offset', $pager->next_set);
162     $pager_next .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'..');
163     }
164    
165     if ($pager->current_page() < $pager->last_page) {
166     $q->param('PAGER_offset', $pager->current_page + 1);
167     $pager_next .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'&gt;&gt;');
168     }
169    
170     $tmpl->param('PAGER_PREV', $pager_prev);
171     $tmpl->param('PAGER_JUMP', $pager_jump);
172     $tmpl->param('PAGER_NEXT', $pager_next);
173    
174     }
175    
176     #
177     # put persisten variables in template
178     #
179    
180     sub make_pager_vars {
181     my $q = shift @_;
182     my $tmpl = shift @_;
183     my @persist_vars = @_;
184     my $hidden_vars = '';
185     foreach my $v (@persist_vars) {
186     $hidden_vars .= '<input type="hidden" name="'.$v.'" value="'.$q->param($v).'"/>'."\n";
187     }
188    
189     $tmpl->param('PAGER_HIDDEN', $hidden_vars);
190     $tmpl->param('PAGER_JAVASCRIPT', qq{
191     <SCRIPT LANGUAGE="Javascript">
192     <!-- Begin
193     // dummy emulator for HTML::Pager templates
194     function PAGER_set_offset_and_submit() {
195     return true;
196     }
197     // End -->
198     </script>
199     });
200     }
201    
202     #--------------------------------------------------------------------------
203    
204 dpavlin 7 sub show_search_form {
205     my $self = shift;
206    
207     # Get the CGI.pm query object
208     my $q = $self->query();
209    
210 dpavlin 198 my $tmpl = $self->load_tmpl(url_ex($q,'search.html'));
211 dpavlin 7 my $html = $tmpl->output;
212    
213     my $fif = new HTML::FillInForm;
214    
215 dpavlin 198 return in_template($q,$fif->fill(scalarref => \$html, fobject => $q,
216 dpavlin 147 target => 'search'));
217 dpavlin 7 }
218    
219     sub show_results_list {
220     my $self = shift;
221    
222     my $q = $self->query();
223    
224     # load template for this page
225    
226     my @s_arr; # all queries are located here
227    
228 dpavlin 47 my @path_arr = $q->param('path');
229     my $full = $q->param('full');
230    
231 dpavlin 304 my @persist_vars = ( 'rm' );
232     my @url_params = ( 'rm=results', 'show_full=1', 'last_PAGER_offset='.($q->param('PAGER_offset') || 0) );
233 dpavlin 112
234 dpavlin 150 # support parametars "f" and "v" for start
235     for(my $i = ""; $i <=30; $i++) {
236 dpavlin 7
237 dpavlin 9 return show_index($self, $i) if ($q->param("f".$i."_index"));
238 dpavlin 112
239 dpavlin 71 next if (! $q->param("v$i"));
240 dpavlin 9 next if (! $q->param("f$i"));
241 dpavlin 7
242 dpavlin 112 push @persist_vars, "f$i";
243     push @persist_vars, "v$i";
244 dpavlin 186 push @persist_vars, "e$i" if ($q->param("e$i"));
245 dpavlin 112
246 dpavlin 126 push @url_params,"f$i=".$q->url_param("f$i");
247 dpavlin 158 foreach my $v ($q->url_param("v$i")) {
248     push @url_params,"v$i=$v";
249     }
250 dpavlin 183 push @url_params,"e$i=".$q->url_param("e$i");
251 dpavlin 126
252 dpavlin 150 my $wc="*"; # swish wildcard
253     $wc="" if ($i eq ""); # don't apply wildcard on field 0
254    
255 dpavlin 7 # re-write query from +/- to and/and not
256 dpavlin 73 my @param_vals = $q->param("v$i");
257     my @swish_q;
258 dpavlin 163 my ($pre,$post,$exact) = ('','','');
259 dpavlin 73 while (my $search = shift @param_vals) {
260     my $s;
261     # remove accents
262 dpavlin 320 $search = my_unac_string($CHARSET,$search);
263 dpavlin 73 while ($search =~ s/\s*("[^"]+")\s*/ /) {
264     $s .= "$1 ";
265     }
266     $search =~ s/^\s+//;
267     $search =~ s/\s+$//;
268 dpavlin 7
269 dpavlin 163 # filed e[nr] is exact match bitmask
270     # 1 = beginning, 2=end, 3=both
271     $pre = '"xxbxx ' if ($q->param("e$i") & 1);
272     $post = ' xxexx"' if ($q->param("e$i") & 2);
273     # add qotes on other side
274     if ($q->param("e$i")) {
275     $pre = '"' if (! $pre);
276     $post = '"' if (! $post);
277 dpavlin 190 # what about wildcards?
278     $wc = '';
279     $wc = '*' if ($q->param("e$i") & 4);
280 dpavlin 163 $exact = '_exact';
281     }
282    
283 dpavlin 73 foreach (split(/\s+/,$search)) {
284     if (m/^([+-])(\S+)/) {
285     $s.= ($s) ? "and " : "";
286     $s.="not " if ($1 eq "-");
287 dpavlin 163 $s.=$2.$wc." ";
288 dpavlin 122 } elsif (m/^\s*(and|or|not)\s*$/i) {
289 dpavlin 163 $s.=$_." ";
290 dpavlin 120 # don't add * to words with less than x chars
291     } elsif (length($_) <= $MIN_WILDCARD) {
292 dpavlin 163 $s.=$_." ";
293 dpavlin 73 } else {
294 dpavlin 163 $s.=$_.$wc." ";
295 dpavlin 73 }
296 dpavlin 7 }
297 dpavlin 73 $s =~ s/\*+/*/g;
298 dpavlin 163 $s = $pre.$s.$post if ($q->param("e$i"));
299 dpavlin 73 push @swish_q,$s;
300 dpavlin 7 }
301 dpavlin 73 # FIXME default operator for multi-value fields is or. There is
302     # no way to change it, except here for now. Is there need?
303 dpavlin 163 push @s_arr, $q->param("f$i")."_swish".$exact."=(".join(" or ",@swish_q).")";
304 dpavlin 7 }
305    
306 dpavlin 198 my $tmpl = $self->load_tmpl(url_ex($q,'results.html'), global_vars => 1);
307 dpavlin 9
308 dpavlin 304 $tmpl->param('url_params',"?".join("&",@url_params));
309    
310 dpavlin 80 sub esc_html {
311     my $html = shift;
312     $html =~ s/</&lt;/g;
313     $html =~ s/>/&gt;/g;
314     return $html;
315     }
316    
317 dpavlin 202 my $sort = 'swishrank';
318     if ($q->param("sort")) {
319     $sort = 'headline';
320     push @persist_vars, "sort";
321     }
322    
323 dpavlin 47 # construct swish query
324     my $sw_q = join(" and ",@s_arr);
325 dpavlin 111 if (@path_arr && $q->param('show_full')) {
326 dpavlin 47 $sw_q .= "and (swishdocpath=\"";
327     $sw_q .= join("\" or swishdocpath=\"",@path_arr);
328     $sw_q .= "\")";
329     $tmpl->param('full',1); # show full records
330 dpavlin 149 } elsif ($q->param('show_full')) {
331     # just show full path, no path defined
332     $tmpl->param('full',1);
333 dpavlin 121 } else {
334     $tmpl->param('full',0);
335 dpavlin 47 }
336 dpavlin 7
337 dpavlin 304 # create new swish instance
338     my $swish = SWISH::API->new($INDEX);
339     $swish->AbortLastError if $swish->Error;
340 dpavlin 47
341 dpavlin 304 # execute query and get number of results from SWISH-E
342     my $search = $swish->New_Search_Object;
343    
344     $search->SetSort($sort);
345    
346     my $results = $search->Execute($sw_q);
347     $swish->AbortLastError if $swish->Error;
348    
349     my $hits = $results->Hits;
350    
351 dpavlin 7 $tmpl->param('hits',$hits);
352 dpavlin 47 $tmpl->param('search',$sw_q);
353 dpavlin 7
354 dpavlin 76 $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);
355     $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);
356 dpavlin 51
357 dpavlin 304 #
358     # build pager
359     #
360 dpavlin 126
361 dpavlin 304 my $current_page = $q->param('PAGER_offset') || 1;
362 dpavlin 7
363 dpavlin 304 my $pager = Data::Pageset->new({
364     'total_entries' => $hits,
365     'entries_per_page' => $ON_PAGE,
366     'current_page' => $current_page,
367     'pages_per_set' => $pages_per_set,
368     });
369 dpavlin 7
370 dpavlin 304 $results->SeekResult( $pager->first - 1 );
371 dpavlin 7
372 dpavlin 304 # get number of entries on this page
373     my $i = $pager->entries_on_this_page;
374    
375     # results from swish for template
376     my @pager_data_list;
377    
378     for(my $i=$pager->first; $i<=$pager->last; $i++) {
379    
380     my $result = $results->NextResult;
381     last if (! $result);
382    
383     my $r = {
384     nr => $i,
385     path => $result->Property('swishdocpath'),
386     headline => esc_html($from_utf8->convert($result->Property('headline'))),
387     rank => $result->Property('swishrank')
388     };
389    
390     $r->{html} = back2html($from_utf8->convert($result->Property('html'))) if ($q->param('show_full'));
391    
392     push @pager_data_list, $r;
393     }
394    
395    
396    
397     # put something in template
398     make_pager($q, $tmpl, $pager);
399     make_pager_vars($q, $tmpl, @persist_vars);
400     $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
401    
402     my $html = $tmpl->output;
403    
404 dpavlin 198 return in_template($q,$html);
405 dpavlin 7 }
406    
407 dpavlin 9 sub show_index {
408     my $self = shift;
409     my $i = shift; # field number
410    
411     my $q = $self->query();
412    
413 dpavlin 11 my $field = $q->param("f$i");
414     my $limit = $q->param("v$i");
415    
416 dpavlin 9 my $html;
417    
418 dpavlin 53 my $index = new index_DBI(
419     $cfg_global->val('global', 'dbi_dbd'),
420     $cfg_global->val('global', 'dbi_dsn'),
421     $cfg_global->val('global', 'dbi_user'),
422     $cfg_global->val('global', 'dbi_passwd') || ''
423     );
424 dpavlin 9
425 dpavlin 140 my $total = $index->count($field,$limit);
426 dpavlin 304
427 dpavlin 12 if (! $total) {
428 dpavlin 198 my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html'));
429 dpavlin 11 $tmpl->param('field',$field);
430     $html = $tmpl->output;
431     return $html;
432     }
433 dpavlin 9
434 dpavlin 198 my $tmpl = $self->load_tmpl(url_ex($q,'index_res.html'), global_vars => 1);
435 dpavlin 12 $tmpl->param('field',$field);
436     $tmpl->param('limit',$limit);
437     $tmpl->param('total',$total);
438 dpavlin 11
439 dpavlin 51 # FIXME I should set offset and leave out limit from fetch!!
440 dpavlin 16 # if (! $q->param("PAGER_offset") {
441     # $q->param("Pager_offet)
442     # }
443    
444 dpavlin 11
445 dpavlin 304 #
446     # build pager
447     #
448     my $pager = Data::Pageset->new({
449     'total_entries' => $total,
450     'entries_per_page' => $ON_PAGE,
451     'current_page' => $q->param('PAGER_offset') || 1,
452     'pages_per_set' => $pages_per_set
453     });
454 dpavlin 11
455 dpavlin 304 my @persist_vars = qw{rm f$i v$i f$i_index offset};
456    
457     make_pager($q, $tmpl, $pager);
458     make_pager_vars($q, $tmpl, @persist_vars);
459    
460     my @pager_data_list = $index->fetch($field,$limit, $pager->first - 1, $pager->entries_on_this_page);
461     $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
462    
463     return in_template($q,$tmpl->output);
464 dpavlin 9 }
465    
466 dpavlin 7 1;

Properties

Name Value
cvs2svn:cvs-rev 1.40

  ViewVC Help
Powered by ViewVC 1.1.26