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