]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/CGI/Pkgreport.pm
c73cb1d816cdfdbccf058620fb8112ed4141c1b6
[debbugs.git] / Debbugs / CGI / Pkgreport.pm
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.
4 #
5 # [Other people have contributed to this file; their copyrights should
6 # be listed here too.]
7 # Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
8
9
10 package Debbugs::CGI::Pkgreport;
11
12 =head1 NAME
13
14 Debbugs::CGI::Pkgreport -- specific routines for the pkgreport cgi script
15
16 =head1 SYNOPSIS
17
18
19 =head1 DESCRIPTION
20
21
22 =head1 BUGS
23
24 None known.
25
26 =cut
27
28 use warnings;
29 use strict;
30 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
31 use Exporter qw(import);
32
33 use IO::Scalar;
34 use Params::Validate qw(validate_with :types);
35
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);
42
43 use Debbugs::Text qw(:templates);
44 use Encode qw(decode_utf8);
45
46 use POSIX qw(strftime);
47
48
49 BEGIN{
50      ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
51      $DEBUG = 0 unless defined $DEBUG;
52
53      @EXPORT = ();
54      %EXPORT_TAGS = (html => [qw(short_bug_status_html pkg_htmlizebugs),
55                              ],
56                      misc => [qw(generate_package_info),
57                               qw(determine_ordering),
58                              ],
59                     );
60      @EXPORT_OK = (qw());
61      Exporter::export_ok_tags(keys %EXPORT_TAGS);
62      $EXPORT_TAGS{all} = [@EXPORT_OK];
63 }
64
65 =head2 generate_package_info
66
67      generate_package_info($srcorbin,$package)
68
69 Generates the informational bits for a package and returns it
70
71 =cut
72
73 sub generate_package_info{
74      my %param = validate_with(params => \@_,
75                                spec  => {binary => {type => BOOLEAN,
76                                                     default => 1,
77                                                    },
78                                          package => {type => SCALAR,#|ARRAYREF,
79                                                     },
80                                          options => {type => HASHREF,
81                                                     },
82                                          bugs    => {type => ARRAYREF,
83                                                     },
84                                          schema => {type => OBJECT,
85                                                     optional => 1,
86                                                    },
87                                         },
88                               );
89
90      my $output_scalar = '';
91      my $output = globify_scalar(\$output_scalar);
92
93      my $package = $param{package};
94
95      my %pkgsrc = %{getpkgsrc()};
96      my $srcforpkg = $package;
97      if ($param{binary}) {
98          $srcforpkg =
99              binary_to_source(source_only => 1,
100                               scalar_only => 1,
101                               binary => $package,
102                               exists $param{schema}?(schema => $param{schema}):(),
103                              );
104      }
105
106      my $showpkg = html_escape($package);
107      my @maint = package_maintainer($param{binary}?'binary':'source',
108                                     $package
109                                    );
110      if (@maint) {
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";
116      }
117      else {
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";
121      }
122      my @pkgs = getsrcpkgs($srcforpkg);
123      @pkgs = grep( !/^\Q$package\E$/, @pkgs );
124      if ( @pkgs ) {
125           @pkgs = sort @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";
128           }
129           else {
130                print {$output} "<p>You may want to refer to the following individual bug pages:\n";
131           }
132           #push @pkgs, $src if ( $src && !grep(/^\Q$src\E$/, @pkgs) );
133           print {$output} scalar package_links(package=>[@pkgs]);
134           print {$output} ".\n";
135      }
136      my @references;
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>";
141      }
142      else {
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");
146           }
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>);
153           }
154           # Only output this if the source listing is non-trivial.
155           if ($param{binary} and $srcforpkg) {
156                push @references,
157                     "to the source package ".
158                          package_links(src=>$srcforpkg,
159                                        options => $param{options}) .
160                               "'s bug page";
161           }
162      }
163      if (@references) {
164           $references[$#references] = "or $references[$#references]" if @references > 1;
165           print {$output} "<p>You might like to refer ", join(", ", @references), ".</p>\n";
166      }
167      if (@maint) {
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}");
171      }
172      return decode_utf8($output_scalar);
173 }
174
175
176 =head2 short_bug_status_html
177
178      print short_bug_status_html(status => read_bug(bug => 5),
179                                  options => \%param,
180                                 );
181
182 =over
183
184 =item status -- status hashref as returned by read_bug
185
186 =item options -- hashref of options to pass to package_links (defaults
187 to an empty hashref)
188
189 =item bug_options -- hashref of options to pass to bug_links (default
190 to an empty hashref)
191
192 =item snippet -- optional snippet of information about the bug to
193 display below
194
195
196 =back
197
198
199
200 =cut
201
202 sub short_bug_status_html {
203      my %param = validate_with(params => \@_,
204                                spec   => {status => {type => HASHREF,
205                                                     },
206                                           options => {type => HASHREF,
207                                                       default => {},
208                                                      },
209                                           bug_options => {type => HASHREF,
210                                                           default => {},
211                                                          },
212                                           snippet => {type => SCALAR,
213                                                       default => '',
214                                                      },
215                                          },
216                               );
217
218      my %status = %{$param{status}};
219
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})];
223
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};
231           }
232      }
233
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};
241           }
242      }
243      my $days = bug_archiveable(bug => $status{id},
244                                 status => \%status,
245                                 days_until => 1,
246                                );
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,
253                                           },
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,
260                                           },
261                             );
262 }
263
264
265 sub pkg_htmlizebugs {
266      my %param = validate_with(params => \@_,
267                                spec   => {bugs => {type => ARRAYREF,
268                                                   },
269                                           names => {type => ARRAYREF,
270                                                    },
271                                           title => {type => ARRAYREF,
272                                                    },
273                                           prior => {type => ARRAYREF,
274                                                    },
275                                           order => {type => ARRAYREF,
276                                                    },
277                                           ordering => {type => SCALAR,
278                                                       },
279                                           bugusertags => {type => HASHREF,
280                                                           default => {},
281                                                          },
282                                           bug_rev => {type => BOOLEAN,
283                                                       default => 0,
284                                                      },
285                                           bug_order => {type => SCALAR,
286                                                        },
287                                           repeatmerged => {type => BOOLEAN,
288                                                            default => 1,
289                                                           },
290                                           include => {type => ARRAYREF,
291                                                       default => [],
292                                                      },
293                                           exclude => {type => ARRAYREF,
294                                                       default => [],
295                                                      },
296                                           this     => {type => SCALAR,
297                                                        default => '',
298                                                       },
299                                           options  => {type => HASHREF,
300                                                        default => {},
301                                                       },
302                                           dist     => {type => SCALAR,
303                                                        optional => 1,
304                                                       },
305                                           schema   => {type => OBJECT,
306                                                        optional => 1,
307                                                       },
308                                          }
309                               );
310      my @bugs = @{$param{bugs}};
311
312      my @status = ();
313      my %count;
314      my $header = '';
315      my $footer = "<h2 class=\"outstanding\">Summary</h2>\n";
316
317      if (@bugs == 0) {
318           return "<HR><H2>No reports found!</H2></HR>\n";
319      }
320
321      if ( $param{bug_rev} ) {
322           @bugs = sort {$b<=>$a} @bugs;
323      }
324      else {
325           @bugs = sort {$a<=>$b} @bugs;
326      }
327      my %seenmerged;
328
329      my %common = (
330                    'show_list_header' => 1,
331                    'show_list_footer' => 1,
332                   );
333
334      my %section = ();
335      # Make the include/exclude map
336      my %include;
337      my %exclude;
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) {
342                $key = 'tags';
343                $value = $include;
344           }
345           push @{$include{$key}}, split /\s*,\s*/, $value;
346      }
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) {
351                $key = 'tags';
352                $value = $exclude;
353           }
354           push @{$exclude{$key}}, split /\s*,\s*/, $value;
355      }
356
357      my $binary_to_source_cache = {};
358      my $statuses =
359          get_bug_statuses(bug => \@bugs,
360                           (map {exists $param{$_}?($_,$param{$_}):()}
361                            qw(dist version schema bugusertags)
362                           ),
363                           (exists $param{arch}?(arch => $param{arch}):(arch => $config{default_architectures})),
364                           binary_to_source_cache => $binary_to_source_cache,
365                          );
366      for my $bug (keys %{$statuses}) {
367          next unless %{$statuses->{$bug}};
368          next if bug_filter(bug => $bug,
369                             status => $statuses->{$bug},
370                             repeat_merged => $param{repeatmerged},
371                             seen_merged => \%seenmerged,
372                             (keys %include ? (include => \%include):()),
373                             (keys %exclude ? (exclude => \%exclude):()),
374                            );
375
376          my $html = "<li>";     #<a href=\"%s\">#%d: %s</a>\n<br>",
377          $html .= short_bug_status_html(status  => $statuses->{$bug},
378                                         options => $param{options},
379                                        ) . "\n";
380          push @status, [ $bug, $statuses->{$bug}, $html ];
381      }
382      if ($param{bug_order} eq 'age') {
383           # MWHAHAHAHA
384           @status = sort {$a->[1]{log_modified} <=> $b->[1]{log_modified}} @status;
385      }
386      elsif ($param{bug_order} eq 'agerev') {
387           @status = sort {$b->[1]{log_modified} <=> $a->[1]{log_modified}} @status;
388      }
389      for my $entry (@status) {
390           my $key = "";
391           for my $i (0..$#{$param{prior}}) {
392                my $v = get_bug_order_index($param{prior}[$i], $entry->[1]);
393                $count{"g_${i}_${v}"}++;
394                $key .= "_$v";
395           }
396           $section{$key} .= $entry->[2];
397           $count{"_$key"}++;
398      }
399
400      my $result = "";
401      if ($param{ordering} eq "raw") {
402           $result .= "<UL class=\"bugs\">\n" . join("", map( { $_->[ 2 ] } @status ) ) . "</UL>\n";
403      }
404      else {
405           $header .= "<div class=\"msgreceived\">\n<ul>\n";
406           my @keys_in_order = ("");
407           for my $o (@{$param{order}}) {
408                push @keys_in_order, "X";
409                while ((my $k = shift @keys_in_order) ne "X") {
410                     for my $k2 (@{$o}) {
411                          $k2+=0;
412                          push @keys_in_order, "${k}_${k2}";
413                     }
414                }
415           }
416           for my $order (@keys_in_order) {
417                next unless defined $section{$order};
418                my @ttl = split /_/, $order;
419                shift @ttl;
420                my $title = $param{title}[0]->[$ttl[0]] . " bugs";
421                if ($#ttl > 0) {
422                     $title .= " -- ";
423                     $title .= join("; ", grep {($_ || "") ne ""}
424                                    map { $param{title}[$_]->[$ttl[$_]] } 1..$#ttl);
425                }
426                $title = html_escape($title);
427
428                my $count = $count{"_$order"};
429                my $bugs = $count == 1 ? "bug" : "bugs";
430
431                $header .= "<li><a href=\"#$order\">$title</a> ($count $bugs)</li>\n";
432                if ($common{show_list_header}) {
433                     my $count = $count{"_$order"};
434                     my $bugs = $count == 1 ? "bug" : "bugs";
435                     $result .= "<H2 CLASS=\"outstanding\"><a name=\"$order\"></a>$title ($count $bugs)</H2>\n";
436                }
437                else {
438                     $result .= "<H2 CLASS=\"outstanding\">$title</H2>\n";
439                }
440                $result .= "<div class=\"msgreceived\">\n<UL class=\"bugs\">\n";
441                $result .= "\n\n\n\n";
442                $result .= $section{$order};
443                $result .= "\n\n\n\n";
444                $result .= "</UL>\n</div>\n";
445           } 
446           $header .= "</ul></div>\n";
447
448           $footer .= "<div class=\"msgreceived\">\n<ul>\n";
449           for my $i (0..$#{$param{prior}}) {
450                my $local_result = '';
451                foreach my $key ( @{$param{order}[$i]} ) {
452                     my $count = $count{"g_${i}_$key"};
453                     next if !$count or !$param{title}[$i]->[$key];
454                     $local_result .= "<li>$count $param{title}[$i]->[$key]</li>\n";
455                }
456                if ( $local_result ) {
457                     $footer .= "<li>$param{names}[$i]<ul>\n$local_result</ul></li>\n";
458                }
459           }
460           $footer .= "</ul>\n</div>\n";
461      }
462
463      $result = $header . $result if ( $common{show_list_header} );
464      $result .= $footer if ( $common{show_list_footer} );
465      return $result;
466 }
467
468 sub parse_order_statement_into_boolean {
469     my ($statement,$status,$tags) = @_;
470
471     if (not defined $tags) {
472         $tags = {map { $_, 1 } split / /, $status->{"tags"}
473                 }
474             if defined $status->{"tags"};
475
476     }
477     # replace all + with &&
478     $statement =~ s/\+/&&/g;
479     # replace all , with ||
480     $statement =~ s/,/||/g;
481     $statement =~ s{([^\&\|\=]+) # field
482                     =
483                     ([^\&\|\=]+) # value
484               }{
485                   my $ok = 0;
486                   if ($1 eq 'tag') {
487                       $ok = 1 if defined $tags->{$2};
488                   } else {
489                       $ok = 1 if defined $status->{$1} and
490                           $status->{$1} eq $2;
491                   }
492                   $ok;
493               }exg;
494     # check that the parsed statement is just valid boolean statements
495     if ($statement =~ /^([01\(\)\&\|]+)$/) {
496         return eval "$1";
497     } else {
498         # this is an invalid boolean statement
499         return 0;
500     }
501 }
502
503 sub get_bug_order_index {
504      my $order = shift;
505      my $status = shift;
506      my $pos = 0;
507      my $tags = {map { $_, 1 } split / /, $status->{"tags"}
508                 }
509          if defined $status->{"tags"};
510      for my $el (@${order}) {
511          if (not length $el or
512              parse_order_statement_into_boolean($el,$status,$tags)
513             ) {
514              return $pos;
515          }
516          $pos++;
517      }
518      return $pos;
519 }
520
521 # sets: my @names; my @prior; my @title; my @order;
522
523 sub determine_ordering {
524      my %param = validate_with(params => \@_,
525                               spec => {cats => {type => HASHREF,
526                                                },
527                                        param => {type => HASHREF,
528                                                 },
529                                        ordering => {type => SCALARREF,
530                                                    },
531                                        names    => {type => ARRAYREF,
532                                                    },
533                                        pend_rev => {type => BOOLEAN,
534                                                     default => 0,
535                                                    },
536                                        sev_rev  => {type => BOOLEAN,
537                                                     default => 0,
538                                                    },
539                                        prior    => {type => ARRAYREF,
540                                                    },
541                                        title    => {type => ARRAYREF,
542                                                    },
543                                        order    => {type => ARRAYREF,
544                                                    },
545                                       },
546                              );
547      $param{cats}{status}[0]{ord} = [ reverse @{$param{cats}{status}[0]{ord}} ]
548           if ($param{pend_rev});
549      $param{cats}{severity}[0]{ord} = [ reverse @{$param{cats}{severity}[0]{ord}} ]
550           if ($param{sev_rev});
551
552      my $i;
553      if (defined $param{param}{"pri0"}) {
554           my @c = ();
555           $i = 0;
556           while (defined $param{param}{"pri$i"}) {
557                my $h = {};
558
559                my ($pri) = make_list($param{param}{"pri$i"});
560                if ($pri =~ m/^([^:]*):(.*)$/) {
561                     $h->{"nam"} = $1; # overridden later if necesary
562                     $h->{"pri"} = [ map { "$1=$_" } (split /,/, $2) ];
563                }
564                else {
565                     $h->{"pri"} = [ split /,/, $pri ];
566                }
567
568                ($h->{"nam"}) = make_list($param{param}{"nam$i"})
569                     if (defined $param{param}{"nam$i"});
570                $h->{"ord"} = [ map {split /\s*,\s*/} make_list($param{param}{"ord$i"}) ]
571                     if (defined $param{param}{"ord$i"});
572                $h->{"ttl"} = [ map {split /\s*,\s*/} make_list($param{param}{"ttl$i"}) ]
573                     if (defined $param{param}{"ttl$i"});
574
575                push @c, $h;
576                $i++;
577           }
578           $param{cats}{"_"} = [@c];
579           ${$param{ordering}} = "_";
580      }
581
582      ${$param{ordering}} = "normal" unless defined $param{cats}{${$param{ordering}}};
583
584      sub get_ordering {
585           my @res;
586           my $cats = shift;
587           my $o = shift;
588           for my $c (@{$cats->{$o}}) {
589                if (ref($c) eq "HASH") {
590                     push @res, $c;
591                }
592                else {
593                     push @res, get_ordering($cats, $c);
594                }
595           }
596           return @res;
597      }
598      my @cats = get_ordering($param{cats}, ${$param{ordering}});
599
600      sub toenglish {
601           my $expr = shift;
602           $expr =~ s/[+]/ and /g;
603           $expr =~ s/[a-z]+=//g;
604           return $expr;
605      }
606  
607      $i = 0;
608      for my $c (@cats) {
609           $i++;
610           push @{$param{prior}}, $c->{"pri"};
611           push @{$param{names}}, ($c->{"nam"} || "Bug attribute #" . $i);
612           if (defined $c->{"ord"}) {
613                push @{$param{order}}, $c->{"ord"};
614           }
615           else {
616                push @{$param{order}}, [ 0..$#{$param{prior}[-1]} ];
617           }
618           my @t = @{ $c->{"ttl"} } if defined $c->{ttl};
619           if (@t < $#{$param{prior}[-1]}) {
620                push @t, map { toenglish($param{prior}[-1][$_]) } @t..($#{$param{prior}[-1]});
621           }
622           push @t, $c->{"def"} || "";
623           push @{$param{title}}, [@t];
624      }
625 }
626
627
628
629
630 1;
631
632
633 __END__
634
635
636
637
638
639