/[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 202 by dpavlin, Sun Jan 18 16:23:03 2004 UTC branches/tehnika/WebPac.pm revision 458 by dpavlin, Tue Sep 21 16:53:44 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 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!";
52          my $tpl = shift || die "url_ex needs template name!";          my $tpl = shift || die "url_ex needs template name!";
53          return suff2file($BASE_PATH, $q->url(-path => 1),$TEMPLATE_PATH,$tpl);          return suff2file($BASE_PATH, $q->url(-absolute => 1,-path => 1),$TEMPLATE_PATH,$tpl);
54  }  }
55    
56  sub suff2file($$$$) {  sub suff2file($$$$) {
# Line 53  sub suff2file($$$$) { Line 60  sub suff2file($$$$) {
60    
61          # strip everything to and including base path, leaving only          # strip everything to and including base path, leaving only
62          # additional (virtual) path          # additional (virtual) path
63          if ($p =~ s,^.*?$base_path,,) {          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;                  $p =~ s,/*,,g;
69                  my ($name,$ext) = split(/\./,$tpl);                  my ($name,$ext) = split(/\./,$tpl);
70                  $p = $name . $p . "." . $ext;                  $p = $name . $p . "." . $ext;
# Line 97  sub in_template { Line 108  sub in_template {
108                  die "can't parse TEMPLATE path";                  die "can't parse TEMPLATE path";
109          }          }
110    
111          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);
112          if (open(T, $master_tpl)) {          if (open(T, $master_tpl)) {
113                  my $template_html = join("\n",<T>);                  my $template_html = join("\n",<T>);
114                  close(T);                  close(T);
# Line 108  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            foreach my $v (@persist_vars) {
186                    foreach my $val ($q->param($v)) {
187                            $hidden_vars .= '<input type="hidden" name="'.$v.'" value="'.$val.'"/>'."\n";
188                    }
189            }
190    
191            $tmpl->param('PAGER_HIDDEN', $hidden_vars);
192            $tmpl->param('PAGER_JAVASCRIPT', qq{
193    <SCRIPT LANGUAGE="Javascript">
194    <!-- Begin
195            // dummy emulator for HTML::Pager templates
196            function PAGER_set_offset_and_submit() {
197                    return true;
198            }
199    // End -->
200    </script>  
201            });
202    }
203    
204    #--------------------------------------------------------------------------
205    
206  sub show_search_form {  sub show_search_form {
207          my $self = shift;          my $self = shift;
208    
# Line 128  sub show_results_list { Line 223  sub show_results_list {
223    
224          my $q = $self->query();          my $q = $self->query();
225    
         my @swish_results;      # results from swish  
   
226          # load template for this page          # load template for this page
227    
228          my @s_arr;      # all queries are located here          my @s_arr;      # all queries are located here
# Line 137  sub show_results_list { Line 230  sub show_results_list {
230          my @path_arr = $q->param('path');          my @path_arr = $q->param('path');
231          my $full = $q->param('full');          my $full = $q->param('full');
232    
233          my @persist_vars = ( 'rm' );          my @persist_vars = ( 'rm' );
234          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) );
235    
236          # support parametars "f" and "v" for start          # support parametars "f" and "v" for start
237          for(my $i = ""; $i <=30; $i++) {          for(my $i = ""; $i <=30; $i++) {
# Line 168  sub show_results_list { Line 261  sub show_results_list {
261                  while (my $search = shift @param_vals) {                  while (my $search = shift @param_vals) {
262                          my $s;                          my $s;
263                          # remove accents                          # remove accents
264                          $search = unac_string($CHARSET,$search);                          $search = my_unac_string($CHARSET,$search);
265                          while ($search =~ s/\s*("[^"]+")\s*/ /) {                          while ($search =~ s/\s*("[^"]+")\s*/ /) {
266                                  $s .= "$1 ";                                  $s .= "$1 ";
267                          }                          }
# Line 214  sub show_results_list { Line 307  sub show_results_list {
307    
308          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);
309    
310            $tmpl->param('url_params',"?".join("&",@url_params));
311    
312          sub esc_html {          sub esc_html {
313                  my $html = shift;                  my $html = shift;
314                  $html =~ s/</&lt;/g;                  $html =~ s/</&lt;/g;
# Line 227  sub show_results_list { Line 322  sub show_results_list {
322                  push @persist_vars, "sort";                  push @persist_vars, "sort";
323          }          }
324    
         # call swish  
         my $sh = SWISH->connect('Fork',  
                 prog     => $SWISH,  
                 indexes  => $INDEX,  
                 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,  
                 sortorder => $sort,  
         );  
   
         die $SWISH::errstr unless $sh;  
325          # construct swish query          # construct swish query
326          my $sw_q = join(" and ",@s_arr);          my $sw_q = join(" and ",@s_arr);
327          if (@path_arr && $q->param('show_full')) {          if (@path_arr && $q->param('show_full')) {
# Line 263  sub show_results_list { Line 336  sub show_results_list {
336                  $tmpl->param('full',0);                  $tmpl->param('full',0);
337          }          }
338    
339          my $hits = $sh->query($sw_q);          # create new swish instance
340            my $swish = SWISH::API->new($INDEX);
341            $swish->AbortLastError if $swish->Error;
342    
343            # execute query and get number of results from SWISH-E
344            my $search = $swish->New_Search_Object;
345    
346            $search->SetSort($sort);
347    
348            my $results = $search->Execute($sw_q);
349            $swish->AbortLastError if $swish->Error;
350    
351            my $hits = $results->Hits;
352    
353          $tmpl->param('hits',$hits);          $tmpl->param('hits',$hits);
354          $tmpl->param('search',$sw_q);          $tmpl->param('search',$sw_q);
# Line 271  sub show_results_list { Line 356  sub show_results_list {
356          $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);          $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0);
357          $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);          $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0);
358    
359          $tmpl->param('url_params',"?".join("&",@url_params));          #
360            # build pager
361            #
362    
363          # create a Pager object          my $current_page = $q->param('PAGER_offset') || 1;
364          my $pager = HTML::Pager->new(  
365                  # required parameters          my $pager = Data::Pageset->new({
366                  query => $q,                  'total_entries' => $hits,
367                  get_data_callback => sub {                  'entries_per_page' => $ON_PAGE,
368                          my ($offset, $rows) = @_;                  'current_page' => $current_page,
369                    'pages_per_set' => $pages_per_set,
370                          my @result;          });
371                          for (my $i=0; $i<$rows; $i++) {  
372                                  my $r = $swish_results[$offset+$i];          $results->SeekResult( $pager->first - 1 );
373                                  if ($r && $tmpl->param('full')) {  
374                                          push @result, $r;          # get number of entries on this page
375                                  } elsif ($r) {          my $i = $pager->entries_on_this_page;
                                         # 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,  
         );  
376    
377          my $html = $pager->output;          # results from swish for template
378            my @pager_data_list;
379    
380            for(my $i=$pager->first; $i<=$pager->last; $i++) {
381    
382                    my $result = $results->NextResult;
383                    last if (! $result);
384    
385                    my $r = {
386                            nr => $i,
387                            path => $result->Property('swishdocpath'),
388                            headline => esc_html($from_utf8->convert($result->Property('headline'))),
389                            rank => $result->Property('swishrank')
390                    };
391    
392                    $r->{html} = back2html($from_utf8->convert($result->Property('html'))) if ($q->param('show_full'));
393    
394                    push @pager_data_list, $r;
395            }
396    
397    
398    
399            # put something in template
400            make_pager($q, $tmpl, $pager);
401            make_pager_vars($q, $tmpl, @persist_vars);
402            $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
403    
404            my $html = $tmpl->output;
405    
406          return in_template($q,$html);          return in_template($q,$html);
407  }  }
# Line 329  sub show_index { Line 425  sub show_index {
425          );          );
426    
427          my $total = $index->count($field,$limit);          my $total = $index->count($field,$limit);
428    
429          if (! $total) {          if (! $total) {
430                  my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html'));                  my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html'));
431                  $tmpl->param('field',$field);                  $tmpl->param('field',$field);
# Line 346  sub show_index { Line 443  sub show_index {
443  #               $q->param("Pager_offet)  #               $q->param("Pager_offet)
444  #       }  #       }
445    
         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,  
         );  
446    
447          return in_template($q,$pager->output);          #
448            # build pager
449            #
450            my $pager = Data::Pageset->new({
451                    'total_entries' => $total,
452                    'entries_per_page' => $ON_PAGE,
453                    'current_page' => $q->param('PAGER_offset') || 1,
454                    'pages_per_set' => $pages_per_set
455            });
456    
457            my @persist_vars = qw{rm f$i v$i f$i_index offset};
458    
459            make_pager($q, $tmpl, $pager);
460            make_pager_vars($q, $tmpl, @persist_vars);
461    
462            my @pager_data_list = $index->fetch($field,$limit, $pager->first - 1, $pager->entries_on_this_page);
463            $tmpl->param('PAGER_DATA_LIST', \@pager_data_list);
464    
465            return in_template($q,$tmpl->output);
466  }  }
467    
468  1;  1;

Legend:
Removed from v.202  
changed lines
  Added in v.458

  ViewVC Help
Powered by ViewVC 1.1.26