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