query.cgi 9.15 KB
Newer Older
1
#!/usr/bin/perl -wT
2 3 4
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
terry%netscape.com's avatar
terry%netscape.com committed
5
#
6 7
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
terry%netscape.com's avatar
terry%netscape.com committed
8

9
use strict;
10
use lib qw(. lib);
terry%netscape.com's avatar
terry%netscape.com committed
11

12
use Bugzilla;
13
use Bugzilla::Bug;
14
use Bugzilla::Constants;
15
use Bugzilla::Search;
16
use Bugzilla::User;
17
use Bugzilla::Util;
18
use Bugzilla::Error;
19
use Bugzilla::Product;
20
use Bugzilla::Keyword;
21
use Bugzilla::Field;
22
use Bugzilla::Install::Util qw(vers_cmp);
23
use Bugzilla::Token;
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
###############
# Subroutines #
###############

sub get_product_values {
    my ($products, $field, $vars) = @_;
    my @all_values = map { @{ $_->$field } } @$products;

    my (@unique, %duplicates, %duplicate_count, %seen);
    foreach my $value (@all_values) {
        my $lc_name = lc($value->name);
        if ($seen{$lc_name}) {
            $duplicate_count{$seen{$lc_name}->id}++;
            $duplicates{$value->id} = $seen{$lc_name};
            next;
        }
        push(@unique, $value);
        $seen{$lc_name} = $value;
    }

45
    $field =~ s/s$//;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    if ($field eq 'version') {
        @unique = sort { vers_cmp(lc($a->name), lc($b->name)) } @unique;
    }
    else {
        @unique = sort { lc($a->name) cmp lc($b->name) } @unique;
    }

    $field = 'target_milestone' if $field eq 'milestone';
    $vars->{duplicates}->{$field} = \%duplicates;
    $vars->{duplicate_count}->{$field} = \%duplicate_count;
    # "component" is a reserved word in Template Toolkit.
    $field = 'component_' if $field eq 'component';
    $vars->{$field} = \@unique;
}

###############
# Main Script #
###############

65
my $cgi = Bugzilla->cgi;
66
my $dbh = Bugzilla->dbh;
67 68
my $template = Bugzilla->template;
my $vars = {};
69
my $buffer = $cgi->query_string();
70

71
my $user = Bugzilla->login();
72
my $userid = $user->id;
73

74
if ($cgi->param('nukedefaultquery')) {
75
    if ($userid) {
76 77
        my $token = $cgi->param('token');
        check_hash_token($token, ['nukedefaultquery']);
78 79 80
        $dbh->do("DELETE FROM namedqueries" .
                 " WHERE userid = ? AND name = ?", 
                 undef, ($userid, DEFAULT_QUERY_NAME));
81
    }
82
    $buffer = "";
terry%netscape.com's avatar
terry%netscape.com committed
83 84
}

85 86 87
# We are done with changes committed to the DB.
$dbh = Bugzilla->switch_to_shadow_db;

88
my $userdefaultquery;
89
if ($userid) {
90 91 92 93
    $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
94 95
}

96
local our %default;
97

98 99 100 101
# 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 {
102
    my ($buf) = @_;
103
    my $cgi = Bugzilla->cgi;
104
    $buf = new Bugzilla::CGI($buf);
105
    my $foundone = 0;
106

107 108 109 110 111 112 113 114 115 116
    # If there are old-style boolean charts in the URL (from an old saved
    # search or from an old link on the web somewhere) then convert them
    # to the new "custom search" format so that the form is populated
    # properly.
    my $any_boolean_charts = grep { /^field-?\d+/ } $buf->param();
    if ($any_boolean_charts) {
        my $search = new Bugzilla::Search(params => scalar $buf->Vars);
        $search->boolean_charts_to_custom_search($buf);
    }

117 118 119
    # Query parameters that don't represent form fields on this page.
    my @skip = qw(format query_format list_id columnlist);

120
    # Iterate over the URL parameters
121
    foreach my $name ($buf->param()) {
122 123
        next if grep { $_ eq $name } @skip;
        $foundone = 1;
124
        my @values = $buf->param($name);
125 126 127 128 129
        
        # If the name is a single letter followed by numbers, it's part
        # of Custom Search. We store these as an array of hashes.
        if ($name =~ /^([[:lower:]])(\d+)$/) {
            $default{'custom_search'}->[$2]->{$1} = $values[0];
130
        }
131 132 133
        # 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.
134
        elsif ($name =~ /^(\w+)(\d)$/) {
135
            $default{$1}->[$2] = $values[0];
terry%netscape.com's avatar
terry%netscape.com committed
136
        }
137 138
        else {
            push (@{ $default{$name} }, @values);
139 140
        }
    }
141

142
    return $foundone;
terry%netscape.com's avatar
terry%netscape.com committed
143
}
144

145
if (!PrefillForm($buffer)) {
146 147 148
    # Ah-hah, there was no form stuff specified.  Do it again with the
    # default query.
    if ($userdefaultquery) {
149
        PrefillForm($userdefaultquery);
150
    } else {
151
        PrefillForm(Bugzilla->params->{"defaultquery"});
152 153
    }
}
154

155 156
# 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.
157 158
my @selectable_products = sort {lc($a->name) cmp lc($b->name)} 
                               @{$user->get_selectable_products};
159
Bugzilla::Product::preload(\@selectable_products);
160
$vars->{'product'} = \@selectable_products;
161

