query.cgi 15.1 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 70
my $user = Bugzilla->user;
my $userid = $user->id;
71

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

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

119
my $userdefaultquery;
120
if ($userid) {
121 122 123 124
    $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
125 126
}

127
my %default;
128

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

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

203

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

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

218
GetVersionTable();
219

220 221
# 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.
222

223 224
my @selectable_product_objects = @{$user->get_selectable_products};

225 226 227
my %component_set;
my %version_set;
my %milestone_set;
228 229 230 231
# extract product names
my @products = map { $_->name } @selectable_product_objects;

foreach my $prod_name (@products) {
232 233
    # 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.
234 235
    if ($::components{$prod_name}) {
        foreach my $c (@{$::components{$prod_name}}) {
236
            $component_set{$c} = 1;
237 238
        }
    }
239
    foreach my $v (@{$::versions{$prod_name}}) {
240
        $version_set{$v} = 1;
241
    }
242
    foreach my $m (@{$::target_milestone{$prod_name}}) {
243
        $milestone_set{$m} = 1;
244
    }
245 246
}

247 248 249
# @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;
250

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

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

299 300
# Create data structures representing each classification
if (Param('useclassification')) {
301
    $vars->{'classification'} = $user->get_selectable_classifications;
302 303
}

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

307
$vars->{'version'} = \@versions;
308

309 310
if (Param('usetargetmilestone')) {
    $vars->{'target_milestone'} = \@milestones;
311 312
}

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

315 316 317 318 319
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;

320 321
my @chfields;

322
push @chfields, "[Bug creation]";
323 324 325 326

# 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) {
327 328 329
    if ($val eq 'classification_id') {
        $val = 'classification';
    } elsif ($val eq 'product_id') {
330 331 332 333 334 335 336
        $val = 'product';
    } elsif ($val eq 'component_id') {
        $val = 'component';
    }
    push @chfields, $val;
}

337 338 339 340 341 342 343 344
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;
345 346 347 348 349 350 351
$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
352
my @fields;
353
push(@fields, { name => "noop", description => "---" });
354
push(@fields, $dbh->bz_get_field_defs());
355
$vars->{'fields'} = \@fields;
356

357 358 359
# 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.
360
foreach my $cmd (grep(/^cmd-/, $cgi->param)) {
361
    if ($cmd =~ /^cmd-add(\d+)-(\d+)-(\d+)$/) {
362
        $cgi->param(-name => "field$1-$2-$3", -value => "xyzzy");
363 364
    }
}
365

366 367
if (!$cgi->param('field0-0-0')) {
    $cgi->param(-name => 'field0-0-0', -value => "xyzzy");
368 369
}

370 371 372 373
# 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;
374
for (my $chart = 0; $cgi->param("field$chart-0-0"); $chart++) {
375
    my @rows;
376
    for (my $row = 0; $cgi->param("field$chart-$row-0"); $row++) {
377
        my @cols;
378 379
        for (my $col = 0; $cgi->param("field$chart-$row-$col"); $col++) {
            push(@cols, { field => $cgi->param("field$chart-$row-$col"),
380 381
                          type => $cgi->param("type$chart-$row-$col") || 'noop',
                          value => $cgi->param("value$chart-$row-$col") || '' });
382
        }
383
        push(@rows, \@cols);
384
    }
385
    push(@charts, {'rows' => \@rows, 'negate' => scalar($cgi->param("negate$chart")) });
386 387
}

388
$default{'charts'} = \@charts;
389

390
# Named queries
391
if ($userid) {
392 393 394 395 396
     $vars->{'namedqueries'} = $dbh->selectcol_arrayref(
           "SELECT name FROM namedqueries " .
            "WHERE userid = ? AND name != ?" .
         "ORDER BY name",
         undef, ($userid, DEFAULT_QUERY_NAME));
397
}
terry%netscape.com's avatar
terry%netscape.com committed
398

399 400 401
# Sort order
my $deforder;
my @orders = ('Bug Number', 'Importance', 'Assignee', 'Last Changed');
402

403
if ($cgi->cookie('LASTORDER')) {
404 405 406
    $deforder = "Reuse same sort as last time";
    unshift(@orders, $deforder);
}
407

408
if ($cgi->param('order')) { $deforder = $cgi->param('order') }
409

410 411 412
$vars->{'userdefaultquery'} = $userdefaultquery;
$vars->{'orders'} = \@orders;
$default{'querytype'} = $deforder || 'Importance';
terry%netscape.com's avatar
terry%netscape.com committed
413

414 415
if (($cgi->param('query_format') || $cgi->param('format') || "")
    eq "create-series") {
416 417 418 419
    require Bugzilla::Chart;
    $vars->{'category'} = Bugzilla::Chart::getVisibleSeries();
}

420 421 422
$vars->{'known_name'} = $cgi->param('known_name');


423 424
# Add in the defaults.
$vars->{'default'} = \%default;
425

426 427 428 429 430 431 432 433 434 435 436 437
$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';
    }
}

438 439
# Set cookie to current format as default, but only if the format
# one that we should remember.
440
if (defined($vars->{'format'}) && IsValidQueryType($vars->{'format'})) {
441 442 443 444
    $cgi->send_cookie(-name => 'DEFAULTFORMAT',
                      -value => $vars->{'format'},
                      -expires => "Fri, 01-Jan-2038 00:00:00 GMT");
}
445

446
# Generate and return the UI (HTML page) from the appropriate template.
447 448 449
# If we submit back to ourselves (for e.g. boolean charts), we need to
# preserve format information; hence query_format taking priority over
# format.
450 451 452
my $format = $template->get_format("search/search", 
                                   $vars->{'query_format'} || $vars->{'format'}, 
                                   scalar $cgi->param('ctype'));
453 454 455

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

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