duplicates.cgi 9.19 KB
Newer Older
1
#!/usr/bin/perl -wT
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Gervase Markham <gerv@gerv.net>
#
# Generates mostfreq list from data collected by collectstats.pl.

25

26
use strict;
27

28
use AnyDBM_File;
29 30 31

use lib qw(.);

32 33
require "globals.pl";

34
use Bugzilla;
35
use Bugzilla::Search;
36
use Bugzilla::Config qw(:DEFAULT $datadir);
37
use Bugzilla::Constants;
38 39

my $cgi = Bugzilla->cgi;
40

41 42 43
# Go directly to the XUL version of the duplicates report (duplicates.xul)
# if the user specified ctype=xul.  Adds params if they exist, and directs
# the user to a signed copy of the script in duplicates.jar if it exists.
44 45
if (defined $cgi->param('ctype') && $cgi->param('ctype') eq "xul") {
    my $params = CanonicaliseParams($cgi->query_string(), ["format", "ctype"]);
46
    my $url = (-e "duplicates.jar" ? "duplicates.jar!/" : "") . 
47
          "duplicates.xul" . ($params ? "?$params" : "") . "\n\n";
48 49

    print $cgi->redirect($url);
50 51 52
    exit;
}

53 54
my $template = Bugzilla->template;
my $vars = {};
55

56 57
GetVersionTable();

58 59 60 61 62 63 64
# collectstats.pl uses duplicates.cgi to generate the RDF duplicates stats.
# However, this conflicts with requirelogin if it's enabled; so we make
# logging-in optional if we are running from the command line.
if ($::ENV{'GATEWAY_INTERFACE'} eq "cmdline") {
    Bugzilla->login(LOGIN_OPTIONAL);
}
else {
65
    Bugzilla->login();
66
}
67

68
my $dbh = Bugzilla->switch_to_shadow_db();
69

70
my %dbmcount;
71 72 73
my %count;
my %before;

74
# Get params from URL
75 76
sub formvalue {
    my ($name, $default) = (@_);
77
    return $cgi->param($name) || $default || "";
78
}
79

80 81 82 83
my $sortby = formvalue("sortby");
my $changedsince = formvalue("changedsince", 7);
my $maxrows = formvalue("maxrows", 100);
my $openonly = formvalue("openonly");
84
my $reverse = formvalue("reverse") ? 1 : 0;
85
my @query_products = $cgi->param('product');
86 87
my $sortvisible = formvalue("sortvisible");
my @buglist = (split(/[:,]/, formvalue("bug_id")));
88

89
my $product_id;
90 91
foreach my $p (@query_products) {
    $product_id = get_product_id($p);
92
    if (!$product_id) {
93
        ThrowUserError("invalid_product_name",
94
                       { product => $p });
95 96 97
    }
}

98 99
# Small backwards-compatibility hack, dated 2002-04-10.
$sortby = "count" if $sortby eq "dup_count";
100

101
# Open today's record of dupes
102 103
my $today = days_ago(0);
my $yesterday = days_ago(1);
104

105 106 107 108 109 110 111 112 113
# We don't know the exact file name, because the extention depends on the
# underlying dbm library, which could be anything. We can't glob, because
# perl < 5.6 considers if (<*>) { ... } to be tainted
# Instead, just check the return value for today's data and yesterday's,
# and ignore file not found errors

use Errno;
use Fcntl;

114
if (!tie(%dbmcount, 'AnyDBM_File', "$datadir/duplicates/dupes$today",
115 116
         O_RDONLY, 0644)) {
    if ($!{ENOENT}) {
117
        if (!tie(%dbmcount, 'AnyDBM_File', "$datadir/duplicates/dupes$yesterday",
118
                 O_RDONLY, 0644)) {
119
            my $vars = { today => $today };
120
            if ($!{ENOENT}) {
121
                ThrowUserError("no_dupe_stats", $vars);
122
            } else {
123
                $vars->{'error_msg'} = $!;
124
                ThrowUserError("no_dupe_stats_error_yesterday", $vars);
125 126 127
            }
        }
    } else {
128 129
        ThrowUserError("no_dupe_stats_error_today",
                       { error_msg => $! });
130
    }
131 132 133 134
}

# Copy hash (so we don't mess up the on-disk file when we remove entries)
%count = %dbmcount;
135 136 137

# Remove all those dupes under the threshold parameter. 
# We do this, before the sorting, for performance reasons.
138
my $threshold = Param("mostfreqthreshold");
139

140 141
while (my ($key, $value) = each %count) {
    delete $count{$key} if ($value < $threshold);
142 143 144
    
    # If there's a buglist, restrict the bugs to that list.
    delete $count{$key} if $sortvisible && (lsearch(\@buglist, $key) == -1);
145 146
}

147 148 149 150 151 152 153 154 155
my $origmaxrows = $maxrows;
detaint_natural($maxrows)
  || ThrowUserError("invalid_maxrows", { maxrows => $origmaxrows});

