query.cgi 13.3 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::Bug;
34
use Bugzilla::Constants;
35
use Bugzilla::Search;
36
use Bugzilla::User;
37
use Bugzilla::Util;
38
use Bugzilla::Product;
39
use Bugzilla::Keyword;
40

41 42 43 44 45 46 47 48
use vars qw(
    @legal_resolution
    @legal_bug_status
    @legal_opsys
    @legal_platform
    @legal_priority
    @legal_severity
);
49

50
my $cgi = Bugzilla->cgi;
51
my $dbh = Bugzilla->dbh;
52 53
my $template = Bugzilla->template;
my $vars = {};
54
my $buffer = $cgi->query_string();
55

56
my $user = Bugzilla->login();
57
my $userid = $user->id;
58

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

97
if ($cgi->param('nukedefaultquery')) {
98
    if ($userid) {
99 100 101
        $dbh->do("DELETE FROM namedqueries" .
                 " WHERE userid = ? AND name = ?", 
                 undef, ($userid, DEFAULT_QUERY_NAME));
102
    }
103
    $buffer = "";
terry%netscape.com's avatar
terry%netscape.com committed
104 105
}

106
my $userdefaultquery;
107
if ($userid) {
108 109 110 111
    $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
112 113
}

114
my %default;
115

116 117 118 119
# 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 {
120 121
    my ($buf) = (@_);
    my $foundone = 0;
122 123

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

190

191
if (!PrefillForm($buffer)) {
192 193 194
    # Ah-hah, there was no form stuff specified.  Do it again with the
    # default query.
    if ($userdefaultquery) {
195
        PrefillForm($userdefaultquery);
196
    } else {
197
        PrefillForm(Param("defaultquery"));
198 199
    }
}
200

201 202
if ($default{'chfieldto'}->[0] eq "") {
    $default{'chfieldto'} = ["Now"];
terry%netscape.com's avatar
terry%netscape.com committed
203 204
}

205 206
# 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.
207 208
my @selectable_products = sort {lc($a->name) cmp lc($b->name)} 
                               @{$user->get_selectable_products};
209

210
# Create the component, version and milestone lists.
211 212 213 214 215 216 217 218
my %components;
my %versions;
my %milestones;

foreach my $product (@selectable_products) {
    $components{$_->name} = 1 foreach (@{$product->components});
    $versions{$_->name}   = 1 foreach (@{$product->versions});
    $milestones{$_->name} = 1 foreach (@{$product->milestones});
219 220
}

221 222 223 224
my @components = sort(keys %components);
my @versions = sort(keys %versions);
my @milestones = sort(keys %milestones);

225
$vars->{'product'} = \@selectable_products;
226

227 228
# Create data structures representing each classification
if (Param('useclassification')) {
229
    $vars->{'classification'} = $user->get_selectable_classifications;
230 231
}

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

235
$vars->{'version'} = \@versions;
236

237 238
if (Param('usetargetmilestone')) {
    $vars->{'target_milestone'} = \@milestones;
239 240
}

241
$vars->{'have_keywords'} = Bugzilla::Keyword::keyword_count();
242

243 244
GetVersionTable();

245 246 247 248 249
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;

250 251
my @chfields;

252
push @chfields, "[Bug creation]";
253 254 255

# This is what happens when you have variables whose definition depends
# on the DB schema, and then the underlying schema changes...
256
foreach my $val (editable_bug_fields()) {
257 258 259
    if ($val eq 'classification_id') {
        $val = 'classification';
    } elsif ($val eq 'product_id') {
260 261 262 263 264 265 266
        $val = 'product';
    } elsif ($val eq 'component_id') {
        $val = 'component';
    }
    push @chfields, $val;
}

267 268 269 270 271 272 273 274
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;
275 276 277 278 279 280 281
$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
282
my @fields;
283
push(@fields, { name => "noop", description => "---" });
284
push(@fields, $dbh->bz_get_field_defs());
285
@fields = sort {lc($a->{'description'}) cmp lc($b->{'description'})} @fields;
286
$vars->{'fields'} = \@fields;
287

