--- trunk/WebPac.pm 2004/04/04 22:11:13 303 +++ trunk/WebPac.pm 2004/04/17 20:40:28 304 @@ -3,13 +3,13 @@ use base 'CGI::Application'; use strict; -use HTML::Pager; use HTML::FillInForm; -use SWISH; +use SWISH::API; use Text::Iconv; use DBI; use Config::IniFiles; use Text::Unaccent; +use Data::Pageset; use lib '..'; use index_DBI_cache; @@ -30,6 +30,9 @@ my $TEMPLATE =$cfg_global->val('webpac', 'template'); my $UNAC_FILTER =$cfg_global->val('global', 'unac_filter'); my $BASE_PATH =$cfg_global->val('webpac', 'base_path'); +# for pager +my $pages_per_set = $cfg_global->val('webpac', 'pages_per_set') || 10; + if ($UNAC_FILTER) { require $UNAC_FILTER; @@ -112,6 +115,88 @@ } } +#-------------------------------------------------------------------------- + +# +# make pager navigation and fill template variables +# compatibile with HTML::Pager +# + +sub make_pager($$$) { + my ($q,$tmpl,$pager) = @_; + + # + # pager navigation + # + my ($pager_prev,$pager_next, $pager_jump) = ('','',''); + + my $nav_fmt=qq{ %s }; + + if ($pager->current_page() > $pager->first_page) { + $q->param('PAGER_offset', $pager->current_page - 1); + $pager_prev .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'<<'); + } + + if ($pager->previous_set) { + $q->param('PAGER_offset', $pager->previous_set); + $pager_prev .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'..'); + } + + + foreach my $p (@{$pager->pages_in_set()}) { + next if ($p < 0); + if($p == $pager->current_page()) { + $pager_jump .= "$p "; + } else { + $q->param('PAGER_offset', $p); + $pager_jump .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),$p); + } + } + + if ($pager->next_set) { + $q->param('PAGER_offset', $pager->next_set); + $pager_next .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'..'); + } + + if ($pager->current_page() < $pager->last_page) { + $q->param('PAGER_offset', $pager->current_page + 1); + $pager_next .= sprintf($nav_fmt,$q->url(-relative=>1, -query=>1),'>>'); + } + + $tmpl->param('PAGER_PREV', $pager_prev); + $tmpl->param('PAGER_JUMP', $pager_jump); + $tmpl->param('PAGER_NEXT', $pager_next); + +} + +# +# put persisten variables in template +# + +sub make_pager_vars { + my $q = shift @_; + my $tmpl = shift @_; + my @persist_vars = @_; + my $hidden_vars = ''; + foreach my $v (@persist_vars) { + $hidden_vars .= ''."\n"; + } + + $tmpl->param('PAGER_HIDDEN', $hidden_vars); + $tmpl->param('PAGER_JAVASCRIPT', qq{ + + }); +} + +#-------------------------------------------------------------------------- + sub show_search_form { my $self = shift; @@ -132,8 +217,6 @@ my $q = $self->query(); - my @swish_results; # results from swish - # load template for this page my @s_arr; # all queries are located here @@ -141,8 +224,8 @@ my @path_arr = $q->param('path'); my $full = $q->param('full'); - my @persist_vars = ( 'rm' ); - my @url_params = ( 'rm=results', 'show_full=1', 'last_PAGER_offset='.$q->param('PAGER_offset') || 0 ); + my @persist_vars = ( 'rm' ); + my @url_params = ( 'rm=results', 'show_full=1', 'last_PAGER_offset='.($q->param('PAGER_offset') || 0) ); # support parametars "f" and "v" for start for(my $i = ""; $i <=30; $i++) { @@ -218,6 +301,8 @@ my $tmpl = $self->load_tmpl(url_ex($q,'results.html'), global_vars => 1); + $tmpl->param('url_params',"?".join("&",@url_params)); + sub esc_html { my $html = shift; $html =~ s/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; # construct swish query my $sw_q = join(" and ",@s_arr); if (@path_arr && $q->param('show_full')) { @@ -267,7 +330,19 @@ $tmpl->param('full',0); } - my $hits = $sh->query($sw_q); + # create new swish instance + my $swish = SWISH::API->new($INDEX); + $swish->AbortLastError if $swish->Error; + + # execute query and get number of results from SWISH-E + my $search = $swish->New_Search_Object; + + $search->SetSort($sort); + + my $results = $search->Execute($sw_q); + $swish->AbortLastError if $swish->Error; + + my $hits = $results->Hits; $tmpl->param('hits',$hits); $tmpl->param('search',$sw_q); @@ -275,41 +350,52 @@ $tmpl->param('PAGER_offset',$q->param("PAGER_offset") || 0); $tmpl->param('last_PAGER_offset',$q->param("last_PAGER_offset") || 0); - $tmpl->param('url_params',"?".join("&",@url_params)); + # + # build pager + # - # create a Pager object - my $pager = HTML::Pager->new( - # required parameters - query => $q, - get_data_callback => sub { - my ($offset, $rows) = @_; - - my @result; - for (my $i=0; $i<$rows; $i++) { - my $r = $swish_results[$offset+$i]; - 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, - ); + my $current_page = $q->param('PAGER_offset') || 1; + + my $pager = Data::Pageset->new({ + 'total_entries' => $hits, + 'entries_per_page' => $ON_PAGE, + 'current_page' => $current_page, + 'pages_per_set' => $pages_per_set, + }); + + $results->SeekResult( $pager->first - 1 ); + + # get number of entries on this page + my $i = $pager->entries_on_this_page; + + # results from swish for template + my @pager_data_list; + + for(my $i=$pager->first; $i<=$pager->last; $i++) { + + my $result = $results->NextResult; + last if (! $result); - my $html = $pager->output; + my $r = { + nr => $i, + path => $result->Property('swishdocpath'), + headline => esc_html($from_utf8->convert($result->Property('headline'))), + rank => $result->Property('swishrank') + }; + + $r->{html} = back2html($from_utf8->convert($result->Property('html'))) if ($q->param('show_full')); + + push @pager_data_list, $r; + } + + + + # put something in template + make_pager($q, $tmpl, $pager); + make_pager_vars($q, $tmpl, @persist_vars); + $tmpl->param('PAGER_DATA_LIST', \@pager_data_list); + + my $html = $tmpl->output; return in_template($q,$html); } @@ -333,6 +419,7 @@ ); my $total = $index->count($field,$limit); + if (! $total) { my $tmpl = $self->load_tmpl(url_ex($q,'no_index.html')); $tmpl->param('field',$field); @@ -350,26 +437,26 @@ # $q->param("Pager_offet) # } - 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, - ); - return in_template($q,$pager->output); + # + # build pager + # + my $pager = Data::Pageset->new({ + 'total_entries' => $total, + 'entries_per_page' => $ON_PAGE, + 'current_page' => $q->param('PAGER_offset') || 1, + 'pages_per_set' => $pages_per_set + }); + + my @persist_vars = qw{rm f$i v$i f$i_index offset}; + + make_pager($q, $tmpl, $pager); + make_pager_vars($q, $tmpl, @persist_vars); + + my @pager_data_list = $index->fetch($field,$limit, $pager->first - 1, $pager->entries_on_this_page); + $tmpl->param('PAGER_DATA_LIST', \@pager_data_list); + + return in_template($q,$tmpl->output); } 1;