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