/usr/share/perl5/Data/Pageset.pm is in libdata-pageset-perl 1.05-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | package Data::Pageset;
use strict;
use Carp;
use Data::Page;
use vars qw(@ISA $VERSION);
@ISA = qw(Data::Page);
$VERSION = '1.05';
=head1 NAME
Data::Pageset - Page numbering and page sets
=head1 SYNOPSIS
use Data::Pageset;
my $page_info = Data::Pageset->new({
'total_entries' => $total_entries,
'entries_per_page' => $entries_per_page,
# Optional, will use defaults otherwise.
'current_page' => $current_page,
'pages_per_set' => $pages_per_set,
'mode' => 'fixed', # default, or 'slide'
});
# General page information
print " First page: ", $page_info->first_page, "\n";
print " Last page: ", $page_info->last_page, "\n";
print " Next page: ", $page_info->next_page, "\n";
print " Previous page: ", $page_info->previous_page, "\n";
# Results on current page
print "First entry on page: ", $page_info->first, "\n";
print " Last entry on page: ", $page_info->last, "\n";
# Can add in the pages per set after the object is created
$page_info->pages_per_set($pages_per_set);
# Page set information
print "First page of previous page set: ", $page_info->previous_set, "\n";
print " First page of next page set: ", $page_info->next_set, "\n";
# Print the page numbers of the current set
foreach my $page (@{$page_info->pages_in_set()}) {
if($page == $page_info->current_page()) {
print "<b>$page</b> ";
} else {
print "$page ";
}
}
=head1 DESCRIPTION
The object produced by Data::Pageset can be used to create page
navigation, it inherits from Data::Page and has access to all
methods from this object.
In addition it also provides methods for dealing with set of pages,
so that if there are too many pages you can easily break them
into chunks for the user to browse through.
You can even choose to view page numbers in your set in a 'sliding'
fassion.
The object can easily be passed to a templating system
such as Template Toolkit or be used within a script.
=head1 METHODS
=head2 new()
use Data::Pageset;
my $page_info = Data::Pageset->new({
'total_entries' => $total_entries,
'entries_per_page' => $entries_per_page,
# Optional, will use defaults otherwise.
'current_page' => $current_page,
'pages_per_set' => $pages_per_set,
'mode' => 'slide', # default fixed
});
This is the constructor of the object, it requires an anonymous
hash containing the 'total_entries', how many data units you have,
and the number of 'entries_per_page' to display. Optionally
the 'current_page' (defaults to page 1) and pages_per_set (how
many pages to display, defaults to 10) can be added.
The mode (which defaults to 'fixed') determins how the paging
will work, for example with 10 pages_per_set and the current_page
set to 18 you will get the following results:
=head3 Fixed:
=over 4
=item Pages in set:
11,12,13,14,15,16,17,18,19,20
=item Previous page set:
1
=item Next page set:
21
=back
=head3 Slide:
=over 4
=item Pages in set:
14,15,16,17,18,19,20,21,22,23
=item Previous page set:
9
=item Next page set:
24
=back
You can not change modes once the object is created.
=cut
sub new {
my ( $class, $conf ) = @_;
my $self = {};
croak "total_entries and entries_per_page must be supplied"
unless defined $conf->{'total_entries'}
&& defined $conf->{'entries_per_page'};
$conf->{'current_page'} = 1 unless defined $conf->{'current_page'};
$conf->{pages_per_set} = 10 unless defined $conf->{'pages_per_set'};
if ( defined $conf->{'mode'} && $conf->{'mode'} eq 'slide' ) {
$self->{mode} = 'slide';
} else {
$self->{mode} = 'fixed';
}
bless( $self, $class );
$self->total_entries( $conf->{'total_entries'} );
$self->entries_per_page( $conf->{'entries_per_page'} );
$self->current_page( $conf->{'current_page'} );
$self->pages_per_set( $conf->{'pages_per_set'} );
return $self;
}
=head2 current_page()
$page_info->current_page($page_num);
This method sets the current_page to the argument supplied, it can also be
set in the constructor, but you may want to reuse the object if printing out
multiple pages. It will then return the page number once set.
If this method is called without any arguments it returns the current page number.
=cut
sub current_page {
my $self = shift;
if (@_) {
# Set current page
$self->_current_page_accessor(@_);
# Redo calculations, using current pages_per_set value
$self->pages_per_set( $self->pages_per_set() );
}
# Not sure if there is some cleaver way of calling SUPER here,
# think it would have to be wrapped in an eval
return $self->first_page
if $self->_current_page_accessor < $self->first_page;
return $self->last_page
if $self->_current_page_accessor > $self->last_page;
return $self->_current_page_accessor();
}
=head2 pages_per_set()
$page_info->pages_per_set($number_of_pages_per_set);
Calling this method initalises the calculations required to use
the paging methods below. The value can also be passed into
the constructor method new().
If called without any arguments it will return the current
number of pages per set.
=cut
sub pages_per_set {
my $self = shift;
my $max_pages_per_set = shift;
# set as undef so it at least exists
$self->{PAGE_SET_PAGES_PER_SET} = undef
unless exists $self->{PAGE_SET_PAGES_PER_SET};
# Not trying to set, so return current number;
return $self->{PAGE_SET_PAGES_PER_SET} unless $max_pages_per_set;
$self->{PAGE_SET_PAGES_PER_SET} = $max_pages_per_set;
unless ( $max_pages_per_set > 1 ) {
# Only have one page in the set, must be page 1
$self->{PAGE_SET_PREVIOUS} = $self->current_page() - 1
if $self->current_page != 1;
$self->{PAGE_SET_PAGES} = [1];
$self->{PAGE_SET_NEXT} = $self->current_page() + 1
if $self->current_page() < $self->last_page();
} else {
if ( $self->{mode} eq 'fixed' ) {
my $starting_page = $self->_calc_start_page($max_pages_per_set);
my $end_page = $starting_page + $max_pages_per_set - 1;
if ( $end_page < $self->last_page() ) {
$self->{PAGE_SET_NEXT} = $end_page + 1;
}
if ( $starting_page > 1 ) {
$self->{PAGE_SET_PREVIOUS}
= $starting_page - $max_pages_per_set;
# I can't see a reason for this to be here!
#$self->{PAGE_SET_PREVIOUS} = 1 if $self->{PAGE_SET_PREVIOUS} < 1;
}
$end_page = $self->last_page() if $self->last_page() < $end_page;
$self->{PAGE_SET_PAGES} = [ $starting_page .. $end_page ];
} else {
# We're in slide mode
# See if we have enough pages to slide
if ( $max_pages_per_set >= $self->last_page() ) {
# No sliding, no next/prev pageset
$self->{PAGE_SET_PAGES} = [ '1' .. $self->last_page() ];
} else {
# Find the middle rounding down - we want more pages after, than before
my $middle = int( $max_pages_per_set / 2 );
# offset for extra value right of center on even numbered sets
my $offset = 1;
if ( $max_pages_per_set % 2 != 0 ) {
# must have been an odd number, add one
$middle++;
$offset = 0;
}
my $starting_page = $self->current_page() - $middle + 1;
$starting_page = 1 if $starting_page < 1;
my $end_page = $starting_page + $max_pages_per_set - 1;
$end_page = $self->last_page()
if $self->last_page() < $end_page;
if ( $self->current_page() <= $middle ) {
# near the start of the page numbers
$self->{PAGE_SET_NEXT}
= $max_pages_per_set + $middle - $offset;
$self->{PAGE_SET_PAGES} = [ '1' .. $max_pages_per_set ];
} elsif ( $self->current_page()
> ( $self->last_page() - $middle - $offset ) )
{
# near the end of the page numbers
$self->{PAGE_SET_PREVIOUS}
= $self->last_page()
- $max_pages_per_set
- $middle + 1;
$self->{PAGE_SET_PAGES}
= [ ( $self->last_page() - $max_pages_per_set + 1 )
.. $self->last_page() ];
} else {
# Start scrolling baby!
$self->{PAGE_SET_PAGES} = [ $starting_page .. $end_page ];
$self->{PAGE_SET_PREVIOUS}
= $starting_page - $middle - $offset;
$self->{PAGE_SET_PREVIOUS} = 1
if $self->{PAGE_SET_PREVIOUS} < 1;
$self->{PAGE_SET_NEXT} = $end_page + $middle;
}
}
}
}
}
=head2 previous_set()
print "Back to previous set which starts at ", $page_info->previous_set(), "\n";
This method returns the page number at the start of the previous page set.
undef is return if pages_per_set has not been set.
=cut
sub previous_set {
my $self = shift;
return $self->{PAGE_SET_PREVIOUS} if defined $self->{PAGE_SET_PREVIOUS};
return undef;
}
=head2 next_set()
print "Next set starts at ", $page_info->next_set(), "\n";
This method returns the page number at the start of the next page set.
undef is return if pages_per_set has not been set.
=cut
sub next_set {
my $self = shift;
return $self->{PAGE_SET_NEXT} if defined $self->{PAGE_SET_NEXT};
return undef;
}
=head2 pages_in_set()
foreach my $page_num (@{$page_info->pages_in_set()}) {
print "Page: $page_num \n";
}
This method returns an array ref of the the page numbers within
the current set. undef is return if pages_per_set has not been set.
=cut
sub pages_in_set {
my $self = shift;
return $self->{PAGE_SET_PAGES};
}
# Calc the first page in the current set
sub _calc_start_page {
my ( $self, $max_page_links_per_page ) = @_;
my $start_page;
my $current_page = $self->current_page();
my $max_pages_per_set;
my $current_page_set = 0;
if ( $max_page_links_per_page > 0 ) {
$current_page_set = int( $current_page / $max_page_links_per_page );
if ( $current_page % $max_page_links_per_page == 0 ) {
$current_page_set = $current_page_set - 1;
}
}
$start_page = ( $current_page_set * $max_page_links_per_page ) + 1;
return $start_page;
}
=head1 EXPORT
None by default.
=head1 AUTHOR
Leo Lapworth C<< <LLAP@cuckoo.org> >>
=head1 SVN
http://code.google.com/p/llap/
=head1 CONTRIBUTORS
Ryan D Johnson C<< <ryan@innerfence.com> >>
PLOBBES
=head1 SEE ALSO
L<Data::Page>.
=head1 COPYRIGHT
Copyright (C) 2007, Leo Lapworth
This module is free software; you can redistribute it or modify it
under the same terms as Perl itself.
=cut
1;
|