]> git.deb.at Git - deb/packages.git/blob - lib/Packages/Search.pm
Paging stuff that is independent from any backend considerations
[deb/packages.git] / lib / Packages / Search.pm
1 #
2 # Packages::Search
3 #
4 # Copyright (C) 2004-2006 Frank Lichtenheld <frank@lichtenheld.de>
5
6 # The code is based on the old search_packages.pl script that
7 # was:
8 #
9 # Copyright (C) 1998 James Treacy
10 # Copyright (C) 2000, 2001 Josip Rodin
11 # Copyright (C) 2001 Adam Heath
12 # Copyright (C) 2004 Martin Schulze
13 #
14 #    This program is free software; you can redistribute it and/or modify
15 #    it under the terms of the GNU General Public License as published by
16 #    the Free Software Foundation; either version 1 of the License, or
17 #    (at your option) any later version.
18 #
19 #    This program is distributed in the hope that it will be useful,
20 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
21 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 #    GNU General Public License for more details.
23 #
24 #    You should have received a copy of the GNU General Public License
25 #    along with this program; if not, write to the Free Software
26 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 #
28
29 =head1 NAME
30
31 Packages::Search - 
32
33 =head1 SYNOPSIS
34
35 =head1 DESCRIPTION
36
37 =over 4
38
39 =cut
40
41 package Packages::Search;
42
43 use strict;
44 use warnings;
45
46 use CGI qw( -oldstyle_urls );
47 use POSIX;
48 use HTML::Entities;
49
50 use Deb::Versions;
51 use Exporter;
52
53 our @ISA = qw( Exporter );
54
55 our @EXPORT_OK = qw( nextlink prevlink indexline
56                      resperpagelink );
57 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
58
59 our $VERSION = 0.01;
60
61 our $USE_PAGED_MODE = 1;
62 use constant DEFAULT_PAGE => 1;
63 use constant DEFAULT_RES_PER_PAGE => 50;
64 our %page_params = ( page => { default => DEFAULT_PAGE,
65                                match => '(\d+)' },
66                      number => { default => DEFAULT_RES_PER_PAGE,
67                                  match => '(\d+)' } );
68
69 our $debug = 0;
70
71 sub parse_params {
72     my ( $cgi, $params_def ) = @_;
73
74     my %params_ret = ( values => {}, errors => {} );
75     my %params;
76     if ($USE_PAGED_MODE) {
77         print "DEBUG: Use PAGED_MODE<br>" if $debug;
78         %params = %$params_def;
79         foreach (keys %page_params) {
80             delete $params{$_};
81         }
82         %params = ( %params, %page_params );
83     } else {
84         %params = %$params_def;
85     }
86
87     foreach my $param ( keys %params ) {
88         
89         print "<hr><p>DEBUG: Param <strong>$param</strong><br>" if $debug;
90
91         my $p_value_orig = $cgi->param($param);
92
93         if (!defined($p_value_orig)
94             && defined $params_def->{$param}{alias}
95             && defined $cgi->param($params_def->{$param}{alias})) {
96             $p_value_orig = $cgi->param($params_def->{$param}{alias});
97             print "DEBUG: Used alias <strong>$params_def->{$param}{alias}</strong><br>"
98                 if $debug;
99         }
100
101         my @p_value = ($p_value_orig);
102
103         print "DEBUG: Value (Orig) ".($p_value_orig||"")."<br>" if $debug;
104
105         if ($params_def->{$param}{array} && defined $p_value_orig) {
106             @p_value = split /$params_def->{$param}{array}/, $p_value_orig;
107             print "DEBUG: Value (Array Split) ".
108                 join('##',@p_value)."<br>" if $debug;
109         }
110
111         if ($params_def->{$param}{match} && defined $p_value_orig) {
112             @p_value = map
113             { $_ =~ m/$params_def->{$param}{match}/; $_ = $1 }
114             @p_value;
115         }
116         @p_value = grep { defined $_ } @p_value;
117
118         print "DEBUG: Value (Match) ".
119             join('##',@p_value)."<br>" if $debug;
120
121         unless (@p_value) {
122             if (defined $params{$param}{default}) {
123                 @p_value = ($params{$param}{default});
124             } else {
125                 @p_value = undef;
126                 $params_ret{errors}{$param} = "undef";
127                 next;
128             }
129         }
130
131         print "DEBUG: Value (Default) ".
132             join('##',@p_value)."<br>" if $debug;
133         my @p_value_no_replace = @p_value;
134
135         if ($params{$param}{replace} && @p_value) {
136             @p_value = ();
137             foreach my $pattern (keys %{$params{$param}{replace}}) {
138                 foreach (@p_value_no_replace) {
139                     if ($_ eq $pattern) {
140                         my $replacement = $params{$param}{replace}{$_};
141                         if (ref $replacement) {
142                             push @p_value, @$replacement;
143                         } else {
144                             push @p_value, $replacement;
145                         }
146                     } else {
147                         push @p_value, $_;
148                     }
149                 }
150             }
151         }
152         
153         print "DEBUG: Value (Final) ".
154             join('##',@p_value)."<br>" if $debug;
155
156         if ($params_def->{$param}{array}) {
157             $params_ret{values}{$param} = {
158                 orig => $p_value_orig,
159                 no_replace => \@p_value_no_replace,
160                 final => \@p_value,
161             };
162         } else {
163             $params_ret{values}{$param} = {
164                 orig => $p_value_orig,
165                 no_replace => $p_value_no_replace[0],
166                 final => $p_value[0],
167             };
168         }
169     }
170
171     if ($USE_PAGED_MODE) {
172         $cgi->delete( "page" );
173         $cgi->delete( "number" );
174     }
175
176     return %params_ret;
177 }
178
179 sub start { 
180     my $params = shift;
181
182     my $page = $params->{values}{page}{final}
183     || DEFAULT_PAGE;
184     my $res_per_page = $params->{values}{number}{final}
185     || DEFAULT_RES_PER_PAGE;
186
187     return 1 if $res_per_page =~ /^all$/i;
188     return $res_per_page * ($page - 1) + 1;
189 }
190
191 sub end {
192     my $params = shift;
193
194     my $page = $params->{values}{page}{final}
195     || DEFAULT_PAGE;
196     my $res_per_page = $params->{values}{number}{final}
197     || DEFAULT_RES_PER_PAGE;
198
199     return $page * $res_per_page;
200 }
201
202 sub indexline {
203     my ($cgi, $params, $num_res) = @_;
204
205     my $index_line = "";
206     my $page = $params->{values}{page}{final}
207     || DEFAULT_PAGE;
208     my $res_per_page = $params->{values}{number}{final}
209     || DEFAULT_RES_PER_PAGE;
210     my $numpages = ceil($num_res /
211                         $res_per_page);
212     for (my $i = 1; $i <= $numpages; $i++) {
213         if ($i == $page) {
214             $index_line .= $i;
215         } else {
216             $index_line .= "<a href=\"".encode_entities($cgi->self_url).
217                 "&amp;page=$i&amp;number=$res_per_page\">".
218                 "$i</a>";
219         }
220         if ($i < $numpages) {
221            $index_line .= " | ";
222         }
223     }
224     return $index_line;
225 }
226
227 sub nextlink {
228     my ($cgi, $params, $no_results ) = @_;
229
230     my $page = $params->{values}{page}{final}
231     || DEFAULT_PAGE;
232     $page++;
233     my $res_per_page = $params->{values}{number}{final}
234     || DEFAULT_RES_PER_PAGE;
235
236     if ((($page-1)*$res_per_page + 1) > $no_results) {
237         return "&gt;&gt;";
238     }
239
240     return "<a href=\"".encode_entities($cgi->self_url).
241         "&amp;page=$page&amp;number=$res_per_page\">&gt;&gt;</a>";
242 }
243
244 sub prevlink {
245     my ($cgi, $params ) = @_;
246
247     my $page = $params->{values}{page}{final}
248     || DEFAULT_PAGE;
249     $page--;
250     if (!$page) {
251         return "&lt;&lt;";
252     }
253
254     my $res_per_page = $params->{values}{number}{final}
255     || DEFAULT_RES_PER_PAGE;
256
257     return "<a href=\"".encode_entities($cgi->self_url).
258         "&amp;page=$page&amp;number=$res_per_page\">&lt;&lt;</a>";
259 }
260
261 sub resperpagelink {
262     my ($cgi, $params, $res_per_page ) = @_;
263
264     my $page;
265     if ($res_per_page =~ /^all$/i) {
266         $page = 1;
267     } else {
268         $page = ceil(start( $params ) / $res_per_page);
269     }
270
271     return "<a href=\"".encode_entities($cgi->self_url).
272         "&amp;page=$page&amp;number=$res_per_page\">$res_per_page</a>";
273 }
274
275
276 1;