query.cgi 12.2 KB
Newer Older
1
#!/usr/bonsaitools/bin/perl -wT
2
# -*- Mode: perl; indent-tabs-mode: nil -*-
terry%netscape.com's avatar
terry%netscape.com committed
3
#
4 5 6 7 8 9 10 11 12 13
# 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.
#
terry%netscape.com's avatar
terry%netscape.com committed
14
# The Original Code is the Bugzilla Bug Tracking System.
15
#
terry%netscape.com's avatar
terry%netscape.com committed
16
# The Initial Developer of the Original Code is Netscape Communications
17 18 19 20
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
terry%netscape.com's avatar
terry%netscape.com committed
21
# Contributor(s): Terry Weissman <terry@mozilla.org>
22
#                 David Gardiner <david.gardiner@unisa.edu.au>
23
#                 Matthias Radestock <matthias@sorted.org>
24
#                 Gervase Markham <gerv@gerv.net>
terry%netscape.com's avatar
terry%netscape.com committed
25

26 27
use diagnostics;
use strict;
28
use lib ".";
terry%netscape.com's avatar
terry%netscape.com committed
29

30
require "CGI.pl";
terry%netscape.com's avatar
terry%netscape.com committed
31

32 33
# Prevents &make_options in CGI.pl from throwing an error if we give it
# an invalid list of selections (from a remembered query containing values
34
# that no longer exist), since we don't want to die in the query page.
35
$::CheckOptionValues = 0;
terry%netscape.com's avatar
terry%netscape.com committed
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
use vars qw(
    @CheckOptionValues
    @legal_resolution
    @legal_bug_status
    @legal_components
    @legal_keywords
    @legal_opsys
    @legal_platform
    @legal_priority
    @legal_product
    @legal_severity
    @legal_target_milestone
    @legal_versions
    @log_columns
    %versions
    %components
    %FORM
    $template
    $vars
);
57 58

if (defined $::FORM{"GoAheadAndLogIn"}) {
terry%netscape.com's avatar
terry%netscape.com committed
59 60
    # We got here from a login page, probably from relogin.cgi.  We better
    # make sure the password is legit.
61
    confirm_login();
62 63 64
} else {
    quietly_check_login();
}
terry%netscape.com's avatar
terry%netscape.com committed
65

66
# Backwards compatibility hack -- if there are any of the old QUERY_*
67
# cookies around, and we are logged in, then move them into the database
68 69
# and nuke the cookie. This is required for Bugzilla 2.8 and earlier.
if ($::userid) {
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    my @oldquerycookies;
    foreach my $i (keys %::COOKIE) {
        if ($i =~ /^QUERY_(.*)$/) {
            push(@oldquerycookies, [$1, $i, $::COOKIE{$i}]);
        }
    }
    if (defined $::COOKIE{'DEFAULTQUERY'}) {
        push(@oldquerycookies, [$::defaultqueryname, 'DEFAULTQUERY',
                                $::COOKIE{'DEFAULTQUERY'}]);
    }
    if (@oldquerycookies) {
        foreach my $ref (@oldquerycookies) {
            my ($name, $cookiename, $value) = (@$ref);
            if ($value) {
                my $qname = SqlQuote($name);
                SendSQL("SELECT query FROM namedqueries " .
86
                        "WHERE userid = $::userid AND name = $qname");
87 88 89 90
                my $query = FetchOneColumn();
                if (!$query) {
                    SendSQL("REPLACE INTO namedqueries " .
                            "(userid, name, query) VALUES " .
91
                            "($::userid, $qname, " . SqlQuote($value) . ")");
92 93
                }
            }
94 95
            print "Set-Cookie: $cookiename= ; path=" . Param("cookiepath") . 
                  "; expires=Sun, 30-Jun-1980 00:00:00 GMT\n";
96 97
        }
    }
98
}
terry%netscape.com's avatar
terry%netscape.com committed
99

