You need to sign in or sign up before continuing.
query.cgi 15.2 KB
Newer Older
1
#!/usr/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>
25
#                 Byron Jones <bugzilla@glob.com.au>
26
#                 Max Kanat-Alexander <mkanat@bugzilla.org>
terry%netscape.com's avatar
terry%netscape.com committed
27

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

31
require "globals.pl";
terry%netscape.com's avatar
terry%netscape.com committed
32

33
use Bugzilla::Constants;
34
use Bugzilla::Search;
35
use Bugzilla::User;
36
use Bugzilla::Util;
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
use vars qw(
    @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
    $template
    $vars
);
56

57
my $cgi = Bugzilla->cgi;
58
my $dbh = Bugzilla->dbh;
59
my $buffer = $cgi->query_string();
60

61
if ($cgi->param("GoAheadAndLogIn")) {
terry%netscape.com's avatar
terry%netscape.com committed
62 63
    # We got here from a login page, probably from relogin.cgi.  We better
    # make sure the password is legit.
64
    Bugzilla->login(LOGIN_REQUIRED);
65
} else {
66
    Bugzilla->login();
67
}
terry%netscape.com's avatar
terry%netscape.com committed
68

69
my $userid = Bugzilla->user->id;
70

71
# Backwards compatibility hack -- if there are any of the old QUERY_*
72
# cookies around, and we are logged in, then move them into the database
73
# and nuke the cookie. This is required for Bugzilla 2.8 and earlier.
74
if ($userid) {
75
    my @oldquerycookies;
76
    foreach my $i ($cgi->cookie()) {
77
        if ($i =~ /^QUERY_(.*)$/) {
78
            push(@oldquerycookies, [$1, $i, $cgi->cookie($i)]);
79 80
        }
    }
81
    if (defined $cgi->cookie('DEFAULTQUERY')) {
82
        push(@oldquerycookies, [DEFAULT_QUERY_NAME, 'DEFAULTQUERY',
83
                                $cgi->cookie('DEFAULTQUERY')]);
84 85 86 87 88
    }
    if (@oldquerycookies) {
        foreach my $ref (@oldquerycookies) {
            my ($name, $cookiename, $value) = (@$ref);
            if ($value) {
89 90 91
                # If the query name contains invalid characters, don't import.
                $name =~ /[<>&]/ && next;
                trick_taint($name);
92
                $dbh->bz_lock_tables('namedqueries WRITE');
93 94 95 96
                my $query = $dbh->selectrow_array(
                    "SELECT query FROM namedqueries " .
                     "WHERE userid = ? AND name = ?",
                     undef, ($userid, $name));
97
                if (!$query) {
98
                    $dbh->do("INSERT INTO namedqueries " .
99
                            "(userid, name, query) VALUES " .
100
                            "(?, ?, ?)", undef, ($userid, $name, $value));
101
                }
102
                $dbh->bz_unlock_tables();
103
            }
104
            $cgi->remove_cookie($cookiename);
105 106
        }
    }
107
}
terry%netscape.com's avatar
terry%netscape.com committed
108

109
if ($cgi->param('nukedefaultquery')) {
110
    if ($userid) {
111 112 113
        $dbh->do("DELETE FROM namedqueries" .
                 " WHERE userid = ? AND name = ?", 
                 undef, ($userid, DEFAULT_QUERY_NAME));
114
    }
115
    $buffer = "";
terry%netscape.com's avatar
terry%netscape.com committed
116 117
}

118
my $userdefaultquery;
119
if ($userid) {
120 121 122 123
    $userdefaultquery = $dbh->selectrow_array(
        "SELECT query FROM namedqueries " .
         "WHERE userid = ? AND name = ?", 
         undef, ($userid, DEFAULT_QUERY_NAME));
terry%netscape.com's avatar
terry%netscape.com committed
124 125
}

126
my %default;
127

128 129 130 131
# 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 {
132 133
    my ($buf) = (@_);
    my $foundone = 0;
134 135

    # Nothing must be undef, otherwise the template complains.
136 137
    foreach my $name ("bug_status", "resolution", "assigned_to",
                      "rep_platform", "priority", "bug_severity",
138
                      "classification", "product", "reporter", "op_sys",
139
                      "component", "version", "chfield", "chfieldfrom",
140
                      "chfieldto", "chfieldvalue", "target_milestone",
141 142
                      "email", "emailtype", "emailreporter",
                      "emailassigned_to", "emailcc", "emailqa_contact",
143
                      "emaillongdesc", "content",
144 145 146
                      "changedin", "votes", "short_desc", "short_desc_type",
                      "long_desc", "long_desc_type", "bug_file_loc",
                      "bug_file_loc_type", "status_whiteboard",
147
                      "status_whiteboard_type", "bug_id",
148
                      "bugidtype", "keywords", "keywords_type",
149
                      "x_axis_field", "y_axis_field", "z_axis_field",
150 151 152
                      "chart_format", "cumulate", "x_labels_vertical",
                      "category", "subcategory", "name", "newcategory",
                      "newsubcategory", "public", "frequency") 
