8 our @ISA = qw( Exporter );
9 our @EXPORT = qw( DEBUG debug fatal_error get_mime );
10 our @EXPORT_OK = qw( error hint msg note get_all_messages
11 make_url make_search_url );
13 # define this to 0 in production mode
14 use constant DEBUG => 1;
19 'txt.gz' => 'text/plain',
21 rss => 'application/rss+xml',
22 rfc822 => 'text/plain',
26 return $mime_types{$_[0]} || $_[1] || 'text/html';
29 our (@fatal_errors, @errors, @debug, @hints);
33 @fatal_errors = @errors = @debug = @hints = ();
38 push @fatal_errors, $_[0];
39 $http_code = $_[1] if $_[1];
49 push(@debug, $_[0]) if $debug > $lvl;
51 sub get_errors { (@fatal_errors, @errors) }
53 return unless $debug && @debug;
56 sub get_hints { @hints };
57 sub get_all_messages {
59 errors => [ @fatal_errors, @errors ],
60 debugs => $debug ? \@debug : [],
65 our $USE_PAGED_MODE = 1;
66 use constant DEFAULT_PAGE => 1;
67 use constant DEFAULT_RES_PER_PAGE => 50;
68 our %page_params = ( page => { default => DEFAULT_PAGE,
70 number => { default => DEFAULT_RES_PER_PAGE,
74 my ( $cgi, $params_def, $opts ) = @_;
76 my %params_ret = ( values => {}, errors => {} );
78 if ($USE_PAGED_MODE) {
79 debug( "Use PAGED_MODE", 2 ) if DEBUG;
80 %params = %$params_def;
81 foreach (keys %page_params) {
84 %params = ( %params, %page_params );
86 %params = %$params_def;
89 foreach my $param ( keys %params ) {
91 debug( "Param $param", 2 ) if DEBUG;
93 my $p_value_orig = $cgi->param($param);
95 if (!defined($p_value_orig)
96 && defined $params_def->{$param}{alias}
97 && defined $cgi->param($params_def->{$param}{alias})) {
98 $p_value_orig = $cgi->param($params_def->{$param}{alias});
99 debug( "Used alias <strong>$params_def->{$param}{alias}</strong>",
103 my @p_value = ($p_value_orig);
105 debug( "Value (Orig) ".($p_value_orig||""), 2 ) if DEBUG;
107 if ($params_def->{$param}{array} && defined $p_value_orig) {
108 @p_value = split /$params_def->{$param}{array}/, $p_value_orig;
109 debug( "Value (Array Split) ". join('##',@p_value), 2 ) if DEBUG;
112 if ($params_def->{$param}{match} && defined $p_value_orig) {
114 { $_ =~ m/$params_def->{$param}{match}/; $_ = $1 }
117 @p_value = grep { defined $_ } @p_value;
119 debug( "Value (Match) ". join('##',@p_value), 2 ) if DEBUG;
122 if (defined $params{$param}{default}) {
123 @p_value = ($params{$param}{default});
126 $params_ret{errors}{$param} = "undef";
131 debug( "Value (Default) ". join('##',@p_value), 2 ) if DEBUG;
132 my @p_value_no_replace = @p_value;
134 if ($params{$param}{replace} && @p_value) {
135 foreach my $pattern (keys %{$params{$param}{replace}}) {
136 my @p_value_tmp = @p_value;
138 foreach (@p_value_tmp) {
139 if ($_ eq $pattern) {
140 my $replacement = $params{$param}{replace}{$_};
141 if (ref $replacement) {
142 push @p_value, @$replacement;
144 push @p_value, $replacement;
153 debug( "Value (Final) ". join('##',@p_value), 2 ) if DEBUG;
155 if ($params_def->{$param}{array}) {
156 $params_ret{values}{$param} = {
157 orig => $p_value_orig,
158 no_replace => \@p_value_no_replace,
161 @{$params_def->{$param}{var}} = @p_value
162 if $params_def->{$param}{var};
164 $params_ret{values}{$param} = {
165 orig => $p_value_orig,
166 no_replace => $p_value_no_replace[0],
167 final => $p_value[0],
169 ${$params_def->{$param}{var}} = $p_value[0]
170 if $params_def->{$param}{var};
172 $opts->{$param} = $params_ret{values}{$param}{final} if $opts;
175 if ($USE_PAGED_MODE) {
176 $cgi->delete( "page" );
177 $cgi->delete( "number" );
186 my $page = $params->{values}{page}{final}
188 my $res_per_page = $params->{values}{number}{final}
189 || DEFAULT_RES_PER_PAGE;
191 return 1 if $res_per_page =~ /^all$/i;
192 return $res_per_page * ($page - 1) + 1;
199 debug( "end: ".Dumper($params) ) if DEBUG;
200 my $page = $params->{page}
202 my $res_per_page = $params->{number}
203 || DEFAULT_RES_PER_PAGE;
205 return $page * $res_per_page;
209 my ($cgi, $params, $num_res) = @_;
212 my $page = $params->{page}
214 my $res_per_page = $params->{number}
215 || DEFAULT_RES_PER_PAGE;
216 my $numpages = ceil($num_res /
218 for (my $i = 1; $i <= $numpages; $i++) {
222 $index_line .= "<a href=\"".encode_entities($cgi->self_url).
223 "&page=$i&number=$res_per_page\">".
226 if ($i < $numpages) {
227 $index_line .= " | ";
234 my ($cgi, $params, $no_results ) = @_;
236 my $page = $params->{page}
239 my $res_per_page = $params->{number}
240 || DEFAULT_RES_PER_PAGE;
242 if ((($page-1)*$res_per_page + 1) > $no_results) {
246 return "<a href=\"".encode_entities($cgi->self_url).
247 "&page=$page&number=$res_per_page\">>></a>";
251 my ($cgi, $params ) = @_;
253 my $page = $params->{page}
260 my $res_per_page = $params->{number}
261 || DEFAULT_RES_PER_PAGE;
263 return "<a href=\"".encode_entities($cgi->self_url).
264 "&page=$page&number=$res_per_page\"><<</a>";
268 my ($cgi, $params, $res_per_page ) = @_;
271 if ($res_per_page =~ /^all$/i) {
274 $page = ceil(start( $params ) / $res_per_page);
277 return "<a href=\"".encode_entities($cgi->self_url).
278 "&page=$page&number=$res_per_page\">$res_per_page</a>";
282 my ( $input, $no_results, $opts ) = @_;
285 if ($no_results > $opts->{number}) {
287 $index_line = prevlink( $input, $opts)." | ".
288 indexline( $input, $opts, $no_results)." | ".
289 nextlink( $input, $opts, $no_results);
291 print "<p style=\"text-align:center\">$index_line</p>";
295 #sub multipageheader {
296 # my ( $input, $no_results, $opts ) = @_;
299 # if ($opts->{number} =~ /^all$/i) {
301 # $end = $no_results;
302 # $opts->{number} = $no_results;
303 # $opts->{number_all}++;
305 # $start = Packages::Search::start( $opts );
306 # $end = Packages::Search::end( $opts );
307 # if ($end > $no_results) { $end = $no_results; }
310 # print "<p>Found <em>$no_results</em> matching packages,";
311 # if ($end == $start) {
312 # print " displaying package $end.</p>";
314 # print " displaying packages $start to $end.</p>";
317 # printindexline( $input, $no_results, $opts );
319 # if ($no_results > 100) {
320 # print "<p>Results per page: ";
321 # my @resperpagelinks;
322 # for (50, 100, 200) {
323 # if ($opts->{number} == $_) {
324 # push @resperpagelinks, $_;
326 # push @resperpagelinks, resperpagelink($input,$opts,$_);
329 # if ($opts->{number_all}) {
330 # push @resperpagelinks, "all";
332 # push @resperpagelinks, resperpagelink($input, $opts, "all");
334 # print join( " | ", @resperpagelinks )."</p>";
336 # return ( $start, $end );
342 $string =~ s/[^\w:.-]/_/g;
346 our ( %url_params, %query_params );
349 my ($input, $params, $opts) = @_;
354 if ($params->{values}{lang}{orig} &&
355 (my $l = $params->{values}{lang}{no_replace})) {
356 $url_params{lang} = $l;
358 if ($params->{values}{source}{no_replace}) {
359 $url_params{source} = 'source';
360 $query_params{source} = 1;
362 foreach my $p (qw(suite arch)) {
363 if ($params->{values}{$p}{orig}
364 && (ref $params->{values}{$p}{final} eq 'ARRAY')
365 && @{$params->{values}{$p}{final}}) {
366 if (@{$params->{values}{$p}{final}} == 1) {
367 $url_params{$p} = $params->{values}{$p}{final}[0];
370 join(",",@{$params->{values}{$p}{no_replace}});
374 foreach my $p (qw(format searchon mode exact debug)) {
375 if ($params->{values}{$p}{orig}
376 && (my $pv = $params->{values}{$p}{no_replace})) {
377 $url_params{$p} = $pv;
382 debug( join("\n",Dumper(\%url_params,\%query_params)), 2 ) if DEBUG;
386 my ($add_path, $add_query, @override) = @_;
387 my (@path, @query_string) = ()x2;
389 if (ref $override[0]) {
390 $override = $override[0];
391 } elsif (@override) {
392 $override = { @override };
395 push @path, $Packages::Config::ROOT;
396 foreach my $p (qw(lang source suite archive arch)) {
397 my $val = $url_params{$p};
398 $val = $override->{$p} if exists $override->{$p};
399 push @path, $val if $val;
401 foreach my $p (qw(format debug)) {
402 my $val = $url_params{$p};
403 $val = $query_params{$p} if exists $query_params{$p};
404 $val = $override->{$p} if exists $override->{$p};
405 push @query_string, "$p=$val" if $val;
407 push @path, $add_path if $add_path and $add_path ne '/';
408 push @query_string, $add_query if $add_query;
410 my $path = join( '/', @path );
411 my $query_string = join( '&', @query_string );
412 $path .= '/' if $add_path and $add_path eq '/';
413 $path .= "?$query_string" if $query_string;
418 sub make_search_url {
419 my ($add_path, $add_query, @override) = @_;
420 my (@path, @query_string) = ()x2;
422 if (ref $override[0]) {
423 $override = $override[0];
424 } elsif (@override) {
425 $override = { @override };
428 push @path, $Packages::Config::SEARCH_URL
429 if $Packages::Config::SEARCH_URL;
430 foreach my $p (qw(lang source suite archive section subsection
431 arch exact mode searchon format debug)) {
432 my $val = $url_params{$p};
433 $val = $query_params{$p} if exists $query_params{$p};
434 $val = $override->{$p} if exists $override->{$p};
435 push @query_string, "$p=$val" if $val;
437 push @path, $add_path if $add_path;
438 push @query_string, $add_query if $add_query;
440 my $path = join( '/', @path );
441 my $query_string = join( '&', @query_string );
443 return "$path?$query_string";