288 289 290
# 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.
291
foreach my $cmd (grep(/^cmd-/, $cgi->param)) {
292
    if ($cmd =~ /^cmd-add(\d+)-(\d+)-(\d+)$/) {
293
        $cgi->param(-name => "field$1-$2-$3", -value => "xyzzy");
294 295
    }
}
296

297 298
if (!$cgi->param('field0-0-0')) {
    $cgi->param(-name => 'field0-0-0', -value => "xyzzy");
299 300
}

301 302 303 304
# 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;
305
for (my $chart = 0; $cgi->param("field$chart-0-0"); $chart++) {
306
    my @rows;
307
    for (my $row = 0; $cgi->param("field$chart-$row-0"); $row++) {
308
        my @cols;
309
        for (my $col = 0; $cgi->param("field$chart-$row-$col"); $col++) {
310 311 312 313
            my $value = $cgi->param("value$chart-$row-$col");
            if (!defined($value)) {
                $value = '';
            }
314
            push(@cols, { field => $cgi->param("field$chart-$row-$col"),
315
                          type => $cgi->param("type$chart-$row-$col") || 'noop',
316
                          value => $value });
317
        }
318
        push(@rows, \@cols);
319
    }
320
    push(@charts, {'rows' => \@rows, 'negate' => scalar($cgi->param("negate$chart")) });
321 322
}

323
$default{'charts'} = \@charts;
324

325
# Named queries
326
if ($userid) {
327 328
     $vars->{'namedqueries'} = $dbh->selectcol_arrayref(
           "SELECT name FROM namedqueries " .
329
            "WHERE userid = ? AND name != ? " .
330 331
         "ORDER BY name",
         undef, ($userid, DEFAULT_QUERY_NAME));
332
}
terry%netscape.com's avatar
terry%netscape.com committed
333

334 335 336
# Sort order
my $deforder;
my @orders = ('Bug Number', 'Importance', 'Assignee', 'Last Changed');
337

338
if ($cgi->cookie('LASTORDER')) {
339 340 341
    $deforder = "Reuse same sort as last time";
    unshift(@orders, $deforder);
}
342

343
if ($cgi->param('order')) { $deforder = $cgi->param('order') }
344

345 346 347
$vars->{'userdefaultquery'} = $userdefaultquery;
$vars->{'orders'} = \@orders;
$default{'querytype'} = $deforder || 'Importance';
terry%netscape.com's avatar
terry%netscape.com committed
348

349 350
if (($cgi->param('query_format') || $cgi->param('format') || "")
    eq "create-series") {
351 352 353 354
    require Bugzilla::Chart;
    $vars->{'category'} = Bugzilla::Chart::getVisibleSeries();
}

355 356 357
$vars->{'known_name'} = $cgi->param('known_name');


358 359
# Add in the defaults.
$vars->{'default'} = \%default;
360

361 362 363
$vars->{'format'} = $cgi->param('format');
$vars->{'query_format'} = $cgi->param('query_format');

364
# Set default page to "specific" if none provided
365 366 367 368 369 370 371 372
if (!($cgi->param('query_format') || $cgi->param('format'))) {
    if (defined $cgi->cookie('DEFAULTFORMAT')) {
        $vars->{'format'} = $cgi->cookie('DEFAULTFORMAT');
    } else {
        $vars->{'format'} = 'specific';
    }
}

373 374
# Set cookie to current format as default, but only if the format
# one that we should remember.
375
if (defined($vars->{'format'}) && IsValidQueryType($vars->{'format'})) {
376 377 378 379
    $cgi->send_cookie(-name => 'DEFAULTFORMAT',
                      -value => $vars->{'format'},
                      -expires => "Fri, 01-Jan-2038 00:00:00 GMT");
}
380

381
# Generate and return the UI (HTML page) from the appropriate template.
382 383 384
# If we submit back to ourselves (for e.g. boolean charts), we need to
# preserve format information; hence query_format taking priority over
# format.
385 386 387
my $format = $template->get_format("search/search", 
                                   $vars->{'query_format'} || $vars->{'format'}, 
                                   scalar $cgi->param('ctype'));
388 389 390

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

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