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

Diff of /branches/tehnika/WebPac.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/WebPac.pm revision 186 by dpavlin, Sat Nov 29 18:40:19 2003 UTC branches/tehnika/WebPac.pm revision 669 by dpavlin, Wed Feb 16 00:04:24 2005 UTC
# Line 3  package WebPac; Line 3  package WebPac;
3  use base 'CGI::Application';  use base 'CGI::Application';
4  use strict;  use strict;
5    
 use HTML::Pager;  
6  use HTML::FillInForm;  use HTML::FillInForm;
7  use SWISH;  use SWISH::API;
8  use Text::Iconv;  use Text::Iconv;
9  use DBI;  use DBI;
10  use Config::IniFiles;  use Config::IniFiles;
11  use Text::Unaccent;  use Text::Unaccent;
12    use Data::Pageset;
13    
14  use lib '..';  use lib '..';
15  use index_DBI_cache;  use index_DBI_filter;
16  use back2html;  use back2html;
17    
18    
# Line 28  my $MAX_HITS = $cfg_global->val('webpac' Line 28  my $MAX_HITS = $cfg_global->val('webpac'
28  my $ON_PAGE =$cfg_global->val('webpac', 'on_page') || 10;  my $ON_PAGE =$cfg_global->val('webpac', 'on_page') || 10;
29  my $MIN_WILDCARD =$cfg_global->val('webpac', 'min_wildcard') || 1;  my $MIN_WILDCARD =$cfg_global->val('webpac', 'min_wildcard') || 1;
30  my $TEMPLATE =$cfg_global->val('webpac', 'template');  my $TEMPLATE =$cfg_global->val('webpac', 'template');
31  my $UNAC_FILTER =$cfg_global->val('global', 'unac_filter');  my $UNAC_FILTER =$cfg_global->val('global', 'my_unac_filter');
32    my $BASE_PATH =$cfg_global->val('webpac', 'base_path');
33    # for pager
34    my $pages_per_set = $cfg_global->val('webpac', 'pages_per_set') || 10;
35    
36    Text::Iconv->raise_error(0);     # Conversion errors raise exceptions
37    
38    my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
39    
40  if ($UNAC_FILTER) {  if ($UNAC_FILTER) {
41          require $UNAC_FILTER;          require $UNAC_FILTER;
42    } else {
43            sub WebPac::my_unac_string {
44                    my ($charset, $string) = (@_);
45                    return $string;
46            }
47  }  }
48    
49  Text::Iconv->raise_error(0);     # Conversion errors raise exceptions  # 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            return suff2file($BASE_PATH, $q->url(-absolute => 1,-path => 1),$TEMPLATE_PATH,$tpl);
54    }
55    
56  my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);  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            if ($base_path eq "/") {
64                    $p =~ s,/*,,g;
65                    my ($name,$ext) = split(/\./,$tpl);
66                    $p = $name . "-" . $p . "." . $ext;
67            } elsif ($p =~ s,^.*?$base_path,,) {
68                    $p =~ s,/*,,g;
69                    my ($name,$ext) = split(/\./,$tpl);
70                    $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  sub setup {  sub setup {
85          my $self = shift;          my $self = shift;
# Line 55  sub setup { Line 97  sub setup {
97  }  }
98    
99  sub in_template {  sub in_template {
100          my $html = shift || "This page is left unintentionally blank";          my $q = shift || die "need CGI object!";
101            my $html = shift || die "This page is left unintentionally blank";
102          return $html if (! defined($TEMPLATE));          return $html if (! defined($TEMPLATE));
103          if (open(T, $TEMPLATE)) {  
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            my $master_tpl = suff2file($BASE_PATH, $q->url(-absolute => 1, -path => 1),$dir,$tpl);
112            if (open(T, $master_tpl)) {
113                  my $template_html = join("\n",<T>);                  my $template_html = join("\n",<T>);
114                  close(T);                  close(T);
115                  $template_html =~ s/##webpac##/$html/gsi;                  $template_html =~ s/##webpac##/$html/gsi;
116                  return $template_html;                  return $template_html;
117          } else {          } else {
118                  return "Can't read template '$TEMPLATE'";                  return "Can't read template '$master_tpl'";
119          }          }
120  }  }
121    
122    #--------------------------------------------------------------------------
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                    foreach my $val ($q->param($v)) {
187                            next if (! $val || $val eq '');
188                            $hidden_vars .= '<input type="hidden" name="'.$v.'" value="'.$val.'"/>'."\n";
189                    }
190            }
191    
192            $tmpl->param('PAGER_HIDDEN', $hidden_vars);
193            $tmpl->param('PAGER_JAVASCRIPT', qq#
194    <SCRIPT LANGUAGE="Javascript">
195    <!-- Begin
196            // dummy emulator for HTML::Pager templates
197            function PAGER_set_offset_and_submit() {
198                    return true;
199            }
200    // End -->
201    </script>  
202            #);
203    }
204    
205    #--------------------------------------------------------------------------
206    
207  sub show_search_form {  sub show_search_form {
208          my $self = shift;          my $self = shift;
209    
210          # Get the CGI.pm query object          # Get the CGI.pm query object
211          my $q = $self->query();          my $q = $self->query();
212    
213          my $tmpl = $self->load_tmpl('search.html');          my $tmpl = $self->load_tmpl(url_ex($q,'search.html'));
214          my $html = $tmpl->output;          my $html = $tmpl->output;
215    
216          my $fif = new HTML::FillInForm;          my $fif = new HTML::FillInForm;
217    
218          return in_template($fif->fill(scalarref => \$html, fobject => $q,          return in_template($q,$fif->fill(scalarref => \$html, fobject => $q,
219                  target => 'search'));                  target => 'search'));
220  }  }
221    
# Line 87  sub show_results_list { Line 224  sub show_results_list {
224    
225          my $q = $self->query();          my $q = $self->query();
226    
         my @swish_results;      # results from swish  
   
227          # load template for this page          # load template for this page
228    
229          my @s_arr;      # all queries are located here          my @s_arr;      # all queries are located here
# Line 96  sub show_results_list { Line 231  sub show_results_list {
231          my @path_arr = $q->param('path');          my @path_arr = $q->param('path');
232          my $full = $q->param('full');          my $full = $q->param('full');
233    
234          my @persist_vars = ( 'rm' );          my @persist_vars = ( 'rm', 'persist_search' );
235          my @url_params = ( 'rm=results', 'show_full=1', 'last_PAGER_offset='.$q->param('PAGER_offset') || 0 );          my @url_params = ( 'rm=results', 'show_full=1', 'last_PAGER_offset='.($q->param('PAGER_offset') || 0) );
236    
237            my @persist_search_vars;
238            my @url_params_persist;
239            if ($q->param("persist_search")) {
240                    @persist_search_vars = split(/\s*,\s*/, $q->param("persist_search"));
241                    push @url_params_persist, "persist_search=".$q->url_param("persist_search");
242                    push @url_params,"persist_search=".$q->url_param("persist_search");
243            }
244    
245          # support parametars "f" and "v" for start          # support parametars "f" and "v" for start
246          for(my $i = ""; $i <=30; $i++) {          for(my $i = 0; $i <=30; $i++) {
247    
248                    $i = '' if ($i == 0);
249    
250                  return show_index($self, $i) if ($q->param("f".$i."_index"));                  return show_index($self, $i) if ($q->param("f".$i."_index"));
251    
252                  next if (! $q->param("v$i"));                  next if (! $q->param("v$i") || $q->param("v$i") eq '');
253                  next if (! $q->param("f$i"));                  next if (! $q->param("f$i"));
254    
255                    my $persist = grep(/^$i$/,@persist_search_vars);
256            
257                  push @persist_vars, "f$i";                  push @persist_vars, "f$i";
258                  push @persist_vars, "v$i";                  push @persist_vars, "v$i";
259                  push @persist_vars, "e$i" if ($q->param("e$i"));                  push @persist_vars, "e$i" if ($q->param("e$i"));
260    
261                    # create url parametars (and persistent ones)
262    
263                  push @url_params,"f$i=".$q->url_param("f$i");                  push @url_params,"f$i=".$q->url_param("f$i");
264                    push @url_params_persist,"f$i=".$q->url_param("f$i") if ($persist);
265    
266                  foreach my $v ($q->url_param("v$i")) {                  foreach my $v ($q->url_param("v$i")) {
267                          push @url_params,"v$i=$v";                          push @url_params,"v$i=$v";
268                            push @url_params_persist,"v$i=$v" if ($persist);
269                    }
270    
271                    if ($q->param("e$i")) {
272                            push @url_params,"e$i=".$q->url_param("e$i");
273    #                       push @url_params_persist,"e$i=".$q->url_param("e$i");
274                  }                  }
                 push @url_params,"e$i=".$q->url_param("e$i");  
275    
276                  my $wc="*";     # swish wildcard                  my $wc="*";     # swish wildcard
277                  $wc="" if ($i eq "");   # don't apply wildcard on field 0                  $wc="" if ($i eq "");   # don't apply wildcard on field 0
# Line 127  sub show_results_list { Line 283  sub show_results_list {
283                  while (my $search = shift @param_vals) {                  while (my $search = shift @param_vals) {
284                          my $s;                          my $s;
285                          # remove accents                          # remove accents
286                          $search = unac_string($CHARSET,$search);                          $search = my_unac_string($CHARSET,$search);
287                          while ($search =~ s/\s*("[^"]+")\s*/ /) {                          while ($search =~ s/\s*("[^"]+")\s*/ /) {
288                                  $s .= "$1 ";                                  $s .= "$1 ";
289                          }                          }
# Line 136  sub show_results_list { Line 292  sub show_results_list {
292    
293                          # filed e[nr] is exact match bitmask                          # filed e[nr] is exact match bitmask
294                          # 1 = beginning, 2=end, 3=both                          # 1 = beginning, 2=end, 3=both
295                          $pre = '"xxbxx ' if ($q->param("e$i") & 1);                          my $exact_flag = $q->param("e$i") || 0;
296                          $post = ' xxexx"' if ($q->param("e$i") & 2);                          $pre = '"xxbxx ' if ($exact_flag & 1);
297                            $post = ' xxexx"' if ($exact_flag & 2);
298                          # add qotes on other side                          # add qotes on other side
299                          if ($q->param("e$i")) {                          if ($q->param("e$i")) {
300                                  $pre = '"' if (! $pre);                                  $pre = '"' if (! $pre);
301                                  $post = '"' if (! $post);                                  $post = '"' if (! $post);
302                                  $wc = '';       # don't use windcard in exact                                  # what about wildcards?
303                                    $wc = '';
304                                    $wc = '*' if ($q->param("e$i") & 4);
305                                  $exact = '_exact';                                  $exact = '_exact';
306                          }                          }
307    
# Line 169  sub show_results_list { Line 328  sub show_results_list {
328                  push @s_arr, $q->param("f$i")."_swish".$exact."=(".join(" or ",@swish_q).")";                  push @s_arr, $q->param("f$i")."_swish".$exact."=(".join(" or ",@swish_q).")";
329          }          }
330    
331          my $tmpl = $self->load_tmpl('results.html', global_vars => 1);          my $tmpl = $self->load_tmpl(url_ex($q,'results.html'), global_vars => 1);
332    
333            $tmpl->param('url_params',"?".join("&",@url_params));
334    
335          sub esc_html {          sub esc_html {
336                  my $html = shift;                  my $html = shift;
# Line 178  sub show_results_list { Line 339  sub show_results_list {
339                  return $html;                  return $html;
340          }          }
341    
342          # call swish          my $sort = 'swishrank';
343          my $sh = SWISH->connect('Fork',          if ($q->param("sort")) {
344                  prog     => $SWISH,                  $sort = 'headline';
345                  indexes  => $INDEX,                  push @persist_vars, "sort";
346                  properties  => [qw/swishdocpath swishrank swishtitle headline html/],          }
347                  results  => sub {  
348                          my ($sh,$hit) = @_;          my $sortby = $q->param("sortby");
349            if ($sortby) {
350                          push @swish_results, {                  $sort = $sortby;
351                                  nr => ($#swish_results + 2),                  push @persist_vars, "sortby";
352                                  path => $hit->swishdocpath,          }
353                                  headline => esc_html($from_utf8->convert($hit->headline)),          $tmpl->param('url_params_paths',"?".join("&",@url_params).'&'.join("&",map { my $t = $_; $t =~ s/\#/%23/g; "path=$t"; } @path_arr));
                                 html => back2html($from_utf8->convert($hit->html)),  
                                 rank => $hit->swishrank };  
   
                 },  
                 #startnum => 0,  
                 maxhits => $MAX_HITS  
         );  
354    
         die $SWISH::errstr unless $sh;  
355          # construct swish query          # construct swish query
356          my $sw_q = join(" and ",@s_arr);          my $sw_q = join(" and ",@s_arr);
357          if (@path_arr && $q->param('show_full')) {          if (@path_arr && $q->param('show_full')) {
# Line 213  sub show_results_list { Line 366  sub show_results_list {
366                  $tmpl->param('full',0);                  $tmpl->param('full',0);
367          }          }
368    
369          my $hits = $sh->query($sw_q);          my $swish_msg = ' ';
370    
371            # create new swish instance
372            my $swish = SWISH::API->new($INDEX);
373            $swish_msg .= $swish->ErrorString." ".$swish->LastErrorMsg if $swish->Error;
374    
375            # execute query and get number of results from SWISH-E
376            my $search = $swish->New_Search_Object;
377    
378            $search->SetSort($sort);
379    
380            my $results = $search->Execute($sw_q);
381            $swish_msg .= $swish->ErrorString." ".$swish->LastErrorMsg if $swish->Error;
382    
383            my $hits = $results->Hits;
384    
385          $tmpl->param('hits',$hits);          $tmpl->param('hits',$hits);
386          $tmpl->param('search',$sw_q);          my $search_msg = $sw_q;
387            $search_msg .= '<em>'.$swish_msg.'</em>' if ($swish_msg);
388            $tmpl->param('search', $search_msg);
389    
390          $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);          $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);
391          $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);          $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);
392    
393          $tmpl->param('url_params',"?".join("&",@url_params));          #
394            # build pager
395            #
396    
397          # create a Pager object          my $current_page = $q->param('PAGER_offset') || 1;
398          my $pager = HTML::Pager->new(  
399                  # required parameters          my $pager = Data::Pageset->new({
400                  query => $q,                  'total_entries' => $hits,
401                  get_data_callback => sub {                  'entries_per_page' => $ON_PAGE,
402                          my ($offset, $rows) = @_;                  'current_page' => $current_page,
403                    'pages_per_set' => $pages_per_set,
404                          my @result;          });
405                          for (my $i=0; $i<$rows; $i++) {  
406                                  my $r = $swish_results[$offset+$i];          $results->SeekResult( $pager->first - 1 );
407                                  if ($r && $tmpl->param('full')) {  
408                                          push @result, $r;          # get number of entries on this page
409                                  } elsif ($r) {          my $i = $pager->entries_on_this_page;
410                                          # if not full output, skip html  
411                                          delete $r->{html};          # results from swish for template
412                                          push @result, $r;          my @pager_data_list;
413                                  }  
414                          }          for(my $i=$pager->first; $i<=$pager->last; $i++) {
415                          return \@result;  
416                  },                  my $result = $results->NextResult;
417                  rows => $hits,                  last if (! $result);
418                  page_size => $ON_PAGE,  
419                  # some optional parameters                  my $r = {
420                  persist_vars => [ @persist_vars ],                          nr => $i,
421                  #cell_space_color => '#000000',                          path => $result->Property('swishdocpath'),
422                  #cell_background_color => '#ffffff',                          headline => esc_html($from_utf8->convert($result->Property('headline'))),
423                  #nav_background_color => '#dddddd',                          rank => $result->Property('swishrank')
424                  #javascript_presubmit => 'last_minute_javascript()',                  };
425                  debug => 1,  
426                  template => $tmpl,                  $r->{html} = back2html($from_utf8->convert($result->Property('html')), join("&",@url_params_persist)) if ($q->param('show_full'));
427          );  
428                    push @pager_data_list, $r;
429            }
430    
         my $html = $pager->output;  
