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,
87 my $output_scalar = '';
88 my $output = globify_scalar(\$output_scalar);
90 my $package = $param{package};
92 my %pkgsrc = %{getpkgsrc()};
93 my $srcforpkg = $package;
94 if ($param{binary} and exists $pkgsrc{$package}
95 and defined $pkgsrc{$package}) {
96 $srcforpkg = $pkgsrc{$package};
99 my $showpkg = html_escape($package);
100 my $maintainers = getmaintainers();
101 my $maint = $maintainers->{$srcforpkg};
102 if (defined $maint) {
103 print {$output} '<p>';
104 print {$output} (($maint =~ /,/)? "Maintainer for $showpkg is "
105 : "Maintainers for $showpkg are ") .
106 package_links(maintainer => $maint);
107 print {$output} ".</p>\n";
110 print {$output} "<p>There is no maintainer for $showpkg. ".
111 "This means that this package no longer exists (or never existed). ".
112 "Please do not report new bugs against this package. </p>\n";
114 my @pkgs = getsrcpkgs($srcforpkg);
115 @pkgs = grep( !/^\Q$package\E$/, @pkgs );
118 if ($param{binary}) {
119 print {$output} "<p>You may want to refer to the following packages that are part of the same source:\n";
122 print {$output} "<p>You may want to refer to the following individual bug pages:\n";
124 #push @pkgs, $src if ( $src && !grep(/^\Q$src\E$/, @pkgs) );
125 print {$output} scalar package_links(package=>[@pkgs]);
126 print {$output} ".\n";
129 my $pseudodesc = getpseudodesc();
130 if ($package and defined($pseudodesc) and exists($pseudodesc->{$package})) {
131 push @references, "to the <a href=\"$config{web_domain}/pseudo-packages$config{html_suffix}\">".
132 "list of other pseudo-packages</a>";
134 elsif (not defined $maint and not @{$param{bugs}}) {
135 # don't bother printing out this information, because it's
136 # already present above.
137 # print {$output} "<p>There is no record of the " . html_escape($package) .
138 # ($param{binary} ? " package" : " source package") .
139 # ", and no bugs have been filed against it.</p>";
142 if ($package and defined $config{package_pages} and length $config{package_pages}) {
143 push @references, sprintf "to the <a href=\"%s\">%s package page</a>",
144 html_escape("$config{package_pages}/$package"), html_escape("$package");
146 if (defined $config{package_tracking_domain} and
147 length $config{package_tracking_domain}) {
148 my $ptslink = $param{binary} ? $srcforpkg : $package;
149 # the pts only wants the source, and doesn't care about src: (#566089)
150 $ptslink =~ s/^src://;
151 push @references, q(to the <a href=").html_escape("$config{package_tracking_domain}/$ptslink").q(">Package Tracking System</a>);
153 # Only output this if the source listing is non-trivial.
154 if ($param{binary} and $srcforpkg) {
156 "to the source package ".
157 package_links(src=>$srcforpkg,
158 options => $param{options}) .
163 $references[$#references] = "or $references[$#references]" if @references > 1;
164 print {$output} "<p>You might like to refer ", join(", ", @references), ".</p>\n";
166 if (defined $maint) {
167 print {$output} "<p>If you find a bug not listed here, please\n";
168 printf {$output} "<a href=\"%s\">report it</a>.</p>\n",
169 html_escape("$config{web_domain}/Reporting$config{html_suffix}");
171 return decode_utf8($output_scalar);
175 =head2 short_bug_status_html
177 print short_bug_status_html(status => read_bug(bug => 5),
183 =item status -- status hashref as returned by read_bug
185 =item options -- hashref of options to pass to package_links (defaults
188 =item bug_options -- hashref of options to pass to bug_links (default
191 =item snippet -- optional snippet of information about the bug to
201 sub short_bug_status_html {
202 my %param = validate_with(params => \@_,
203 spec => {status => {type => HASHREF,
205 options => {type => HASHREF,
208 bug_options => {type => HASHREF,
211 snippet => {type => SCALAR,
217 my %status = %{$param{status}};
219 $status{tags_array} = [sort(split(/\s+/, $status{tags}))];
220 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
221 $status{mergedwith_array} = [split(/ /,$status{mergedwith})];
223 my @blockedby= split(/ /, $status{blockedby});
224 $status{blockedby_array} = [];
225 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
226 for my $b (@blockedby) {
227 my %s = %{get_bug_status($b)};
228 next if (defined $s{pending} and $s{pending} eq 'fixed') or (defined $s{done} and length $s{done});
229 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
233 my @blocks= split(/ /, $status{blocks});
234 $status{blocks_array} = [];
235 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
236 for my $b (@blocks) {
237 my %s = %{get_bug_status($b)};
238 next if (defined $s{pending} and $s{pending} eq 'fixed') or (defined $s{done} and length $s{done});
239 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
242 my $days = bug_archiveable(bug => $status{id},
246 $status{archive_days} = $days;
247 return fill_in_template(template => 'cgi/short_bug_status',
248 variables => {status => \%status,
249 isstrongseverity => \&Debbugs::Status::isstrongseverity,
250 html_escape => \&Debbugs::CGI::html_escape,
251 looks_like_number => \&Scalar::Util::looks_like_number,
253 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
254 '&bug_links' => \&Debbugs::CGI::bug_links,
255 '&version_url' => \&Debbugs::CGI::version_url,
256 '&secs_to_english' => \&Debbugs::Common::secs_to_english,
257 '&strftime' => \&POSIX::strftime,
258 '&maybelink' => \&Debbugs::CGI::maybelink,
264 sub pkg_htmlizebugs {
265 my %param = validate_with(params => \@_,
266 spec => {bugs => {type => ARRAYREF,
268 names => {type => ARRAYREF,
270 title => {type => ARRAYREF,
272 prior => {type => ARRAYREF,
274 order => {type => ARRAYREF,
276 ordering => {type => SCALAR,
278 bugusertags => {type => HASHREF,
281 bug_rev => {type => BOOLEAN,
284 bug_order => {type => SCALAR,
286 repeatmerged => {type => BOOLEAN,
289 include => {type => ARRAYREF,
292 exclude => {type => ARRAYREF,
295 this => {type => SCALAR,
298 options => {type => HASHREF,
301 dist => {type => SCALAR,
304 schema => {type => OBJECT,
309 my @bugs = @{$param{bugs}};
314 my $footer = "<h2 class=\"outstanding\">Summary</h2>\n";
317 return "<HR><H2>No reports found!</H2></HR>\n";
320 if ( $param{bug_rev} ) {
321 @bugs = sort {$b<=>$a} @bugs;
324 @bugs = sort {$a<=>$b} @bugs;
329 'show_list_header' => 1,
330 'show_list_footer' => 1,
334 # Make the include/exclude map
337 for my $include (make_list($param{include})) {
338 next unless defined $include;
339 my ($key,$value) = split /\s*:\s*/,$include,2;
340 unless (defined $value) {
344 push @{$include{$key}}, split /\s*,\s*/, $value;
346 for my $exclude (make_list($param{exclude})) {
347 next unless defined $exclude;
348 my ($key,$value) = split /\s*:\s*/,$exclude,2;
349 unless (defined $value) {
353 push @{$exclude{$key}}, split /\s*,\s*/, $value;
356 my $binary_to_source_cache = {};
357 foreach my $bug (@bugs) {
358 my %status = %{get_bug_status(bug=>$bug,
359 (map {exists $param{$_}?($_,$param{$_}):()}
360 qw(dist version schema bugusertags)
362 (exists $param{arch}?(arch => $param{arch}):(arch => $config{default_architectures})),
363 binary_to_source_cache => $binary_to_source_cache,
366 next if bug_filter(bug => $bug,
368 repeat_merged => $param{repeatmerged},
369 seen_merged => \%seenmerged,
370 (keys %include ? (include => \%include):()),
371 (keys %exclude ? (exclude => \%exclude):()),
374 my $html = "<li>"; #<a href=\"%s\">#%d: %s</a>\n<br>",
375 $html .= short_bug_status_html(status => \%status,
376 options => $param{options},
378 push @status, [ $bug, \%status, $html ];
380 if ($param{bug_order} eq 'age') {
382 @status = sort {$a->[1]{log_modified} <=> $b->[1]{log_modified}} @status;
384 elsif ($param{bug_order} eq 'agerev') {
385 @status = sort {$b->[1]{log_modified} <=> $a->[1]{log_modified}} @status;
387 for my $entry (@status) {
389 for my $i (0..$#{$param{prior}}) {
390 my $v = get_bug_order_index($param{prior}[$i], $entry->[1]);
391 $count{"g_${i}_${v}"}++;
394 $section{$key} .= $entry->[2];
399 if ($param{ordering} eq "raw") {
400 $result .= "<UL class=\"bugs\">\n" . join("", map( { $_->[ 2 ] } @status ) ) . "</UL>\n";
403 $header .= "<div class=\"msgreceived\">\n<ul>\n";
404 my @keys_in_order = ("");
405 for my $o (@{$param{order}}) {
406 push @keys_in_order, "X";
407 while ((my $k = shift @keys_in_order) ne "X") {
410 push @keys_in_order, "${k}_${k2}";
414 for my $order (@keys_in_order) {
415 next unless defined $section{$order};
416 my @ttl = split /_/, $order;
418 my $title = $param{title}[0]->[$ttl[0]] . " bugs";
421 $title .= join("; ", grep {($_ || "") ne ""}
422 map { $param{title}[$_]->[$ttl[$_]] } 1..$#ttl);
424 $title = html_escape($title);
426 my $count = $count{"_$order"};
427 my $bugs = $count == 1 ? "bug" : "bugs";
429 $header .= "<li><a href=\"#$order\">$title</a> ($count $bugs)</li>\n";
430 if ($common{show_list_header}) {
431 my $count = $count{"_$order"};
432 my $bugs = $count == 1 ? "bug" : "bugs";
433 $result .= "<H2 CLASS=\"outstanding\"><a name=\"$order\"></a>$title ($count $bugs)</H2>\n";
436 $result .= "<H2 CLASS=\"outstanding\">$title</H2>\n";
438 $result .= "<div class=\"msgreceived\">\n<UL class=\"bugs\">\n";
439 $result .= "\n\n\n\n";
440 $result .= $section{$order};
441 $result .= "\n\n\n\n";
442 $result .= "</UL>\n</div>\n";
444 $header .= "</ul></div>\n";
446 $footer .= "<div class=\"msgreceived\">\n<ul>\n";
447 for my $i (0..$#{$param{prior}}) {
448 my $local_result = '';
449 foreach my $key ( @{$param{order}[$i]} ) {
450 my $count = $count{"g_${i}_$key"};
451 next if !$count or !$param{title}[$i]->[$key];
452 $local_result .= "<li>$count $param{title}[$i]->[$key]</li>\n";
454 if ( $local_result ) {
455 $footer .= "<li>$param{names}[$i]<ul>\n$local_result</ul></li>\n";
458 $footer .= "</ul>\n</div>\n";
461 $result = $header . $result if ( $common{show_list_header} );
462 $result .= $footer if ( $common{show_list_footer} );
466 sub parse_order_statement_into_boolean {
467 my ($statement,$status,$tags) = @_;
469 if (not defined $tags) {
470 $tags = {map { $_, 1 } split / /, $status->{"tags"}
472 if defined $status->{"tags"};
475 # replace all + with &&
476 $statement =~ s/\+/&&/g;
477 # replace all , with ||
478 $statement =~ s/,/||/g;
479 $statement =~ s{([^\&\|\=]+) # field
485 $ok = 1 if defined $tags->{$2};
487 $ok = 1 if defined $status->{$1} and
492 # check that the parsed statement is just valid boolean statements
493 if ($statement =~ /^([01\(\)\&\|]+)$/) {
496 # this is an invalid boolean statement
501 sub get_bug_order_index {
505 my $tags = {map { $_, 1 } split / /, $status->{"tags"}
507 if defined $status->{"tags"};
508 for my $el (@${order}) {
509 if (not length $el or
510 parse_order_statement_into_boolean($el,$status,$tags)
519 # sets: my @names; my @prior; my @title; my @order;
521 sub determine_ordering {
522 my %param = validate_with(params => \@_,
523 spec => {cats => {type => HASHREF,
525 param => {type => HASHREF,
527 ordering => {type => SCALARREF,
529 names => {type => ARRAYREF,
531 pend_rev => {type => BOOLEAN,
534 sev_rev => {type => BOOLEAN,
537 prior => {type => ARRAYREF,
539 title => {type => ARRAYREF,
541 order => {type => ARRAYREF,
545 $param{cats}{status}[0]{ord} = [ reverse @{$param{cats}{status}[0]{ord}} ]
546 if ($param{pend_rev});
547 $param{cats}{severity}[0]{ord} = [ reverse @{$param{cats}{severity}[0]{ord}} ]
548 if ($param{sev_rev});
551 if (defined $param{param}{"pri0"}) {
554 while (defined $param{param}{"pri$i"}) {
557 my ($pri) = make_list($param{param}{"pri$i"});
558 if ($pri =~ m/^([^:]*):(.*)$/) {
559 $h->{"nam"} = $1; # overridden later if necesary
560 $h->{"pri"} = [ map { "$1=$_" } (split /,/, $2) ];
563 $h->{"pri"} = [ split /,/, $pri ];
566 ($h->{"nam"}) = make_list($param{param}{"nam$i"})
567 if (defined $param{param}{"nam$i"});
568 $h->{"ord"} = [ map {split /\s*,\s*/} make_list($param{param}{"ord$i"}) ]
569 if (defined $param{param}{"ord$i"});
570 $h->{"ttl"} = [ map {split /\s*,\s*/} make_list($param{param}{"ttl$i"}) ]
571 if (defined $param{param}{"ttl$i"});
576 $param{cats}{"_"} = [@c];
577 ${$param{ordering}} = "_";
580 ${$param{ordering}} = "normal" unless defined $param{cats}{${$param{ordering}}};
586 for my $c (@{$cats->{$o}}) {
587 if (ref($c) eq "HASH") {
591 push @res, get_ordering($cats, $c);
596 my @cats = get_ordering($param{cats}, ${$param{ordering}});
600 $expr =~ s/[+]/ and /g;
601 $expr =~ s/[a-z]+=//g;
608 push @{$param{prior}}, $c->{"pri"};
609 push @{$param{names}}, ($c->{"nam"} || "Bug attribute #" . $i);
610 if (defined $c->{"ord"}) {
611 push @{$param{order}}, $c->{"ord"};
614 push @{$param{order}}, [ 0..$#{$param{prior}[-1]} ];
616 my @t = @{ $c->{"ttl"} } if defined $c->{ttl};
617 if (@t < $#{$param{prior}[-1]}) {
618 push @t, map { toenglish($param{prior}[-1][$_]) } @t..($#{$param{prior}[-1]});
620 push @t, $c->{"def"} || "";
621 push @{$param{title}}, [@t];