162
# Create the component, version and milestone lists.
163 164
foreach my $field (qw(components versions milestones)) {
    get_product_values(\@selectable_products, $field, $vars);
165 166
}

167
# Create data structures representing each classification
168
if (Bugzilla->params->{'useclassification'}) {
169
    $vars->{'classification'} = $user->get_selectable_classifications;
170 171
}

172 173
my @chfields;

174
push @chfields, "[Bug creation]";
175 176 177

# This is what happens when you have variables whose definition depends
# on the DB schema, and then the underlying schema changes...
178
foreach my $val (editable_bug_fields()) {
179 180 181
    if ($val eq 'classification_id') {
        $val = 'classification';
    } elsif ($val eq 'product_id') {
182 183 184 185 186 187 188
        $val = 'product';
    } elsif ($val eq 'component_id') {
        $val = 'component';
    }
    push @chfields, $val;
}

189
if ($user->is_timetracker) {
190 191
    push @chfields, "work_time";
} else {
192
    @chfields = grep($_ ne "deadline", @chfields);
193 194 195 196 197
    @chfields = grep($_ ne "estimated_time", @chfields);
    @chfields = grep($_ ne "remaining_time", @chfields);
}
@chfields = (sort(@chfields));
$vars->{'chfield'} = \@chfields;
198 199 200 201 202 203
$vars->{'bug_status'} = Bugzilla::Field->new({name => 'bug_status'})->legal_values;
$vars->{'rep_platform'} = Bugzilla::Field->new({name => 'rep_platform'})->legal_values;
$vars->{'op_sys'} = Bugzilla::Field->new({name => 'op_sys'})->legal_values;
$vars->{'priority'} = Bugzilla::Field->new({name => 'priority'})->legal_values;
$vars->{'bug_severity'} = Bugzilla::Field->new({name => 'bug_severity'})->legal_values;
$vars->{'resolution'} = Bugzilla::Field->new({name => 'resolution'})->legal_values;
204 205

# Boolean charts
206
my @fields = @{ Bugzilla->fields({ obsolete => 0 }) };
207 208

# If we're not in the time-tracking group, exclude time-tracking fields.
209
if (!$user->is_timetracker) {
210
    foreach my $tt_field (TIMETRACKING_FIELDS) {
211 212 213 214 215 216
        @fields = grep($_->name ne $tt_field, @fields);
    }
}

@fields = sort {lc($a->description) cmp lc($b->description)} @fields;
unshift(@fields, { name => "noop", description => "---" });
217
$vars->{'fields'} = \@fields;
218

219
# Named queries
220
if ($userid) {
221 222
     $vars->{'namedqueries'} = $dbh->selectcol_arrayref(
           "SELECT name FROM namedqueries " .
223
            "WHERE userid = ? AND name != ? " .
224 225
         "ORDER BY name",
         undef, ($userid, DEFAULT_QUERY_NAME));
226
}
terry%netscape.com's avatar
terry%netscape.com committed
227

228 229 230
# Sort order
my $deforder;
my @orders = ('Bug Number', 'Importance', 'Assignee', 'Last Changed');
231

232
if ($cgi->cookie('LASTORDER')) {
233 234 235
    $deforder = "Reuse same sort as last time";
    unshift(@orders, $deforder);
}
236

237
if ($cgi->param('order')) { $deforder = $cgi->param('order') }
238

239 240
$vars->{'userdefaultquery'} = $userdefaultquery;
$vars->{'orders'} = \@orders;
241
$default{'order'} = [$deforder || 'Importance'];
terry%netscape.com's avatar
terry%netscape.com committed
242

243 244
if (($cgi->param('query_format') || $cgi->param('format') || "")
    eq "create-series") {
245 246 247 248
    require Bugzilla::Chart;
    $vars->{'category'} = Bugzilla::Chart::getVisibleSeries();
}

249
$vars->{'known_name'} = $cgi->param('known_name');
250
$vars->{'columnlist'} = $cgi->param('columnlist');
251 252


253 254
# Add in the defaults.
$vars->{'default'} = \%default;
255

256 257 258
$vars->{'format'} = $cgi->param('format');
$vars->{'query_format'} = $cgi->param('query_format');

259
# Set default page to "specific" if none provided
260 261 262 263 264 265 266 267
if (!($cgi->param('query_format') || $cgi->param('format'))) {
    if (defined $cgi->cookie('DEFAULTFORMAT')) {
        $vars->{'format'} = $cgi->cookie('DEFAULTFORMAT');
    } else {
        $vars->{'format'} = 'specific';
    }
}

268 269
# Set cookie to current format as default, but only if the format
# one that we should remember.
270
if (defined($vars->{'format'}) && IsValidQueryType($vars->{'format'})) {
271 272 273 274
    $cgi->send_cookie(-name => 'DEFAULTFORMAT',
                      -value => $vars->{'format'},
                      -expires => "Fri, 01-Jan-2038 00:00:00 GMT");
}
275

276
# Generate and return the UI (HTML page) from the appropriate template.
277 278 279
# If we submit back to ourselves (for e.g. boolean charts), we need to
# preserve format information; hence query_format taking priority over
# format.
280 281 282
my $format = $template->get_format("search/search", 
                                   $vars->{'query_format'} || $vars->{'format'}, 
                                   scalar $cgi->param('ctype'));
283 284 285

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

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