summaryrefslogtreecommitdiff
blob: eddf0f106c6aa878c63c74463d8a7a5dc2f835a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/perl -T
# 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/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

use 5.10.1;
use strict;
use warnings;

use lib qw(. lib);

## REDHAT EXTENSION START 731285
use List::MoreUtils qw(insert_after);
## REDHAT EXTENSION END 731285

use Bugzilla;
use Bugzilla::Bug;
use Bugzilla::Constants;
use Bugzilla::Search;
use Bugzilla::Search::Saved;
use Bugzilla::User;
use Bugzilla::Util;
use Bugzilla::Error;
use Bugzilla::Product;
use Bugzilla::Version;
use Bugzilla::Keyword;
use Bugzilla::Field;
use Bugzilla::Token;

###############
# Subroutines #
###############

local our %default;

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

  ## REDHAT EXTENSION BEGIN 1284339
  # Only show values for selected products
  my $cgi       = Bugzilla->cgi;
  my @cgi_prods = $cgi->param('product');
  if (!@cgi_prods) {
    @cgi_prods = @{$default{'product'}} if (defined($default{'product'}));
  }
  foreach my $product (@$products) {
    next unless grep { $_ eq $product->{name} } @cgi_prods;
    @all_values = (@all_values, @{$product->$field});
  }
  ## REDHAT EXTENSION END 1284339

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

  $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';
  $field = 'target_release'   if $field eq 'release';
  $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 #
###############

my $cgi      = Bugzilla->cgi;
my $dbh      = Bugzilla->dbh;
my $template = Bugzilla->template;
my $vars     = {};
my $buffer   = $cgi->query_string();

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

if ($cgi->param('nukedefaultquery')) {
  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();
    }
  }
  $buffer = "";
}

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

my $userdefaultquery;
if ($userid) {
  $userdefaultquery
    = $dbh->selectrow_array(
    "SELECT query FROM namedqueries " . "WHERE userid = ? AND name = ?",
    undef, ($userid, DEFAULT_QUERY_NAME));
}

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

    ## REDHAT EXTENSION BEGIN 1205491
    # Rewrite isnull -> isempty and isnotnull -> isnotempty.
    $values[0] = 'isempty'    if $values[0] eq 'isnull';
    $values[0] = 'isnotempty' if $values[0] eq 'isnotnull';
    ## REDHAT EXTENSION END 1205491

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

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

  return $foundone;
}

if (!PrefillForm($buffer)) {

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

# 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.
my @selectable_products
  = sort { lc($a->name) cmp lc($b->name) } @{$user->get_selectable_products};
## REDHAT EXTENSION START 823339
# Only show info (components, versions, etc..) on selected products or
# selected components (and only if they are in the selectable_products array
my @product_info = @selectable_products;
if ($default{product} and scalar @{$default{product}}) {
  my %products = map { $_ => 1 } @{$default{product}};
  @product_info = grep { $products{$_->name} } @product_info;
}
if ($cgi->param('classification')) {
  my %classifications = map { $_ => 1 } $cgi->param('classification');
  @product_info
    = grep { $classifications{$_->classification->name} } @product_info;
  @selectable_products
    = grep { $classifications{$_->classification->name} } @selectable_products;
}

## REDHAT EXTENSION START 1157849
# This preloads all the components and impacts the advanced search load time
# Make it stop doing that before you renable this
#Bugzilla::Product::preload(\@product_info);
## REDHAT EXTENSION END 1157849
$vars->{'product'} = \@selectable_products;

## REDHAT EXTENSION START 1157849
# Create the component, version and milestone lists.
foreach my $field (qw(versions milestones releases)) {
  get_product_values(\@product_info, $field, $vars);
}

if ($cgi->param('component')) {
  my @components = map { {name => $_, id => $_} } $cgi->param('component');
  $vars->{'component_'} = \@components;
}

if ($default{component} and scalar @{$default{component}}) {
  my @component_ = $vars->{'component_'} ? @{$vars->{'component_'}} : ();
  push @component_, map { {name => $_, id => $_} } @{$default{component}};
  $vars->{'component_'} = \@component_;
}

## REDHAT EXTENSION END 1157849

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

my @chfields;

push @chfields, "[Bug creation]";

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

if ($user->is_timetracker) {
  push @chfields, "work_time";
}
else {
  foreach my $tt_field (TIMETRACKING_FIELDS) {
    @chfields = grep($_ ne $tt_field, @chfields);
  }
}
@chfields = (sort(@chfields));
$vars->{'chfield'} = \@chfields;
$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;

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

my %exclude_fields = ();

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

# Exclude fields turned off by params
my %param_controlled_fields = (
  'useqacontact'        => 'qa_contact',
  'usetargetmilestone'  => 'target_milestone',
  'useclassification'   => 'classification',
  'usestatuswhiteboard' => 'status_whiteboard'
);

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

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

@fields = sort { lc($a->description) cmp lc($b->description) } @fields;

## REDHAT EXTENSION START 731285 BUGBUG
if ($user->is_insider()) {
  insert_after { $_->name eq 'component' }
  {name => 'component_c', description => 'Capacity Component List'} => @fields;
  insert_after { $_->name eq 'component' }
  {name => 'component_a', description => 'Approved Component List'} => @fields;
}
## REDHAT EXTENSION END 731285

unshift(@fields, {name => "noop", description => "---"});
$vars->{'fields'} = \@fields;

# Named queries
if ($userid) {
  $vars->{'namedqueries'} = $dbh->selectcol_arrayref(
    "SELECT name FROM namedqueries "
      . "WHERE userid = ? AND name != ? "
      . "ORDER BY name",
    undef,
    ($userid, DEFAULT_QUERY_NAME)
  );
}

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

if ($cgi->cookie('LASTORDER')) {
  $deforder = "Reuse same sort as last time";
  unshift(@orders, $deforder);
  $vars->{'lastorder'} = $cgi->cookie('LASTORDER');    ## REDHAT EXTENSION 1443918
}

if ($cgi->param('order')) {
   #$deforder = $cgi->param('order');
   $deforder = "Reuse same sort as last time";
   $vars->{'lastorder'} = $cgi->param('order');
}

$vars->{'userdefaultquery'} = $userdefaultquery;
$vars->{'orders'}           = \@orders;
## REDHAT EXTENSION 18716 - Allow defaultquery to set order also
$default{'order'} = [$deforder || $default{'order'}[0] || 'Importance'];

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

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

## RED HAT EXTENSION START 2124411
my $columns = Bugzilla::Search::COLUMNS;
my $cols;
if (defined $cgi->param('columnlist')) {
  if ($cgi->param('columnlist') eq "all") {

    # If the value of the CGI parameter is "all", display all columns,
    # but remove the redundant "short_desc" column.
    $cols = join(',', grep($_ ne 'short_desc', keys(%$columns)));
  }
  else {
    $cols = $cgi->param('columnlist');
  }
}
elsif (defined $cgi->cookie('COLUMNLIST')) {
  $cols = join(',', split(/ /, $cgi->cookie('COLUMNLIST')));
}
else {
  # Use the default list of columns.
  $cols = join(',', DEFAULT_COLUMN_LIST);
}

$vars->{'columnlist'} = $cols;
$vars->{'row_group1'} = defined $cgi->param('row_group1');
$vars->{'row_group2'} = defined $cgi->param('row_group2');
## RED HAT EXTENSION END 2124411

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

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

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

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

# Generate and return the UI (HTML page) from the appropriate template.
# 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 = $template->get_format(
  "search/search",
  $vars->{'query_format'} || $vars->{'format'},
  scalar $cgi->param('ctype')
);

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

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