DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (d5c3254b3e56)

VCS Links

Line Code
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 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
#!/usr/bin/perl -w
# -*- mode: cperl; c-basic-offset: 8; indent-tabs-mode: nil; -*-

# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
#
# 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.
#
# The Original Code is Litmus.
#
# The Initial Developer of the Original Code is
# the Mozilla Corporation.
# Portions created by the Initial Developer are Copyright (C) 2006
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#   Chris Cooper <ccooper@deadsquid.com>
#   Zach Lipton <zach@zachlipton.com>
#
# ***** END LICENSE BLOCK *****

use strict;
$|++;

my $t0;
if ($Litmus::Config::DEBUG) {
  use Time::HiRes qw( gettimeofday tv_interval );
  $t0 = [gettimeofday];
}

use JSON -convert_blessed_universally;
use Litmus;
use Litmus::Auth;
use Litmus::Cache;
use Litmus::Error;
use Litmus::DB::Testresult;
use Litmus::FormWidget;

use CGI;

Litmus->init();
my $c = Litmus->cgi();
print $c->header();

# Hash refs for maintaining state in the search form.
my $defaults = undef;
my $order_bys = undef;

my $MAX_SORT_FIELDS = 10;
my $MAX_SEARCH_FIELDS = 10;

my $criteria = "Custom<br/>";
my ($results, $pager);
my @where;
my @order_by;
my $limit;
my $where_criteria = "";
my $order_by_criteria = "";
my $limit_criteria = "";
my $page = 1;

my $cookie =  Litmus::Auth::getCookie();
my $is_admin = Litmus::Auth::istrusted($cookie);

sub validMatchCriteria() {
  my $match_criteria = shift;
  my @valid_matches = ("contains_all",
                       "contains_any",
                       "contains",
                       "contains_case",
                       "not_contain",
                       "not_contain_any",
                       "regexp",
                       "not_regexp"
                      );
  foreach my $index (0..$#valid_matches) {
    next if ($match_criteria ne $valid_matches[$index]);
    return $index+1;
  }
  return 0;
}