153
    {
154 155 156 157 158 159
        # 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} = ["", "", ""];
160
    }
161 162 163
 
 
    # Iterate over the URL parameters
164 165 166 167 168
    foreach my $item (split(/\&/, $buf)) {
        my @el = split(/=/, $item);
        my $name = $el[0];
        my $value;
        if ($#el > 0) {
169
            $value = Bugzilla::Util::url_decode($el[1]);
terry%netscape.com's avatar
terry%netscape.com committed
170
        } else {
171 172
            $value = "";
        }
173
        
174 175 176 177 178 179 180 181
        # If the name begins with field, type, or value, then it is part of
        # the boolean charts. Because these are built different than the rest
        # of the form, we don't need to save a default value. We do, however,
        # need to indicate that we found something so the default query isn't
        # added in if all we have are boolean chart items.
        if ($name =~ m/^(?:field|type|value)/) {
            $foundone = 1;
        }
182 183 184
        # 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.
185
        elsif ($name =~ m/^(.+)(\d)$/ && defined($default{$1})) {
186
            $foundone = 1;
187
            $default{$1}->[$2] = $value;
terry%netscape.com's avatar
terry%netscape.com committed
188
        }
189 190 191 192 193 194 195 196 197 198
        # 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);
        }        
    }        
199
    return $foundone;
terry%netscape.com's avatar
terry%netscape.com committed
200
}
201

202

203
if (!PrefillForm($buffer)) {
204 205 206
    # Ah-hah, there was no form stuff specified.  Do it again with the
    # default query.
    if ($userdefaultquery) {
207
        PrefillForm($userdefaultquery);
208
    } else {
209
        PrefillForm(Param("defaultquery"));
210 211
    }
}
212

213 214
if ($default{'chfieldto'}->[0] eq "") {
    $default{'chfieldto'} = ["Now"];
terry%netscape.com's avatar
terry%netscape.com committed
215 216
}

217
GetVersionTable();
218

219 220
# if using groups for entry, then we don't want people to see products they 
# don't have access to. Remove them from the list.
221

222
my @products = ();
223 224 225
my %component_set;
my %version_set;
my %milestone_set;
226
foreach my $p (GetSelectableProducts()) {
227 228 229 230 231 232
    # 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;
233 234
        }
    }
235 236
    foreach my $v (@{$::versions{$p}}) {
        $version_set{$v} = 1;
237
    }
238 239
    foreach my $m (@{$::target_milestone{$p}}) {
        $milestone_set{$m} = 1;
240
    }
241 242
}

243 244 245
# @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;
246

247 248 249 250 251 252 253
# 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;
254
    }
255
}
256 257 258
foreach my $v (@::legal_versions) {
    if ($version_set{$v}) {
        push @versions, $v;
259
    }
260
}
261 262 263
foreach my $m (@::legal_target_milestone) {
    if ($milestone_set{$m}) {
        push @milestones, $m;
264
    }
265 266
}

267 268 269 270
# Create data structures representing each product.
for (my $i = 0; $i < @products; ++$i) {
    my $p = $products[$i];
    
271 272 273 274 275
    # Bug 190611: band-aid to avoid crashing with no versions defined
    if (!defined ($::components{$p})) {
        $::components{$p} = [];
    }
    
276 277 278 279 280 281 282 283
    # Create hash to hold attributes for each product.
    my %product = (
        'name'       => $p,
        'components' => [ sort { lc($a) cmp lc($b) } @{$::components{$p}} ],
        'versions'   => [ sort { lc($a) cmp lc($b) } @{$::versions{$p}}   ]
    );
    
    if (Param('usetargetmilestone')) {
284 285
        # Sorting here is required for ordering multiple selections 
        # correctly; see bug 97736 for discussion on how to fix this
286 287 288 289 290 291
        $product{'milestones'} =  
                      [ sort { lc($a) cmp lc($b) } @{$::target_milestone{$p}} ];
    }
    
    # Assign hash back to product array.
    $products[$i] = \%product;
292
}
293
$vars->{'product'} = \@products;
294

295 296 297 298
# Create data structures representing each classification
if (Param('useclassification')) {
    my @classifications = ();

299
    foreach my $c (GetSelectableClassifications()) {
300 301 302 303 304 305 306 307 308 309 310
        # Create hash to hold attributes for each classification.
        my %classification = (
            'name'       => $c,
            'products'   => [ GetSelectableProducts(0,$c) ]
        );
        # Assign hash back to classification array.
        push @classifications, \%classification;
    }
    $vars->{'classification'} = \@classifications;
}

311 312
# We use 'component_' because 'component' is a Template Toolkit reserved word.
$vars->{'component_'} = \@components;
terry%netscape.com's avatar
terry%netscape.com committed
313

314
$vars->{'version'} = \@versions;
315

316 317
if (Param('usetargetmilestone')) {
    $vars->{'target_milestone'} = \@milestones;
318 319
}

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

322 323 324 325 326
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;

327 328
my @chfields;

329
push @chfields, "[Bug creation]";
330 331 332 333

