You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2010/11/12 02:59:33 UTC
[lucy-commits] svn commit: r1034244 - in /incubator/lucy/trunk/perl:
lib/Lucy/Docs/Tutorial/Simple.pod sample/search.cgi
Author: marvin
Date: Fri Nov 12 01:59:32 2010
New Revision: 1034244
URL: http://svn.apache.org/viewvc?rev=1034244&view=rev
Log:
Eliminate dependency on Data::Pageset in sample/tutorial application, falling
back to somewhat more obtuse manual generation of paging links for search
results.
Modified:
incubator/lucy/trunk/perl/lib/Lucy/Docs/Tutorial/Simple.pod
incubator/lucy/trunk/perl/sample/search.cgi
Modified: incubator/lucy/trunk/perl/lib/Lucy/Docs/Tutorial/Simple.pod
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/perl/lib/Lucy/Docs/Tutorial/Simple.pod?rev=1034244&r1=1034243&r2=1034244&view=diff
==============================================================================
--- incubator/lucy/trunk/perl/lib/Lucy/Docs/Tutorial/Simple.pod (original)
+++ incubator/lucy/trunk/perl/lib/Lucy/Docs/Tutorial/Simple.pod Fri Nov 12 01:59:32 2010
@@ -103,8 +103,9 @@ The beginning is dedicated to CGI proces
my $path_to_index = '/path/to/index';
use CGI;
+ use List::Util qw( max min );
+ use POSIX qw( ceil );
use Encode qw( decode );
- use Data::Pageset;
use LucyX::Simple;
my $cgi = CGI->new;
@@ -173,18 +174,10 @@ The rest of the script is just text wran
= qq|<p>No matches for <strong>$escaped_q</strong></p>|;
}
else {
- my $current_page = ( $offset / $page_size ) + 1;
- my $pager = Data::Pageset->new(
- { total_entries => $total_hits,
- entries_per_page => $page_size,
- current_page => $current_page,
- pages_per_set => 10,
- mode => 'slide',
- }
- );
- my $last_result = $pager->last;
- my $first_result = $pager->first;
-
+ # Calculate the nums for the first and last hit to display.
+ my $last_result = min( ( $offset + $hits_per_page ), $total_hits );
+ my $first_result = min( ( $offset + 1 ), $last_result );
+
# Display the result nums, start paging info.
$paging_info = qq|
<p>
@@ -195,22 +188,28 @@ The rest of the script is just text wran
<p>
Results Page:
|;
-
+
+ # Calculate first and last hits pages to display / link to.
+ my $current_page = int( $first_result / $hits_per_page ) + 1;
+ my $last_page = ceil( $total_hits / $hits_per_page );
+ my $first_page = max( 1, ( $current_page - 9 ) );
+ $last_page = min( $last_page, ( $current_page + 10 ) );
+
# Create a url for use in paging links.
my $href = $cgi->url( -relative => 1 );
$href .= "?q=" . CGI::escape($query_string);
$href .= ";category=" . CGI::escape($category);
$href .= ";offset=" . CGI::escape($offset);
-
+
# Generate the "Prev" link.
if ( $current_page > 1 ) {
my $new_offset = ( $current_page - 2 ) * $page_size;
$href =~ s/(?<=offset=)\d+/$new_offset/;
$paging_info .= qq|<a href="$href"><= Prev</a>\n|;
}
-
+
# Generate paging links.
- for my $page_num ( @{ $pager->pages_in_set } ) {
+ for my $page_num ( $first_page .. $last_page ) {
if ( $page_num == $current_page ) {
$paging_info .= qq|$page_num \n|;
}
@@ -220,21 +219,21 @@ The rest of the script is just text wran
$paging_info .= qq|<a href="$href">$page_num</a>\n|;
}
}
-
+
# Generate the "Next" link.
- if ( $current_page != $pager->last_page ) {
+ if ( $current_page != $last_page ) {
my $new_offset = $current_page * $page_size;
$href =~ s/(?<=offset=)\d+/$new_offset/;
$paging_info .= qq|<a href="$href">Next =></a>\n|;
}
-
+
# Close tag.
$paging_info .= "</p>\n";
}
-
+
return $paging_info;
}
-
+
# Print content to output.
sub blast_out_content {
my ( $query_string, $hit_list, $paging_info ) = @_;
Modified: incubator/lucy/trunk/perl/sample/search.cgi
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/perl/sample/search.cgi?rev=1034244&r1=1034243&r2=1034244&view=diff
==============================================================================
--- incubator/lucy/trunk/perl/sample/search.cgi (original)
+++ incubator/lucy/trunk/perl/sample/search.cgi Fri Nov 12 01:59:32 2010
@@ -22,8 +22,9 @@ use warnings;
my $path_to_index = '/path/to/index';
use CGI;
+use List::Util qw( max min );
+use POSIX qw( ceil );
use Encode qw( decode );
-use Data::Pageset;
use Lucy::Search::IndexSearcher;
use Lucy::Highlight::Highlighter;
use Lucy::Search::QueryParser;
@@ -112,17 +113,9 @@ sub generate_paging_info {
= qq|<p>No matches for <strong>$escaped_q</strong></p>|;
}
else {
- my $current_page = ( $offset / $page_size ) + 1;
- my $pager = Data::Pageset->new(
- { total_entries => $total_hits,
- entries_per_page => $page_size,
- current_page => $current_page,
- pages_per_set => 10,
- mode => 'slide',
- }
- );
- my $last_result = $pager->last;
- my $first_result = $pager->first;
+ # Calculate the nums for the first and last hit to display.
+ my $last_result = min( ( $offset + $hits_per_page ), $total_hits );
+ my $first_result = min( ( $offset + 1 ), $last_result );
# Display the result nums, start paging info.
$paging_info = qq|
@@ -135,6 +128,12 @@ sub generate_paging_info {
Results Page:
|;
+ # Calculate first and last hits pages to display / link to.
+ my $current_page = int( $first_result / $hits_per_page ) + 1;
+ my $last_page = ceil( $total_hits / $hits_per_page );
+ my $first_page = max( 1, ( $current_page - 9 ) );
+ $last_page = min( $last_page, ( $current_page + 10 ) );
+
# Create a url for use in paging links.
my $href = $cgi->url( -relative => 1 );
$href .= "?q=" . CGI::escape($query_string);
@@ -149,7 +148,7 @@ sub generate_paging_info {
}
# Generate paging links.
- for my $page_num ( @{ $pager->pages_in_set } ) {
+ for my $page_num ( $first_page .. $last_page ) {
if ( $page_num == $current_page ) {
$paging_info .= qq|$page_num \n|;
}
@@ -161,7 +160,7 @@ sub generate_paging_info {
}
# Generate the "Next" link.
- if ( $current_page != $pager->last_page ) {
+ if ( $current_page != $last_page ) {
my $new_offset = $current_page * $page_size;
$href =~ s/(?<=offset=)\d+/$new_offset/;
$paging_info .= qq|<a href="$href">Next =></a>\n|;