query.cgi 9.21 KB
Newer Older
1
#!/usr/bin/perl -T
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 5.10.1;
10
use strict;
11 12
use warnings;

13
use lib qw(. lib);
terry%netscape.com's avatar
terry%netscape.com committed
14

15
use Bugzilla;
16
use Bugzilla::Bug;
17
use Bugzilla::Constants;
18
use Bugzilla::Search;
19
use Bugzilla::Search::Saved;
20
use Bugzilla::User;
21
use Bugzilla::Util;
22
use Bugzilla::Error;
23
use Bugzilla::Product;
24
use Bugzilla::Version;
25
use Bugzilla::Keyword;
26
use Bugzilla::Field;
27
use Bugzilla::Token;
28

29 30 31 32 33
###############
# Subroutines #
###############

sub get_product_values {
34 35 36 37 38 39 40 41 42 43
  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;
44
    }
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    push(@unique, $value);
    $seen{$lc_name} = $value;
  }

  $field =~ s/s$//;
  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;
64 65 66 67 68 69
}

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

70 71
my $cgi      = Bugzilla->cgi;
my $dbh      = Bugzilla->dbh;
72
my $template = Bugzilla->template;
73 74
my $vars     = {};
my $buffer   = $cgi->query_string();
75

76
my $user   = Bugzilla->login();
77
my $userid = $user->id;
78

79
if ($cgi->param('nukedefaultquery')) {
80 81 82 83 84 85 86
  if ($userid) {
    my $token = $cgi->param('token');
    check_hash_token($token, ['nukedefaultquery']);
    my $named_queries = Bugzilla::Search::Saved->match(
      {userid => $userid, name => DEFAULT_QUERY_NAME});
    if (@$named_queries) {
      $named_queries->[0]->remove_from_db();
87
    }
88 89
  }
  $buffer = "";
terry%netscape.com's avatar
terry%netscape.com committed
90 91
}

92 93 94
# We are done with changes committed to the DB.
$dbh = Bugzilla->switch_to_shadow_db;

95
my $userdefaultquery;
96
if ($userid) {
97 98 99 100
  $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
101 102
}

103
local our %default;
104

105 106 107 108
# 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 {
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  my ($buf) = @_;
  my $cgi = Bugzilla->cgi;
  $buf = new Bugzilla::CGI($buf);
  my $foundone = 0;

  # 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);
  }

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

  # Iterate over the URL parameters
  foreach my $name ($buf->param()) {
    next if grep { $_ eq $name } @skip;
    $foundone = 1;
    my @values = $buf->param($name);

    # 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];
137 138
    }

139 140 141 142 143 144 145 146
    # 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.
    elsif ($name =~ /^(\w+)(\d)$/) {
      $default{$1}->[$2] = $values[0];
    }
    else {
      push(@{$default{$name}}, @values);
147
    }
148
  }
149

150
  return $foundone;
terry%netscape.com's avatar
terry%netscape.com committed
151
}
152

153
if (!PrefillForm($buffer)) {
154 155 156 157 158 159 160 161 162

  # Ah-hah, there was no form stuff specified.  Do it again with the
  # default query.
  if ($userdefaultquery) {
    PrefillForm($userdefaultquery);
  }
  else {
    PrefillForm(Bugzilla->params->{"defaultquery"});
  }
163
}
164

165
# if using groups for entry, then we don't want people to see products they
166
# don't have access to. Remove them from the list.
167 168
my @selectable_products
  = sort { lc($a->name) cmp lc($b->name) } @{$user->get_selectable_products};
169
Bugzilla::Product::preload(\@selectable_products);
170
$vars->{'product'} = \@selectable_products;
171

172
# Create the component, version and milestone lists.
173
foreach my $field (qw(components versions milestones)) {
174
  get_product_values(\@selectable_products, $field, $vars);
175 176
}

177
# Create data structures representing each classification
178
if (Bugzilla->params->{'useclassification'}) {
179
  $vars->{'classification'} = $user->get_selectable_classifications;
180 181
}

182 183
my @chfields;

184
push @chfields, "[Bug creation]";
185 186 187

# This is what happens when you have variables whose definition depends
# on the DB schema, and then the underlying schema changes...
188
foreach my $val (editable_bug_fields()) {
189 190 191 192 193 194 195 196 197 198
  if ($val eq 'classification_id') {
    $val = 'classification';
  }
  elsif ($val eq 'product_id') {
    $val = 'product';
  }
  elsif ($val eq 'component_id') {
    $val = 'component';
  }
  push @chfields, $val;
199 200
}