# This is what happens when you have variables whose definition depends
# on the DB schema, and then the underlying schema changes...
foreach my $val (@::log_columns) {
334 335 336
    if ($val eq 'classification_id') {
        $val = 'classification';
    } elsif ($val eq 'product_id') {
337 338 339 340 341 342 343
        $val = 'product';
    } elsif ($val eq 'component_id') {
        $val = 'component';
    }
    push @chfields, $val;
}

344 345 346 347 348 349 350 351
if (UserInGroup(Param('timetrackinggroup'))) {
    push @chfields, "work_time";
} else {
    @chfields = grep($_ ne "estimated_time", @chfields);
    @chfields = grep($_ ne "remaining_time", @chfields);
}
@chfields = (sort(@chfields));
$vars->{'chfield'} = \@chfields;
352 353 354 355 356 357 358
$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;

# Boolean charts
359
my @fields;
360
push(@fields, { name => "noop", description => "---" });
361
push(@fields, $dbh->bz_get_field_defs());
362
$vars->{'fields'} = \@fields;
363

364 365 366
# 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.
367
foreach my $cmd (grep(/^cmd-/, $cgi->param)) {
368
    if ($cmd =~ /^cmd-add(\d+)-(\d+)-(\d+)$/) {
369
        $cgi->param(-name => "field$1-$2-$3", -value => "xyzzy");
370 371
    }
}
372

373 374
if (!$cgi->param('field0-0-0')) {
    $cgi->param(-name => 'field0-0-0', -value => "xyzzy");
375 376
}

377 378 379 380
# 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;
381
for (my $chart = 0; $cgi->param("field$chart-0-0"); $chart++) {
382
    my @rows;
383
    for (my $row = 0; $cgi->param("field$chart-$row-0"); $row++) {
384
        my @cols;
385 386 387 388
        for (my $col = 0; $cgi->param("field$chart-$row-$col"); $col++) {
            push(@cols, { field => $cgi->param("field$chart-$row-$col"),
                          type => $cgi->param("type$chart-$row-$col"),
                          value => $cgi->param("value$chart-$row-$col") });
389
        }
390
        push(@rows, \@cols);
391
    }
392
    push(@charts, {'rows' => \@rows, 'negate' => scalar($cgi->param("negate$chart")) });
393 394
}

395
$default{'charts'} = \@charts;
396

397
# Named queries
398
if ($userid) {
399 400 401 402 403
     $vars->{'namedqueries'} = $dbh->selectcol_arrayref(
           "SELECT name FROM namedqueries " .
            "WHERE userid = ? AND name != ?" .
         "ORDER BY name",
         undef, ($userid, DEFAULT_QUERY_NAME));
404
}
terry%netscape.com's avatar
terry%netscape.com committed
405

406 407 408
# Sort order
my $deforder;
my @orders = ('Bug Number', 'Importance', 'Assignee', 'Last Changed');
409

410
if ($cgi->cookie('LASTORDER')) {
411 412 413
    $deforder = "Reuse same sort as last time";
    unshift(@orders, $deforder);
}
414

415
if ($cgi->param('order')) { $deforder = $cgi->param('order') }
416

417 418 419
$vars->{'userdefaultquery'} = $userdefaultquery;
$vars->{'orders'} = \@orders;
$default{'querytype'} = $deforder || 'Importance';
terry%netscape.com's avatar
terry%netscape.com committed
420

421 422
if (($cgi->param('query_format') || $cgi->param('format') || "")
    eq "create-series") {
423 424 425 426
    require Bugzilla::Chart;
    $vars->{'category'} = Bugzilla::Chart::getVisibleSeries();
}

427 428 429
$vars->{'known_name'} = $cgi->param('known_name');


430 431
# Add in the defaults.
$vars->{'default'} = \%default;
432

433 434 435 436 437 438 439 440 441 442 443 444
$vars->{'format'} = $cgi->param('format');
$vars->{'query_format'} = $cgi->param('query_format');

# Set default page to "specific" if none proviced
if (!($cgi->param('query_format') || $cgi->param('format'))) {
    if (defined $cgi->cookie('DEFAULTFORMAT')) {
        $vars->{'format'} = $cgi->cookie('DEFAULTFORMAT');
    } else {
        $vars->{'format'} = 'specific';
    }
}

445 446
# Set cookie to current format as default, but only if the format
# one that we should remember.
447
if (defined($vars->{'format'}) && IsValidQueryType($vars->{'format'})) {
448 449 450 451
    $cgi->send_cookie(-name => 'DEFAULTFORMAT',
                      -value => $vars->{'format'},
                      -expires => "Fri, 01-Jan-2038 00:00:00 GMT");
}
452

453
# Generate and return the UI (HTML page) from the appropriate template.
454 455 456 457
# If we submit back to ourselves (for e.g. boolean charts), we need to
# preserve format information; hence query_format taking priority over
# format.
my $format = GetFormat("search/search", 
458
                       $vars->{'query_format'} || $vars->{'format'}, 
459
                       scalar $cgi->param('ctype'));
460 461 462

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

463
$template->process($format->{'template'}, $vars)
464
  || ThrowTemplateError($template->error());