100
if ($::FORM{'nukedefaultquery'}) {
101
    if ($::userid) {
102
        SendSQL("DELETE FROM namedqueries " .
103
                "WHERE userid = $::userid AND name = '$::defaultqueryname'");
104 105
    }
    $::buffer = "";
terry%netscape.com's avatar
terry%netscape.com committed
106 107
}

108
my $userdefaultquery;
109
if ($::userid) {
110
    SendSQL("SELECT query FROM namedqueries " .
111
            "WHERE userid = $::userid AND name = '$::defaultqueryname'");
112
    $userdefaultquery = FetchOneColumn();
terry%netscape.com's avatar
terry%netscape.com committed
113 114
}

115
my %default;
116

117 118 119 120
# We pass the defaults as a hash of references to arrays. For those
# Items which are single-valued, the template should only reference [0]
# and ignore any multiple values.
sub PrefillForm {
121 122
    my ($buf) = (@_);
    my $foundone = 0;
123 124

    # Nothing must be undef, otherwise the template complains.
125 126 127 128
    foreach my $name ("bug_status", "resolution", "assigned_to",
                      "rep_platform", "priority", "bug_severity",
                      "product", "reporter", "op_sys",
                      "component", "version", "chfield", "chfieldfrom",
129
                      "chfieldto", "chfieldvalue", "target_milestone",
130 131 132
                      "email", "emailtype", "emailreporter",
                      "emailassigned_to", "emailcc", "emailqa_contact",
                      "emaillongdesc",
133 134 135
                      "changedin", "votes", "short_desc", "short_desc_type",
                      "long_desc", "long_desc_type", "bug_file_loc",
                      "bug_file_loc_type", "status_whiteboard",
136 137
                      "status_whiteboard_type", "bug_id",
                      "bugidtype", "keywords", "keywords_type") {
138 139 140 141 142 143
        # This is a bit of a hack. The default, empty list has 
        # three entries to accommodate the needs of the email fields -
        # we use each position to denote the relevant field. Array
        # position 0 is unused for email fields because the form 
        # parameters historically started at 1.
        $default{$name} = ["", "", ""];
144
    }
145 146 147
 
 
    # Iterate over the URL parameters
148 149 150 151 152 153
    foreach my $item (split(/\&/, $buf)) {
        my @el = split(/=/, $item);
        my $name = $el[0];
        my $value;
        if ($#el > 0) {
            $value = url_decode($el[1]);
terry%netscape.com's avatar
terry%netscape.com committed
154
        } else {
155 156
            $value = "";
        }
157 158 159 160 161
        
        # If the name ends in a number (which it does for the fields which
        # are part of the email searching), we use the array
        # positions to show the defaults for that number field.
        if ($name =~ m/^(.+)(\d)$/ && defined($default{$1})) {
162
            $foundone = 1;
163
            $default{$1}->[$2] = $value;
terry%netscape.com's avatar
terry%netscape.com committed
164
        }
165 166 167 168 169 170 171 172 173 174
        # If there's no default yet, we replace the blank string.
        elsif (defined($default{$name}) && $default{$name}->[0] eq "") {
            $foundone = 1;
            $default{$name} = [$value]; 
        } 
        # If there's already a default, we push on the new value.
        elsif (defined($default{$name})) {
            push (@{$default{$name}}, $value);
        }        
    }        
175
    return $foundone;
terry%netscape.com's avatar
terry%netscape.com committed
176
}
177

178

179
if (!PrefillForm($::buffer)) {
180 181 182
    # Ah-hah, there was no form stuff specified.  Do it again with the
    # default query.
    if ($userdefaultquery) {
183
        PrefillForm($userdefaultquery);
184
    } else {
185
        PrefillForm(Param("defaultquery"));
186 187
    }
}
188

189 190
if ($default{'chfieldto'}->[0] eq "") {
    $default{'chfieldto'} = ["Now"];
terry%netscape.com's avatar
terry%netscape.com committed
191 192
}

