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::Collection::Bug;
39 use List::AllUtils qw(apply);
41 use Debbugs::Config qw(:config :globals);
42 use Debbugs::CGI qw(:url :html :util);
43 use Debbugs::Common qw(:misc :util :date);
44 use Debbugs::Status qw(:status);
45 use Debbugs::Bugs qw(bug_filter);
46 use Debbugs::Packages qw(:mapping);
48 use Debbugs::Text qw(:templates);
49 use Encode qw(decode_utf8);
51 use POSIX qw(strftime);
55 ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
56 $DEBUG = 0 unless defined $DEBUG;
59 %EXPORT_TAGS = (html => [qw(short_bug_status_html pkg_htmlizebugs),
61 misc => [qw(generate_package_info),
62 qw(determine_ordering),
66 Exporter::export_ok_tags(keys %EXPORT_TAGS);
67 $EXPORT_TAGS{all} = [@EXPORT_OK];
70 =head2 generate_package_info
72 generate_package_info($srcorbin,$package)
74 Generates the informational bits for a package and returns it
78 sub generate_package_info{
79 my %param = validate_with(params => \@_,
80 spec => {binary => {type => BOOLEAN,
83 package => {type => SCALAR,#|ARRAYREF,
85 options => {type => HASHREF,
87 bugs => {type => ARRAYREF,
89 schema => {type => OBJECT,
95 my $output_scalar = '';
96 my $output = globify_scalar(\$output_scalar);
98 my $package = $param{package};
100 my $srcforpkg = $package;
101 if ($param{binary}) {
103 binary_to_source(source_only => 1,
106 hash_slice(%param,qw(schema)),
110 my $showpkg = html_escape($package);
111 my @maint = package_maintainer($param{binary}?'binary':'source',
113 hash_slice(%param,qw(schema)),
116 print {$output} '<p>';
117 print {$output} (@maint > 1? "Maintainer for $showpkg is "
118 : "Maintainers for $showpkg are ") .
119 package_links(maintainer => \@maint);
120 print {$output} ".</p>\n";
123 print {$output} "<p>There is no maintainer for $showpkg. ".
124 "This means that this package no longer exists (or never existed). ".
125 "Please do not report new bugs against this package. </p>\n";
127 my @pkgs = source_to_binary(source => $srcforpkg,
128 hash_slice(%param,qw(schema)),
130 # if there are distributions, only bother to
131 # show packages which are currently in a
133 @{$config{distributions}//[]} ?
134 (dist => [@{$config{distributions}}]) : (),
135 ) if defined $srcforpkg;
136 @pkgs = grep( !/^\Q$package\E$/, @pkgs );
139 if ($param{binary}) {
140 print {$output} "<p>You may want to refer to the following packages that are part of the same source:\n";
143 print {$output} "<p>You may want to refer to the following individual bug pages:\n";
145 #push @pkgs, $src if ( $src && !grep(/^\Q$src\E$/, @pkgs) );
146 print {$output} scalar package_links(package=>[@pkgs]);
147 print {$output} ".\n";
150 my $pseudodesc = getpseudodesc();
151 if ($package and defined($pseudodesc) and exists($pseudodesc->{$package})) {
152 push @references, "to the <a href=\"$config{web_domain}/pseudo-packages$config{html_suffix}\">".
153 "list of other pseudo-packages</a>";
156 if ($package and defined $config{package_pages} and length $config{package_pages}) {
157 push @references, sprintf "to the <a href=\"%s\">%s package page</a>",
158 html_escape("$config{package_pages}/$package"), html_escape("$package");
160 if (defined $config{package_tracking_domain} and
161 length $config{package_tracking_domain}) {
162 my $ptslink = $param{binary} ? $srcforpkg : $package;
163 # the pts only wants the source, and doesn't care about src: (#566089)
164 $ptslink =~ s/^src://;
165 push @references, q(to the <a href=").html_escape("$config{package_tracking_domain}/$ptslink").q(">Package Tracking System</a>);
167 # Only output this if the source listing is non-trivial.
168 if ($param{binary} and $srcforpkg) {
170 "to the source package ".
171 package_links(src=>$srcforpkg,
172 options => $param{options}) .
177 $references[$#references] = "or $references[$#references]" if @references > 1;
178 print {$output} "<p>You might like to refer ", join(", ", @references), ".</p>\n";
181 print {$output} "<p>If you find a bug not listed here, please\n";
182 printf {$output} "<a href=\"%s\">report it</a>.</p>\n",
183 html_escape("$config{web_domain}/Reporting$config{html_suffix}");
185 return decode_utf8($output_scalar);
189 =head2 short_bug_status_html
191 print short_bug_status_html(status => read_bug(bug => 5),
197 =item status -- status hashref as returned by read_bug
199 =item options -- hashref of options to pass to package_links (defaults
202 =item bug_options -- hashref of options to pass to bug_links (default
205 =item snippet -- optional snippet of information about the bug to
215 sub short_bug_status_html {
216 my %param = validate_with(params => \@_,
217 spec => {bug => {type => OBJECT,
218 isa => 'Debbugs::Bug',
223 return fill_in_template(template => 'cgi/short_bug_status',
224 variables => {bug => $param{bug},
225 isstrongseverity => \&Debbugs::Status::isstrongseverity,
226 html_escape => \&Debbugs::CGI::html_escape,
227 looks_like_number => \&Scalar::Util::looks_like_number,
229 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
230 '&bug_links' => \&Debbugs::CGI::bug_links,
231 '&version_url' => \&Debbugs::CGI::version_url,
232 '&secs_to_english' => \&Debbugs::Common::secs_to_english,
233 '&strftime' => \&POSIX::strftime,
234 '&maybelink' => \&Debbugs::CGI::maybelink,
240 sub pkg_htmlizebugs {
241 my %param = validate_with(params => \@_,
242 spec => {bugs => {type => OBJECT,
244 names => {type => ARRAYREF,
246 title => {type => ARRAYREF,
248 prior => {type => ARRAYREF,
250 order => {type => ARRAYREF,
252 ordering => {type => SCALAR,
254 bugusertags => {type => HASHREF,
257 bug_rev => {type => BOOLEAN,
260 bug_order => {type => SCALAR,
262 repeatmerged => {type => BOOLEAN,
265 include => {type => ARRAYREF,
268 exclude => {type => ARRAYREF,
271 this => {type => SCALAR,
274 options => {type => HASHREF,
277 dist => {type => SCALAR,
280 schema => {type => OBJECT,
285 my $bugs = $param{bugs};
288 my $footer = "<h2 class=\"outstanding\">Summary</h2>\n";
290 if ($bugs->count == 0) {
291 return "<HR><H2>No reports found!</H2></HR>\n";
297 'show_list_header' => 1,
298 'show_list_footer' => 1,
302 # Make the include/exclude map
305 for my $include (make_list($param{include})) {
306 next unless defined $include;
307 my ($key,$value) = split /\s*:\s*/,$include,2;
308 unless (defined $value) {
312 push @{$include{$key}}, split /\s*,\s*/, $value;
314 for my $exclude (make_list($param{exclude})) {
315 next unless defined $exclude;
316 my ($key,$value) = split /\s*:\s*/,$exclude,2;
317 unless (defined $value) {
321 push @{$exclude{$key}}, split /\s*,\s*/, $value;
324 my $sorter = sub {$_[0]->id <=> $_[1]->id};
325 if ($param{bug_rev}) {
326 $sorter = sub {$_[1]->id <=> $_[0]->id}
328 elsif ($param{bug_order} eq 'age') {
329 $sorter = sub {$_[0]->modified->epoch <=> $_[1]->modified->epoch};
331 elsif ($param{bug_order} eq 'agerev') {
332 $sorter = sub {$_[1]->modified->epoch <=> $_[0]->modified->epoch};
335 for my $bug ($bugs->sort($sorter)) {
337 $bug->filter(repeat_merged => $param{repeatmerged},
338 seen_merged => \%seenmerged,
339 (keys %include ? (include => \%include):()),
340 (keys %exclude ? (exclude => \%exclude):()),
343 my $html = "<li>"; #<a href=\"%s\">#%d: %s</a>\n<br>",
344 $html .= short_bug_status_html(bug => $bug,
346 push @status, [ $bug, $html ];
348 # parse bug order indexes into subroutines
352 [map {parse_order_statement_to_subroutine($_)} @{$a}];
354 for my $entry (@status) {
356 for my $i (0..$#order_subs) {
357 my $v = get_bug_order_index($order_subs[$i], $entry->[0]);
358 $count{"g_${i}_${v}"}++;
361 $section{$key} .= $entry->[1];
366 if ($param{ordering} eq "raw") {
367 $result .= "<UL class=\"bugs\">\n" . join("", map( { $_->[ 1 ] } @status ) ) . "</UL>\n";
370 $header .= "<div class=\"msgreceived\">\n<ul>\n";
371 my @keys_in_order = ("");
372 for my $o (@{$param{order}}) {
373 push @keys_in_order, "X";
374 while ((my $k = shift @keys_in_order) ne "X") {
377 push @keys_in_order, "${k}_${k2}";
381 for my $order (@keys_in_order) {
382 next unless defined $section{$order};
383 my @ttl = split /_/, $order;
385 my $title = $param{title}[0]->[$ttl[0]] . " bugs";
388 $title .= join("; ", grep {($_ || "") ne ""}
389 map { $param{title}[$_]->[$ttl[$_]] } 1..$#ttl);
391 $title = html_escape($title);
393 my $count = $count{"_$order"};
394 my $bugs = $count == 1 ? "bug" : "bugs";
396 $header .= "<li><a href=\"#$order\">$title</a> ($count $bugs)</li>\n";
397 if ($common{show_list_header}) {
398 my $count = $count{"_$order"};
399 my $bugs = $count == 1 ? "bug" : "bugs";
400 $result .= "<H2 CLASS=\"outstanding\"><a name=\"$order\"></a>$title ($count $bugs)</H2>\n";
403 $result .= "<H2 CLASS=\"outstanding\">$title</H2>\n";
405 $result .= "<div class=\"msgreceived\">\n<UL class=\"bugs\">\n";
406 $result .= "\n\n\n\n";
407 $result .= $section{$order};
408 $result .= "\n\n\n\n";
409 $result .= "</UL>\n</div>\n";
411 $header .= "</ul></div>\n";
413 $footer .= "<div class=\"msgreceived\">\n<ul>\n";
414 for my $i (0..$#{$param{prior}}) {
415 my $local_result = '';
416 foreach my $key ( @{$param{order}[$i]} ) {
417 my $count = $count{"g_${i}_$key"};
418 next if !$count or !$param{title}[$i]->[$key];
419 $local_result .= "<li>$count $param{title}[$i]->[$key]</li>\n";
421 if ( $local_result ) {
422 $footer .= "<li>$param{names}[$i]<ul>\n$local_result</ul></li>\n";
425 $footer .= "</ul>\n</div>\n";
428 $result = $header . $result if ( $common{show_list_header} );
429 $result .= $footer if ( $common{show_list_footer} );
433 sub parse_order_statement_to_subroutine {
434 my ($statement) = @_;
435 if (not defined $statement or not length $statement) {
436 return sub {return 1};
438 croak "invalid statement '$statement'" unless
439 $statement =~ /^(?:(package|tag|pending|severity) # field
441 ([^=|\&,\+]+(?:,[^=|\&,+])*) #value
442 (\+|,|$) # joiner or end
443 )+ # one or more of these statements
446 while ($statement =~ /(?<joiner>^|,|\+) # joiner
447 (?<field>package|tag|pending|severity) # field
449 (?<value>[^=|\&,\+]+(?:,[^=|\&,\+])*) #value
451 my $field = $+{field};
452 my $value = $+{value};
453 my $joiner = $+{joiner} // '';
454 my @vals = apply {quotemeta($_)} split /,/,$value;
455 if (length $joiner) {
456 if ($joiner eq '+') {
457 push @sub_bits, ' and ';
460 push @sub_bits, ' or ';
464 for my $val (@vals) {
465 if ($field =~ /package|severity/o) {
466 push @vals_bits, '$_[0]->status->'.$field.
468 } elsif ($field eq 'tag') {
469 push @vals_bits, '$_[0]->tags->is_set('.
471 } elsif ($field eq 'pending') {
472 push @vals_bits, '$_[0]->'.$field.
476 push @sub_bits ,' ('.join(' or ',@vals_bits).') ';
478 # return a subroutine reference which determines whether an order statement
480 my $sub = 'sub { return ('.join ("\n",@sub_bits).');};';
481 my $subref = eval $sub;
483 croak "Unable to generate subroutine: $@; $sub";
488 sub parse_order_statement_into_boolean {
489 my ($statement,$status,$tags) = @_;
491 if (not defined $tags) {
492 $tags = {map { $_, 1 } split / /, $status->{"tags"}
494 if defined $status->{"tags"};
497 # replace all + with &&
498 $statement =~ s/\+/&&/g;
499 # replace all , with ||
500 $statement =~ s/,/||/g;
501 $statement =~ s{([^\&\|\=]+) # field
507 $ok = 1 if defined $tags->{$2};
509 $ok = 1 if defined $status->{$1} and
514 # check that the parsed statement is just valid boolean statements
515 if ($statement =~ /^([01\(\)\&\|]+)$/) {
518 # this is an invalid boolean statement
523 sub get_bug_order_index {
524 my ($order,$bug) = @_;
526 for my $el (@{$order}) {
535 # sets: my @names; my @prior; my @title; my @order;
537 sub determine_ordering {
538 my %param = validate_with(params => \@_,
539 spec => {cats => {type => HASHREF,
541 param => {type => HASHREF,
543 ordering => {type => SCALARREF,
545 names => {type => ARRAYREF,
547 pend_rev => {type => BOOLEAN,
550 sev_rev => {type => BOOLEAN,
553 prior => {type => ARRAYREF,
555 title => {type => ARRAYREF,
557 order => {type => ARRAYREF,
561 $param{cats}{status}[0]{ord} = [ reverse @{$param{cats}{status}[0]{ord}} ]
562 if ($param{pend_rev});
563 $param{cats}{severity}[0]{ord} = [ reverse @{$param{cats}{severity}[0]{ord}} ]
564 if ($param{sev_rev});
567 if (defined $param{param}{"pri0"}) {
570 while (defined $param{param}{"pri$i"}) {
573 my ($pri) = make_list($param{param}{"pri$i"});
574 if ($pri =~ m/^([^:]*):(.*)$/) {
575 $h->{"nam"} = $1; # overridden later if necesary
576 $h->{"pri"} = [ map { "$1=$_" } (split /,/, $2) ];
579 $h->{"pri"} = [ split /,/, $pri ];
582 ($h->{"nam"}) = make_list($param{param}{"nam$i"})
583 if (defined $param{param}{"nam$i"});
584 $h->{"ord"} = [ map {split /\s*,\s*/} make_list($param{param}{"ord$i"}) ]
585 if (defined $param{param}{"ord$i"});
586 $h->{"ttl"} = [ map {split /\s*,\s*/} make_list($param{param}{"ttl$i"}) ]
587 if (defined $param{param}{"ttl$i"});
592 $param{cats}{"_"} = [@c];
593 ${$param{ordering}} = "_";
596 ${$param{ordering}} = "normal" unless defined $param{cats}{${$param{ordering}}};
602 for my $c (@{$cats->{$o}}) {
603 if (ref($c) eq "HASH") {
607 push @res, get_ordering($cats, $c);
612 my @cats = get_ordering($param{cats}, ${$param{ordering}});
616 $expr =~ s/[+]/ and /g;
617 $expr =~ s/[a-z]+=//g;
624 push @{$param{prior}}, $c->{"pri"};
625 push @{$param{names}}, ($c->{"nam"} || "Bug attribute #" . $i);
626 if (defined $c->{"ord"}) {
627 push @{$param{order}}, $c->{"ord"};
630 push @{$param{order}}, [ 0..$#{$param{prior}[-1]} ];
632 my @t = @{ $c->{"ttl"} } if defined $c->{ttl};
633 if (@t < $#{$param{prior}[-1]}) {
634 push @t, map { toenglish($param{prior}[-1][$_]) } @t..($#{$param{prior}[-1]});
636 push @t, $c->{"def"} || "";
637 push @{$param{title}}, [@t];