/[webpac]/branches/tehnika/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/tehnika/WebPac.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 305 - (hide annotations)
Sat Apr 17 20:50:56 2004 UTC (15 years, 9 months ago) by dpavlin
File size: 11635 byte(s)
updated branches to HEAD

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

Properties

Name Value
cvs2svn:cvs-rev 1.40

  ViewVC Help
Powered by ViewVC 1.1.26