431    
432          return in_template($html);  
433            # put something in template
434            make_pager($q, $tmpl, $pager);
435            make_pager_vars($q, $tmpl, @persist_vars);
436            $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
437    
438            my $html = $tmpl->output;
439    
440            return in_template($q,$html);
441  }  }
442    
443  sub show_index {  sub show_index {
# Line 269  sub show_index { Line 449  sub show_index {
449          my $field = $q->param("f$i");          my $field = $q->param("f$i");
450          my $limit = $q->param("v$i");          my $limit = $q->param("v$i");
451    
452            my $filter = $q->param("filter");
453    
454          my $html;          my $html;
455    
456          my $index = new index_DBI(          my $index = new index_DBI(
# Line 278  sub show_index { Line 460  sub show_index {
460                  $cfg_global->val('global', 'dbi_passwd') || ''                  $cfg_global->val('global', 'dbi_passwd') || ''
461          );          );
462    
463          my $total = $index->count($field,$limit);          my $total = $index->count($field,$limit,$filter);
464          if (! $total) {  
465                  my $tmpl = $self->load_tmpl('no_index.html');          if (! defined($total)) {
466                    my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html'));
467                  $tmpl->param('field',$field);                  $tmpl->param('field',$field);
468                  $html = $tmpl->output;                  $html = $tmpl->output;
469                  return $html;                  return $html;
470          }          }
471    
472          my $tmpl = $self->load_tmpl('index_res.html', global_vars => 1);          my $tmpl = $self->load_tmpl(url_ex($q,'index_res.html'), global_vars => 1);
473          $tmpl->param('field',$field);          $tmpl->param('field',$field);
474          $tmpl->param('limit',$limit);          $tmpl->param('limit',$limit);
475          $tmpl->param('total',$total);          $tmpl->param('total',$total);
# Line 296  sub show_index { Line 479  sub show_index {
479  #               $q->param("Pager_offet)  #               $q->param("Pager_offet)
480  #       }  #       }
481    
         my $pager = HTML::Pager->new(  
                 query => $q,  
                 get_data_callback => sub {  
                         my ($offset, $rows) = @_;  
   
                         my @result = $index->fetch($field,$limit, $offset, $rows);  
                         return \@result;  
                 },  
                 rows => $total,  
                 page_size => $ON_PAGE,  
                 persist_vars => [  
                         'rm',  
                         "f$i", "v$i", "f".$i."_index",  
                         'offset',  
                         ],  
                 debug => 1,  
                 template => $tmpl,  
         );  
482    
483          return in_template($pager->output);          #
484            # build pager
485            #
486            my $pager = Data::Pageset->new({
487                    'total_entries' => $total,
488                    'entries_per_page' => $ON_PAGE,
489                    'current_page' => $q->param('PAGER_offset') || 1,
490                    'pages_per_set' => $pages_per_set
491            });
492    
493            my @persist_vars = qw{rm f$i v$i f$i_index offset};
494    
495            make_pager($q, $tmpl, $pager);
496            make_pager_vars($q, $tmpl, @persist_vars);
497    
498            my @pager_data_list = $index->fetch($field,$limit, $pager->first - 1, $pager->entries_on_this_page, $filter);
499            $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
500    
501            return in_template($q,$tmpl->output);
502  }  }
503    
504  1;  1;

Legend:
Removed from v.186  
changed lines
  Added in v.669

  ViewVC Help
Powered by ViewVC 1.1.26