193
GetVersionTable();
194

195
# if using usebuggroups, then we don't want people to see products they don't
196
# have access to. Remove them from the list.
197

198
my @products = ();
199 200 201
my %component_set;
my %version_set;
my %milestone_set;
202
foreach my $p (@::legal_product) {
203 204 205
    # If we're using bug groups to restrict entry on products, and
    # this product has a bug group, and the user is not in that
    # group, we don't want to include that product in this list.
206 207 208 209 210 211 212 213
    next if (Param("usebuggroups") && GroupExists($p) && !UserInGroup($p));

    # We build up boolean hashes in the "-set" hashes for each of these things 
    # before making a list because there may be duplicates names across products.
    push @products, $p;
    if ($::components{$p}) {
        foreach my $c (@{$::components{$p}}) {
            $component_set{$c} = 1;
214 215
        }
    }
216 217
    foreach my $v (@{$::versions{$p}}) {
        $version_set{$v} = 1;
218
    }
219 220
    foreach my $m (@{$::target_milestone{$p}}) {
        $milestone_set{$m} = 1;
221
    }
222 223
}

224 225 226
# @products is now all the products we are ever concerned with, as a list
# %x_set is now a unique "list" of the relevant components/versions/tms
@products = sort { lc($a) cmp lc($b) } @products;
227

228 229 230 231 232 233 234
# Create the component, version and milestone lists.
my @components = ();
my @versions = ();
my @milestones = ();
foreach my $c (@::legal_components) {
    if ($component_set{$c}) {
        push @components, $c;
235
    }
236
}
237 238 239
foreach my $v (@::legal_versions) {
    if ($version_set{$v}) {
        push @versions, $v;
240
    }
241
}
242 243 244
foreach my $m (@::legal_target_milestone) {
    if ($milestone_set{$m}) {
        push @milestones, $m;
245
    }
246 247
}

248 249 250 251 252 253
# Sort the component list...
my $comps = \%::components;
foreach my $p (@products) {
    my @tmp = sort { lc($a) cmp lc($b) } @{$comps->{$p}};
    $comps->{$p} = \@tmp;
}    
terry%netscape.com's avatar
terry%netscape.com committed
254

255 256 257 258 259 260
# and the version list...
my $vers = \%::versions;
foreach my $p (@products) {
    my @tmp = sort { lc($a) cmp lc($b) } @{$vers->{$p}};
    $vers->{$p} = \@tmp;
}    
terry%netscape.com's avatar
terry%netscape.com committed
261

262 263 264 265 266 267 268 269
# and the milestone list.
my $mstones;
if (Param('usetargetmilestone')) {
    $mstones = \%::target_milestone;
    foreach my $p (@products) {
        my @tmp = sort { lc($a) cmp lc($b) } @{$mstones->{$p}};
        $mstones->{$p} = \@tmp;
    }    
270 271
}

272 273 274 275
# "foo" or "foos" is a list of all the possible (or legal) products, 
# components, versions or target milestones.
# "foobyproduct" is a hash, keyed by product, of sorted lists
# of the same data.
276

277
$vars->{'product'} = \@products;
278

279 280 281
# We use 'component_' because 'component' is a Template Toolkit reserved word.
$vars->{'componentsbyproduct'} = $comps;
$vars->{'component_'} = \@components;
terry%netscape.com's avatar
terry%netscape.com committed
282

283 284
$vars->{'versionsbyproduct'} = $vers;
$vars->{'version'} = \@versions;
285

286 287 288
if (Param('usetargetmilestone')) {
    $vars->{'milestonesbyproduct'} = $mstones;
    $vars->{'target_milestone'} = \@milestones;
289 290
}

291
$vars->{'have_keywords'} = scalar(@::legal_keywords);
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306
push @::legal_resolution, "---"; # Oy, what a hack.
shift @::legal_resolution; 
      # Another hack - this array contains "" for some reason. See bug 106589.
