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 hash_slice(%param,qw(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 = source_to_binary(source => $srcforpkg,
123 hash_slice(%param,qw(schema)),
125 # if there are distributions, only bother to
126 # show packages which are currently in a
128 @{$config{distributions}//[]} ?
129 (dist => [@{$config{distributions}}]) : (),
131 @pkgs = grep( !/^\Q$package\E$/, @pkgs );
134 if ($param{binary}) {
135 print {$output} "<p>You may want to refer to the following packages that are part of the same source:\n";
138 print {$output} "<p>You may want to refer to the following individual bug pages:\n";
140 #push @pkgs, $src if ( $src && !grep(/^\Q$src\E$/, @pkgs) );
141 print {$output} scalar package_links(package=>[@pkgs]);
142 print {$output} ".\n";
145 my $pseudodesc = getpseudodesc();
146 if ($package and defined($pseudodesc) and exists($pseudodesc->{$package})) {
147 push @references, "to the <a href=\"$config{web_domain}/pseudo-packages$config{html_suffix}\">".
148 "list of other pseudo-packages</a>";
151 if ($package and defined $config{package_pages} and length $config{package_pages}) {
152 push @references, sprintf "to the <a href=\"%s\">%s package page</a>",
153 html_escape("$config{package_pages}/$package"), html_escape("$package");
155 if (defined $config{package_tracking_domain} and
156 length $config{package_tracking_domain}) {
157 my $ptslink = $param{binary} ? $srcforpkg : $package;
158 # the pts only wants the source, and doesn't care about src: (#566089)
159 $ptslink =~ s/^src://;
160 push @references, q(to the <a href=").html_escape("$config{package_tracking_domain}/$ptslink").q(">Package Tracking System</a>);
162 # Only output this if the source listing is non-trivial.
163 if ($param{binary} and $srcforpkg) {
165 "to the source package ".
166 package_links(src=>$srcforpkg,
167 options => $param{options}) .
172 $references[$#references] = "or $references[$#references]" if @references > 1;
173 print {$output} "<p>You might like to refer ", join(", ", @references), ".</p>\n";
176 print {$output} "<p>If you find a bug not listed here, please\n";
177 printf {$output} "<a href=\"%s\">report it</a>.</p>\n",
178 html_escape("$config{web_domain}/Reporting$config{html_suffix}");
180 return decode_utf8($output_scalar);
184 =head2 short_bug_status_html
186 print short_bug_status_html(status => read_bug(bug => 5),
192 =item status -- status hashref as returned by read_bug
194 =item options -- hashref of options to pass to package_links (defaults
197 =item bug_options -- hashref of options to pass to bug_links (default
200 =item snippet -- optional snippet of information about the bug to
210 sub short_bug_status_html {
211 my %param = validate_with(params => \@_,
212 spec => {status => {type => HASHREF,
214 options => {type => HASHREF,
217 bug_options => {type => HASHREF,
220 snippet => {type => SCALAR,
226 my %status = %{$param{status}};
228 $status{tags_array} = [sort(split(/\s+/, $status{tags}))];
229 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
230 $status{mergedwith_array} = [split(/ /,$status{mergedwith})];
232 my @blockedby= split(/ /, $status{blockedby});
233 $status{blockedby_array} = [];
234 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
235 for my $b (@blockedby) {
236 my %s = %{get_bug_status($b)};
237 next if (defined $s{pending} and $s{pending} eq 'fixed') or (defined $s{done} and length $s{done});
238 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
242 my @blocks= split(/ /, $status{blocks});
243 $status{blocks_array} = [];
244 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
245 for my $b (@blocks) {
246 my %s = %{get_bug_status($b)};
247 next if (defined $s{pending} and $s{pending} eq 'fixed') or (defined $s{done} and length $s{done});
248 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
251 my $days = bug_archiveable(bug => $status{id},
255 $status{archive_days} = $days;
256 return fill_in_template(template => 'cgi/short_bug_status',
257 variables => {status => \%status,
258 isstrongseverity => \&Debbugs::Status::isstrongseverity,
259 html_escape => \&Debbugs::CGI::html_escape,
260 looks_like_number => \&Scalar::Util::looks_like_number,
262 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
263 '&bug_links' => \&Debbugs::CGI::bug_links,
264 '&version_url' => \&Debbugs::CGI::version_url,
265 '&secs_to_english' => \&Debbugs::Common::secs_to_english,
266 '&strftime' => \&POSIX::strftime,
267 '&maybelink' => \&Debbugs::CGI::maybelink,
273 sub pkg_htmlizebugs {
274 my %param = validate_with(params => \@_,
275 spec => {bugs => {type => ARRAYREF,
277 names => {type => ARRAYREF,
279 title => {type => ARRAYREF,
281 prior => {type => ARRAYREF,
283 order => {type => ARRAYREF,
285 ordering => {type => SCALAR,
287 bugusertags => {type => HASHREF,
290 bug_rev => {type => BOOLEAN,
293 bug_order => {type => SCALAR,
295 repeatmerged => {type => BOOLEAN,
298 include => {type => ARRAYREF,
301 exclude => {type => ARRAYREF,
304 this => {type => SCALAR,
307 options => {type => HASHREF,
310 dist => {type => SCALAR,
313 schema => {type => OBJECT,
318 my @bugs = @{$param{bugs}};
323 my $footer = "<h2 class=\"outstanding\">Summary</h2>\n";
326 return "<HR><H2>No reports found!</H2></HR>\n";
329 if ( $param{bug_rev} ) {
330 @bugs = sort {$b<=>$a} @bugs;
333 @bugs = sort {$a<=>$b} @bugs;
338 'show_list_header' => 1,
339 'show_list_footer' => 1,
343 # Make the include/exclude map
346 for my $include (make_list($param{include})) {
347 next unless defined $include;
348 my ($key,$value) = split /\s*:\s*/,$include,2;
349 unless (defined $value) {
353 push @{$include{$key}}, split /\s*,\s*/, $value;
355 for my $exclude (make_list($param{exclude})) {
356 next unless defined $exclude;
357 my ($key,$value) = split /\s*:\s*/,$exclude,2;
358 unless (defined $value) {
362 push @{$exclude{$key}}, split /\s*,\s*/, $value;
365 my $binary_to_source_cache = {};
367 get_bug_statuses(bug => \@bugs,
369 qw(dist version schema bugusertags),
371 (exists $param{arch}?(arch => $param{arch}):(arch => $config{default_architectures})),
372 binary_to_source_cache => $binary_to_source_cache,
374 for my $bug (sort {$a <=> $b} keys %{$statuses}) {
375 next unless %{$statuses->{$bug}};
376 next if bug_filter(bug => $bug,
377 status => $statuses->{$bug},
378 repeat_merged => $param{repeatmerged},
379 seen_merged => \%seenmerged,
380 (keys %include ? (include => \%include):()),
381 (keys %exclude ? (exclude => \%exclude):()),
384 my $html = "<li>"; #<a href=\"%s\">#%d: %s</a>\n<br>",
385 $html .= short_bug_status_html(status => $statuses->{$bug},
386 options => $param{options},
388 push @status, [ $bug, $statuses->{$bug}, $html ];
390 if ($param{bug_order} eq 'age') {
392 @status = sort {$a->[1]{log_modified} <=> $b->[1]{log_modified}} @status;
394 elsif ($param{bug_order} eq 'agerev') {
395 @status = sort {$b->[1]{log_modified} <=> $a->[1]{log_modified}} @status;
397 for my $entry (@status) {
399 for my $i (0..$#{$param{prior}}) {
400 my $v = get_bug_order_index($param{prior}[$i], $entry->[1]);
401 $count{"g_${i}_${v}"}++;
404 $section{$key} .= $entry->[2];
409 if ($param{ordering} eq "raw") {
410 $result .= "<UL class=\"bugs\">\n" . join("", map( { $_->[ 2 ] } @status ) ) . "</UL>\n";
413 $header .= "<div class=\"msgreceived\">\n<ul>\n";
414 my @keys_in_order = ("");
415 for my $o (@{$param{order}}) {
416 push @keys_in_order, "X";
417 while ((my $k = shift @keys_in_order) ne "X") {
420 push @keys_in_order, "${k}_${k2}";
424 for my $order (@keys_in_order) {
425 next unless defined $section{$order};
426 my @ttl = split /_/, $order;
428 my $title = $param{title}[0]->[$ttl[0]] . " bugs";
431 $title .= join("; ", grep {($_ || "") ne ""}
432 map { $param{title}[$_]->[$ttl[$_]] } 1..$#ttl);
434 $title = html_escape($title);
436 my $count = $count{"_$order"};
437 my $bugs = $count == 1 ? "bug" : "bugs";
439 $header .= "<li><a href=\"#$order\">$title</a> ($count $bugs)</li>\n";
440 if ($common{show_list_header}) {
441 my $count = $count{"_$order"};
442 my $bugs = $count == 1 ? "bug" : "bugs";
443 $result .= "<H2 CLASS=\"outstanding\"><a name=\"$order\"></a>$title ($count $bugs)</H2>\n";
446 $result .= "<H2 CLASS=\"outstanding\">$title</H2>\n";
448 $result .= "<div class=\"msgreceived\">\n<UL class=\"bugs\">\n";
449 $result .= "\n\n\n\n";
450 $result .= $section{$order};
451 $result .= "\n\n\n\n";
452 $result .= "</UL>\n</div>\n";
454 $header .= "</ul></div>\n";
456 $footer .= "<div class=\"msgreceived\">\n<ul>\n";
457 for my $i (0..$#{$param{prior}}) {
458 my $local_result = '';
459 foreach my $key ( @{$param{order}[$i]} ) {
460 my $count = $count{"g_${i}_$key"};
461 next if !$count or !$param{title}[$i]->[$key];
462 $local_result .= "<li>$count $param{title}[$i]->[$key]</li>\n";
464 if ( $local_result ) {
465 $footer .= "<li>$param{names}[$i]<ul>\n$local_result</ul></li>\n";
468 $footer .= "</ul>\n</div>\n";
471 $result = $header . $result if ( $common{show_list_header} );
472 $result .= $footer if ( $common{show_list_footer} );
476 sub parse_order_statement_into_boolean {
477 my ($statement,$status,$tags) = @_;
479 if (not defined $tags) {
480 $tags = {map { $_, 1 } split / /, $status->{"tags"}
482 if defined $status->{"tags"};
485 # replace all + with &&
486 $statement =~ s/\+/&&/g;
487 # replace all , with ||
488 $statement =~ s/,/||/g;
489 $statement =~ s{([^\&\|\=]+) # field
495 $ok = 1 if defined $tags->{$2};
497 $ok = 1 if defined $status->{$1} and
502 # check that the parsed statement is just valid boolean statements
503 if ($statement =~ /^([01\(\)\&\|]+)$/) {
506 # this is an invalid boolean statement
511 sub get_bug_order_index {
515 my $tags = {map { $_, 1 } split / /, $status->{"tags"}
517 if defined $status->{"tags"};
518 for my $el (@${order}) {
519 if (not length $el or
520 parse_order_statement_into_boolean($el,$status,$tags)
529 # sets: my @names; my @prior; my @title; my @order;
531 sub determine_ordering {
532 my %param = validate_with(params => \@_,
533 spec => {cats => {type => HASHREF,
535 param => {type => HASHREF,
537 ordering => {type => SCALARREF,
539 names => {type => ARRAYREF,
541 pend_rev => {type => BOOLEAN,
544 sev_rev => {type => BOOLEAN,
547 prior => {type => ARRAYREF,
549 title => {type => ARRAYREF,
551 order => {type => ARRAYREF,
555 $param{cats}{status}[0]{ord} = [ reverse @{$param{cats}{status}[0]{ord}} ]
556 if ($param{pend_rev});
557 $param{cats}{severity}[0]{ord} = [ reverse @{$param{cats}{severity}[0]{ord}} ]
558 if ($param{sev_rev});
561 if (defined $param{param}{"pri0"}) {
564 while (defined $param{param}{"pri$i"}) {
567 my ($pri) = make_list($param{param}{"pri$i"});
568 if ($pri =~ m/^([^:]*):(.*)$/) {
569 $h->{"nam"} = $1; # overridden later if necesary
570 $h->{"pri"} = [ map { "$1=$_" } (split /,/, $2) ];
573 $h->{"pri"} = [ split /,/, $pri ];
576 ($h->{"nam"}) = make_list($param{param}{"nam$i"})
577 if (defined $param{param}{"nam$i"});
578 $h->{"ord"} = [ map {split /\s*,\s*/} make_list($param{param}{"ord$i"}) ]
579 if (defined $param{param}{"ord$i"});
580 $h->{"ttl"} = [ map {split /\s*,\s*/} make_list($param{param}{"ttl$i"}) ]
581 if (defined $param{param}{"ttl$i"});
586 $param{cats}{"_"} = [@c];
587 ${$param{ordering}} = "_";
590 ${$param{ordering}} = "normal" unless defined $param{cats}{${$param{ordering}}};
596 for my $c (@{$cats->{$o}}) {
597 if (ref($c) eq "HASH") {
601 push @res, get_ordering($cats, $c);
606 my @cats = get_ordering($param{cats}, ${$param{ordering}});
610 $expr =~ s/[+]/ and /g;
611 $expr =~ s/[a-z]+=//g;
618 push @{$param{prior}}, $c->{"pri"};
619 push @{$param{names}}, ($c->{"nam"} || "Bug attribute #" . $i);
620 if (defined $c->{"ord"}) {
621 push @{$param{order}}, $c->{"ord"};
624 push @{$param{order}}, [ 0..$#{$param{prior}[-1]} ];
626 my @t = @{ $c->{"ttl"} } if defined $c->{ttl};
627 if (@t < $#{$param{prior}[-1]}) {
628 push @t, map { toenglish($param{prior}[-1][$_]) } @t..($#{$param{prior}[-1]});
630 push @t, $c->{"def"} || "";
631 push @{$param{title}}, [@t];