]> git.deb.at Git - deb/packages.git/blob - lib/Packages/Search.pm
ee3b7d2e3e56cf6aee45ab7844cd759e1f6151ff
[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, $opts ) = @_;
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             @{$params_def->{$param}{var}} = @p_value
163                 if $params_def->{$param}{var};
164         } else {
165             $params_ret{values}{$param} = {
166                 orig => $p_value_orig,
167                 no_replace => $p_value_no_replace[0],
168                 final => $p_value[0],
169             };
170             ${$params_def->{$param}{var}} = $p_value[0]
171                 if $params_def->{$param}{var};
172         }
173         $opts->{$param} = $params_ret{values}{$param}{final} if $opts;
174     }
175
176     if ($USE_PAGED_MODE) {
177         $cgi->delete( "page" );
178         $cgi->delete( "number" );
179     }
180
181     return %params_ret;
182 }
183
184 sub start { 
185     my $params = shift;
186
187     my $page = $params->{values}{page}{final}
188     || DEFAULT_PAGE;
189     my $res_per_page = $params->{values}{number}{final}
190     || DEFAULT_RES_PER_PAGE;
191
192     return 1 if $res_per_page =~ /^all$/i;
193     return $res_per_page * ($page - 1) + 1;
194 }
195
196 sub end {
197     my $params = shift;
198
199     my $page = $params->{values}{page}{final}
200     || DEFAULT_PAGE;
201     my $res_per_page = $params->{values}{number}{final}
202     || DEFAULT_RES_PER_PAGE;
203
204     return $page * $res_per_page;
205 }
206
207 sub indexline {
208     my ($cgi, $params, $num_res) = @_;
209
210     my $index_line = "";
211     my $page = $params->{values}{page}{final}
212     || DEFAULT_PAGE;
213     my $res_per_page = $params->{values}{number}{final}
214     || DEFAULT_RES_PER_PAGE;
215     my $numpages = ceil($num_res /
216                         $res_per_page);
217     for (my $i = 1; $i <= $numpages; $i++) {
218         if ($i == $page) {
219             $index_line .= $i;
220         } else {
221             $index_line .= "<a href=\"".encode_entities($cgi->self_url).
222                 "&amp;page=$i&amp;number=$res_per_page\">".
223                 "$i</a>";
224         }
225         if ($i < $numpages) {
226            $index_line .= " | ";
227         }
228     }
229     return $index_line;
230 }
231
232 sub nextlink {
233     my ($cgi, $params, $no_results ) = @_;
234
235     my $page = $params->{values}{page}{final}
236     || DEFAULT_PAGE;
237     $page++;
238     my $res_per_page = $params->{values}{number}{final}
239     || DEFAULT_RES_PER_PAGE;
240
241     if ((($page-1)*$res_per_page + 1) > $no_results) {
242         return "&gt;&gt;";
243     }
244
245     return "<a href=\"".encode_entities($cgi->self_url).
246         "&amp;page=$page&amp;number=$res_per_page\">&gt;&gt;</a>";
247 }
248
249 sub prevlink {
250     my ($cgi, $params ) = @_;
251
252     my $page = $params->{values}{page}{final}
253     || DEFAULT_PAGE;
254     $page--;
255     if (!$page) {
256         return "&lt;&lt;";
257     }
258
259     my $res_per_page = $params->{values}{number}{final}
260     || DEFAULT_RES_PER_PAGE;
261
262     return "<a href=\"".encode_entities($cgi->self_url).
263         "&amp;page=$page&amp;number=$res_per_page\">&lt;&lt;</a>";
264 }
265
266 sub resperpagelink {
267     my ($cgi, $params, $res_per_page ) = @_;
268
269     my $page;
270     if ($res_per_page =~ /^all$/i) {
271         $page = 1;
272     } else {
273         $page = ceil(start( $params ) / $res_per_page);
274     }
275
276     return "<a href=\"".encode_entities($cgi->self_url).
277         "&amp;page=$page&amp;number=$res_per_page\">$res_per_page</a>";
278 }
279
280
281 1;