201
if ($user->is_timetracker) {
202 203 204 205 206 207
  push @chfields, "work_time";
}
else {
  foreach my $tt_field (TIMETRACKING_FIELDS) {
    @chfields = grep($_ ne $tt_field, @chfields);
  }
208 209 210
}
@chfields = (sort(@chfields));
$vars->{'chfield'} = \@chfields;
211 212 213 214 215
$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;
216
$vars->{'priority'} = Bugzilla::Field->new({name => 'priority'})->legal_values;
217 218 219 220
$vars->{'bug_severity'}
  = Bugzilla::Field->new({name => 'bug_severity'})->legal_values;
$vars->{'resolution'}
  = Bugzilla::Field->new({name => 'resolution'})->legal_values;
221 222

# Boolean charts
223
my @fields = @{Bugzilla->fields({obsolete => 0})};
224

225 226
my %exclude_fields = ();

227
# If we're not in the time-tracking group, exclude time-tracking fields.
228
if (!$user->is_timetracker) {
229 230 231
  foreach my $tt_field (TIMETRACKING_FIELDS) {
    $exclude_fields{$tt_field} = 1;
  }
232 233
}

234
# Exclude fields turned off by params
235 236 237 238 239 240
my %param_controlled_fields = (
  'useqacontact'        => 'qa_contact',
  'usetargetmilestone'  => 'target_milestone',
  'useclassification'   => 'classification',
  'usestatuswhiteboard' => 'status_whiteboard'
);
241 242

while (my ($param, $field) = each %param_controlled_fields) {
243
  $exclude_fields{$field} = 1 unless Bugzilla->params->{$param};
244 245 246 247
}

@fields = grep(!$exclude_fields{$_->name}, @fields);

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

252
# Named queries
253
if ($userid) {
254 255 256 257 258 259 260
  $vars->{'namedqueries'} = $dbh->selectcol_arrayref(
    "SELECT name FROM namedqueries "
      . "WHERE userid = ? AND name != ? "
      . "ORDER BY name",
    undef,
    ($userid, DEFAULT_QUERY_NAME)
  );
261
}
terry%netscape.com's avatar
terry%netscape.com committed
262

263 264 265
# Sort order
my $deforder;
my @orders = ('Bug Number', 'Importance', 'Assignee', 'Last Changed');
266

267
if ($cgi->cookie('LASTORDER')) {
268 269
  $deforder = "Reuse same sort as last time";
  unshift(@orders, $deforder);
270
}
271

272
if ($cgi->param('order')) { $deforder = $cgi->param('order') }
273

274
$vars->{'userdefaultquery'} = $userdefaultquery;
275
$vars->{'orders'}           = \@orders;
276
$default{'order'} = [$deforder || 'Importance'];
terry%netscape.com's avatar
terry%netscape.com committed
277

278 279 280 281 282
if (
  ($cgi->param('query_format') || $cgi->param('format') || "") eq "create-series")
{
  require Bugzilla::Chart;
  $vars->{'category'} = Bugzilla::Chart::getVisibleSeries();
283 284
}

285
$vars->{'known_name'} = $cgi->param('known_name');
286
$vars->{'columnlist'} = $cgi->param('columnlist');
287 288


289 290
# Add in the defaults.
$vars->{'default'} = \%default;
291

292
$vars->{'format'}       = $cgi->param('format');
293 294
$vars->{'query_format'} = $cgi->param('query_format');

295
# Set default page to "specific" if none provided
296
if (!($cgi->param('query_format') || $cgi->param('format'))) {
297 298 299 300 301 302
  if (defined $cgi->cookie('DEFAULTFORMAT')) {
    $vars->{'format'} = $cgi->cookie('DEFAULTFORMAT');
  }
  else {
    $vars->{'format'} = 'specific';
  }
303 304
}

305 306
# Set cookie to current format as default, but only if the format
# one that we should remember.
307
if (defined($vars->{'format'}) && IsValidQueryType($vars->{'format'})) {
308 309 310 311 312
  $cgi->send_cookie(
    -name    => 'DEFAULTFORMAT',
    -value   => $vars->{'format'},
    -expires => "Fri, 01-Jan-2038 00:00:00 GMT"
  );
313
}
314

315
# Generate and return the UI (HTML page) from the appropriate template.
316 317 318
# If we submit back to ourselves (for e.g. boolean charts), we need to
# preserve format information; hence query_format taking priority over
# format.
319 320 321 322 323
my $format = $template->get_format(
  "search/search",
  $vars->{'query_format'} || $vars->{'format'},
  scalar $cgi->param('ctype')
);
324 325 326

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

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