if ($c->param) {

    foreach my $param ($c->param) {
        my $value = $c->param($param);
        next if ($value eq '');
        if ($param eq 'page') {
            $page = $value;
            if ($page !~ /^\d+$/) {
                $page = 1;
            }
            next;
        }

        if ($param =~ /sort_field(\d+)/) {
            # We slot sort fields into the @order_by array based on their
            # field_num. Empty array slots will be ignored when the SQL 
            # is built. We set an upper limit on the number of sort fields 
            # we can handle to prevent abuse.
            my $field_num = $1;
            next if ($field_num > $MAX_SORT_FIELDS);
            next if (!$value or $value eq "");
            my @valid_fields = qw/ branch created locale platform product email summary result_status testcase_id testgroup vetted_date /;
            next if (!exists {map { $_=> 1 } @valid_fields}->{$value} == 1);
            my $sort_field = quotemeta($value);
            my $sort_order = 'ASC';
            if ($c->param("sort_order$field_num")) {
                $sort_order = quotemeta($c->param("sort_order$field_num"));
            }
            $order_by[$field_num] = { field => $sort_field,
                                      direction => $sort_order};

        } elsif ($param =~ /search_field(\d+)/) {
            # We set an upper limit on the number of search fields 
            # we can handle to prevent abuse.
            my $field_num = $1;
            next if ($field_num > $MAX_SEARCH_FIELDS);
            next if (!$value or $value eq "");
            my @valid_fields = qw/ build_id comment locale opsys platform product result_status subgroup emailsummarytestgroup user_agent /;
            next if (!exists {map { $_=> 1 } @valid_fields}->{$value} == 1);
            my $search_field = quotemeta($value);
            my $match_criteria = $c->param("match_criteria$field_num");
            my $value = quotemeta($c->param("search_value$field_num"));
            next if (!$value or $value eq "");
            next if (&validMatchCriteria($match_criteria) == 0);
            my $display_value = $value;
            # Upgrade to utf8 prior to search. 
            utf8::upgrade($value);
            push @where, { 'field' => 'search_field',
                           'search_field' => $search_field,
                           'match_criteria' => $match_criteria,
                           'value' => $value};
            utf8::decode($display_value);
            $where_criteria .= ucfirst $search_field . " $match_criteria '$display_value'<br/>";

        } elsif ($param eq 'start_date') {
            my $start_date = $value;
            $start_date =~ s/[^0-9A-Za-z ]/ /g;
            my $end_date;
            # Use 'now' as the default end date.
            if ($c->param('end_date') and $c->param('end_date') ne '') {
                $end_date = $c->param('end_date');
                $end_date =~ s/[^0-9A-Za-z ]/ /g;
            } else {
                $end_date = 'Now';
            }
            push @where, { field => 'start_date',
                           value => $start_date};
            push @where, { field => 'end_date',
                           value => $end_date};
            $where_criteria .= "Date between '$start_date' and '$end_date'<br/>";
        } elsif ($param eq 'vetted_end_date') {
          # Ignore if we also have a start date.
          next if ($c->param('vetted_start_date'));
          my $vetted_end_date = $value;
          $vetted_end_date =~ s/[^0-9A-Za-z ]/ /g; 
          push @where, { field => 'vetted_end_date',
                         value => $vetted_end_date};
          $where_criteria .= "Vetted before '$vetted_end_date'<br/>"
        } elsif ($param eq 'vetted_start_date') {
          my $vetted_start_date = $value;
          $vetted_start_date =~ s/[^0-9A-Za-z ]/ /g; 
          my $vetted_end_date;
          # Use 'now' as the default end date.
          if ($c->param('vetted_end_date') and $c->param('vetted_end_date') ne '') {
            $vetted_end_date = $c->param('vetted_end_date');
            $vetted_end_date =~ s/[^0-9A-Za-z ]/ /g;
          } else {
            $vetted_end_date = 'Now';
          }
          push @where, { field => 'vetted_start_date',
                         value => $vetted_start_date};
          push @where, { field => 'vetted_end_date',
                         value => $vetted_end_date};
          $where_criteria .= "Vetted after '$vetted_start_date'<br/>";
          $where_criteria .= "Vetted before '$vetted_end_date'<br/>"
        } elsif ($param eq 'trusted_only') {
            if ($value ne 'all') {
              if ($value eq '1' or $value eq 'on') {
                push @where, {field => 'trusted_only',
                            value => 1};            
                $limit_criteria .= "Display trusted results only<br/>";
              } else {
                push @where, {field => 'trusted_only',
                            value => '0E0'};            
                $limit_criteria .= "Display untrusted results only<br/>";
              }
            }
        } elsif ($param eq 'valid_only') {
            if ($value ne 'all') {
              if ($value eq '1') {
                push @where, {field => 'valid_only',
                              value => 1};
               $limit_criteria .= "Display valid results only<br/>";
              } else {
                push @where, {field => 'valid_only',
                              value => '0E0'};
               $limit_criteria .= "Display invalid results only<br/>";
              }
            }
        } elsif ($param eq 'vetted_only') {
            if ($value ne 'all') {
              if ($value eq '1') {
                push @where, {field => 'vetted_only',
                              value => 1};            
                $limit_criteria .= "Display vetted results only<br/>";
              } else {
                push @where, {field => 'vetted_only',
                              value => '0E0'};            
                $limit_criteria .= "Display unvetted results only<br/>";
              }
            }
        } elsif ($param eq 'automated') {
            if ($value ne 'all') {
              if ($value eq '1') {
                push @where, {field => 'automated',
                              value => 1};            
                $limit_criteria .= "Display automated results only<br/>";
              } else {
                push @where, {field => 'automated',
                              value => '0E0'};            
                $limit_criteria .= "Display manual results only<br/>";
              }
            }
        } elsif ($param eq 'withbugs') {
            if ($value ne 'all') {
              if ($value eq '1') {
                push @where, {field => 'withbugs',
                              value => 1};            
                $limit_criteria .= "Results with associated bugs<br/>";
              } else {
                push @where, {field => 'withbugs',
                              value => '0E0'};            
                $limit_criteria .= "Results without associated bugs<br/>";
              }
            }
        } elsif ($param eq 'my_results_only') {
            push @where, {field => 'user_id',
                          value => $cookie->{'user_id'}};
            $limit_criteria .= "Display <em>my</em> results only<br/>";
        } elsif ($param eq "limit") {
            if ($value =~ /^\d+$/) {
                $limit = $value;
                next if ($limit == $Litmus::DB::Testresult::_num_results_default);
                $limit_criteria .= "Limit to $limit results per page<br/>";
            }
        } elsif ($param eq 'locale') {
            my $locale = quotemeta($value);
            push @where, {field => 'locale',
                          value => $locale};
            $where_criteria .= "Locale is \'".$locale."\'<br/>";
            $defaults->{locale} =  $locale;
        } elsif ($param eq 'email') {
            if ($value =~ /^(\w\-\_\.)+\@((\w\-\_)+\.)+[a-zA-Z]{2,}$/) {
                push @where, {field => 'email',
                              value => $value};
                $where_criteria .= "Submitted By is like \'".$value."\'<br/>";
                $defaults->{email} =  $value;
            }
        } elsif ($param eq 'product') {
            if ($value =~ /^\d+$/) {
              push @where, {field => $param,
                            value => $value};
              my $product = Litmus::DB::Product->retrieve($value);
              $where_criteria .= "Product is \'".$product->{'name'}."\'<br/>";
            } else {
              $value = quotemeta($value);
              push @where, {field => 'product_name',
                            value => $value};
              $where_criteria .= "Product is \'".$value."\'<br/>";
            }
            $defaults->{product} = $value;
        } elsif ($param eq 'branch') {
            $value = quotemeta($value);
            if ($value =~ /^\d+$/) {
              push @where, {field => $param,
                            value => $value};
              my $branch = Litmus::DB::Branch->retrieve($value);
              $where_criteria .= "Branch is \'".$branch->{'name'}."\'<br/>";
            } else {
              push @where, {field => 'branch_name',
                            value => $value};
              $where_criteria .= "Branch is \'".$value."\'<br/>";
            }
            $defaults->{branch} =  $value;
        } elsif ($param eq 'test_run') {
            if ($value =~ /^\d+$/) {
                push @where, {field => $param,
                              value => $value};
                $where_criteria .= "Test Run ID# is \'".$value."\'<br/>";
                $defaults->{testgroup} = $value;
            }
        } elsif ($param eq 'testgroup' or $param eq 'test_group') {
            $value = quotemeta($value);
            if ($value =~ /^\d+$/) {
               push @where, {field => $param,
                             value => $value};
                my $testgroup = Litmus::DB::Testgroup->retrieve($value);
                $where_criteria .= "Testgroup is \'".$testgroup->{'name'}."\'<br/>";
            } else {
                push @where, {field => 'testgroup_name',
                              value => $value};
                $where_criteria .= "Testgroup is \'".$value."\'<br/>";
            }
            $defaults->{testgroup} = $value;
        } elsif ($param eq 'subgroup') {
            $value = quotemeta($value);
            if ($value =~ /^\d+$/) {
              push @where, {field => $param,
                            value => $value};
              my $subgroup = Litmus::DB::Subgroup->retrieve($value);
              $where_criteria .= "Subgroup is \'".$subgroup->{'name'}."\'<br/>";
            } else { 
               $value = quotemeta($value);
               push @where, {field => 'subgroup_name',
                             value => $value};
              $where_criteria .= "Subgroup is \'".$value."\'<br/>";
            }
            $defaults->{subgroup} = $value;
        } elsif ($param eq 'testcase' or $param eq 'test_id') {
            if ($value =~ /^\d+$/) {
                push @where, {field => $param,
                              value => $value};
                $where_criteria .= "Testcase ID# is \'".${value}."\'<br/>";
                $defaults->{testcase_id} = $value;
            }
        } elsif ($param eq 'platform') {
            $value = quotemeta($value);
            if ($value =~ /^\d+$/) {
              push @where, {field => $param,
                            value => $value};
              my $platform = Litmus::DB::Platform->retrieve($value);
              $where_criteria .= "Platform is \'".$platform->{'name'}."\'<br/>";
            } else {
              push @where, {field => 'platform_name',
                            value => $value};
              $where_criteria .= "Platform is \'".$value."\'<br/>";
            }
            $defaults->{platform} = $value;
        } elsif ($param eq 'opsys') {
            $value = quotemeta($value);
            if ($value =~ /^\d+$/) {
              push @where, {field => $param,
                            value => $value};
              my $opsys = Litmus::DB::Opsys->retrieve($value);
              $where_criteria .= "Operating System is \'".$opsys->{'name'}."\'<br/>";
            } else {
              push @where, {field => 'opsys_name',
                            value => $value};
              $where_criteria .= "Operating System is \'".$value."\'<br/>";
            }
            $defaults->{platform} = $value;
        } elsif ($param eq 'summary') {
            $value = quotemeta($value);
            push @where, {field => $param,
                          value => $value};
            $where_criteria .= "Summary like \'".$value."\'<br/>";
            $defaults->{summary} = $value;
        } elsif ($param eq 'result_status') {
            $value = quotemeta($value);
            push @where, {field => $param,
                          value => $value};
            $where_criteria .= "Status is \'".$value."\'<br/>";
            $defaults->{result_status} = $value;
        } elsif ($param eq 'timespan') {
            $value = quotemeta($value);
            if ($value ne 'all') {
                $value =~ s/[^\-0-9]//g;
                push @where, {field => $param,
                              value => $value};
                $value =~ s/\-//g;
                if ($value == 1) {
                    $where_criteria .= "Submitted in the last day<br/>";
                  } else {
                    $where_criteria .= "Submitted in the last $value days<br/>";
                }
              } else {
                $where_criteria .= "All Results<br/>";
            }
        } elsif ($param eq "has_comments") {
          my $value = quotemeta($value);
          push @where, {field => $param,
                        value => $value};
          $where_criteria .= "Has comments<br/>";
        } else {
            # Skip unknown field
        }
    }
    if ($where_criteria eq '' and 
        scalar(@order_by) == 0 and
        $limit_criteria eq '') {
        ($criteria,$results) = 
          Litmus::DB::Testresult->getDefaultTestResults;
    } else {
        foreach my $order_by_field (@order_by) {
            next if (!$order_by_field);
            $order_by_criteria .= "Order by $order_by_field->{field} $order_by_field->{direction}<br/>";
        }

        $criteria .= $where_criteria . $order_by_criteria . $limit_criteria;
        $criteria =~ s/_/ /g;
        ($results,$pager) = Litmus::DB::Testresult->getTestResults(\@where,
                                                                   \@order_by,
                                                                   $limit,
                                                                   $page);
    }
}


