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