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

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

revision 302 by dpavlin, Sun Apr 4 22:09:57 2004 UTC revision 675 by dpavlin, Mon Feb 21 22:21:53 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');  my $BASE_PATH =$cfg_global->val('webpac', 'base_path');
33    # for pager
34  if ($UNAC_FILTER) {  my $pages_per_set = $cfg_global->val('webpac', 'pages_per_set') || 10;
         require $UNAC_FILTER;  
 }  
35    
36  Text::Iconv->raise_error(0);     # Conversion errors raise exceptions  Text::Iconv->raise_error(0);     # Conversion errors raise exceptions
37    
38  my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);  my $from_utf8 = Text::Iconv->new('UTF8', $CHARSET);
39    
40    if ($UNAC_FILTER) {
41            require $UNAC_FILTER;
42    } else {
43            sub WebPac::my_unac_string {
44                    my ($charset, $string) = (@_);
45                    return $string;
46            }
47    }
48    
49  # use path from cgi script to support templates in subdirs  # use path from cgi script to support templates in subdirs
50  sub url_ex {  sub url_ex {
51          my $q = shift || die "suff2file needs CGI object!";          my $q = shift || die "suff2file needs CGI object!";
# Line 112  sub in_template { Line 119  sub in_template {
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            my $hidden_search = '';
186            foreach my $v (@persist_vars) {
187                    foreach my $val ($q->param($v)) {
188                            next if (! $val || $val eq '');
189                            $hidden_vars .= '<input type="hidden" name="'.$v.'" value="'.$val.'"/>'."\n";
190                            $hidden_search .= '<input type="hidden" name="'.$v.'" value="'.$val.'"/>'."\n" if ($v ne "rm");
191                    }
192            }
193    
194            $tmpl->param('PAGER_HIDDEN', $hidden_vars);
195            $tmpl->param('SEARCH_HIDDEN', $hidden_search);
196            $tmpl->param('PAGER_JAVASCRIPT', qq#
197    <SCRIPT LANGUAGE="Javascript">
198    <!-- Begin
199            // dummy emulator for HTML::Pager templates
200            function PAGER_set_offset_and_submit() {
201                    return true;
202            }
203    // End -->
204    </script>  
205            #);
206    }
207    
208    #--------------------------------------------------------------------------
209    
210  sub show_search_form {  sub show_search_form {
211          my $self = shift;          my $self = shift;
212    
# Line 132  sub show_results_list { Line 227  sub show_results_list {
227    
228          my $q = $self->query();          my $q = $self->query();
229    
         my @swish_results;      # results from swish  
   
230          # load template for this page          # load template for this page
231    
232          my @s_arr;      # all queries are located here          my @s_arr;      # all queries are located here
# Line 141  sub show_results_list { Line 234  sub show_results_list {
234          my @path_arr = $q->param('path');          my @path_arr = $q->param('path');
235          my $full = $q->param('full');          my $full = $q->param('full');
236    
237          my @persist_vars = ( 'rm' );          my @persist_vars = ( 'rm', 'persist_search' );
238          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) );
239    
240            my @persist_search_vars;
241            my @url_params_persist;
242            if ($q->param("persist_search")) {
243                    @persist_search_vars = split(/\s*,\s*/, $q->param("persist_search"));
244                    push @url_params_persist, "persist_search=".$q->url_param("persist_search");
245                    push @url_params,"persist_search=".$q->url_param("persist_search");
246            }
247    
248          # support parametars "f" and "v" for start          # support parametars "f" and "v" for start
249          for(my $i = ""; $i <=30; $i++) {          for(my $i = 0; $i <=30; $i++) {
250    
251                    $i = '' if ($i == 0);
252    
253                  return show_index($self, $i) if ($q->param("f".$i."_index"));                  return show_index($self, $i) if ($q->param("f".$i."_index"));
254    
255                  next if (! $q->param("v$i"));                  next if (! $q->param("v$i") || $q->param("v$i") eq '');
256                  next if (! $q->param("f$i"));                  next if (! $q->param("f$i"));
257    
258                    my $persist = grep(/^$i$/,@persist_search_vars);
259            
260                  push @persist_vars, "f$i";                  push @persist_vars, "f$i";
261                  push @persist_vars, "v$i";                  push @persist_vars, "v$i";
262                  push @persist_vars, "e$i" if ($q->param("e$i"));                  push @persist_vars, "e$i" if ($q->param("e$i"));
263    
264                    # create url parametars (and persistent ones)
265    
266                  push @url_params,"f$i=".$q->url_param("f$i");                  push @url_params,"f$i=".$q->url_param("f$i");
267                    push @url_params_persist,"f$i=".$q->url_param("f$i") if ($persist);
268    
269                  foreach my $v ($q->url_param("v$i")) {                  foreach my $v ($q->url_param("v$i")) {
270                          push @url_params,"v$i=$v";                          push @url_params,"v$i=$v";
271                            push @url_params_persist,"v$i=$v" if ($persist);
272                    }
273    
274                    if ($q->param("e$i")) {
275                            push @url_params,"e$i=".$q->url_param("e$i");
276    #                       push @url_params_persist,"e$i=".$q->url_param("e$i");
277                  }                  }
                 push @url_params,"e$i=".$q->url_param("e$i");  
278    
279                  my $wc="*";     # swish wildcard                  my $wc="*";     # swish wildcard
280                  $wc="" if ($i eq "");   # don't apply wildcard on field 0                  $wc="" if ($i eq "");   # don't apply wildcard on field 0
# Line 172  sub show_results_list { Line 286  sub show_results_list {
286                  while (my $search = shift @param_vals) {                  while (my $search = shift @param_vals) {
287                          my $s;                          my $s;
288                          # remove accents                          # remove accents
289                          $search = unac_string($CHARSET,$search);                          $search = my_unac_string($CHARSET,$search);
290                          while ($search =~ s/\s*("[^"]+")\s*/ /) {                          while ($search =~ s/\s*("[^"]+")\s*/ /) {
291                                  $s .= "$1 ";                                  $s .= "$1 ";
292                          }                          }
# Line 181  sub show_results_list { Line 295  sub show_results_list {
295    
296                          # filed e[nr] is exact match bitmask                          # filed e[nr] is exact match bitmask
297                          # 1 = beginning, 2=end, 3=both                          # 1 = beginning, 2=end, 3=both
298                          $pre = '"xxbxx ' if ($q->param("e$i") & 1);                          my $exact_flag = $q->param("e$i") || 0;
299                          $post = ' xxexx"' if ($q->param("e$i") & 2);                          $pre = '"xxbxx ' if ($exact_flag & 1);
300                            $post = ' xxexx"' if ($exact_flag & 2);
301                          # add qotes on other side                          # add qotes on other side
302                          if ($q->param("e$i")) {                          if ($q->param("e$i")) {
303                                  $pre = '"' if (! $pre);                                  $pre = '"' if (! $pre);
# Line 216  sub show_results_list { Line 331  sub show_results_list {
331                  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).")";
332          }          }
333    
334          my $tmpl = $self->load_tmpl(url_ex($q,'results.html'), global_vars => 1);          my $tmpl = $self->load_tmpl(url_ex($q,'results.html'), global_vars => 1, die_on_bad_params => 0);
335    
336            $tmpl->param('url_params',"?".join("&",@url_params));
337    
338          sub esc_html {          sub esc_html {
339                  my $html = shift;                  my $html = shift;
# Line 231  sub show_results_list { Line 348  sub show_results_list {
348                  push @persist_vars, "sort";                  push @persist_vars, "sort";
349          }          }
350    
351          # call swish          my $sortby = $q->param("sortby");
352          my $sh = SWISH->connect('Fork',          if ($sortby) {
353                  prog     => $SWISH,                  $sort = $sortby;
354                  indexes  => $INDEX,                  push @persist_vars, "sortby";
355                  properties  => [qw/swishdocpath swishrank swishtitle headline html/],          }
356                  results  => sub {          $tmpl->param('url_params_paths',"?".join("&",@url_params).'&'.join("&",map { my $t = $_; $t =~ s/\#/%23/g; "path=$t"; } @path_arr));
                         my ($sh,$hit) = @_;  
   
                         push @swish_results, {  
                                 nr => ($#swish_results + 2),  
                                 path => $hit->swishdocpath,  
                                 headline => esc_html($from_utf8->convert($hit->headline)),  
                                 html => back2html($from_utf8->convert($hit->html)),  
                                 rank => $hit->swishrank };  
   
                 },  
                 #startnum => 0,  
                 maxhits => $MAX_HITS,  
                 sortorder => $sort,  
         );  
357    
         die $SWISH::errstr unless $sh;  
358          # construct swish query          # construct swish query
359          my $sw_q = join(" and ",@s_arr);          my $sw_q = join(" and ",@s_arr);
360          if (@path_arr && $q->param('show_full')) {          if (@path_arr && $q->param('show_full')) {
# Line 267  sub show_results_list { Line 369  sub show_results_list {
369                  $tmpl->param('full',0);                  $tmpl->param('full',0);
370          }          }
371    
372          my $hits = $sh->query($sw_q);          my $swish_msg = ' ';
373    
374            # create new swish instance
375            my $swish = SWISH::API->new($INDEX);
376            $swish_msg .= $swish->ErrorString." ".$swish->LastErrorMsg if $swish->Error;
377    
378            # execute query and get number of results from SWISH-E
379            my $search = $swish->New_Search_Object;
380    
381            $search->SetSort($sort);
382    
383            my $results = $search->Execute($sw_q);
384            $swish_msg .= $swish->ErrorString." ".$swish->LastErrorMsg if $swish->Error;
385    
386            my $hits = $results->Hits;
387    
388          $tmpl->param('hits',$hits);          $tmpl->param('hits',$hits);
389          $tmpl->param('search',$sw_q);          my $search_msg = $sw_q;
390            $search_msg .= '<em>'.$swish_msg.'</em>' if ($swish_msg);
391            $tmpl->param('search', $search_msg);
392    
393          $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);          $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);
394          $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);          $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);
395    
396          $tmpl->param('url_params',"?".join("&",@url_params));          #
397            # build pager
398            #
399    
400          # create a Pager object          my $current_page = $q->param('PAGER_offset') || 1;
401          my $pager = HTML::Pager->new(  
402                  # required parameters          my $pager = Data::Pageset->new({
403                  query => $q,                  'total_entries' => $hits,
404                  get_data_callback => sub {                  'entries_per_page' => $ON_PAGE,
405                          my ($offset, $rows) = @_;                  'current_page' => $current_page,
406                    'pages_per_set' => $pages_per_set,
407                          my @result;          });
408                          for (my $i=0; $i<$rows; $i++) {  
409                                  my $r = $swish_results[$offset+$i];          $results->SeekResult( $pager->first - 1 );
                                 if ($r && $tmpl->param('full')) {  
                                         push @result, $r;  
                                 } elsif ($r) {  
                                         # if not full output, skip html  
                                         delete $r->{html};  
                                         push @result, $r;  
                                 }  
                         }  
                         return \@result;  
                 },  
                 rows => $hits,  
                 page_size => $ON_PAGE,  
                 # some optional parameters  
                 persist_vars => [ @persist_vars ],  
                 #cell_space_color => '#000000',  
                 #cell_background_color => '#ffffff',  
                 #nav_background_color => '#dddddd',  
                 #javascript_presubmit => 'last_minute_javascript()',  
                 debug => 1,  
                 template => $tmpl,  
         );  
410    
411          my $html = $pager->output;          # get number of entries on this page
412            my $i = $pager->entries_on_this_page;
413    
414            # results from swish for template
415            my @pager_data_list;
416    
417            for(my $i=$pager->first; $i<=$pager->last; $i++) {
418    
419                    my $result = $results->NextResult;
420                    last if (! $result);
421    
422                    my $r = {
423                            nr => $i,
424                            path => $result->Property('swishdocpath'),
425                            headline => esc_html($from_utf8->convert($result->Property('headline'))),
426                            rank => $result->Property('swishrank')
427                    };
428    
429                    $r->{html} = back2html($from_utf8->convert($result->Property('html')), join("&",@url_params_persist)) if ($q->param('show_full'));
430    
431                    push @pager_data_list, $r;
432            }
433    
434    
435    
436            # put something in template
437            make_pager($q, $tmpl, $pager);
438            make_pager_vars($q, $tmpl, @persist_vars);
439            $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
440    
441            my $html = $tmpl->output;
442    
443          return in_template($q,$html);          return in_template($q,$html);
444  }  }
# Line 323  sub show_index { Line 452  sub show_index {
452          my $field = $q->param("f$i");          my $field = $q->param("f$i");
453          my $limit = $q->param("v$i");          my $limit = $q->param("v$i");
454    
455            my $filter = $q->param("filter");
456    
457          my $html;          my $html;
458    
459          my $index = new index_DBI(          my $index = new index_DBI(
# Line 332  sub show_index { Line 463  sub show_index {
463                  $cfg_global->val('global', 'dbi_passwd') || ''                  $cfg_global->val('global', 'dbi_passwd') || ''
464          );          );
465    
466          my $total = $index->count($field,$limit);          my $total = $index->count($field,$limit,$filter);
467          if (! $total) {  
468            if (! defined($total)) {
469                  my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html'));                  my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html'));
470                  $tmpl->param('field',$field);                  $tmpl->param('field',$field);
471                  $html = $tmpl->output;                  $html = $tmpl->output;
472                  return $html;                  return $html;
473          }          }
474    
475          my $tmpl = $self->load_tmpl(url_ex($q,'index_res.html'), global_vars => 1);          my $tmpl = $self->load_tmpl(url_ex($q,'index_res.html'), global_vars => 1, die_on_bad_params => 0);
476          $tmpl->param('field',$field);          $tmpl->param('field',$field);
477          $tmpl->param('limit',$limit);          $tmpl->param('limit',$limit);
478          $tmpl->param('total',$total);          $tmpl->param('total',$total);
# Line 350  sub show_index { Line 482  sub show_index {
482  #               $q->param("Pager_offet)  #               $q->param("Pager_offet)
483  #       }  #       }
484    
         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,  
         );  
485    
486          return in_template($q,$pager->output);          #
487            # build pager
488            #
489            my $pager = Data::Pageset->new({
490                    'total_entries' => $total,
491                    'entries_per_page' => $ON_PAGE,
492                    'current_page' => $q->param('PAGER_offset') || 1,
493                    'pages_per_set' => $pages_per_set
494            });
495    
496            my @persist_vars = qw{rm f$i v$i f$i_index offset};
497    
498            make_pager($q, $tmpl, $pager);
499            make_pager_vars($q, $tmpl, @persist_vars);
500    
501            my @pager_data_list = $index->fetch($field,$limit, $pager->first - 1, $pager->entries_on_this_page, $filter);
502            $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
503    
504            return in_template($q,$tmpl->output);
505  }  }
506    
507  1;  1;

Legend:
Removed from v.302  
changed lines
  Added in v.675

  ViewVC Help
Powered by ViewVC 1.1.26