/[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 687 - (hide annotations)
Tue Mar 1 19:32:23 2005 UTC (14 years, 10 months ago) by dpavlin
File size: 13500 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 652 use index_DBI_filter;
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 321 my $UNAC_FILTER =$cfg_global->val('global', 'my_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 321 Text::Iconv->raise_error(0); # Conversion errors raise exceptions
37 dpavlin 305
38 dpavlin 321 my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
39    
40 dpavlin 164 if ($UNAC_FILTER) {
41     require $UNAC_FILTER;
42 dpavlin 321 } 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 303 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 303 if ($base_path eq "/") {
64 dpavlin 198 $p =~ s,/*,,g;
65     my ($name,$ext) = split(/\./,$tpl);
66 dpavlin 303 $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 303 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 305 #--------------------------------------------------------------------------
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 dpavlin 671 next if ($p <= 0);
152 dpavlin 305 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 dpavlin 684 my $hidden_search = '';
186 dpavlin 305 foreach my $v (@persist_vars) {
187 dpavlin 458 foreach my $val ($q->param($v)) {
188 dpavlin 637 next if (! $val || $val eq '');
189 dpavlin 687 $val =~ s/"/&quote;/g;
190 dpavlin 458 $hidden_vars .= '<input type="hidden" name="'.$v.'" value="'.$val.'"/>'."\n";
191 dpavlin 684 $hidden_search .= '<input type="hidden" name="'.$v.'" value="'.$val.'"/>'."\n" if ($v ne "rm");
192 dpavlin 458 }
193 dpavlin 305 }
194    
195     $tmpl->param('PAGER_HIDDEN', $hidden_vars);
196 dpavlin 684 $tmpl->param('SEARCH_HIDDEN', $hidden_search);
197 dpavlin 637 $tmpl->param('PAGER_JAVASCRIPT', qq#
198 dpavlin 305 <SCRIPT LANGUAGE="Javascript">
199     <!-- Begin
200     // dummy emulator for HTML::Pager templates
201     function PAGER_set_offset_and_submit() {
202     return true;
203     }
204     // End -->
205     </script>
206 dpavlin 637 #);
207 dpavlin 305 }
208    
209     #--------------------------------------------------------------------------
210    
211 dpavlin 7 sub show_search_form {
212     my $self = shift;
213    
214     # Get the CGI.pm query object
215     my $q = $self->query();
216    
217 dpavlin 198 my $tmpl = $self->load_tmpl(url_ex($q,'search.html'));
218 dpavlin 7 my $html = $tmpl->output;
219    
220     my $fif = new HTML::FillInForm;
221    
222 dpavlin 198 return in_template($q,$fif->fill(scalarref => \$html, fobject => $q,
223 dpavlin 147 target => 'search'));
224 dpavlin 7 }
225    
226     sub show_results_list {
227     my $self = shift;
228    
229     my $q = $self->query();
230    
231 dpavlin 684 # submit was reset?
232     if ($q->param('reset')) {
233     $q->delete_all;
234     return $self->show_search_form();
235     }
236    
237 dpavlin 7 # load template for this page
238    
239     my @s_arr; # all queries are located here
240    
241 dpavlin 47 my @path_arr = $q->param('path');
242     my $full = $q->param('full');
243    
244 dpavlin 637 my @persist_vars = ( 'rm', 'persist_search' );
245 dpavlin 305 my @url_params = ( 'rm=results', 'show_full=1', 'last_PAGER_offset='.($q->param('PAGER_offset') || 0) );
246 dpavlin 112
247 dpavlin 637 my @persist_search_vars;
248     my @url_params_persist;
249     if ($q->param("persist_search")) {
250     @persist_search_vars = split(/\s*,\s*/, $q->param("persist_search"));
251     push @url_params_persist, "persist_search=".$q->url_param("persist_search");
252     push @url_params,"persist_search=".$q->url_param("persist_search");
253     }
254    
255 dpavlin 150 # support parametars "f" and "v" for start
256 dpavlin 637 for(my $i = 0; $i <=30; $i++) {
257 dpavlin 7
258 dpavlin 637 $i = '' if ($i == 0);
259    
260 dpavlin 9 return show_index($self, $i) if ($q->param("f".$i."_index"));
261 dpavlin 112
262 dpavlin 637 next if (! $q->param("v$i") || $q->param("v$i") eq '');
263 dpavlin 9 next if (! $q->param("f$i"));
264 dpavlin 7
265 dpavlin 637 my $persist = grep(/^$i$/,@persist_search_vars);
266    
267 dpavlin 112 push @persist_vars, "f$i";
268     push @persist_vars, "v$i";
269 dpavlin 186 push @persist_vars, "e$i" if ($q->param("e$i"));
270 dpavlin 112
271 dpavlin 637 # create url parametars (and persistent ones)
272    
273 dpavlin 126 push @url_params,"f$i=".$q->url_param("f$i");
274 dpavlin 637 push @url_params_persist,"f$i=".$q->url_param("f$i") if ($persist);
275    
276 dpavlin 158 foreach my $v ($q->url_param("v$i")) {
277 dpavlin 684 # escape quotes so that phrase search work
278     $v =~ s/"/%22/g;
279 dpavlin 158 push @url_params,"v$i=$v";
280 dpavlin 637 push @url_params_persist,"v$i=$v" if ($persist);
281 dpavlin 158 }
282 dpavlin 126
283 dpavlin 637 if ($q->param("e$i")) {
284     push @url_params,"e$i=".$q->url_param("e$i");
285 dpavlin 640 # push @url_params_persist,"e$i=".$q->url_param("e$i");
286 dpavlin 637 }
287    
288 dpavlin 150 my $wc="*"; # swish wildcard
289     $wc="" if ($i eq ""); # don't apply wildcard on field 0
290    
291 dpavlin 7 # re-write query from +/- to and/and not
292 dpavlin 73 my @param_vals = $q->param("v$i");
293     my @swish_q;
294 dpavlin 163 my ($pre,$post,$exact) = ('','','');
295 dpavlin 73 while (my $search = shift @param_vals) {
296     my $s;
297     # remove accents
298 dpavlin 321 $search = my_unac_string($CHARSET,$search);
299 dpavlin 73 while ($search =~ s/\s*("[^"]+")\s*/ /) {
300     $s .= "$1 ";
301     }
302     $search =~ s/^\s+//;
303     $search =~ s/\s+$//;
304 dpavlin 7
305 dpavlin 163 # filed e[nr] is exact match bitmask
306     # 1 = beginning, 2=end, 3=both
307 dpavlin 637 my $exact_flag = $q->param("e$i") || 0;
308     $pre = '"xxbxx ' if ($exact_flag & 1);
309     $post = ' xxexx"' if ($exact_flag & 2);
310 dpavlin 163 # add qotes on other side
311     if ($q->param("e$i")) {
312     $pre = '"' if (! $pre);
313     $post = '"' if (! $post);
314 dpavlin 190 # what about wildcards?
315     $wc = '';
316     $wc = '*' if ($q->param("e$i") & 4);
317 dpavlin 163 $exact = '_exact';
318     }
319    
320 dpavlin 73 foreach (split(/\s+/,$search)) {
321     if (m/^([+-])(\S+)/) {
322     $s.= ($s) ? "and " : "";
323     $s.="not " if ($1 eq "-");
324 dpavlin 163 $s.=$2.$wc." ";
325 dpavlin 122 } elsif (m/^\s*(and|or|not)\s*$/i) {
326 dpavlin 163 $s.=$_." ";
327 dpavlin 120 # don't add * to words with less than x chars
328     } elsif (length($_) <= $MIN_WILDCARD) {
329 dpavlin 163 $s.=$_." ";
330 dpavlin 73 } else {
331 dpavlin 163 $s.=$_.$wc." ";
332 dpavlin 73 }
333 dpavlin 7 }
334 dpavlin 73 $s =~ s/\*+/*/g;
335 dpavlin 163 $s = $pre.$s.$post if ($q->param("e$i"));
336 dpavlin 73 push @swish_q,$s;
337 dpavlin 7 }
338 dpavlin 73 # FIXME default operator for multi-value fields is or. There is
339     # no way to change it, except here for now. Is there need?
340 dpavlin 163 push @s_arr, $q->param("f$i")."_swish".$exact."=(".join(" or ",@swish_q).")";
341 dpavlin 7 }
342    
343 dpavlin 671 my $tmpl = $self->load_tmpl(url_ex($q,'results.html'), global_vars => 1, die_on_bad_params => 0);
344 dpavlin 9
345 dpavlin 305 $tmpl->param('url_params',"?".join("&",@url_params));
346    
347 dpavlin 80 sub esc_html {
348     my $html = shift;
349     $html =~ s/</&lt;/g;
350     $html =~ s/>/&gt;/g;
351     return $html;
352     }
353    
354 dpavlin 202 my $sort = 'swishrank';
355     if ($q->param("sort")) {
356     $sort = 'headline';
357     push @persist_vars, "sort";
358     }
359    
360 dpavlin 669 my $sortby = $q->param("sortby");
361     if ($sortby) {
362     $sort = $sortby;
363     push @persist_vars, "sortby";
364     }
365     $tmpl->param('url_params_paths',"?".join("&",@url_params).'&'.join("&",map { my $t = $_; $t =~ s/\#/%23/g; "path=$t"; } @path_arr));
366    
367 dpavlin 47 # construct swish query
368     my $sw_q = join(" and ",@s_arr);
369 dpavlin 637 if (@path_arr && $q->param('show_full')) {
370 dpavlin 47 $sw_q .= "and (swishdocpath=\"";
371     $sw_q .= join("\" or swishdocpath=\"",@path_arr);
372     $sw_q .= "\")";
373 dpavlin 637 $tmpl->param('full',1); # show full records
374     } elsif ($q->param('show_full')) {
375 dpavlin 149 # just show full path, no path defined
376     $tmpl->param('full',1);
377 dpavlin 121 } else {
378     $tmpl->param('full',0);
379 dpavlin 47 }
380 dpavlin 7
381 dpavlin 669 my $swish_msg = ' ';
382    
383 dpavlin 305 # create new swish instance
384     my $swish = SWISH::API->new($INDEX);
385 dpavlin 669 $swish_msg .= $swish->ErrorString." ".$swish->LastErrorMsg if $swish->Error;
386 dpavlin 47
387 dpavlin 305 # execute query and get number of results from SWISH-E
388     my $search = $swish->New_Search_Object;
389    
390     $search->SetSort($sort);
391    
392     my $results = $search->Execute($sw_q);
393 dpavlin 669 $swish_msg .= $swish->ErrorString." ".$swish->LastErrorMsg if $swish->Error;
394 dpavlin 305
395     my $hits = $results->Hits;
396    
397 dpavlin 7 $tmpl->param('hits',$hits);
398 dpavlin 669 my $search_msg = $sw_q;
399     $search_msg .= '<em>'.$swish_msg.'</em>' if ($swish_msg);
400     $tmpl->param('search', $search_msg);
401 dpavlin 7
402 dpavlin 76 $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);
403     $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);
404 dpavlin 51
405 dpavlin 305 #
406     # build pager
407     #
408 dpavlin 126
409 dpavlin 305 my $current_page = $q->param('PAGER_offset') || 1;
410 dpavlin 7
411 dpavlin 305 my $pager = Data::Pageset->new({
412     'total_entries' => $hits,
413     'entries_per_page' => $ON_PAGE,
414     'current_page' => $current_page,
415     'pages_per_set' => $pages_per_set,
416     });
417 dpavlin 7
418 dpavlin 305 $results->SeekResult( $pager->first - 1 );
419 dpavlin 7
420 dpavlin 305 # get number of entries on this page
421     my $i = $pager->entries_on_this_page;
422    
423     # results from swish for template
424     my @pager_data_list;
425    
426     for(my $i=$pager->first; $i<=$pager->last; $i++) {
427    
428     my $result = $results->NextResult;
429     last if (! $result);
430    
431     my $r = {
432     nr => $i,
433     path => $result->Property('swishdocpath'),
434     headline => esc_html($from_utf8->convert($result->Property('headline'))),
435     rank => $result->Property('swishrank')
436     };
437    
438 dpavlin 637 $r->{html} = back2html($from_utf8->convert($result->Property('html')), join("&",@url_params_persist)) if ($q->param('show_full'));
439 dpavlin 305
440     push @pager_data_list, $r;
441     }
442    
443    
444    
445     # put something in template
446     make_pager($q, $tmpl, $pager);
447     make_pager_vars($q, $tmpl, @persist_vars);
448     $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
449    
450     my $html = $tmpl->output;
451    
452 dpavlin 198 return in_template($q,$html);
453 dpavlin 7 }
454    
455 dpavlin 9 sub show_index {
456     my $self = shift;
457     my $i = shift; # field number
458    
459     my $q = $self->query();
460    
461 dpavlin 11 my $field = $q->param("f$i");
462     my $limit = $q->param("v$i");
463    
464 dpavlin 652 my $filter = $q->param("filter");
465    
466 dpavlin 9 my $html;
467    
468 dpavlin 53 my $index = new index_DBI(
469     $cfg_global->val('global', 'dbi_dbd'),
470     $cfg_global->val('global', 'dbi_dsn'),
471     $cfg_global->val('global', 'dbi_user'),
472     $cfg_global->val('global', 'dbi_passwd') || ''
473     );
474 dpavlin 9
475 dpavlin 652 my $total = $index->count($field,$limit,$filter);
476 dpavlin 305
477 dpavlin 652 if (! defined($total)) {
478 dpavlin 198 my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html'));
479 dpavlin 11 $tmpl->param('field',$field);
480     $html = $tmpl->output;
481     return $html;
482     }
483 dpavlin 9
484 dpavlin 671 my $tmpl = $self->load_tmpl(url_ex($q,'index_res.html'), global_vars => 1, die_on_bad_params => 0);
485 dpavlin 12 $tmpl->param('field',$field);
486     $tmpl->param('limit',$limit);
487     $tmpl->param('total',$total);
488 dpavlin 687 $tmpl->param('filter',$filter);
489 dpavlin 11
490 dpavlin 51 # FIXME I should set offset and leave out limit from fetch!!
491 dpavlin 16 # if (! $q->param("PAGER_offset") {
492     # $q->param("Pager_offet)
493     # }
494    
495 dpavlin 11
496 dpavlin 305 #
497     # build pager
498     #
499     my $pager = Data::Pageset->new({
500     'total_entries' => $total,
501     'entries_per_page' => $ON_PAGE,
502     'current_page' => $q->param('PAGER_offset') || 1,
503     'pages_per_set' => $pages_per_set
504     });
505 dpavlin 11
506 dpavlin 305 my @persist_vars = qw{rm f$i v$i f$i_index offset};
507    
508     make_pager($q, $tmpl, $pager);
509     make_pager_vars($q, $tmpl, @persist_vars);
510    
511 dpavlin 652 my @pager_data_list = $index->fetch($field,$limit, $pager->first - 1, $pager->entries_on_this_page, $filter);
512 dpavlin 305 $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
513    
514     return in_template($q,$tmpl->output);
515 dpavlin 9 }
516    
517 dpavlin 7 1;

Properties

Name Value
cvs2svn:cvs-rev 1.40

  ViewVC Help
Powered by ViewVC 1.1.26