my $origchangedsince = $changedsince;
detaint_natural($changedsince)
  || ThrowUserError("invalid_changedsince", 
                    { changedsince => $origchangedsince });

156
# Try and open the database from "changedsince" days ago
157
my $dobefore = 0;
158
my %delta;
159 160
my $whenever = days_ago($changedsince);    

161
if (!tie(%before, 'AnyDBM_File', "$datadir/duplicates/dupes$whenever",
162 163 164
         O_RDONLY, 0644)) {
    # Ignore file not found errors
    if (!$!{ENOENT}) {
165 166 167 168 169
        ThrowUserError("no_dupe_stats_error_whenever",
                       { error_msg => $!,
                         changedsince => $changedsince,
                         whenever => $whenever,
                       });
170 171
    }
} else {
172
    # Calculate the deltas
173
    ($delta{$_} = $count{$_} - ($before{$_} || 0)) foreach (keys(%count));
174 175 176 177 178 179 180

    $dobefore = 1;
}

my @bugs;
my @bug_ids; 

181
if (scalar(%count)) {
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    # use Bugzilla::Search so that we get the security checking
    my $params = new Bugzilla::CGI({ 'bug_id' => [keys %count] });

    if ($openonly) {
        $params->param('resolution', '---');
    } else {
        # We want to show bugs which:
        # a) Aren't CLOSED; and
        # b)  i) Aren't VERIFIED; OR
        #    ii) Were resolved INVALID/WONTFIX

        # The rationale behind this is that people will eventually stop
        # reporting fixed bugs when they get newer versions of the software,
        # but if the bug is determined to be erroneous, people will still
        # keep reporting it, so we do need to show it here.

        # a)
        $params->param('field0-0-0', 'bug_status');
        $params->param('type0-0-0', 'notequals');
        $params->param('value0-0-0', 'CLOSED');

        # b) i)
        $params->param('field0-1-0', 'bug_status');
        $params->param('type0-1-0', 'notequals');
        $params->param('value0-1-0', 'VERIFIED');

        # b) ii)
        $params->param('field0-1-1', 'resolution');
        $params->param('type0-1-1', 'anyexact');
        $params->param('value0-1-1', 'INVALID,WONTFIX');
    }

    # Restrict to product if requested
215
    if ($cgi->param('product')) {
216
        $params->param('product', join(',', @query_products));
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    }

    my $query = new Bugzilla::Search('fields' => [qw(bugs.bug_id
                                                     map_components.name
                                                     bugs.bug_severity
                                                     bugs.op_sys
                                                     bugs.target_milestone
                                                     bugs.short_desc
                                                     bugs.bug_status
                                                     bugs.resolution
                                                    )
                                                 ],
                                     'params' => $params,
                                    );

232
    my $results = $dbh->selectall_arrayref($query->getSQL());
233

234
    foreach my $result (@$results) {
235 236 237
        # Note: maximum row count is dealt with in the template.

        my ($id, $component, $bug_severity, $op_sys, $target_milestone, 
238
            $short_desc, $bug_status, $resolution) = @$result;
239 240 241 242 243 244 245 246 247 248 249 250 251

        push (@bugs, { id => $id,
                       count => $count{$id},
                       delta => $delta{$id}, 
                       component => $component,
                       bug_severity => $bug_severity,
                       op_sys => $op_sys,
                       target_milestone => $target_milestone,
                       short_desc => $short_desc,
                       bug_status => $bug_status, 
                       resolution => $resolution });
        push (@bug_ids, $id); 
    }
252 253 254 255 256 257 258 259 260 261 262 263
}

$vars->{'bugs'} = \@bugs;
$vars->{'bug_ids'} = \@bug_ids;

$vars->{'dobefore'} = $dobefore;
$vars->{'sortby'} = $sortby;
$vars->{'sortvisible'} = $sortvisible;
$vars->{'changedsince'} = $changedsince;
$vars->{'maxrows'} = $maxrows;
$vars->{'openonly'} = $openonly;
$vars->{'reverse'} = $reverse;
264
$vars->{'format'} = $cgi->param('format');
265
$vars->{'query_products'} = \@query_products;
266
$vars->{'products'} = Bugzilla->user->get_selectable_products;
267

268

269 270 271
my $format = $template->get_format("reports/duplicates",
                                   scalar($cgi->param('format')),
                                   scalar($cgi->param('ctype')));
272 273

print $cgi->header($format->{'ctype'});
274 275

# Generate and return the UI (HTML page) from the appropriate template.
276
$template->process($format->{'template'}, $vars)
277
  || ThrowTemplateError($template->error());
278 279 280 281 282


sub days_ago {
    my ($dom, $mon, $year) = (localtime(time - ($_[0]*24*60*60)))[3, 4, 5];
    return sprintf "%04d-%02d-%02d", 1900 + $year, ++$mon, $dom;
283
}