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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 736 - (hide annotations)
Wed May 24 13:35:42 2006 UTC (14 years, 6 months ago) by dpavlin
File size: 14702 byte(s)
updated branches to trunk

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

Properties

Name Value
cvs2svn:cvs-rev 1.40

  ViewVC Help
Powered by ViewVC 1.1.26