# Only show enabled results/
my $enabled=1;
my $community_enabled=1;
if ($is_admin) {
  $enabled=0;
  $community_enabled=0;
}

# Populate each of our form widgets for select/input.
# Set a default value as appropriate.
my $products = Litmus::FormWidget->getProducts($enabled);
my $branches = Litmus::FormWidget->getBranches($enabled);
my $test_runs = Litmus::FormWidget->getTestRuns($enabled);
my $testgroups = Litmus::FormWidget->getTestgroups($enabled);
my $platforms = Litmus::FormWidget->getPlatforms;
my $opsyses = Litmus::FormWidget->getOpsyses;
my $locales = Litmus::FormWidget->getLocales;
my $result_statuses = Litmus::FormWidget->getResultStatuses;

my $users;
if ($is_admin) {
  $users = Litmus::FormWidget->getUsers;
}

my $json = new JSON;
my $products_js = $json->allow_unknown->convert_blessed->encode($products);
my $branches_js = $json->allow_unknown->convert_blessed->encode($branches);
my $testgroups_js = $json->allow_unknown->convert_blessed->encode($testgroups);
my $opsyses_js = $json->allow_unknown->convert_blessed->encode($opsyses);

my $fields = Litmus::FormWidget->getFields;
my $match_criteria = Litmus::FormWidget->getMatchCriteria;
my $sort_fields = Litmus::FormWidget->getSortFields;

my $title = 'Advanced Search';

my $vars = {
    title => $title,
    criteria => $criteria,
    products_js => $products_js,
    branches_js => $branches_js,
    test_runs => $test_runs,
    testgroups_js => $testgroups_js,
    platforms => $platforms,
    opsyses_js => $opsyses_js,
    locales => $locales,
    result_statuses => $result_statuses,
    users => $users,
    fields => $fields,
    match_criteria => $match_criteria,
    sort_fields => $sort_fields,
};

# Only include results if we have them.
if ($results and scalar @$results > 0) {
  $vars->{results} = $results;  
}  elsif (!$c->param) {    
  $vars->{no_search} = 1;
}

if ($pager) {
    $vars->{pager} = $pager;
}

$vars->{"defaultemail"} = $cookie;
$vars->{"show_admin"} = $is_admin;

Litmus->template()->process("reporting/advanced_search.tmpl", $vars) || 
    internalError(Litmus->template()->error());

if ($Litmus::Config::DEBUG) {
  my $elapsed = tv_interval ( $t0 );
  printf  "<div id='pageload'>Page took %f seconds to load.</div>", $elapsed;
}

exit 0;