]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/CGI/Pkgreport.pm
merge changes from don source
[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 base qw(Exporter);
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
45 use POSIX qw(strftime);
46
47
48 BEGIN{
49      ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
50      $DEBUG = 0 unless defined $DEBUG;
51
52      @EXPORT = ();
53      %EXPORT_TAGS = (html => [qw(short_bug_status_html pkg_htmlizebugs),
54                              ],
55                      misc => [qw(generate_package_info),
56                               qw(determine_ordering),
57                              ],
58                     );
59      @EXPORT_OK = (qw());
60      Exporter::export_ok_tags(keys %EXPORT_TAGS);
61      $EXPORT_TAGS{all} = [@EXPORT_OK];
62 }
63
64 =head2 generate_package_info
65
66      generate_package_info($srcorbin,$package)
67
68 Generates the informational bits for a package and returns it
69
70 =cut
71
72 sub generate_package_info{
73      my %param = validate_with(params => \@_,
74                                spec  => {binary => {type => BOOLEAN,
75                                                     default => 1,
76                                                    },
77                                          package => {type => SCALAR,#|ARRAYREF,
78                                                     },
79                                          options => {type => HASHREF,
80                                                     },
81                                          bugs    => {type => ARRAYREF,
82                                                     },
83                                         },
84                               );
85
86      my $output_scalar = '';
87      my $output = globify_scalar(\$output_scalar);
88
89      my $package = $param{package};
90
91      my %pkgsrc = %{getpkgsrc()};
92      my $srcforpkg = $package;
93      if ($param{binary} and exists $pkgsrc{$package}
94          and defined $pkgsrc{$package}) {
95           $srcforpkg = $pkgsrc{$package};
96      }
97
98      my $showpkg = html_escape($package);
99      my $maintainers = getmaintainers();
100      my $maint = $maintainers->{$srcforpkg};
101      if (defined $maint) {
102           print {$output} '<p>';
103           print {$output} (($maint =~ /,/)? "Maintainer for $showpkg is "
104                            : "Maintainers for $showpkg are ") .
105                                 package_links(maint => $maint);
106           print {$output} ".</p>\n";
107      }
108      else {
109           print {$output} "<p>There is no maintainer for $showpkg. ".
110                "Please do not report new bugs against this package.</p>\n";
111      }
112      my @pkgs = getsrcpkgs($srcforpkg);
113      @pkgs = grep( !/^\Q$package\E$/, @pkgs );
114      if ( @pkgs ) {
115           @pkgs = sort @pkgs;
116           if ($param{binary}) {
117                print {$output} "<p>You may want to refer to the following packages that are part of the same source:\n";
118           }
119           else {
120                print {$output} "<p>You may want to refer to the following individual bug pages:\n";
121           }
122           #push @pkgs, $src if ( $src && !grep(/^\Q$src\E$/, @pkgs) );
123           print {$output} scalar package_links(package=>[@pkgs]);
124           print {$output} ".\n";
125      }
126      my @references;
127      my $pseudodesc = getpseudodesc();
128      if ($package and defined($pseudodesc) and exists($pseudodesc->{$package})) {
129           push @references, "to the <a href=\"http://$config{web_domain}/pseudo-packages$config{html_suffix}\">".
130                "list of other pseudo-packages</a>";
131      }
132      elsif (not defined $maint and not @{$param{bugs}}) {
133           print {$output} "<p>There is no record of the " . html_escape($package) .
134                ($param{binary} ? " package" : " source package") .
135                     ", and no bugs have been filed against it.</p>";
136      }
137      else {
138           if ($package and defined $config{package_pages} and length $config{package_pages}) {
139                push @references, sprintf "to the <a href=\"%s\">%s package page</a>",
140                     html_escape("http://$config{package_pages}/$package"), html_escape("$package");
141           }
142           if (defined $config{subscription_domain} and
143               length $config{subscription_domain}) {
144                my $ptslink = $param{binary} ? $srcforpkg : $package;
145                # the pts only wants the source, and doesn't care about src: (#566089)
146                $ptslink =~ s/^src://;
147                push @references, q(to the <a href="http://).html_escape("$config{subscription_domain}/$ptslink").q(">Package Tracking System</a>);
148           }
149           # Only output this if the source listing is non-trivial.
150           if ($param{binary} and $srcforpkg) {
151                push @references,
152                     "to the source package ".
153                          package_links(src=>$srcforpkg,
154                                        options => $param{options}) .
155                               "'s bug page";
156           }
157      }
158      if (@references) {
159           $references[$#references] = "or $references[$#references]" if @references > 1;
160           print {$output} "<p>You might like to refer ", join(", ", @references), ".</p>\n";
161      }
162      if (defined $maint) {
163           print {$output} "<p>If you find a bug not listed here, please\n";
164           printf {$output} "<a href=\"%s\">report it</a>.</p>\n",
165                html_escape("http://$config{web_domain}/Reporting$config{html_suffix}");
166      }
167      return $output_scalar;
168 }
169
170
171 =head2 short_bug_status_html
172
173      print short_bug_status_html(status => read_bug(bug => 5),
174                                  options => \%param,
175                                 );
176
177 =over
178
179 =item status -- status hashref as returned by read_bug
180
181 =item options -- hashref of options to pass to package_links (defaults
182 to an empty hashref)
183
184 =item bug_options -- hashref of options to pass to bug_links (default
185 to an empty hashref)
186
187 =item snippet -- optional snippet of information about the bug to
188 display below
189
190
191 =back
192
193
194
195 =cut
196
197 sub short_bug_status_html {
198      my %param = validate_with(params => \@_,
199                                spec   => {status => {type => HASHREF,
200                                                     },
201                                           options => {type => HASHREF,
202                                                       default => {},
203                                                      },
204                                           bug_options => {type => HASHREF,
205                                                           default => {},
206                                                          },
207                                           snippet => {type => SCALAR,
208                                                       default => '',
209                                                      },
210                                          },
211                               );
212
213      my %status = %{$param{status}};
214
215      $status{tags_array} = [sort(split(/\s+/, $status{tags}))];
216      $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
217      $status{mergedwith_array} = [split(/ /,$status{mergedwith})];
218
219      my @blockedby= split(/ /, $status{blockedby});
220      $status{blockedby_array} = [];
221      if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
222           for my $b (@blockedby) {
223                my %s = %{get_bug_status($b)};
224                next if $s{"pending"} eq 'fixed' || length $s{done};
225                push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
226           }
227      }
228
229      my @blocks= split(/ /, $status{blocks});
230      $status{blocks_array} = [];
231      if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
232           for my $b (@blocks) {
233                my %s = %{get_bug_status($b)};
234                next if (defined $s{pending} and $s{pending} eq 'fixed') or (defined $s{done} and length $s{done});
235                push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
236           }
237      }
238      my $days = bug_archiveable(bug => $status{id},
239                                 status => \%status,
240                                 days_until => 1,
241                                );
242      $status{archive_days} = $days;
243      return fill_in_template(template => 'cgi/short_bug_status',
244                              variables => {status => \%status,
245                                            isstrongseverity => \&Debbugs::Status::isstrongseverity,
246                                            html_escape   => \&Debbugs::CGI::html_escape,
247                                            looks_like_number => \&Scalar::Util::looks_like_number,
248                                           },
249                              hole_var  => {'&package_links' => \&Debbugs::CGI::package_links,
250                                            '&bug_links'     => \&Debbugs::CGI::bug_links,
251                                            '&version_url'   => \&Debbugs::CGI::version_url,
252                                            '&secs_to_english' => \&Debbugs::Common::secs_to_english,
253                                            '&strftime'      => \&POSIX::strftime,
254                                            '&maybelink'     => \&Debbugs::CGI::maybelink,
255                                           },
256                             );
257 }
258
259
260 sub pkg_htmlizebugs {
261      my %param = validate_with(params => \@_,
262                                spec   => {bugs => {type => ARRAYREF,
263                                                   },
264                                           names => {type => ARRAYREF,
265                                                    },
266                                           title => {type => ARRAYREF,
267                                                    },
268                                           prior => {type => ARRAYREF,
269                                                    },
270                                           order => {type => ARRAYREF,
271                                                    },
272                                           ordering => {type => SCALAR,
273                                                       },
274                                           bugusertags => {type => HASHREF,
275                                                           default => {},
276                                                          },
277                                           bug_rev => {type => BOOLEAN,
278                                                       default => 0,
279                                                      },
280                                           bug_order => {type => SCALAR,
281                                                        },
282                                           repeatmerged => {type => BOOLEAN,
283                                                            default => 1,
284                                                           },
285                                           include => {type => ARRAYREF,
286                                                       default => [],
287                                                      },
288                                           exclude => {type => ARRAYREF,
289                                                       default => [],
290                                                      },
291                                           this     => {type => SCALAR,
292                                                        default => '',
293                                                       },
294                                           options  => {type => HASHREF,
295                                                        default => {},
296                                                       },
297                                           dist     => {type => SCALAR,
298                                                        optional => 1,
299                                                       },
300                                          }
301                               );
302      my @bugs = @{$param{bugs}};
303
304      my @status = ();
305      my %count;
306      my $header = '';
307      my $footer = "<h2 class=\"outstanding\">Summary</h2>\n";
308
309      if (@bugs == 0) {
310           return "<HR><H2>No reports found!</H2></HR>\n";
311      }
312
313      if ( $param{bug_rev} ) {
314           @bugs = sort {$b<=>$a} @bugs;
315      }
316      else {
317           @bugs = sort {$a<=>$b} @bugs;
318      }
319      my %seenmerged;
320
321      my %common = (
322                    'show_list_header' => 1,
323                    'show_list_footer' => 1,
324                   );
325
326      my %section = ();
327      # Make the include/exclude map
328      my %include;
329      my %exclude;
330      for my $include (make_list($param{include})) {
331           next unless defined $include;
332           my ($key,$value) = split /\s*:\s*/,$include,2;
333           unless (defined $value) {
334                $key = 'tags';
335                $value = $include;
336           }
337           push @{$include{$key}}, split /\s*,\s*/, $value;
338      }
339      for my $exclude (make_list($param{exclude})) {
340           next unless defined $exclude;
341           my ($key,$value) = split /\s*:\s*/,$exclude,2;
342           unless (defined $value) {
343                $key = 'tags';
344                $value = $exclude;
345           }
346           push @{$exclude{$key}}, split /\s*,\s*/, $value;
347      }
348
349      foreach my $bug (@bugs) {
350           my %status = %{get_bug_status(bug=>$bug,
351                                         (exists $param{dist}?(dist => $param{dist}):()),
352                                         bugusertags => $param{bugusertags},
353                                         (exists $param{version}?(version => $param{version}):()),
354                                         (exists $param{arch}?(arch => $param{arch}):(arch => $config{default_architectures})),
355                                        )};
356           next unless %status;
357           next if bug_filter(bug => $bug,
358                              status => \%status,
359                              repeat_merged => $param{repeatmerged},
360                              seen_merged => \%seenmerged,
361                              (keys %include ? (include => \%include):()),
362                              (keys %exclude ? (exclude => \%exclude):()),
363                             );
364
365           my $html = "<li>"; #<a href=\"%s\">#%d: %s</a>\n<br>",
366                #bug_url($bug), $bug, html_escape($status{subject});
367           $html .= short_bug_status_html(status  => \%status,
368                                          options => $param{options},
369                                         ) . "\n";
370           push @status, [ $bug, \%status, $html ];
371      }
372      if ($param{bug_order} eq 'age') {
373           # MWHAHAHAHA
374           @status = sort {$a->[1]{log_modified} <=> $b->[1]{log_modified}} @status;
375      }
376      elsif ($param{bug_order} eq 'agerev') {
377           @status = sort {$b->[1]{log_modified} <=> $a->[1]{log_modified}} @status;
378      }
379      for my $entry (@status) {
380           my $key = "";
381           for my $i (0..$#{$param{prior}}) {
382                my $v = get_bug_order_index($param{prior}[$i], $entry->[1]);
383                $count{"g_${i}_${v}"}++;
384                $key .= "_$v";
385           }
386           $section{$key} .= $entry->[2];
387           $count{"_$key"}++;
388      }
389
390      my $result = "";
391      if ($param{ordering} eq "raw") {
392           $result .= "<UL class=\"bugs\">\n" . join("", map( { $_->[ 2 ] } @status ) ) . "</UL>\n";
393      }
394      else {
395           $header .= "<div class=\"msgreceived\">\n<ul>\n";
396           my @keys_in_order = ("");
397           for my $o (@{$param{order}}) {
398                push @keys_in_order, "X";
399                while ((my $k = shift @keys_in_order) ne "X") {
400                     for my $k2 (@{$o}) {
401                          $k2+=0;
402                          push @keys_in_order, "${k}_${k2}";
403                     }
404                }
405           }
406           for my $order (@keys_in_order) {
407                next unless defined $section{$order};
408                my @ttl = split /_/, $order;
409                shift @ttl;
410                my $title = $param{title}[0]->[$ttl[0]] . " bugs";
411                if ($#ttl > 0) {
412                     $title .= " -- ";
413                     $title .= join("; ", grep {($_ || "") ne ""}
414                                    map { $param{title}[$_]->[$ttl[$_]] } 1..$#ttl);
415                }
416                $title = html_escape($title);
417
418                my $count = $count{"_$order"};
419                my $bugs = $count == 1 ? "bug" : "bugs";
420
421                $header .= "<li><a href=\"#$order\">$title</a> ($count $bugs)</li>\n";
422                if ($common{show_list_header}) {
423                     my $count = $count{"_$order"};
424                     my $bugs = $count == 1 ? "bug" : "bugs";
425                     $result .= "<H2 CLASS=\"outstanding\"><a name=\"$order\"></a>$title ($count $bugs)</H2>\n";
426                }
427                else {
428                     $result .= "<H2 CLASS=\"outstanding\">$title</H2>\n";
429                }
430                $result .= "<div class=\"msgreceived\">\n<UL class=\"bugs\">\n";
431                $result .= "\n\n\n\n";
432                $result .= $section{$order};
433                $result .= "\n\n\n\n";
434                $result .= "</UL>\n</div>\n";
435           } 
436           $header .= "</ul></div>\n";
437
438           $footer .= "<div class=\"msgreceived\">\n<ul>\n";
439           for my $i (0..$#{$param{prior}}) {
440                my $local_result = '';
441                foreach my $key ( @{$param{order}[$i]} ) {
442                     my $count = $count{"g_${i}_$key"};
443                     next if !$count or !$param{title}[$i]->[$key];
444                     $local_result .= "<li>$count $param{title}[$i]->[$key]</li>\n";
445                }
446                if ( $local_result ) {
447                     $footer .= "<li>$param{names}[$i]<ul>\n$local_result</ul></li>\n";
448                }
449           }
450           $footer .= "</ul>\n</div>\n";
451      }
452
453      $result = $header . $result if ( $common{show_list_header} );
454      $result .= $footer if ( $common{show_list_footer} );
455      return $result;
456 }
457
458 sub get_bug_order_index {
459      my $order = shift;
460      my $status = shift;
461      my $pos = -1;
462
463      my %tags = ();
464      %tags = map { $_, 1 } split / /, $status->{"tags"}
465           if defined $status->{"tags"};
466
467      for my $el (@${order}) {
468           $pos++;
469           my $match = 1;
470           for my $item (split /[+]/, $el) {
471                my ($f, $v) = split /=/, $item, 2;
472                next unless (defined $f and defined $v);
473                my $isokay = 0;
474                $isokay = 1 if (defined $status->{$f} and $v eq $status->{$f});
475                $isokay = 1 if ($f eq "tag" && defined $tags{$v});
476                unless ($isokay) {
477                     $match = 0;
478                     last;
479                }
480           }
481           if ($match) {
482                return $pos;
483                last;
484           }
485      }
486      return $pos + 1;
487 }
488
489 # sets: my @names; my @prior; my @title; my @order;
490
491 sub determine_ordering {
492      my %param = validate_with(params => \@_,
493                               spec => {cats => {type => HASHREF,
494                                                },
495                                        param => {type => HASHREF,
496                                                 },
497                                        ordering => {type => SCALARREF,
498                                                    },
499                                        names    => {type => ARRAYREF,
500                                                    },
501                                        pend_rev => {type => BOOLEAN,
502                                                     default => 0,
503                                                    },
504                                        sev_rev  => {type => BOOLEAN,
505                                                     default => 0,
506                                                    },
507                                        prior    => {type => ARRAYREF,
508                                                    },
509                                        title    => {type => ARRAYREF,
510                                                    },
511                                        order    => {type => ARRAYREF,
512                                                    },
513                                       },
514                              );
515      $param{cats}{status}[0]{ord} = [ reverse @{$param{cats}{status}[0]{ord}} ]
516           if ($param{pend_rev});
517      $param{cats}{severity}[0]{ord} = [ reverse @{$param{cats}{severity}[0]{ord}} ]
518           if ($param{sev_rev});
519
520      my $i;
521      if (defined $param{param}{"pri0"}) {
522           my @c = ();
523           $i = 0;
524           while (defined $param{param}{"pri$i"}) {
525                my $h = {};
526
527                my ($pri) = make_list($param{param}{"pri$i"});
528                if ($pri =~ m/^([^:]*):(.*)$/) {
529                     $h->{"nam"} = $1; # overridden later if necesary
530                     $h->{"pri"} = [ map { "$1=$_" } (split /,/, $2) ];
531                }
532                else {
533                     $h->{"pri"} = [ split /,/, $pri ];
534                }
535
536                ($h->{"nam"}) = make_list($param{param}{"nam$i"})
537                     if (defined $param{param}{"nam$i"});
538                $h->{"ord"} = [ map {split /\s*,\s*/} make_list($param{param}{"ord$i"}) ]
539                     if (defined $param{param}{"ord$i"});
540                $h->{"ttl"} = [ map {split /\s*,\s*/} make_list($param{param}{"ttl$i"}) ]
541                     if (defined $param{param}{"ttl$i"});
542
543                push @c, $h;
544                $i++;
545           }
546           $param{cats}{"_"} = [@c];
547           ${$param{ordering}} = "_";
548      }
549
550      ${$param{ordering}} = "normal" unless defined $param{cats}{${$param{ordering}}};
551
552      sub get_ordering {
553           my @res;
554           my $cats = shift;
555           my $o = shift;
556           for my $c (@{$cats->{$o}}) {
557                if (ref($c) eq "HASH") {
558                     push @res, $c;
559                }
560                else {
561                     push @res, get_ordering($cats, $c);
562                }
563           }
564           return @res;
565      }
566      my @cats = get_ordering($param{cats}, ${$param{ordering}});
567
568      sub toenglish {
569           my $expr = shift;
570           $expr =~ s/[+]/ and /g;
571           $expr =~ s/[a-z]+=//g;
572           return $expr;
573      }
574  
575      $i = 0;
576      for my $c (@cats) {
577           $i++;
578           push @{$param{prior}}, $c->{"pri"};
579           push @{$param{names}}, ($c->{"nam"} || "Bug attribute #" . $i);
580           if (defined $c->{"ord"}) {
581                push @{$param{order}}, $c->{"ord"};
582           }
583           else {
584                push @{$param{order}}, [ 0..$#{$param{prior}[-1]} ];
585           }
586           my @t = @{ $c->{"ttl"} } if defined $c->{ttl};
587           if (@t < $#{$param{prior}[-1]}) {
588                push @t, map { toenglish($param{prior}[-1][$_]) } @t..($#{$param{prior}[-1]});
589           }
590           push @t, $c->{"def"} || "";
591           push @{$param{title}}, [@t];
592      }
593 }
594
595
596
597
598 1;
599
600
601 __END__
602
603
604
605
606
607