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