]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/CGI/Pkgreport.pm
* Fix link to pseudopackages
[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                               qw(pkg_javascript),
55                               qw(pkg_htmlselectyesno pkg_htmlselectsuite),
56                               qw(buglinklist pkg_htmlselectarch)
57                              ],
58                      misc => [qw(generate_package_info make_order_list),
59                               qw(myurl),
60                               qw(get_bug_order_index determine_ordering),
61                              ],
62                     );
63      @EXPORT_OK = (qw());
64      Exporter::export_ok_tags(keys %EXPORT_TAGS);
65      $EXPORT_TAGS{all} = [@EXPORT_OK];
66 }
67
68 =head2 generate_package_info
69
70      generate_package_info($srcorbin,$package)
71
72 Generates the informational bits for a package and returns it
73
74 =cut
75
76 sub generate_package_info{
77      my %param = validate_with(params => \@_,
78                                spec  => {binary => {type => BOOLEAN,
79                                                     default => 1,
80                                                    },
81                                          package => {type => SCALAR|ARRAYREF,
82                                                     },
83                                          options => {type => HASHREF,
84                                                     },
85                                          bugs    => {type => ARRAYREF,
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} and exists $pkgsrc{$package}
98          and defined $pkgsrc{$package}) {
99           $srcforpkg = $pkgsrc{$package};
100      }
101
102      my $showpkg = html_escape($package);
103      my $maintainers = getmaintainers();
104      my $maint = $maintainers->{$srcforpkg};
105      if (defined $maint) {
106           print {$output} '<p>';
107           print {$output} (($maint =~ /,/)? "Maintainer for $showpkg is "
108                            : "Maintainers for $showpkg are ") .
109                                 package_links(maint => $maint);
110           print {$output} ".</p>\n";
111      }
112      else {
113           print {$output} "<p>No maintainer for $showpkg. Please do not report new bugs against this package.</p>\n";
114      }
115      my @pkgs = getsrcpkgs($srcforpkg);
116      @pkgs = grep( !/^\Q$package\E$/, @pkgs );
117      if ( @pkgs ) {
118           @pkgs = sort @pkgs;
119           if ($param{binary}) {
120                print {$output} "<p>You may want to refer to the following packages that are part of the same source:\n";
121           }
122           else {
123                print {$output} "<p>You may want to refer to the following individual bug pages:\n";
124           }
125           #push @pkgs, $src if ( $src && !grep(/^\Q$src\E$/, @pkgs) );
126           print {$output} scalar package_links(package=>[@pkgs]);
127           print {$output} ".\n";
128      }
129      my @references;
130      my $pseudodesc = getpseudodesc();
131      if ($package and defined($pseudodesc) and exists($pseudodesc->{$package})) {
132           push @references, "to the <a href=\"http://$config{web_domain}/pseudo-packages$config{html_suffix}\">".
133                "list of other pseudo-packages</a>";
134      }
135      else {
136           if ($package and defined $config{package_pages} and length $config{package_pages}) {
137                push @references, sprintf "to the <a href=\"%s\">%s package page</a>",
138                     html_escape("http://$config{package_pages}/$package"), html_escape("$package");
139           }
140           if (defined $config{subscription_domain} and
141               length $config{subscription_domain}) {
142                my $ptslink = $param{binary} ? $srcforpkg : $package;
143                push @references, q(to the <a href="http://).html_escape("$config{subscription_domain}/$ptslink").q(">Package Tracking System</a>);
144           }
145           # Only output this if the source listing is non-trivial.
146           if ($param{binary} and $srcforpkg) {
147                push @references,
148                     "to the source package ".
149                          package_links(src=>$srcforpkg,
150                                        options => $param{options}) .
151                               "'s bug page";
152           }
153      }
154      if (@references) {
155           $references[$#references] = "or $references[$#references]" if @references > 1;
156           print {$output} "<p>You might like to refer ", join(", ", @references), ".</p>\n";
157      }
158      if (defined $maint) {
159           print {$output} "<p>If you find a bug not listed here, please\n";
160           printf {$output} "<a href=\"%s\">report it</a>.</p>\n",
161                html_escape("http://$config{web_domain}/Reporting$config{html_suffix}");
162      }
163      if (not defined $maint and not @{$param{bugs}}) {
164           print {$output} "<p>There is no record of the " . html_escape($package) .
165                ($param{binary} ? " package" : " source package") .
166                     ", and no bugs have been filed against it.</p>";
167      }
168      return $output_scalar;
169 }
170
171
172 =head2 short_bug_status_html
173
174      print short_bug_status_html(status => read_bug(bug => 5),
175                                  options => \%param,
176                                 );
177
178 =over
179
180 =item status -- status hashref as returned by read_bug
181
182 =item options -- hashref of options to pass to package_links (defaults
183 to an empty hashref)
184
185 =item bug_options -- hashref of options to pass to bug_links (default
186 to an empty hashref)
187
188 =item snippet -- optional snippet of information about the bug to
189 display below
190
191
192 =back
193
194
195
196 =cut
197
198 sub short_bug_status_html {
199      my %param = validate_with(params => \@_,
200                                spec   => {status => {type => HASHREF,
201                                                     },
202                                           options => {type => HASHREF,
203                                                       default => {},
204                                                      },
205                                           bug_options => {type => HASHREF,
206                                                           default => {},
207                                                          },
208                                           snippet => {type => SCALAR,
209                                                       default => '',
210                                                      },
211                                          },
212                               );
213
214      my %status = %{$param{status}};
215
216      $status{tags_array} = [sort(split(/\s+/, $status{tags}))];
217      $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
218      $status{mergedwith_array} = [split(/ /,$status{mergedwith})];
219
220      my @blockedby= split(/ /, $status{blockedby});
221      $status{blockedby_array} = [];
222      if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
223           for my $b (@blockedby) {
224                my %s = %{get_bug_status($b)};
225                next if $s{"pending"} eq 'fixed' || length $s{done};
226                push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
227           }
228      }
229
230      my @blocks= split(/ /, $status{blocks});
231      $status{blocks_array} = [];
232      if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
233           for my $b (@blocks) {
234                my %s = %{get_bug_status($b)};
235                next if $s{"pending"} eq 'fixed' || length $s{done};
236                push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
237           }
238      }
239      my $days = bug_archiveable(bug => $status{id},
240                                 status => \%status,
241                                 days_until => 1,
242                                );
243      $status{archive_days} = $days;
244      return fill_in_template(template => 'cgi/short_bug_status',
245                              variables => {status => \%status,
246                                            isstrongseverity => \&Debbugs::Status::isstrongseverity,
247                                            html_escape   => \&Debbugs::CGI::html_escape,
248                                            looks_like_number => \&Scalar::Util::looks_like_number,
249                                           },
250                              hole_var  => {'&package_links' => \&Debbugs::CGI::package_links,
251                                            '&bug_links'     => \&Debbugs::CGI::bug_links,
252                                            '&version_url'   => \&Debbugs::CGI::version_url,
253                                            '&secs_to_english' => \&Debbugs::Common::secs_to_english,
254                                            '&strftime'      => \&POSIX::strftime,
255                                            '&maybelink'     => \&Debbugs::CGI::maybelink,
256                                           },
257                             );
258 }
259
260
261 sub pkg_htmlizebugs {
262      my %param = validate_with(params => \@_,
263                                spec   => {bugs => {type => ARRAYREF,
264                                                   },
265                                           names => {type => ARRAYREF,
266                                                    },
267                                           title => {type => ARRAYREF,
268                                                    },
269                                           prior => {type => ARRAYREF,
270                                                    },
271                                           order => {type => ARRAYREF,
272                                                    },
273                                           ordering => {type => SCALAR,
274                                                       },
275                                           bugusertags => {type => HASHREF,
276                                                           default => {},
277                                                          },
278                                           bug_rev => {type => BOOLEAN,
279                                                       default => 0,
280                                                      },
281                                           bug_order => {type => SCALAR,
282                                                        },
283                                           repeatmerged => {type => BOOLEAN,
284                                                            default => 1,
285                                                           },
286                                           include => {type => ARRAYREF,
287                                                       default => [],
288                                                      },
289                                           exclude => {type => ARRAYREF,
290                                                       default => [],
291                                                      },
292                                           this     => {type => SCALAR,
293                                                        default => '',
294                                                       },
295                                           options  => {type => HASHREF,
296                                                        default => {},
297                                                       },
298                                           dist     => {type => SCALAR,
299                                                        optional => 1,
300                                                       },
301                                          }
302                               );
303      my @bugs = @{$param{bugs}};
304
305      my @status = ();
306      my %count;
307      my $header = '';
308      my $footer = "<h2 class=\"outstanding\">Summary</h2>\n";
309
310      my @dummy = ($gRemoveAge); #, @gSeverityList, @gSeverityDisplay);  #, $gHTMLExpireNote);
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 pkg_javascript {
462      return fill_in_template(template=>'cgi/pkgreport_javascript',
463                             );
464 }
465
466 sub pkg_htmlselectyesno {
467      my ($name, $n, $y, $default) = @_;
468      return sprintf('<select name="%s"><option value=no%s>%s</option><option value=yes%s>%s</option></select>', $name, ($default ? "" : " selected"), $n, ($default ? " selected" : ""), $y);
469 }
470
471 sub pkg_htmlselectsuite {
472      my $id = sprintf "b_%d_%d_%d", $_[0], $_[1], $_[2];
473      my @suites = ("stable", "testing", "unstable", "experimental");
474      my %suiteaka = ("stable", "etch", "testing", "lenny", "unstable", "sid");
475      my $defaultsuite = "unstable";
476
477      my $result = sprintf '<select name=dist id="%s">', $id;
478      for my $s (@suites) {
479           $result .= sprintf '<option value="%s"%s>%s%s</option>',
480                $s, ($defaultsuite eq $s ? " selected" : ""),
481                     $s, (defined $suiteaka{$s} ? " (" . $suiteaka{$s} . ")" : "");
482      }
483      $result .= '</select>';
484      return $result;
485 }
486
487 sub pkg_htmlselectarch {
488      my $id = sprintf "b_%d_%d_%d", $_[0], $_[1], $_[2];
489      my @arches = qw(alpha amd64 arm hppa i386 ia64 m68k mips mipsel powerpc s390 sparc);
490
491      my $result = sprintf '<select name=arch id="%s">', $id;
492      $result .= '<option value="any">any architecture</option>';
493      for my $a (@arches) {
494           $result .= sprintf '<option value="%s">%s</option>', $a, $a;
495      }
496      $result .= '</select>';
497      return $result;
498 }
499
500 sub myurl {
501      my %param = @_;
502      return html_escape(pkg_url(map {exists $param{$_}?($_,$param{$_}):()}
503                                 qw(archive repeatmerged mindays maxdays),
504                                 qw(version dist arch package src tag maint submitter)
505                                )
506                        );
507 }
508
509 sub make_order_list {
510      my $vfull = shift;
511      my @x = ();
512
513      if ($vfull =~ m/^([^:]+):(.*)$/) {
514           my $v = $1;
515           for my $vv (split /,/, $2) {
516                push @x, "$v=$vv";
517           }
518      }
519      else {
520           for my $v (split /,/, $vfull) {
521                next unless $v =~ m/.=./;
522                push @x, $v;
523           }
524      }
525      push @x, "";               # catch all
526      return @x;
527 }
528
529 sub get_bug_order_index {
530      my $order = shift;
531      my $status = shift;
532      my $pos = -1;
533
534      my %tags = ();
535      %tags = map { $_, 1 } split / /, $status->{"tags"}
536           if defined $status->{"tags"};
537
538      for my $el (@${order}) {
539           $pos++;
540           my $match = 1;
541           for my $item (split /[+]/, $el) {
542                my ($f, $v) = split /=/, $item, 2;
543                next unless (defined $f and defined $v);
544                my $isokay = 0;
545                $isokay = 1 if (defined $status->{$f} and $v eq $status->{$f});
546                $isokay = 1 if ($f eq "tag" && defined $tags{$v});
547                unless ($isokay) {
548                     $match = 0;
549                     last;
550                }
551           }
552           if ($match) {
553                return $pos;
554                last;
555           }
556      }
557      return $pos + 1;
558 }
559
560 sub buglinklist {
561      my ($prefix, $infix, @els) = @_;
562      return '' if not @els;
563      return $prefix . bug_linklist($infix,'submitter',@els);
564 }
565
566
567 # sets: my @names; my @prior; my @title; my @order;
568
569 sub determine_ordering {
570      my %param = validate_with(params => \@_,
571                               spec => {cats => {type => HASHREF,
572                                                },
573                                        param => {type => HASHREF,
574                                                 },
575                                        ordering => {type => SCALARREF,
576                                                    },
577                                        names    => {type => ARRAYREF,
578                                                    },
579                                        pend_rev => {type => BOOLEAN,
580                                                     default => 0,
581                                                    },
582                                        sev_rev  => {type => BOOLEAN,
583                                                     default => 0,
584                                                    },
585                                        prior    => {type => ARRAYREF,
586                                                    },
587                                        title    => {type => ARRAYREF,
588                                                    },
589                                        order    => {type => ARRAYREF,
590                                                    },
591                                       },
592                              );
593      $param{cats}{status}[0]{ord} = [ reverse @{$param{cats}{status}[0]{ord}} ]
594           if ($param{pend_rev});
595      $param{cats}{severity}[0]{ord} = [ reverse @{$param{cats}{severity}[0]{ord}} ]
596           if ($param{sev_rev});
597
598      my $i;
599      if (defined $param{param}{"pri0"}) {
600           my @c = ();
601           $i = 0;
602           while (defined $param{param}{"pri$i"}) {
603                my $h = {};
604
605                my ($pri) = make_list($param{param}{"pri$i"});
606                if ($pri =~ m/^([^:]*):(.*)$/) {
607                     $h->{"nam"} = $1; # overridden later if necesary
608                     $h->{"pri"} = [ map { "$1=$_" } (split /,/, $2) ];
609                }
610                else {
611                     $h->{"pri"} = [ split /,/, $pri ];
612                }
613
614                ($h->{"nam"}) = make_list($param{param}{"nam$i"})
615                     if (defined $param{param}{"nam$i"});
616                $h->{"ord"} = [ map {split /\s*,\s*/} make_list($param{param}{"ord$i"}) ]
617                     if (defined $param{param}{"ord$i"});
618                $h->{"ttl"} = [ map {split /\s*,\s*/} make_list($param{param}{"ttl$i"}) ]
619                     if (defined $param{param}{"ttl$i"});
620
621                push @c, $h;
622                $i++;
623           }
624           $param{cats}{"_"} = [@c];
625           ${$param{ordering}} = "_";
626      }
627
628      ${$param{ordering}} = "normal" unless defined $param{cats}{${$param{ordering}}};
629
630      sub get_ordering {
631           my @res;
632           my $cats = shift;
633           my $o = shift;
634           for my $c (@{$cats->{$o}}) {
635                if (ref($c) eq "HASH") {
636                     push @res, $c;
637                }
638                else {
639                     push @res, get_ordering($cats, $c);
640                }
641           }
642           return @res;
643      }
644      my @cats = get_ordering($param{cats}, ${$param{ordering}});
645
646      sub toenglish {
647           my $expr = shift;
648           $expr =~ s/[+]/ and /g;
649           $expr =~ s/[a-z]+=//g;
650           return $expr;
651      }
652  
653      $i = 0;
654      for my $c (@cats) {
655           $i++;
656           push @{$param{prior}}, $c->{"pri"};
657           push @{$param{names}}, ($c->{"nam"} || "Bug attribute #" . $i);
658           if (defined $c->{"ord"}) {
659                push @{$param{order}}, $c->{"ord"};
660           }
661           else {
662                push @{$param{order}}, [ 0..$#{$param{prior}[-1]} ];
663           }
664           my @t = @{ $c->{"ttl"} } if defined $c->{ttl};
665           if (@t < $#{$param{prior}[-1]}) {
666                push @t, map { toenglish($param{prior}[-1][$_]) } @t..($#{$param{prior}[-1]});
667           }
668           push @t, $c->{"def"} || "";
669           push @{$param{title}}, [@t];
670      }
671 }
672
673
674
675
676 1;
677
678
679 __END__
680
681
682
683
684
685