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, @msgs, @hints, @notes);
33 @fatal_errors = @errors = @debug = @msgs = @hints = @notes = ();
38 push @fatal_errors, $_[0];
39 $http_code = $_[1] if $_[1];
49 push(@debug, $_[0]) if $debug > $lvl;
57 sub get_errors { (@fatal_errors, @errors) }
59 return unless $debug && @debug;
62 sub get_msgs { @msgs };
63 sub get_hints { @hints };
64 sub get_notes { @notes };
65 sub get_all_messages {
67 errors => [ @fatal_errors, @errors ],
68 debugs => $debug ? \@debug : [],
75 our $USE_PAGED_MODE = 1;
76 use constant DEFAULT_PAGE => 1;
77 use constant DEFAULT_RES_PER_PAGE => 50;
78 our %page_params = ( page => { default => DEFAULT_PAGE,
80 number => { default => DEFAULT_RES_PER_PAGE,
84 my ( $cgi, $params_def, $opts ) = @_;
86 my %params_ret = ( values => {}, errors => {} );
88 if ($USE_PAGED_MODE) {
89 debug( "Use PAGED_MODE", 2 ) if DEBUG;
90 %params = %$params_def;
91 foreach (keys %page_params) {
94 %params = ( %params, %page_params );
96 %params = %$params_def;
99 foreach my $param ( keys %params ) {
101 debug( "Param <strong>$param</strong>", 2 ) if DEBUG;
103 my $p_value_orig = $cgi->param($param);
105 if (!defined($p_value_orig)
106 && defined $params_def->{$param}{alias}
107 && defined $cgi->param($params_def->{$param}{alias})) {
108 $p_value_orig = $cgi->param($params_def->{$param}{alias});
109 debug( "Used alias <strong>$params_def->{$param}{alias}</strong>",
113 my @p_value = ($p_value_orig);
115 debug( "Value (Orig) ".($p_value_orig||""), 2 ) if DEBUG;
117 if ($params_def->{$param}{array} && defined $p_value_orig) {
118 @p_value = split /$params_def->{$param}{array}/, $p_value_orig;
119 debug( "Value (Array Split) ". join('##',@p_value), 2 ) if DEBUG;
122 if ($params_def->{$param}{match} && defined $p_value_orig) {
124 { $_ =~ m/$params_def->{$param}{match}/; $_ = $1 }
127 @p_value = grep { defined $_ } @p_value;
129 debug( "Value (Match) ". join('##',@p_value), 2 ) if DEBUG;
132 if (defined $params{$param}{default}) {
133 @p_value = ($params{$param}{default});
136 $params_ret{errors}{$param} = "undef";
141 debug( "Value (Default) ". join('##',@p_value), 2 ) if DEBUG;
142 my @p_value_no_replace = @p_value;
144 if ($params{$param}{replace} && @p_value) {
145 foreach my $pattern (keys %{$params{$param}{replace}}) {
146 my @p_value_tmp = @p_value;
148 foreach (@p_value_tmp) {
149 if ($_ eq $pattern) {
150 my $replacement = $params{$param}{replace}{$_};
151 if (ref $replacement) {
152 push @p_value, @$replacement;
154 push @p_value, $replacement;
163 debug( "Value (Final) ". join('##',@p_value), 2 ) if DEBUG;
165 if ($params_def->{$param}{array}) {
166 $params_ret{values}{$param} = {
167 orig => $p_value_orig,
168 no_replace => \@p_value_no_replace,
171 @{$params_def->{$param}{var}} = @p_value
172 if $params_def->{$param}{var};
174 $params_ret{values}{$param} = {
175 orig => $p_value_orig,
176 no_replace => $p_value_no_replace[0],
177 final => $p_value[0],
179 ${$params_def->{$param}{var}} = $p_value[0]
180 if $params_def->{$param}{var};
182 $opts->{$param} = $params_ret{values}{$param}{final} if $opts;
185 if ($USE_PAGED_MODE) {
186 $cgi->delete( "page" );
187 $cgi->delete( "number" );
196 my $page = $params->{values}{page}{final}
198 my $res_per_page = $params->{values}{number}{final}
199 || DEFAULT_RES_PER_PAGE;
201 return 1 if $res_per_page =~ /^all$/i;
202 return $res_per_page * ($page - 1) + 1;
209 debug( "end: ".Dumper($params) ) if DEBUG;
210 my $page = $params->{page}
212 my $res_per_page = $params->{number}
213 || DEFAULT_RES_PER_PAGE;
215 return $page * $res_per_page;
219 my ($cgi, $params, $num_res) = @_;
222 my $page = $params->{page}
224 my $res_per_page = $params->{number}
225 || DEFAULT_RES_PER_PAGE;
226 my $numpages = ceil($num_res /
228 for (my $i = 1; $i <= $numpages; $i++) {
232 $index_line .= "<a href=\"".encode_entities($cgi->self_url).
233 "&page=$i&number=$res_per_page\">".
236 if ($i < $numpages) {
237 $index_line .= " | ";
244 my ($cgi, $params, $no_results ) = @_;
246 my $page = $params->{page}
249 my $res_per_page = $params->{number}
250 || DEFAULT_RES_PER_PAGE;
252 if ((($page-1)*$res_per_page + 1) > $no_results) {
256 return "<a href=\"".encode_entities($cgi->self_url).
257 "&page=$page&number=$res_per_page\">>></a>";
261 my ($cgi, $params ) = @_;
263 my $page = $params->{page}
270 my $res_per_page = $params->{number}
271 || DEFAULT_RES_PER_PAGE;
273 return "<a href=\"".encode_entities($cgi->self_url).
274 "&page=$page&number=$res_per_page\"><<</a>";
278 my ($cgi, $params, $res_per_page ) = @_;
281 if ($res_per_page =~ /^all$/i) {
284 $page = ceil(start( $params ) / $res_per_page);
287 return "<a href=\"".encode_entities($cgi->self_url).
288 "&page=$page&number=$res_per_page\">$res_per_page</a>";
292 my ( $input, $no_results, $opts ) = @_;
295 if ($no_results > $opts->{number}) {
297 $index_line = prevlink( $input, $opts)." | ".
298 indexline( $input, $opts, $no_results)." | ".
299 nextlink( $input, $opts, $no_results);
301 print "<p style=\"text-align:center\">$index_line</p>";
305 #sub multipageheader {
306 # my ( $input, $no_results, $opts ) = @_;
309 # if ($opts->{number} =~ /^all$/i) {
311 # $end = $no_results;
312 # $opts->{number} = $no_results;
313 # $opts->{number_all}++;
315 # $start = Packages::Search::start( $opts );
316 # $end = Packages::Search::end( $opts );
317 # if ($end > $no_results) { $end = $no_results; }
320 # print "<p>Found <em>$no_results</em> matching packages,";
321 # if ($end == $start) {
322 # print " displaying package $end.</p>";
324 # print " displaying packages $start to $end.</p>";
327 # printindexline( $input, $no_results, $opts );
329 # if ($no_results > 100) {
330 # print "<p>Results per page: ";
331 # my @resperpagelinks;
332 # for (50, 100, 200) {
333 # if ($opts->{number} == $_) {
334 # push @resperpagelinks, $_;
336 # push @resperpagelinks, resperpagelink($input,$opts,$_);
339 # if ($opts->{number_all}) {
340 # push @resperpagelinks, "all";
342 # push @resperpagelinks, resperpagelink($input, $opts, "all");
344 # print join( " | ", @resperpagelinks )."</p>";
346 # return ( $start, $end );
352 $string =~ s/[^\w:.-]/_/g;
356 our ( %url_params, %query_params );
359 my ($input, $params, $opts) = @_;
364 if ($params->{values}{lang}{orig} &&
365 (my $l = $params->{values}{lang}{no_replace})) {
366 $url_params{lang} = $l;
368 if ($params->{values}{source}{no_replace}) {
369 $url_params{source} = 'source';
370 $query_params{source} = 1;
372 foreach my $p (qw(suite arch)) {
373 if ($params->{values}{$p}{orig}
374 && (ref $params->{values}{$p}{final} eq 'ARRAY')
375 && @{$params->{values}{$p}{final}}) {
376 if (@{$params->{values}{$p}{final}} == 1) {
377 $url_params{$p} = $params->{values}{$p}{final}[0];
380 join(",",@{$params->{values}{$p}{no_replace}});
384 foreach my $p (qw(format searchon mode exact debug)) {
385 if ($params->{values}{$p}{orig}
386 && (my $pv = $params->{values}{$p}{no_replace})) {
387 $url_params{$p} = $pv;
392 debug( join("\n",Dumper(\%url_params,\%query_params)), 2 ) if DEBUG;
396 my ($add_path, $add_query, @override) = @_;
397 my (@path, @query_string) = ()x2;
399 if (ref $override[0]) {
400 $override = $override[0];
401 } elsif (@override) {
402 $override = { @override };
405 push @path, $Packages::Config::ROOT;
406 foreach my $p (qw(lang source suite archive arch)) {
407 my $val = $url_params{$p};
408 $val = $override->{$p} if exists $override->{$p};
409 push @path, $val if $val;
411 foreach my $p (qw(format debug)) {
412 my $val = $url_params{$p};
413 $val = $query_params{$p} if exists $query_params{$p};
414 $val = $override->{$p} if exists $override->{$p};
415 push @query_string, "$p=$val" if $val;
417 push @path, $add_path if $add_path and $add_path ne '/';
418 push @query_string, $add_query if $add_query;
420 my $path = join( '/', @path );
421 my $query_string = join( '&', @query_string );
422 $path .= '/' if $add_path and $add_path eq '/';
423 $path .= "?$query_string" if $query_string;
428 sub make_search_url {
429 my ($add_path, $add_query, @override) = @_;
430 my (@path, @query_string) = ()x2;
432 if (ref $override[0]) {
433 $override = $override[0];
434 } elsif (@override) {
435 $override = { @override };
438 push @path, $Packages::Config::SEARCH_URL
439 if $Packages::Config::SEARCH_URL;
440 foreach my $p (qw(lang source suite archive section subsection
441 arch exact mode searchon format debug)) {
442 my $val = $url_params{$p};
443 $val = $query_params{$p} if exists $query_params{$p};
444 $val = $override->{$p} if exists $override->{$p};
445 push @query_string, "$p=$val" if $val;
447 push @path, $add_path if $add_path;
448 push @query_string, $add_query if $add_query;
450 my $path = join( '/', @path );
451 my $query_string = join( '&', @query_string );
453 return "$path?$query_string";