]> git.donarmstrong.com Git - debbugs.git/blob - cgi/pkgreport.cgi
properly handle orderings in pkgreport.cgi
[debbugs.git] / cgi / pkgreport.cgi
1 #!/usr/bin/perl -wT
2 # This script is part of debbugs, and is released
3 # under the terms of the GPL version 2, or any later
4 # version at your option.
5 # See the file README and COPYING for more information.
6 #
7 # [Other people have contributed to this file; their copyrights should
8 # go here too.]
9 # Copyright 2004-2006 by Anthony Towns <ajt@debian.org>
10 # Copyright 2007 by Don Armstrong <don@donarmstrong.com>.
11
12
13 use warnings;
14 use strict;
15
16 # Sanitize environent for taint
17 BEGIN{
18     delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
19 }
20
21 binmode(STDOUT,':encoding(UTF-8)');
22 use POSIX qw(strftime nice);
23 use List::Util qw(uniq);
24
25 use Debbugs::Config qw(:globals :text :config);
26
27 use Debbugs::User;
28
29 use Debbugs::Common qw(getparsedaddrs make_list getmaintainers getpseudodesc);
30
31 use Debbugs::Bugs qw(get_bugs bug_filter newest_bug);
32 use Debbugs::Packages qw(getsrcpkgs getpkgsrc get_versions);
33
34 use Debbugs::Status qw(splitpackages);
35
36 use Debbugs::CGI qw(:all);
37
38 use Debbugs::CGI::Pkgreport qw(:all);
39
40 use Debbugs::Text qw(:templates);
41
42 use CGI::Simple;
43 my $q = new CGI::Simple;
44
45 if ($q->request_method() eq 'HEAD') {
46      print $q->header(-type => "text/html",
47                       -charset => 'utf-8',
48                      );
49      exit 0;
50 }
51
52 my $default_params = {ordering => 'normal',
53                       archive  => 0,
54                       repeatmerged => 0,
55                       include      => [],
56                       exclude      => [],
57                      };
58
59 our %param = cgi_parameters(query => $q,
60                             single => [qw(ordering archive repeatmerged),
61                                        qw(bug-rev pend-rev sev-rev),
62                                        qw(maxdays mindays version),
63                                        qw(data which dist newest),
64                                        qw(noaffects),
65                                       ],
66                             default => $default_params,
67                            );
68
69 my ($form_options,$param) = ({},undef);
70 ($form_options,$param)= form_options_and_normal_param(\%param)
71      if $param{form_options};
72
73 %param = %{$param} if defined $param;
74
75 if (exists $param{form_options} and defined $param{form_options}) {
76      delete $param{form_options};
77      delete $param{submit} if exists $param{submit};
78      for my $default (keys %{$default_params}) {
79           if (exists $param{$default} and
80               not ref($default_params->{$default}) and
81               $default_params->{$default} eq $param{$default}
82              ) {
83                delete $param{$default};
84           }
85      }
86      for my $incexc (qw(include exclude)) {
87           next unless exists $param{$incexc};
88           # normalize tag to tags
89           $param{$incexc} = [map {s/^tag:/tags:/; $_} grep /\S\:\S/, make_list($param{$incexc})];
90      }
91      for my $key (keys %package_search_keys) {
92           next unless exists $param{key};
93           $param{$key} = [map {split /\s*,\s*/} make_list($param{$key})];
94      }
95      # kill off keys for which empty values are meaningless
96      for my $key (qw(package src submitter affects severity status dist)) {
97           next unless exists $param{$key};
98           $param{$key} = [grep {defined $_ and length $_}
99                           make_list($param{$key})];
100      }
101      print $q->redirect(munge_url('pkgreport.cgi?',%param));
102      exit 0;
103 }
104
105 # normalize innclude/exclude keys; currently this is in two locations,
106 # which is suboptimal. Closes: #567407
107 for my $incexc (qw(include exclude)) {
108     next unless exists $param{$incexc};
109     # normalize tag to tags
110     $param{$incexc} = [map {s/^tag:/tags:/; $_} make_list($param{$incexc})];
111 }
112
113
114
115 # map from yes|no to 1|0
116 for my $key (qw(repeatmerged bug-rev pend-rev sev-rev)) {
117      if (exists $param{$key}){
118           if ($param{$key} =~ /^no$/i) {
119                $param{$key} = 0;
120           }
121           elsif ($param{$key}) {
122                $param{$key} = 1;
123           }
124      }
125 }
126
127 if (lc($param{archive}) eq 'no') {
128      $param{archive} = 0;
129 }
130 elsif (lc($param{archive}) eq 'yes') {
131      $param{archive} = 1;
132 }
133
134 # fixup dist
135 if (exists $param{dist} and $param{dist} eq '') {
136      delete $param{dist};
137 }
138
139 my $include = $param{'&include'} || $param{'include'} || "";
140 my $exclude = $param{'&exclude'} || $param{'exclude'} || "";
141
142 my $users = $param{'users'} || "";
143
144 my $ordering = $param{'ordering'};
145 my $raw_sort = ($param{'raw'} || "no") eq "yes";
146 my $old_view = ($param{'oldview'} || "no") eq "yes";
147 my $age_sort = ($param{'age'} || "no") eq "yes";
148 unless (defined $ordering) {
149    $ordering = "normal";
150    $ordering = "oldview" if $old_view;
151    $ordering = "raw" if $raw_sort;
152    $ordering = 'age' if $age_sort;
153 }
154 $param{ordering} = $ordering;
155
156 our ($bug_order) = $ordering =~ /(age(?:rev)?)/;
157 $bug_order = '' if not defined $bug_order;
158
159 my $bug_rev = ($param{'bug-rev'} || "no") eq "yes";
160 my $pend_rev = ($param{'pend-rev'} || "no") eq "yes";
161 my $sev_rev = ($param{'sev-rev'} || "no") eq "yes";
162
163 my @inc_exc_mapping = ({name   => 'pending',
164                         incexc => 'include',
165                         key    => 'pend-inc',
166                        },
167                        {name   => 'pending',
168                         incexc => 'exclude',
169                         key    => 'pend-exc',
170                        },
171                        {name   => 'severity',
172                         incexc => 'include',
173                         key    => 'sev-inc',
174                        },
175                        {name   => 'severity',
176                         incexc => 'exclude',
177                         key    => 'sev-exc',
178                        },
179                        {name   => 'subject',
180                         incexc => 'include',
181                         key    => 'includesubj',
182                        },
183                        {name   => 'subject',
184                         incexc => 'exclude',
185                         key    => 'excludesubj',
186                        },
187                       );
188 for my $incexcmap (@inc_exc_mapping) {
189      push @{$param{$incexcmap->{incexc}}}, map {"$incexcmap->{name}:$_"}
190           map{split /\s*,\s*/} make_list($param{$incexcmap->{key}})
191                if exists $param{$incexcmap->{key}};
192      delete $param{$incexcmap->{key}};
193 }
194
195
196 my $maxdays = ($param{'maxdays'} || -1);
197 my $mindays = ($param{'mindays'} || 0);
198 my $version = $param{'version'} || undef;
199
200
201 our %hidden = map { $_, 1 } qw(status severity classification);
202 our %cats = (
203     "status" => [ {
204         "nam" => "Status",
205         "pri" => [map { "pending=$_" }
206             qw(pending forwarded pending-fixed fixed done absent)],
207         "ttl" => ["Outstanding","Forwarded","Pending Upload",
208                   "Fixed in NMU","Resolved","From other Branch"],
209         "def" => "Unknown Pending Status",
210         "ord" => [0,1,2,3,4,5,6],
211     } ],
212     "severity" => [ {
213         "nam" => "Severity",
214         "pri" => [map { "severity=$_" } @gSeverityList],
215         "ttl" => [map { $gSeverityDisplay{$_} } @gSeverityList],
216         "def" => "Unknown Severity",
217         "ord" => [0..@gSeverityList],
218     } ],
219     "classification" => [ {
220         "nam" => "Classification",
221         "pri" => [qw(pending=pending+tag=wontfix 
222                      pending=pending+tag=moreinfo
223                      pending=pending+tag=patch
224                      pending=pending+tag=confirmed
225                      pending=pending)],
226         "ttl" => ["Will Not Fix","More information needed",
227                   "Patch Available","Confirmed"],
228         "def" => "Unclassified",
229         "ord" => [2,3,4,1,0,5],
230     } ],
231     "oldview" => [ qw(status severity) ],
232     "normal" => [ qw(status severity classification) ],
233 );
234
235 if (exists $param{which} and exists $param{data}) {
236      $param{$param{which}} = [exists $param{$param{which}}?(make_list($param{$param{which}})):(),
237                               make_list($param{data}),
238                              ];
239      delete $param{which};
240      delete $param{data};
241 }
242
243 if (defined $param{maintenc}) {
244      $param{maint} = maint_decode($param{maintenc});
245      delete $param{maintenc}
246 }
247
248 if (exists $param{pkg}) {
249      $param{package} = $param{pkg};
250      delete $param{pkg};
251 }
252
253 if (not grep {exists $param{$_}} keys %package_search_keys and exists $param{users}) {
254      $param{usertag} = [make_list($param{users})];
255 }
256
257 my %bugusertags;
258 my %ut;
259 my %seen_users;
260
261 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
262     next unless length($user);
263     add_user($user,\%ut,\%bugusertags,\%seen_users,\%cats,\%hidden);
264 }
265
266 if (defined $param{usertag}) {
267      for my $usertag (make_list($param{usertag})) {
268           my %select_ut = ();
269           my ($u, $t) = split /:/, $usertag, 2;
270           Debbugs::User::read_usertags(\%select_ut, $u);
271           unless (defined $t && $t ne "") {
272                $t = join(",", keys(%select_ut));
273           }
274           add_user($u,\%ut,\%bugusertags,\%seen_users,\%cats,\%hidden);
275           push @{$param{tag}}, split /,/, $t;
276      }
277 }
278
279 quitcgi("You have to choose something to select by", '400 Bad Request')
280   unless grep {exists $param{$_}} keys %package_search_keys;
281
282
283 my $Archived = $param{archive} ? " Archived" : "";
284
285 my $this = munge_url('pkgreport.cgi?',
286                       %param,
287                      );
288
289 my %indexentry;
290 my %strings = ();
291
292 my @bugs;
293
294 # addusers for source and binary packages being searched for
295 my $pkgsrc = getpkgsrc();
296 my $srcpkg = getsrcpkgs();
297 for my $package (# For binary packages, add the binary package
298                  # and corresponding source package
299                  make_list($param{package}||[]),
300                  (map {defined $pkgsrc->{$_}?($pkgsrc->{$_}):()}
301                   make_list($param{package}||[]),
302                  ),
303                  # For source packages, add the source package
304                  # and corresponding binary packages
305                  make_list($param{src}||[]),
306                  (map {defined $srcpkg->{$_}?($srcpkg->{$_}):()}
307                   make_list($param{src}||[]),
308                  ),
309                 ) {
310      next unless defined $package;
311      add_user($package.'@'.$config{usertag_package_domain},
312               \%ut,\%bugusertags,\%seen_users,\%cats,\%hidden)
313           if defined $config{usertag_package_domain};
314 }
315
316
317 # walk through the keys and make the right get_bugs query.
318
319 my $form_option_variables = {};
320 $form_option_variables->{search_key_order} = [@package_search_key_order];
321
322 # Set the title sanely and clean up parameters
323 my @title;
324 my @temp = @package_search_key_order;
325 while (my ($key,$value) = splice @temp, 0, 2) {
326      next unless exists $param{$key};
327      my @entries = ();
328      for my $entry (make_list($param{$key})) {
329           # we'll handle newest below
330           next if $key eq 'newest';
331           my $extra = '';
332           if (exists $param{dist} and ($key eq 'package' or $key eq 'src')) {
333                my %versions = get_versions(package => $entry,
334                                            (exists $param{dist}?(dist => $param{dist}):()),
335                                            (exists $param{arch}?(arch => $param{arch}):(arch => $config{default_architectures})),
336                                            ($key eq 'src'?(arch => q(source)):()),
337                                            no_source_arch => 1,
338                                            return_archs => 1,
339                                           );
340                my $verdesc;
341                if (keys %versions > 1) {
342                     $verdesc = 'versions '. join(', ',
343                                     map { $_ .' ['.join(', ',
344                                                     sort @{$versions{$_}}
345                                                    ).']';
346                                    } keys %versions);
347                }
348                else {
349                     $verdesc = 'version '.join(', ',
350                                                keys %versions
351                                               );
352                }
353                $extra= " ($verdesc)" if keys %versions;
354           }
355           if ($key eq 'maint' and $entry eq '') {
356                push @entries, "no one (packages without maintainers)"
357           }
358           else {
359                push @entries, $entry.$extra;
360           }
361      }
362      push @title,$value.' '.join(' or ', @entries) if @entries;
363 }
364 if (defined $param{newest}) {
365      my $newest_bug = newest_bug();
366      @bugs = ($newest_bug - $param{newest} + 1) .. $newest_bug;
367      push @title, 'in '.@bugs.' newest reports';
368      $param{bugs} = [exists $param{bugs}?make_list($param{bugs}):(),
369                      @bugs,
370                     ];
371 }
372
373 my $title = $gBugs.' '.join(' and ', map {/ or /?"($_)":$_} @title);
374 @title = ();
375
376 #yeah for magick!
377 @bugs = get_bugs((map {exists $param{$_}?($_,$param{$_}):()}
378                   grep {$_ ne 'newest'}
379                   keys %package_search_keys, 'archive'),
380                  usertags => \%ut,
381                 );
382
383 # shove in bugs which affect this package if there is a package or a
384 # source given (by default), but no affects options given
385 if (not exists $param{affects} and not exists $param{noaffects} and
386     (exists $param{src} or
387      exists $param{package})) {
388     push @bugs, get_bugs((map {my $key = $_;
389                                exists $param{$key}?($key =~ /^(?:package|src)$/?'affects':$key,
390                                                   ($key eq 'src'?[map {"src:$_"}make_list($param{$key})]:$param{$_})):()}
391                           grep {$_ ne 'newest'}
392                           keys %package_search_keys, 'archive'),
393                          usertags => \%ut,
394                         );
395 }
396
397 # filter out included or excluded bugs
398
399
400 if (defined $param{version}) {
401      $title .= " at version $param{version}";
402 }
403 elsif (defined $param{dist}) {
404      $title .= " in $param{dist}";
405 }
406
407 $title = html_escape($title);
408
409 my @names; my @prior; my @order;
410 determine_ordering(cats => \%cats,
411                    param => \%param,
412                    ordering => \$ordering,
413                    names => \@names,
414                    prior => \@prior,
415                    title => \@title,
416                    order => \@order,
417                   );
418
419 # strip out duplicate bugs
420 my %bugs;
421 @bugs{@bugs} = @bugs;
422 @bugs = keys %bugs;
423
424 my $result = pkg_htmlizebugs(bugs => \@bugs,
425                              names => \@names,
426                              title => \@title,
427                              order => \@order,
428                              prior => \@prior,
429                              ordering => $ordering,
430                              bugusertags => \%bugusertags,
431                              bug_rev => $bug_rev,
432                              bug_order => $bug_order,
433                              repeatmerged => $param{repeatmerged},
434                              include => $include,
435                              exclude => $exclude,
436                              this => $this,
437                              options => \%param,
438                              (exists $param{dist})?(dist    => $param{dist}):(),
439                             );
440
441 print "Cache-Control: public, max-age=300\n";
442 print "Content-Type: text/html; charset=utf-8\n\n";
443
444 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
445 print "<HTML><HEAD>\n" . 
446     "<TITLE>$title -- $gProject$Archived $gBug report logs</TITLE>\n" .
447     qq(<link rel="stylesheet" href="$gWebHostBugDir/css/bugs.css" type="text/css">) .
448     "</HEAD>\n" .
449     '<BODY onload="pagemain();">' .
450     "\n";
451 print qq(<DIV id="status_mask"></DIV>\n);
452 print "<H1>" . "$gProject$Archived $gBug report logs: $title" .
453       "</H1>\n";
454
455 my $showresult = 1;
456
457 my $pkg = $param{package} if defined $param{package};
458 my $src = $param{src} if defined $param{src};
459
460 my $pseudodesc = getpseudodesc();
461 if (defined $pseudodesc and defined $pkg and exists $pseudodesc->{$pkg}) {
462      delete $param{dist};
463 }
464
465 # output information about the packages
466
467 for my $package (make_list($param{package}||[])) {
468      print generate_package_info(binary => 1,
469                                  package => $package,
470                                  options => \%param,
471                                  bugs    => \@bugs,
472                                 );
473 }
474 for my $package (make_list($param{src}||[])) {
475      print generate_package_info(binary => 0,
476                                  package => $package,
477                                  options => \%param,
478                                  bugs    => \@bugs,
479                                 );
480 }
481
482 if (exists $param{maint} or exists $param{maintenc}) {
483     print "<p>Note that maintainers may use different Maintainer fields for\n";
484     print "different packages, so there may be other reports filed under\n";
485     print "different addresses.\n";
486 }
487 if (exists $param{submitter}) {
488     print "<p>Note that people may use different email accounts for\n";
489     print "different bugs, so there may be other reports filed under\n";
490     print "different addresses.\n";
491 }
492
493 print $result;
494
495 print fill_in_template(template=>'cgi/pkgreport_javascript');
496
497 print qq(<h2 class="outstanding"><!--<a class="options" href="javascript:toggle(1)">-->Options<!--</a>--></h2>\n);
498
499 $param{orderings} =
500     [uniq((grep {!$hidden{$_}} keys %cats),
501           $param{ordering})];
502 print option_form(template => 'cgi/pkgreport_options',
503                   param    => \%param,
504                   form_options => $form_options,
505                   variables => $form_option_variables,
506                  );
507
508 print "<hr>\n";
509 print fill_in_template(template=>'html/html_tail',
510                        hole_var => {'&strftime' => \&POSIX::strftime,
511                                    },
512                       );
513 print "</body></html>\n";
514