$vars->{'resolution'} = \@::legal_resolution;

$vars->{'chfield'} = ["[Bug creation]", @::log_columns];
$vars->{'bug_status'} = \@::legal_bug_status;
$vars->{'rep_platform'} = \@::legal_platform;
$vars->{'op_sys'} = \@::legal_opsys;
$vars->{'priority'} = \@::legal_priority;
$vars->{'bug_severity'} = \@::legal_severity;
$vars->{'userid'} = $::userid;

# Boolean charts
307
my @fields;
308
push(@fields, { name => "noop", description => "---" });
309
ConnectToDatabase();
310 311
SendSQL("SELECT name, description FROM fielddefs ORDER BY sortkey");
while (MoreSQLData()) {
312 313
    my ($name, $description) = FetchSQLData();
    push(@fields, { name => $name, description => $description });
314 315
}

316
$vars->{'fields'} = \@fields;
317

318 319 320
# Creating new charts - if the cmd-add value is there, we define the field
# value so the code sees it and creates the chart. It will attempt to select
# "xyzzy" as the default, and fail. This is the correct behaviour.
321 322
foreach my $cmd (grep(/^cmd-/, keys(%::FORM))) {
    if ($cmd =~ /^cmd-add(\d+)-(\d+)-(\d+)$/) {
323
        $::FORM{"field$1-$2-$3"} = "xyzzy";
324 325
    }
}
326

327 328 329 330
if (!exists $::FORM{'field0-0-0'}) {
    $::FORM{'field0-0-0'} = "xyzzy";
}

331 332 333 334 335
# Create data structure of boolean chart info. It's an array of arrays of
# arrays - with the inner arrays having three members - field, type and
# value.
my @charts;
for (my $chart = 0; $::FORM{"field$chart-0-0"}; $chart++) {
336
    my @rows;
337
    for (my $row = 0; $::FORM{"field$chart-$row-0"}; $row++) {
338
        my @cols;
339 340 341 342
        for (my $col = 0; $::FORM{"field$chart-$row-$col"}; $col++) {
            push(@cols, { field => $::FORM{"field$chart-$row-$col"},
                          type => $::FORM{"type$chart-$row-$col"},
                          value => $::FORM{"value$chart-$row-$col"} });
343
        }
344
        push(@rows, \@cols);
345
    }
346
    push(@charts, \@rows);
347 348
}

349
$default{'charts'} = \@charts;
350

351 352
# Named queries
if ($::userid) {
353
    my @namedqueries;
354 355 356 357 358
    SendSQL("SELECT name FROM namedqueries " .
            "WHERE userid = $::userid AND name != '$::defaultqueryname' " .
            "ORDER BY name");
    while (MoreSQLData()) {
        push(@namedqueries, FetchOneColumn());
359 360
    }
    
361
    $vars->{'namedqueries'} = \@namedqueries;    
362
}
terry%netscape.com's avatar
terry%netscape.com committed
363

364 365 366
# Sort order
my $deforder;
my @orders = ('Bug Number', 'Importance', 'Assignee', 'Last Changed');
367 368 369 370 371

if ($::COOKIE{'LASTORDER'}) {
    $deforder = "Reuse same sort as last time";
    unshift(@orders, $deforder);
}
372

373 374
if ($::FORM{'order'}) { $deforder = $::FORM{'order'} }

375 376 377
$vars->{'userdefaultquery'} = $userdefaultquery;
$vars->{'orders'} = \@orders;
$default{'querytype'} = $deforder || 'Importance';
terry%netscape.com's avatar
terry%netscape.com committed
378

379 380
# Add in the defaults.
$vars->{'default'} = \%default;
381

382 383
# Generate and return the UI (HTML page) from the appropriate template.
print "Content-type: text/html\n\n";
384 385
$template->process("search/search.html.tmpl", $vars)
  || ThrowTemplateError($template->error());