1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later version. See the
3 # file README and COPYING for more information.
5 # [Other people have contributed to this file; their copyrights should
7 # Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
10 package Debbugs::CGI::Pkgreport;
14 Debbugs::CGI::Pkgreport -- specific routines for the pkgreport cgi script
30 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
31 use Exporter qw(import);
34 use Params::Validate qw(validate_with :types);
36 use Debbugs::Config qw(:config :globals);
37 use Debbugs::CGI qw(:url :html :util);
38 use Debbugs::Common qw(:misc :util :date);
39 use Debbugs::Status qw(:status);
40 use Debbugs::Bugs qw(bug_filter);
41 use Debbugs::Packages qw(:mapping);
43 use Debbugs::Text qw(:templates);
44 use Encode qw(decode_utf8);
46 use POSIX qw(strftime);
50 ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
51 $DEBUG = 0 unless defined $DEBUG;
54 %EXPORT_TAGS = (html => [qw(short_bug_status_html pkg_htmlizebugs),
56 misc => [qw(generate_package_info),
57 qw(determine_ordering),
61 Exporter::export_ok_tags(keys %EXPORT_TAGS);
62 $EXPORT_TAGS{all} = [@EXPORT_OK];
65 =head2 generate_package_info
67 generate_package_info($srcorbin,$package)
69 Generates the informational bits for a package and returns it
73 sub generate_package_info{
74 my %param = validate_with(params => \@_,
75 spec => {binary => {type => BOOLEAN,
78 package => {type => SCALAR,#|ARRAYREF,
80 options => {type => HASHREF,
82 bugs => {type => ARRAYREF,
84 schema => {type => OBJECT,
90 my $output_scalar = '';
91 my $output = globify_scalar(\$output_scalar);
93 my $package = $param{package};
95 my %pkgsrc = %{getpkgsrc()};
96 my $srcforpkg = $package;
99 binary_to_source(source_only => 1,
102 exists $param{schema}?(schema => $param{schema}):(),
106 my $showpkg = html_escape($package);
107 my @maint = package_maintainer($param{binary}?'binary':'source',
111 print {$output} '<p>';
112 print {$output} (@maint > 1? "Maintainer for $showpkg is "
113 : "Maintainers for $showpkg are ") .
114 package_links(maintainer => \@maint);
115 print {$output} ".</p>\n";
118 print {$output} "<p>There is no maintainer for $showpkg. ".
119 "This means that this package no longer exists (or never existed). ".
120 "Please do not report new bugs against this package. </p>\n";
122 my @pkgs = getsrcpkgs($srcforpkg);
123 @pkgs = grep( !/^\Q$package\E$/, @pkgs );
126 if ($param{binary}) {
127 print {$output} "<p>You may want to refer to the following packages that are part of the same source:\n";
130 print {$output} "<p>You may want to refer to the following individual bug pages:\n";
132 #push @pkgs, $src if ( $src && !grep(/^\Q$src\E$/, @pkgs) );
133 print {$output} scalar package_links(package=>[@pkgs]);
134 print {$output} ".\n";
137 my $pseudodesc = getpseudodesc();
138 if ($package and defined($pseudodesc) and exists($pseudodesc->{$package})) {
139 push @references, "to the <a href=\"$config{web_domain}/pseudo-packages$config{html_suffix}\">".
140 "list of other pseudo-packages</a>";
143 if ($package and defined $config{package_pages} and length $config{package_pages}) {
144 push @references, sprintf "to the <a href=\"%s\">%s package page</a>",
145 html_escape("$config{package_pages}/$package"), html_escape("$package");
147 if (defined $config{package_tracking_domain} and
148 length $config{package_tracking_domain}) {
149 my $ptslink = $param{binary} ? $srcforpkg : $package;
150 # the pts only wants the source, and doesn't care about src: (#566089)
151 $ptslink =~ s/^src://;
152 push @references, q(to the <a href=").html_escape("$config{package_tracking_domain}/$ptslink").q(">Package Tracking System</a>);
154 # Only output this if the source listing is non-trivial.
155 if ($param{binary} and $srcforpkg) {
157 "to the source package ".
158 package_links(src=>$srcforpkg,
159 options => $param{options}) .
164 $references[$#references] = "or $references[$#references]" if @references > 1;
165 print {$output} "<p>You might like to refer ", join(", ", @references), ".</p>\n";
168 print {$output} "<p>If you find a bug not listed here, please\n";
169 printf {$output} "<a href=\"%s\">report it</a>.</p>\n",
170 html_escape("$config{web_domain}/Reporting$config{html_suffix}");
172 return decode_utf8($output_scalar);
176 =head2 short_bug_status_html
178 print short_bug_status_html(status => read_bug(bug => 5),
184 =item status -- status hashref as returned by read_bug
186 =item options -- hashref of options to pass to package_links (defaults
189 =item bug_options -- hashref of options to pass to bug_links (default
192 =item snippet -- optional snippet of information about the bug to
202 sub short_bug_status_html {
203 my %param = validate_with(params => \@_,
204 spec => {status => {type => HASHREF,
206 options => {type => HASHREF,
209 bug_options => {type => HASHREF,
212 snippet => {type => SCALAR,
218 my %status = %{$param{status}};
220 $status{tags_array} = [sort(split(/\s+/, $status{tags}))];
221 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
222 $status{mergedwith_array} = [split(/ /,$status{mergedwith})];
224 my @blockedby= split(/ /, $status{blockedby});
225 $status{blockedby_array} = [];
226 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
227 for my $b (@blockedby) {
228 my %s = %{get_bug_status($b)};
229 next if (defined $s{pending} and $s{pending} eq 'fixed') or (defined $s{done} and length $s{done});
230 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
234 my @blocks= split(/ /, $status{blocks});
235 $status{blocks_array} = [];
236 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
237 for my $b (@blocks) {
238 my %s = %{get_bug_status($b)};
239 next if (defined $s{pending} and $s{pending} eq 'fixed') or (defined $s{done} and length $s{done});
240 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
243 my $days = bug_archiveable(bug => $status{id},
247 $status{archive_days} = $days;
248 return fill_in_template(template => 'cgi/short_bug_status',
249 variables => {status => \%status,
250 isstrongseverity => \&Debbugs::Status::isstrongseverity,
251 html_escape => \&Debbugs::CGI::html_escape,
252 looks_like_number => \&Scalar::Util::looks_like_number,
254 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
255 '&bug_links' => \&Debbugs::CGI::bug_links,
256 '&version_url' => \&Debbugs::CGI::version_url,
257 '&secs_to_english' => \&Debbugs::Common::secs_to_english,
258 '&strftime' => \&POSIX::strftime,
259 '&maybelink' => \&Debbugs::CGI::maybelink,
265 sub pkg_htmlizebugs {
266 my %param = validate_with(params => \@_,
267 spec => {bugs => {type => ARRAYREF,
269 names => {type => ARRAYREF,
271 title => {type => ARRAYREF,
273 prior => {type => ARRAYREF,
275 order => {type => ARRAYREF,
277 ordering => {type => SCALAR,
279 bugusertags => {type => HASHREF,
282 bug_rev => {type => BOOLEAN,
285 bug_order => {type => SCALAR,
287 repeatmerged => {type => BOOLEAN,
290 include => {type => ARRAYREF,
293 exclude => {type => ARRAYREF,
296 this => {type => SCALAR,
299 options => {type => HASHREF,
302 dist => {type => SCALAR,
305 schema => {type => OBJECT,
310 my @bugs = @{$param{bugs}};
315 my $footer = "<h2 class=\"outstanding\">Summary</h2>\n";
318 return "<HR><H2>No reports found!</H2></HR>\n";
321 if ( $param{bug_rev} ) {
322 @bugs = sort {$b<=>$a} @bugs;
325 @bugs = sort {$a<=>$b} @bugs;
330 'show_list_header' => 1,
331 'show_list_footer' => 1,
335 # Make the include/exclude map
338 for my $include (make_list($param{include})) {
339 next unless defined $include;
340 my ($key,$value) = split /\s*:\s*/,$include,2;
341 unless (defined $value) {
345 push @{$include{$key}}, split /\s*,\s*/, $value;
347 for my $exclude (make_list($param{exclude})) {
348 next unless defined $exclude;
349 my ($key,$value) = split /\s*:\s*/,$exclude,2;
350 unless (defined $value) {
354 push @{$exclude{$key}}, split /\s*,\s*/, $value;
357 my $binary_to_source_cache = {};
358 foreach my $bug (@bugs) {
359 my %status = %{get_bug_status(bug=>$bug,
360 (map {exists $param{$_}?($_,$param{$_}):()}
361 qw(dist version schema bugusertags)
363 (exists $param{arch}?(arch => $param{arch}):(arch => $config{default_architectures})),
364 binary_to_source_cache => $binary_to_source_cache,
367 next if bug_filter(bug => $bug,
369 repeat_merged => $param{repeatmerged},
370 seen_merged => \%seenmerged,
371 (keys %include ? (include => \%include):()),
372 (keys %exclude ? (exclude => \%exclude):()),
375 my $html = "<li>"; #<a href=\"%s\">#%d: %s</a>\n<br>",
376 $html .= short_bug_status_html(status => \%status,
377 options => $param{options},
379 push @status, [ $bug, \%status, $html ];
381 if ($param{bug_order} eq 'age') {
383 @status = sort {$a->[1]{log_modified} <=> $b->[1]{log_modified}} @status;
385 elsif ($param{bug_order} eq 'agerev') {
386 @status = sort {$b->[1]{log_modified} <=> $a->[1]{log_modified}} @status;
388 for my $entry (@status) {
390 for my $i (0..$#{$param{prior}}) {
391 my $v = get_bug_order_index($param{prior}[$i], $entry->[1]);
392 $count{"g_${i}_${v}"}++;
395 $section{$key} .= $entry->[2];
400 if ($param{ordering} eq "raw") {
401 $result .= "<UL class=\"bugs\">\n" . join("", map( { $_->[ 2 ] } @status ) ) . "</UL>\n";
404 $header .= "<div class=\"msgreceived\">\n<ul>\n";
405 my @keys_in_order = ("");
406 for my $o (@{$param{order}}) {
407 push @keys_in_order, "X";
408 while ((my $k = shift @keys_in_order) ne "X") {
411 push @keys_in_order, "${k}_${k2}";
415 for my $order (@keys_in_order) {
416 next unless defined $section{$order};
417 my @ttl = split /_/, $order;
419 my $title = $param{title}[0]->[$ttl[0]] . " bugs";
422 $title .= join("; ", grep {($_ || "") ne ""}
423 map { $param{title}[$_]->[$ttl[$_]] } 1..$#ttl);
425 $title = html_escape($title);
427 my $count = $count{"_$order"};
428 my $bugs = $count == 1 ? "bug" : "bugs";
430 $header .= "<li><a href=\"#$order\">$title</a> ($count $bugs)</li>\n";
431 if ($common{show_list_header}) {
432 my $count = $count{"_$order"};
433 my $bugs = $count == 1 ? "bug" : "bugs";
434 $result .= "<H2 CLASS=\"outstanding\"><a name=\"$order\"></a>$title ($count $bugs)</H2>\n";
437 $result .= "<H2 CLASS=\"outstanding\">$title</H2>\n";
439 $result .= "<div class=\"msgreceived\">\n<UL class=\"bugs\">\n";
440 $result .= "\n\n\n\n";
441 $result .= $section{$order};
442 $result .= "\n\n\n\n";
443 $result .= "</UL>\n</div>\n";
445 $header .= "</ul></div>\n";
447 $footer .= "<div class=\"msgreceived\">\n<ul>\n";
448 for my $i (0..$#{$param{prior}}) {
449 my $local_result = '';
450 foreach my $key ( @{$param{order}[$i]} ) {
451 my $count = $count{"g_${i}_$key"};
452 next if !$count or !$param{title}[$i]->[$key];
453 $local_result .= "<li>$count $param{title}[$i]->[$key]</li>\n";
455 if ( $local_result ) {
456 $footer .= "<li>$param{names}[$i]<ul>\n$local_result</ul></li>\n";
459 $footer .= "</ul>\n</div>\n";
462 $result = $header . $result if ( $common{show_list_header} );
463 $result .= $footer if ( $common{show_list_footer} );
467 sub parse_order_statement_into_boolean {
468 my ($statement,$status,$tags) = @_;
470 if (not defined $tags) {
471 $tags = {map { $_, 1 } split / /, $status->{"tags"}
473 if defined $status->{"tags"};
476 # replace all + with &&
477 $statement =~ s/\+/&&/g;
478 # replace all , with ||
479 $statement =~ s/,/||/g;
480 $statement =~ s{([^\&\|\=]+) # field
486 $ok = 1 if defined $tags->{$2};
488 $ok = 1 if defined $status->{$1} and
493 # check that the parsed statement is just valid boolean statements
494 if ($statement =~ /^([01\(\)\&\|]+)$/) {
497 # this is an invalid boolean statement
502 sub get_bug_order_index {
506 my $tags = {map { $_, 1 } split / /, $status->{"tags"}
508 if defined $status->{"tags"};
509 for my $el (@${order}) {
510 if (not length $el or
511 parse_order_statement_into_boolean($el,$status,$tags)
520 # sets: my @names; my @prior; my @title; my @order;
522 sub determine_ordering {
523 my %param = validate_with(params => \@_,
524 spec => {cats => {type => HASHREF,
526 param => {type => HASHREF,
528 ordering => {type => SCALARREF,
530 names => {type => ARRAYREF,
532 pend_rev => {type => BOOLEAN,
535 sev_rev => {type => BOOLEAN,
538 prior => {type => ARRAYREF,
540 title => {type => ARRAYREF,
542 order => {type => ARRAYREF,
546 $param{cats}{status}[0]{ord} = [ reverse @{$param{cats}{status}[0]{ord}} ]
547 if ($param{pend_rev});
548 $param{cats}{severity}[0]{ord} = [ reverse @{$param{cats}{severity}[0]{ord}} ]
549 if ($param{sev_rev});
552 if (defined $param{param}{"pri0"}) {
555 while (defined $param{param}{"pri$i"}) {
558 my ($pri) = make_list($param{param}{"pri$i"});
559 if ($pri =~ m/^([^:]*):(.*)$/) {
560 $h->{"nam"} = $1; # overridden later if necesary
561 $h->{"pri"} = [ map { "$1=$_" } (split /,/, $2) ];
564 $h->{"pri"} = [ split /,/, $pri ];
567 ($h->{"nam"}) = make_list($param{param}{"nam$i"})
568 if (defined $param{param}{"nam$i"});
569 $h->{"ord"} = [ map {split /\s*,\s*/} make_list($param{param}{"ord$i"}) ]
570 if (defined $param{param}{"ord$i"});
571 $h->{"ttl"} = [ map {split /\s*,\s*/} make_list($param{param}{"ttl$i"}) ]
572 if (defined $param{param}{"ttl$i"});
577 $param{cats}{"_"} = [@c];
578 ${$param{ordering}} = "_";
581 ${$param{ordering}} = "normal" unless defined $param{cats}{${$param{ordering}}};
587 for my $c (@{$cats->{$o}}) {
588 if (ref($c) eq "HASH") {
592 push @res, get_ordering($cats, $c);
597 my @cats = get_ordering($param{cats}, ${$param{ordering}});
601 $expr =~ s/[+]/ and /g;
602 $expr =~ s/[a-z]+=//g;
609 push @{$param{prior}}, $c->{"pri"};
610 push @{$param{names}}, ($c->{"nam"} || "Bug attribute #" . $i);
611 if (defined $c->{"ord"}) {
612 push @{$param{order}}, $c->{"ord"};
615 push @{$param{order}}, [ 0..$#{$param{prior}[-1]} ];
617 my @t = @{ $c->{"ttl"} } if defined $c->{ttl};
618 if (@t < $#{$param{prior}[-1]}) {
619 push @t, map { toenglish($param{prior}[-1][$_]) } @t..($#{$param{prior}[-1]});
621 push @t, $c->{"def"} || "";
622 push @{$param{title}}, [@t];