/[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 198 by dpavlin, Sun Dec 21 06:35:43 2003 UTC revision 304 by dpavlin, Sat Apr 17 20:40:28 2004 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_cache;
# Line 30  my $MIN_WILDCARD =$cfg_global->val('webp Line 30  my $MIN_WILDCARD =$cfg_global->val('webp
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', '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    my $pages_per_set = $cfg_global->val('webpac', 'pages_per_set') || 10;
35    
36    
37  if ($UNAC_FILTER) {  if ($UNAC_FILTER) {
38          require $UNAC_FILTER;          require $UNAC_FILTER;
# Line 43  my $from_utf8 = Text::Iconv->new('UTF8', Line 46  my $from_utf8 = Text::Iconv->new('UTF8',
46  sub url_ex {  sub url_ex {
47          my $q = shift || die "suff2file needs CGI object!";          my $q = shift || die "suff2file needs CGI object!";
48          my $tpl = shift || die "url_ex needs template name!";          my $tpl = shift || die "url_ex needs template name!";
49          return suff2file($BASE_PATH, $q->url(-path => 1),$TEMPLATE_PATH,$tpl);          return suff2file($BASE_PATH, $q->url(-absolute => 1,-path => 1),$TEMPLATE_PATH,$tpl);
50  }  }
51    
52  sub suff2file($$$$) {  sub suff2file($$$$) {
# Line 53  sub suff2file($$$$) { Line 56  sub suff2file($$$$) {
56    
57          # strip everything to and including base path, leaving only          # strip everything to and including base path, leaving only
58          # additional (virtual) path          # additional (virtual) path
59          if ($p =~ s,^.*?$base_path,,) {          if ($base_path eq "/") {
60                    $p =~ s,/*,,g;
61                    my ($name,$ext) = split(/\./,$tpl);
62                    $p = $name . "-" . $p . "." . $ext;
63            } elsif ($p =~ s,^.*?$base_path,,) {
64                  $p =~ s,/*,,g;                  $p =~ s,/*,,g;
65                  my ($name,$ext) = split(/\./,$tpl);                  my ($name,$ext) = split(/\./,$tpl);
66                  $p = $name . $p . "." . $ext;                  $p = $name . $p . "." . $ext;
# Line 97  sub in_template { Line 104  sub in_template {
104                  die "can't parse TEMPLATE path";                  die "can't parse TEMPLATE path";
105          }          }
106    
107          my $master_tpl = suff2file($BASE_PATH, $q->url(-path => 1),$dir,$tpl);          my $master_tpl = suff2file($BASE_PATH, $q->url(-absolute => 1, -path => 1),$dir,$tpl);
108          if (open(T, $master_tpl)) {          if (open(T, $master_tpl)) {
109                  my $template_html = join("\n",<T>);                  my $template_html = join("\n",<T>);
110                  close(T);                  close(T);
# Line 108  sub in_template { Line 115  sub in_template {
115          }          }
116  }  }
117    
118    #--------------------------------------------------------------------------
119    
120    #
121    # make pager navigation and fill template variables
122    # compatibile with HTML::Pager
123    #
124    
125    sub make_pager($$$) {
126            my ($q,$tmpl,$pager) = @_;
127    
128            #
129            # pager navigation
130            #
131            my ($pager_prev,$pager_next, $pager_jump) = ('','','');
132    
133            my $nav_fmt=qq{ <a href="%s">%s</a> };
134    
135            if ($pager->current_page() > $pager->first_page) {
136                    $q->param('PAGER_offset', $pager->current_page - 1);
137                    $pager_prev .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'&lt;&lt;');
138            }
139    
140            if ($pager->previous_set) {
141                    $q->param('PAGER_offset', $pager->previous_set);
142                    $pager_prev .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'..');
143            }
144    
145    
146            foreach my $p (@{$pager->pages_in_set()}) {
147                    next if ($p < 0);
148                    if($p == $pager->current_page()) {
149                            $pager_jump .= "<b>$p</b> ";
150                    } else {
151                            $q->param('PAGER_offset', $p);
152                            $pager_jump .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),$p);
153                    }
154            }
155    
156            if ($pager->next_set) {
157                    $q->param('PAGER_offset', $pager->next_set);
158                    $pager_next .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'..');
159            }
160    
161            if ($pager->current_page() < $pager->last_page) {
162                    $q->param('PAGER_offset', $pager->current_page + 1);
163                    $pager_next .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'&gt;&gt;');
164            }
165    
166            $tmpl->param('PAGER_PREV', $pager_prev);
167            $tmpl->param('PAGER_JUMP', $pager_jump);
168            $tmpl->param('PAGER_NEXT', $pager_next);
169    
170    }
171    
172    #
173    # put persisten variables in template
174    #
175    
176    sub make_pager_vars {
177            my $q = shift @_;
178            my $tmpl = shift @_;
179            my @persist_vars = @_;
180            my $hidden_vars = '';
181            foreach my $v (@persist_vars) {
182                    $hidden_vars .= '<input type="hidden" name="'.$v.'" value="'.$q->param($v).'"/>'."\n";
183            }
184    
185            $tmpl->param('PAGER_HIDDEN', $hidden_vars);
186            $tmpl->param('PAGER_JAVASCRIPT', qq{
187    <SCRIPT LANGUAGE="Javascript">
188    <!-- Begin
189            // dummy emulator for HTML::Pager templates
190            function PAGER_set_offset_and_submit() {
191                    return true;
192            }
193    // End -->
194    </script>  
195            });
196    }
197    
198    #--------------------------------------------------------------------------
199    
200  sub show_search_form {  sub show_search_form {
201          my $self = shift;          my $self = shift;
202    
# Line 128  sub show_results_list { Line 217  sub show_results_list {
217    
218          my $q = $self->query();          my $q = $self->query();
219    
         my @swish_results;      # results from swish  
   
220          # load template for this page          # load template for this page
221    
222          my @s_arr;      # all queries are located here          my @s_arr;      # all queries are located here
# Line 137  sub show_results_list { Line 224  sub show_results_list {
224          my @path_arr = $q->param('path');          my @path_arr = $q->param('path');
225          my $full = $q->param('full');          my $full = $q->param('full');
226    
227          my @persist_vars = ( 'rm' );          my @persist_vars = ( 'rm' );
228          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) );
229    
230          # support parametars "f" and "v" for start          # support parametars "f" and "v" for start
231          for(my $i = ""; $i <=30; $i++) {          for(my $i = ""; $i <=30; $i++) {
# Line 214  sub show_results_list { Line 301  sub show_results_list {
301    
302          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);
303    
304            $tmpl->param('url_params',"?".join("&",@url_params));
305    
306          sub esc_html {          sub esc_html {
307                  my $html = shift;                  my $html = shift;
308                  $html =~ s/</&lt;/g;                  $html =~ s/</&lt;/g;
# Line 221  sub show_results_list { Line 310  sub show_results_list {
310                  return $html;                  return $html;
311          }          }
312    
313          # call swish          my $sort = 'swishrank';
314          my $sh = SWISH->connect('Fork',          if ($q->param("sort")) {
315                  prog     => $SWISH,                  $sort = 'headline';
316                  indexes  => $INDEX,                  push @persist_vars, "sort";
317                  properties  => [qw/swishdocpath swishrank swishtitle headline html/],          }
                 results  => sub {  
                         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  
         );  
318    
         die $SWISH::errstr unless $sh;  
319          # construct swish query          # construct swish query
320          my $sw_q = join(" and ",@s_arr);          my $sw_q = join(" and ",@s_arr);
321          if (@path_arr && $q->param('show_full')) {          if (@path_arr && $q->param('show_full')) {
# Line 256  sub show_results_list { Line 330  sub show_results_list {
330                  $tmpl->param('full',0);                  $tmpl->param('full',0);
331          }          }
332    
333          my $hits = $sh->query($sw_q);          # create new swish instance
334            my $swish = SWISH::API->new($INDEX);
335            $swish->AbortLastError if $swish->Error;
336    
337            # execute query and get number of results from SWISH-E
338            my $search = $swish->New_Search_Object;
339    
340            $search->SetSort($sort);
341    
342            my $results = $search->Execute($sw_q);
343            $swish->AbortLastError if $swish->Error;
344    
345            my $hits = $results->Hits;
346    
347          $tmpl->param('hits',$hits);          $tmpl->param('hits',$hits);
348          $tmpl->param('search',$sw_q);          $tmpl->param('search',$sw_q);
# Line 264  sub show_results_list { Line 350  sub show_results_list {
350          $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);          $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);
351          $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);          $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);
352    
353          $tmpl->param('url_params',"?".join("&",@url_params));          #
354            # build pager
355            #
356    
357          # create a Pager object          my $current_page = $q->param('PAGER_offset') || 1;
358          my $pager = HTML::Pager->new(  
359                  # required parameters          my $pager = Data::Pageset->new({
360                  query => $q,                  'total_entries' => $hits,
361                  get_data_callback => sub {                  'entries_per_page' => $ON_PAGE,
362                          my ($offset, $rows) = @_;                  'current_page' => $current_page,
363                    'pages_per_set' => $pages_per_set,
364                          my @result;          });
365                          for (my $i=0; $i<$rows; $i++) {  
366                                  my $r = $swish_results[$offset+$i];          $results->SeekResult( $pager->first - 1 );
367                                  if ($r && $tmpl->param('full')) {  
368                                          push @result, $r;          # get number of entries on this page
369                                  } elsif ($r) {          my $i = $pager->entries_on_this_page;
370                                          # if not full output, skip html  
371                                          delete $r->{html};          # results from swish for template
372                                          push @result, $r;          my @pager_data_list;
373                                  }  
374                          }          for(my $i=$pager->first; $i<=$pager->last; $i++) {
                         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,  
         );  
375    
376          my $html = $pager->output;                  my $result = $results->NextResult;
377                    last if (! $result);
378    
379                    my $r = {
380                            nr => $i,
381                            path => $result->Property('swishdocpath'),
382                            headline => esc_html($from_utf8->convert($result->Property('headline'))),
383                            rank => $result->Property('swishrank')
384                    };
385    
386                    $r->{html} = back2html($from_utf8->convert($result->Property('html'))) if ($q->param('show_full'));
387    
388                    push @pager_data_list, $r;
389            }
390    
391    
392    
393            # put something in template
394            make_pager($q, $tmpl, $pager);
395            make_pager_vars($q, $tmpl, @persist_vars);
396            $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
397    
398            my $html = $tmpl->output;
399    
400          return in_template($q,$html);          return in_template($q,$html);
401  }  }
# Line 322  sub show_index { Line 419  sub show_index {
419          );          );
420    
421          my $total = $index->count($field,$limit);          my $total = $index->count($field,$limit);
422    
423          if (! $total) {          if (! $total) {
424                  my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html'));                  my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html'));
425                  $tmpl->param('field',$field);                  $tmpl->param('field',$field);
# Line 339  sub show_index { Line 437  sub show_index {
437  #               $q->param("Pager_offet)  #               $q->param("Pager_offet)
438  #       }  #       }
439    
         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,  
         );  
440    
441          return in_template($q,$pager->output);          #
442            # build pager
443            #
444            my $pager = Data::Pageset->new({
445                    'total_entries' => $total,
446                    'entries_per_page' => $ON_PAGE,
447                    'current_page' => $q->param('PAGER_offset') || 1,
448                    'pages_per_set' => $pages_per_set
449            });
450    
451            my @persist_vars = qw{rm f$i v$i f$i_index offset};
452    
453            make_pager($q, $tmpl, $pager);
454            make_pager_vars($q, $tmpl, @persist_vars);
455    
456            my @pager_data_list = $index->fetch($field,$limit, $pager->first - 1, $pager->entries_on_this_page);
457            $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
458    
459            return in_template($q,$tmpl->output);
460  }  }
461    
462  1;  1;

Legend:
Removed from v.198  
changed lines
  Added in v.304

  ViewVC Help
Powered by ViewVC 1.1.26