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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 710 - (hide annotations)
Sun Sep 11 18:45:50 2005 UTC (14 years, 4 months ago) by dpavlin
File size: 14441 byte(s)
updated branches to head (added cook_url_params)

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

Properties

Name Value
cvs2svn:cvs-rev 1.40

  ViewVC Help
Powered by ViewVC 1.1.26