9 our @ISA = qw( Exporter );
10 our @EXPORT = qw( fatal_error error hint debug msg note
11 print_errors print_hints print_debug print_msgs
12 print_notes DEBUG make_url make_search_url );
14 # define this to 0 in production mode
15 use constant DEBUG => 1;
18 our (@fatal_errors, @errors, @debug, @msgs, @hints, @notes);
21 @fatal_errors = @errors = @debug = @msgs = @hints = @notes = ();
25 push @fatal_errors, $_[0];
35 push(@debug, $_[0]) if $debug > $lvl;
44 return unless @fatal_errors || @errors;
45 print '<div class="perror">';
46 foreach ((@fatal_errors, @errors)) {
47 print "<p>ERROR: $_</p>";
52 return unless $debug && @debug;
53 print '<div class="pdebug">';
54 print '<h2>Debugging:</h2><pre>';
62 print '<div class="phints">';
69 print '<div class="pmsgs">';
77 my ( $title, $note ) = @$_;
79 print '<div class="pnotes">';
81 print "<h2>$title</h2>";
85 print "<p>$note</p></div>";
89 our $USE_PAGED_MODE = 1;
90 use constant DEFAULT_PAGE => 1;
91 use constant DEFAULT_RES_PER_PAGE => 50;
92 our %page_params = ( page => { default => DEFAULT_PAGE,
94 number => { default => DEFAULT_RES_PER_PAGE,
98 my ( $cgi, $params_def, $opts ) = @_;
100 my %params_ret = ( values => {}, errors => {} );
102 if ($USE_PAGED_MODE) {
103 debug( "Use PAGED_MODE", 2 ) if DEBUG;
104 %params = %$params_def;
105 foreach (keys %page_params) {
108 %params = ( %params, %page_params );
110 %params = %$params_def;
113 foreach my $param ( keys %params ) {
115 debug( "Param <strong>$param</strong>", 2 ) if DEBUG;
117 my $p_value_orig = $cgi->param($param);
119 if (!defined($p_value_orig)
120 && defined $params_def->{$param}{alias}
121 && defined $cgi->param($params_def->{$param}{alias})) {
122 $p_value_orig = $cgi->param($params_def->{$param}{alias});
123 debug( "Used alias <strong>$params_def->{$param}{alias}</strong>",
127 my @p_value = ($p_value_orig);
129 debug( "Value (Orig) ".($p_value_orig||""), 2 ) if DEBUG;
131 if ($params_def->{$param}{array} && defined $p_value_orig) {
132 @p_value = split /$params_def->{$param}{array}/, $p_value_orig;
133 debug( "Value (Array Split) ". join('##',@p_value), 2 ) if DEBUG;
136 if ($params_def->{$param}{match} && defined $p_value_orig) {
138 { $_ =~ m/$params_def->{$param}{match}/; $_ = $1 }
141 @p_value = grep { defined $_ } @p_value;
143 debug( "Value (Match) ". join('##',@p_value), 2 ) if DEBUG;
146 if (defined $params{$param}{default}) {
147 @p_value = ($params{$param}{default});
150 $params_ret{errors}{$param} = "undef";
155 debug( "Value (Default) ". join('##',@p_value), 2 ) if DEBUG;
156 my @p_value_no_replace = @p_value;
158 if ($params{$param}{replace} && @p_value) {
159 foreach my $pattern (keys %{$params{$param}{replace}}) {
160 my @p_value_tmp = @p_value;
162 foreach (@p_value_tmp) {
163 if ($_ eq $pattern) {
164 my $replacement = $params{$param}{replace}{$_};
165 if (ref $replacement) {
166 push @p_value, @$replacement;
168 push @p_value, $replacement;
177 debug( "Value (Final) ". join('##',@p_value), 2 ) if DEBUG;
179 if ($params_def->{$param}{array}) {
180 $params_ret{values}{$param} = {
181 orig => $p_value_orig,
182 no_replace => \@p_value_no_replace,
185 @{$params_def->{$param}{var}} = @p_value
186 if $params_def->{$param}{var};
188 $params_ret{values}{$param} = {
189 orig => $p_value_orig,
190 no_replace => $p_value_no_replace[0],
191 final => $p_value[0],
193 ${$params_def->{$param}{var}} = $p_value[0]
194 if $params_def->{$param}{var};
196 $opts->{$param} = $params_ret{values}{$param}{final} if $opts;
199 if ($USE_PAGED_MODE) {
200 $cgi->delete( "page" );
201 $cgi->delete( "number" );
210 my $page = $params->{values}{page}{final}
212 my $res_per_page = $params->{values}{number}{final}
213 || DEFAULT_RES_PER_PAGE;
215 return 1 if $res_per_page =~ /^all$/i;
216 return $res_per_page * ($page - 1) + 1;
223 debug( "end: ".Dumper($params) ) if DEBUG;
224 my $page = $params->{page}
226 my $res_per_page = $params->{number}
227 || DEFAULT_RES_PER_PAGE;
229 return $page * $res_per_page;
233 my ($cgi, $params, $num_res) = @_;
236 my $page = $params->{page}
238 my $res_per_page = $params->{number}
239 || DEFAULT_RES_PER_PAGE;
240 my $numpages = ceil($num_res /
242 for (my $i = 1; $i <= $numpages; $i++) {
246 $index_line .= "<a href=\"".encode_entities($cgi->self_url).
247 "&page=$i&number=$res_per_page\">".
250 if ($i < $numpages) {
251 $index_line .= " | ";
258 my ($cgi, $params, $no_results ) = @_;
260 my $page = $params->{page}
263 my $res_per_page = $params->{number}
264 || DEFAULT_RES_PER_PAGE;
266 if ((($page-1)*$res_per_page + 1) > $no_results) {
270 return "<a href=\"".encode_entities($cgi->self_url).
271 "&page=$page&number=$res_per_page\">>></a>";
275 my ($cgi, $params ) = @_;
277 my $page = $params->{page}
284 my $res_per_page = $params->{number}
285 || DEFAULT_RES_PER_PAGE;
287 return "<a href=\"".encode_entities($cgi->self_url).
288 "&page=$page&number=$res_per_page\"><<</a>";
292 my ($cgi, $params, $res_per_page ) = @_;
295 if ($res_per_page =~ /^all$/i) {
298 $page = ceil(start( $params ) / $res_per_page);
301 return "<a href=\"".encode_entities($cgi->self_url).
302 "&page=$page&number=$res_per_page\">$res_per_page</a>";
306 my ( $input, $no_results, $opts ) = @_;
309 if ($no_results > $opts->{number}) {
311 $index_line = prevlink( $input, $opts)." | ".
312 indexline( $input, $opts, $no_results)." | ".
313 nextlink( $input, $opts, $no_results);
315 print "<p style=\"text-align:center\">$index_line</p>";
319 #sub multipageheader {
320 # my ( $input, $no_results, $opts ) = @_;
323 # if ($opts->{number} =~ /^all$/i) {
325 # $end = $no_results;
326 # $opts->{number} = $no_results;
327 # $opts->{number_all}++;
329 # $start = Packages::Search::start( $opts );
330 # $end = Packages::Search::end( $opts );
331 # if ($end > $no_results) { $end = $no_results; }
334 # print "<p>Found <em>$no_results</em> matching packages,";
335 # if ($end == $start) {
336 # print " displaying package $end.</p>";
338 # print " displaying packages $start to $end.</p>";
341 # printindexline( $input, $no_results, $opts );
343 # if ($no_results > 100) {
344 # print "<p>Results per page: ";
345 # my @resperpagelinks;
346 # for (50, 100, 200) {
347 # if ($opts->{number} == $_) {
348 # push @resperpagelinks, $_;
350 # push @resperpagelinks, resperpagelink($input,$opts,$_);
353 # if ($opts->{number_all}) {
354 # push @resperpagelinks, "all";
356 # push @resperpagelinks, resperpagelink($input, $opts, "all");
358 # print join( " | ", @resperpagelinks )."</p>";
360 # return ( $start, $end );
363 our ( %url_params, %query_params );
366 my ($input, $params, $opts) = @_;
371 if ($params->{values}{lang}{orig} &&
372 (my $l = $params->{values}{lang}{no_replace})) {
373 $url_params{lang} = $l;
375 if ($params->{values}{source}{no_replace}) {
376 $url_params{source} = 'source';
377 $query_params{source} = 1;
379 foreach my $p (qw(suite arch)) {
380 if ($params->{values}{$p}{orig}
381 && (ref $params->{values}{$p}{final} eq 'ARRAY')
382 && @{$params->{values}{$p}{final}}) {
383 if (@{$params->{values}{$p}{final}} == 1) {
384 $url_params{$p} = $params->{values}{$p}{final}[0];
387 join(",",@{$params->{values}{$p}{no_replace}});
391 foreach my $p (qw(format searchon mode exact debug)) {
392 if ($params->{values}{$p}{orig}
393 && (my $pv = $params->{values}{$p}{no_replace})) {
394 $url_params{$p} = $pv;
399 debug( join("\n",Dumper(\%url_params,\%query_params)), 2 ) if DEBUG;
403 my ($add_path, $add_query, $override) = @_;
404 my (@path, @query_string) = ()x2;
407 push @path, $Packages::Config::ROOT;
408 foreach my $p (qw(lang source suite archive arch)) {
409 my $val = $url_params{$p};
410 $val = $override->{$p} if exists $override->{$p};
411 push @path, $val if $val;
413 foreach my $p (qw(format debug)) {
414 my $val = $url_params{$p};
415 $val = $query_params{$p} if exists $query_params{$p};
416 $val = $override->{$p} if exists $override->{$p};
417 push @query_string, "$p=$val" if $val;
419 push @path, $add_path if $add_path and $add_path ne '/';
420 push @query_string, $add_query if $add_query;
422 my $path = join( '/', @path );
423 my $query_string = join( '&', @query_string );
424 $path .= '/' if $add_path and $add_path eq '/';
425 $path .= "?$query_string" if $query_string;
430 sub make_search_url {
431 my ($add_path, $add_query, $override) = @_;
432 my (@path, @query_string) = ()x2;
435 push @path, $Packages::Config::SEARCH_URL
436 if $Packages::Config::SEARCH_URL;
437 foreach my $p (qw(lang source suite archive section subsection
438 arch exact mode searchon format debug)) {
439 my $val = $url_params{$p};
440 $val = $query_params{$p} if exists $query_params{$p};
441 $val = $override->{$p} if exists $override->{$p};
442 push @query_string, "$p=$val" if $val;
444 push @path, $add_path if $add_path;
445 push @query_string, $add_query if $add_query;
447 my $path = join( '/', @path );
448 my $query_string = join( '&', @query_string );
450 return "$path?$query_string";