buglist.cgi 52.4 KB
Newer Older
1 2
#!/usr/bonsaitools/bin/perl -w
# -*- 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
#                 Dan Mosedale <dmose@mozilla.org>
23
#                 Stephan Niemz  <st.n@gmx.net>
24
#                 Andreas Franke <afranke@mathweb.org>
terry%netscape.com's avatar
terry%netscape.com committed
25

26 27
use diagnostics;
use strict;
terry%netscape.com's avatar
terry%netscape.com committed
28

29
require "CGI.pl";
30
use Date::Parse;
31

32 33 34 35 36
# Shut up misguided -w warnings about "used only once".  "use vars" just
# doesn't work for me.

sub sillyness {
    my $zz;
37
    $zz = $::db_name;
38
    $zz = $::defaultqueryname;
39
    $zz = $::unconfirmedstate;
40 41 42 43 44 45
    $zz = @::components;
    $zz = @::default_column_list;
    $zz = @::legal_keywords;
    $zz = @::legal_platform;
    $zz = @::legal_priority;
    $zz = @::legal_product;
46
    $zz = @::settable_resolution;
47 48
    $zz = @::legal_severity;
    $zz = @::versions;
49
    $zz = @::target_milestone;
50
    $zz = %::proddesc;
51
};
terry%netscape.com's avatar
terry%netscape.com committed
52

53
my $serverpush = 0;
54

55
ConnectToDatabase();
56

57 58
#print "Content-type: text/plain\n\n";    # Handy for debugging.
#$::FORM{'debug'} = 1;
59 60 61


if (grep(/^cmd-/, keys(%::FORM))) {
62
    my $url = "query.cgi?$::buffer#chart";
63 64 65
    print qq{Refresh: 0; URL=$url
Content-type: text/html

66 67 68
Adding field to query page...
<P>
<A HREF="$url">Click here if page doesn't redisplay automatically.</A>
69 70 71 72 73
};
    exit();
}


74

75
if (!defined $::FORM{'cmdtype'}) {
terry%netscape.com's avatar
terry%netscape.com committed
76
    # This can happen if there's an old bookmark to a query...
77
    $::FORM{'cmdtype'} = 'doit';
terry%netscape.com's avatar
terry%netscape.com committed
78 79
}

80 81 82 83 84 85 86 87

sub SqlifyDate {
    my ($str) = (@_);
    if (!defined $str) {
        $str = "";
    }
    my $date = str2time($str);
    if (!defined $date) {
88
        PuntTryAgain("The string '<tt>".html_quote($str)."</tt>' is not a legal date.");
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    }
    return time2str("%Y/%m/%d %H:%M:%S", $date);
}


sub GetByWordList {
    my ($field, $strs) = (@_);
    my @list;

    foreach my $w (split(/[\s,]+/, $strs)) {
        my $word = $w;
        if ($word ne "") {
            $word =~ tr/A-Z/a-z/;
            $word = SqlQuote(quotemeta($word));
            $word =~ s/^'//;
            $word =~ s/'$//;
            $word = '(^|[^a-z0-9])' . $word . '($|[^a-z0-9])';
            push(@list, "lower($field) regexp '$word'");
        }
    }

    return \@list;
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#
# support for "any/all/nowordssubstr" comparison type ("words as substrings")
#
sub GetByWordListSubstr {
    my ($field, $strs) = (@_);
    my @list;

    foreach my $word (split(/[\s,]+/, $strs)) {
        if ($word ne "") {
            push(@list, "INSTR(LOWER($field), " . lc(SqlQuote($word)) . ")");
        }
    }

    return \@list;
}
128 129 130 131 132 133 134


sub Error {
    my ($str) = (@_);
    if (!$serverpush) {
        print "Content-type: text/html\n\n";
    }
135
    PuntTryAgain($str);
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



sub GenerateSQL {
    my $debug = 0;
    my ($fieldsref, $supptablesref, $wherepartref, $urlstr) = (@_);
    my @fields;
    my @supptables;
    my @wherepart;
    @fields = @$fieldsref if $fieldsref;
    @supptables = @$supptablesref if $supptablesref;
    @wherepart = @$wherepartref if $wherepartref;
    my %F;
    my %M;
    ParseUrlString($urlstr, \%F, \%M);
    my @specialchart;
    my @andlist;

    # First, deal with all the old hard-coded non-chart-based poop.

    unshift(@supptables,
            ("profiles map_assigned_to",
             "profiles map_reporter",
             "LEFT JOIN profiles map_qa_contact ON bugs.qa_contact = map_qa_contact.userid"));
    unshift(@wherepart,
            ("bugs.assigned_to = map_assigned_to.userid",
             "bugs.reporter = map_reporter.userid",
             "bugs.groupset & $::usergroupset = bugs.groupset"));
167

168 169 170 171 172 173 174

    my $minvotes;
    if (defined $F{'votes'}) {
        my $c = trim($F{'votes'});
        if ($c ne "") {
            if ($c !~ /^[0-9]*$/) {
                return Error("The 'At least ___ votes' field must be a\n" .
175 176
                             "simple number. You entered \"" .
                             html_quote($c) . "\", which\n" .
177 178 179 180 181 182 183 184 185
                             "doesn't cut it.");
            }
            push(@specialchart, ["votes", "greaterthan", $c - 1]);
        }
    }

    if ($M{'bug_id'}) {
        my $type = "anyexact";
        if ($F{'bugidtype'} && $F{'bugidtype'} eq 'exclude') {
186
            $type = "nowords";
187 188 189 190 191
        }
        push(@specialchart, ["bug_id", $type, join(',', @{$M{'bug_id'}})]);
    }

    if (defined $F{'sql'}) {
192
        die "Invalid sql: $F{'sql'}" if $F{'sql'} =~ /;/;
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
        push(@wherepart, "( $F{'sql'} )");
    }

    my @legal_fields = ("product", "version", "rep_platform", "op_sys",
                        "bug_status", "resolution", "priority", "bug_severity",
                        "assigned_to", "reporter", "component",
                        "target_milestone", "groupset");

    foreach my $field (keys %F) {
        if (lsearch(\@legal_fields, $field) != -1) {
            push(@specialchart, [$field, "anyexact",
                                 join(',', @{$M{$field}})]);
        }
    }

    if ($F{'keywords'}) {
        my $t = $F{'keywords_type'};
        if (!$t || $t eq "or") {
            $t = "anywords";
        }
        push(@specialchart, ["keywords", $t, $F{'keywords'}]);
    }

    foreach my $id ("1", "2") {
        if (!defined ($F{"email$id"})) {
            next;
        }
        my $email = trim($F{"email$id"});
        if ($email eq "") {
            next;
        }
        my $type = $F{"emailtype$id"};
        if ($type eq "exact") {
            $type = "anyexact";
            foreach my $name (split(',', $email)) {
                $name = trim($name);
                if ($name) {
                    DBNameToIdAndCheck($name);
                }
            }
        }

        my @clist;
        foreach my $field ("assigned_to", "reporter", "cc", "qa_contact") {
            if ($F{"email$field$id"}) {
                push(@clist, $field, $type, $email);
            }
        }
        if ($F{"emaillongdesc$id"}) {
            my $table = "longdescs_";
            push(@supptables, "longdescs $table");
            push(@wherepart, "$table.bug_id = bugs.bug_id");
            my $ptable = "longdescnames_";
246 247
            push(@supptables, "profiles $ptable");
            push(@wherepart, "$table.who = $ptable.userid");
248 249 250 251 252 253
            push(@clist, "$ptable.login_name", $type, $email);
        }
        if (@clist) {
            push(@specialchart, \@clist);
        } else {
            return Error("You must specify one or more fields in which to\n" .
254 255
                         "search for <tt>" .
                         html_quote($email) . "</tt>.\n");
256 257 258
        }
    }

259

260 261 262 263 264
    if (defined $F{'changedin'}) {
        my $c = trim($F{'changedin'});
        if ($c ne "") {
            if ($c !~ /^[0-9]*$/) {
                return Error("The 'changed in last ___ days' field must be\n" .
265 266
                             "a simple number. You entered \"" .
                             html_quote($c) . "\", which\n" .
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
                             "doesn't cut it.");
            }
            push(@specialchart, ["changedin",
                                 "lessthan", $c + 1]);
        }
    }

    my $ref = $M{'chfield'};

    if (defined $ref) {
        my $which = lsearch($ref, "[Bug creation]");
        if ($which >= 0) {
            splice(@$ref, $which, 1);
            push(@specialchart, ["creation_ts", "greaterthan",
                                 SqlifyDate($F{'chfieldfrom'})]);
            my $to = $F{'chfieldto'};
            if (defined $to) {
                $to = trim($to);
                if ($to ne "" && $to !~ /^now$/i) {
                    push(@specialchart, ["creation_ts", "lessthan",
                                         SqlifyDate($to)]);
                }
            }
        }
    }



    if (defined $ref && 0 < @$ref) {
        push(@supptables, "bugs_activity actcheck");
297

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
        my @list;
        foreach my $f (@$ref) {
            push(@list, "\nactcheck.fieldid = " . GetFieldID($f));
        }
        push(@wherepart, "actcheck.bug_id = bugs.bug_id");
        push(@wherepart, "(" . join(' OR ', @list) . ")");
        push(@wherepart, "actcheck.bug_when >= " .
             SqlQuote(SqlifyDate($F{'chfieldfrom'})));
        my $to = $F{'chfieldto'};
        if (defined $to) {
            $to = trim($to);
            if ($to ne "" && $to !~ /^now$/i) {
                push(@wherepart, "actcheck.bug_when <= " .
                     SqlQuote(SqlifyDate($to)));
            }
        }
        my $value = $F{'chfieldvalue'};
        if (defined $value) {
            $value = trim($value);
            if ($value ne "") {
318
                push(@wherepart, "actcheck.added = " .
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
                     SqlQuote($value))
            }
        }
    }


    foreach my $f ("short_desc", "long_desc", "bug_file_loc",
                   "status_whiteboard") {
        if (defined $F{$f}) {
            my $s = trim($F{$f});
            if ($s ne "") {
                my $n = $f;
                my $q = SqlQuote($s);
                my $type = $F{$f . "_type"};
                push(@specialchart, [$f, $type, $s]);
            }
        }
    }
337

338 339
    my $chartid;
    my $f;
340
    my $ff;
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    my $t;
    my $q;
    my $v;
    my $term;
    my %funcsbykey;
    my @funcdefs =
        (
         "^(assigned_to|reporter)," => sub {
             push(@supptables, "profiles map_$f");
             push(@wherepart, "bugs.$f = map_$f.userid");
             $f = "map_$f.login_name";
         },
         "^qa_contact," => sub {
             push(@supptables,
                  "LEFT JOIN profiles map_qa_contact ON bugs.qa_contact = map_qa_contact.userid");
             $f = "map_$f.login_name";
         },
358

359
         "^cc," => sub {
360 361 362
            push(@supptables,                                                  
              ("LEFT JOIN cc cc_$chartid ON bugs.bug_id = cc_$chartid.bug_id LEFT JOIN profiles map_cc_$chartid ON cc_$chartid.who = map_cc_$chartid.userid"));
            $f = "map_cc_$chartid.login_name";  
363
         },
364

365 366 367 368 369 370 371 372 373 374 375
         "^long_?desc,changedby" => sub {
             my $table = "longdescs_$chartid";
             push(@supptables, "longdescs $table");
             push(@wherepart, "$table.bug_id = bugs.bug_id");
             my $id = DBNameToIdAndCheck($v);
             $term = "$table.who = $id";
         },
         "^long_?desc,changedbefore" => sub {
             my $table = "longdescs_$chartid";
             push(@supptables, "longdescs $table");
             push(@wherepart, "$table.bug_id = bugs.bug_id");
376
             $term = "$table.bug_when < " . SqlQuote(SqlifyDate($v));
377 378 379 380 381
         },
         "^long_?desc,changedafter" => sub {
             my $table = "longdescs_$chartid";
             push(@supptables, "longdescs $table");
             push(@wherepart, "$table.bug_id = bugs.bug_id");
382
             $term = "$table.bug_when > " . SqlQuote(SqlifyDate($v));
383 384 385 386 387 388 389
         },
         "^long_?desc," => sub {
             my $table = "longdescs_$chartid";
             push(@supptables, "longdescs $table");
             push(@wherepart, "$table.bug_id = bugs.bug_id");
             $f = "$table.thetext";
         },
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
         "^attachments\..*," => sub {
             my $table = "attachments_$chartid";
             push(@supptables, "LEFT JOIN attachments $table ON bugs.bug_id = $table.bug_id");
             $f =~ m/^attachments\.(.*)$/;
             my $field = $1;
             if ($t eq "changedby") {
                 $v = DBNameToIdAndCheck($v);
                 $q = SqlQuote($v);
                 $field = "submitter_id";
                 $t = "equals";
             } elsif ($t eq "changedbefore") {
                 $v = SqlifyDate($v);
                 $q = SqlQuote($v);
                 $field = "creation_ts";
                 $t = "lessthan";
             } elsif ($t eq "changedafter") {
                 $v = SqlifyDate($v);
                 $q = SqlQuote($v);
                 $field = "creation_ts";
                 $t = "greaterthan";
             }
             if ($field eq "ispatch") {
                 if ($v ne "0" && $v ne "1") {
                     return Error("The only legal values for the 'Attachment is patch' field is 0 or 1.");
                 }
             }
             $f = "$table.$field";
         },
418 419 420 421 422 423 424 425 426 427 428 429
         "^changedin," => sub {
             $f = "(to_days(now()) - to_days(bugs.delta_ts))";
         },

         "^keywords," => sub {
             GetVersionTable();
             my @list;
             my $table = "keywords_$chartid";
             foreach my $value (split(/[\s,]+/, $v)) {
                 if ($value eq '') {
                     next;
                 }
430
                 my $id = GetKeywordIdFromName($value);
431 432 433
                 if ($id) {
                     push(@list, "$table.keywordid = $id");
                 } else {
434 435
                     return Error("Unknown keyword named <code>" .
                                  html_quote($v) . "</code>.\n" .
436
                                  "<P>The legal keyword names are\n" .
437
                                  "<A HREF=describekeywords.cgi>" .
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
                                  "listed here</A>.\n");
                 }
             }
             my $haveawordterm;
             if (@list) {
                 $haveawordterm = "(" . join(' OR ', @list) . ")";
                 if ($t eq "anywords") {
                     $term = $haveawordterm;
                 } elsif ($t eq "allwords") {
                     $ref = $funcsbykey{",$t"};
                     &$ref;
                     if ($term && $haveawordterm) {
                         $term = "(($term) AND $haveawordterm)";
                     }
                 }
             }
             if ($term) {
                 push(@supptables, "keywords $table");
                 push(@wherepart, "$table.bug_id = bugs.bug_id");
             }
         },

460 461 462 463 464 465 466 467 468 469 470 471 472
	 "^dependson," => sub {
                my $table = "dependson_" . $chartid;
		push(@supptables, "dependencies $table");
		$ff = "$table.$f";
		$ref = $funcsbykey{",$t"};
		&$ref;
                push(@wherepart, "$table.blocked = bugs.bug_id");
	 },

	 "^blocked," => sub {
                my $table = "blocked_" . $chartid;
		push(@supptables, "dependencies $table");
		$ff = "$table.$f";
473 474
		$ref = $funcsbykey{",$t"};
		&$ref;
475
                push(@wherepart, "$table.dependson = bugs.bug_id");
476 477
	 },

478 479

         ",equals" => sub {
480
             $term = "$ff = $q";
481 482
         },
         ",notequals" => sub {
483
             $term = "$ff != $q";
484 485
         },
         ",casesubstring" => sub {
486
             $term = "INSTR($ff, $q)";
487 488
         },
         ",(substring|substr)" => sub {
489
             $term = "INSTR(LOWER($ff), " . lc($q) . ")";
490 491
         },
         ",notsubstring" => sub {
492
             $term = "INSTR(LOWER($ff), " . lc($q) . ") = 0";
493 494
         },
         ",regexp" => sub {
495
             $term = "LOWER($ff) REGEXP $q";
496 497
         },
         ",notregexp" => sub {
498
             $term = "LOWER($ff) NOT REGEXP $q";
499 500
         },
         ",lessthan" => sub {
501
             $term = "$ff < $q";
502 503
         },
         ",greaterthan" => sub {
504
             $term = "$ff > $q";
505 506 507 508
         },
         ",anyexact" => sub {
             my @list;
             foreach my $w (split(/,/, $v)) {
509
                 if ($w eq "---" && $f !~ /milestone/) {
510 511
                     $w = "";
                 }
512
                 push(@list, "$ff = " . SqlQuote($w));
513 514 515
             }
             $term = join(" OR ", @list);
         },
516 517 518 519 520 521 522 523 524 525 526 527
         ",anywordssubstr" => sub {
             $term = join(" OR ", @{GetByWordListSubstr($ff, $v)});
         },
         ",allwordssubstr" => sub {
             $term = join(" AND ", @{GetByWordListSubstr($ff, $v)});
         },
         ",nowordssubstr" => sub {
             my @list = @{GetByWordListSubstr($ff, $v)};
             if (@list) {
                 $term = "NOT (" . join(" OR ", @list) . ")";
             }
         },
528
         ",anywords" => sub {
529
             $term = join(" OR ", @{GetByWordList($ff, $v)});
530 531
         },
         ",allwords" => sub {
532
             $term = join(" AND ", @{GetByWordList($ff, $v)});
533 534
         },
         ",nowords" => sub {
535
             my @list = @{GetByWordList($ff, $v)};
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
             if (@list) {
                 $term = "NOT (" . join(" OR ", @list) . ")";
             }
         },
         ",changedbefore" => sub {
             my $table = "act_$chartid";
             my $ftable = "fielddefs_$chartid";
             push(@supptables, "bugs_activity $table");
             push(@supptables, "fielddefs $ftable");
             push(@wherepart, "$table.bug_id = bugs.bug_id");
             push(@wherepart, "$table.fieldid = $ftable.fieldid");
             $term = "($ftable.name = '$f' AND $table.bug_when < $q)";
         },
         ",changedafter" => sub {
             my $table = "act_$chartid";
             my $ftable = "fielddefs_$chartid";
             push(@supptables, "bugs_activity $table");
             push(@supptables, "fielddefs $ftable");
             push(@wherepart, "$table.bug_id = bugs.bug_id");
             push(@wherepart, "$table.fieldid = $ftable.fieldid");
             $term = "($ftable.name = '$f' AND $table.bug_when > $q)";
         },
         ",changedto" => sub {
             my $table = "act_$chartid";
             my $ftable = "fielddefs_$chartid";
             push(@supptables, "bugs_activity $table");
             push(@supptables, "fielddefs $ftable");
             push(@wherepart, "$table.bug_id = bugs.bug_id");
             push(@wherepart, "$table.fieldid = $ftable.fieldid");
565
             $term = "($ftable.name = '$f' AND $table.added = $q)";
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
         },
         ",changedby" => sub {
             my $table = "act_$chartid";
             my $ftable = "fielddefs_$chartid";
             push(@supptables, "bugs_activity $table");
             push(@supptables, "fielddefs $ftable");
             push(@wherepart, "$table.bug_id = bugs.bug_id");
             push(@wherepart, "$table.fieldid = $ftable.fieldid");
             my $id = DBNameToIdAndCheck($v);
             $term = "($ftable.name = '$f' AND $table.who = $id)";
         },
         );
    my @funcnames;
    while (@funcdefs) {
        my $key = shift(@funcdefs);
        my $value = shift(@funcdefs);
        if ($key =~ /^[^,]*$/) {
            die "All defs in %funcs must have a comma in their name: $key";
        }
        if (exists $funcsbykey{$key}) {
            die "Duplicate key in %funcs: $key";
        }
        $funcsbykey{$key} = $value;
        push(@funcnames, $key);
    }


    my $chart = -1;
    my $row = 0;
    foreach my $ref (@specialchart) {
        my $col = 0;
        while (@$ref) {
            $F{"field$chart-$row-$col"} = shift(@$ref);
            $F{"type$chart-$row-$col"} = shift(@$ref);
            $F{"value$chart-$row-$col"} = shift(@$ref);
            if ($debug) {
                print qq{<P>$F{"field$chart-$row-$col"} | $F{"type$chart-$row-$col"} | $F{"value$chart-$row-$col"}*\n};
            }
            $col++;

        }
        $row++;
    }


611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
# A boolean chart is a way of representing the terms in a logical 
# expression.  Bugzilla builds SQL queries depending on how you enter
# terms into the boolean chart. Boolean charts are represented in 
# urls as tree-tuples of (chart id, row, column). The query form 
# (query.cgi) may contain an arbitrary number of boolean charts where
# each chart represents a clause in a SQL query. 
#
# The query form starts out with one boolean chart containing one
# row and one column.  Extra rows can be created by pressing the 
# AND button at the bottom of the chart.  Extra columns are created 
# by pressing the OR button at the right end of the chart. Extra
# charts are created by pressing "Add another boolean chart".
#
# Each chart consists of an artibrary number of rows and columns.
# The terms within a row are ORed together. The expressions represented
# by each row are ANDed together. The expressions represented by each
# chart are ANDed together.
#
#        ----------------------
#        | col2 | col2 | col3 |
# --------------|------|------|
# | row1 |  a1  |  a2  |      |
# |------|------|------|------|  => ((a1 OR a2) AND (b1 OR b2 OR b3) AND (c1))
# | row2 |  b1  |  b2  |  b3  |
# |------|------|------|------|
# | row3 |  c1  |      |      |
# -----------------------------
#
#        --------
#        | col2 |
# --------------|
# | row1 |  d1  | => (d1)
# ---------------
#
# Together, these two charts represent a SQL expression like this
# SELECT blah FROM blah WHERE ( (a1 OR a2)AND(b1 OR b2 OR b3)AND(c1)) AND (d1)
#
# The terms within a single row of a boolean chart are all constraints
# on a single piece of data.  If you're looking for a bug that has two 
# different people cc'd on it, then you need to use two boolean charts. 
# This will find bugs with one CC mathing 'foo@blah.org' and and another 
# CC matching 'bar@blah.org'. 
# 
# --------------------------------------------------------------
# CC    | equal to
# foo@blah.org
# --------------------------------------------------------------
# CC    | equal to
# bar@blah.org
#
# If you try to do this query by pressing the AND button in the 
# original boolean chart then what you'll get is an expression that
# looks for a single CC where the login name is both "foo@blah.org",
# and "bar@blah.org". This is impossible.
#
# --------------------------------------------------------------
# CC    | equal to
# foo@blah.org
# AND
# CC    | equal to
# bar@blah.org
# --------------------------------------------------------------

# $chartid is the number of the current chart whose SQL we're contructing
# $row is the current row of the current chart

# names for table aliases are constructed using $chartid and $row
#   SELECT blah  FROM $table "$table_$chartid_$row" WHERE ....

# $f  = field of table in bug db (e.g. bug_id, reporter, etc)
# $ff = qualified field name (field name prefixed by table)
#       e.g. bugs_activity.bug_id
# $t  = type of query. e.g. "equal to", "changed after", case sensitive substr"
# $v  = value - value the user typed in to the form 
# $q  = sanitized version of user input (SqlQuote($v))
# @supptables = Tables and/or table aliases used in query
# %suppseen   = A hash used to store all the tables in supptables to weed
#               out duplicates.
# $suppstring = String which is pasted into query containing all table names


    $row = 0;
693 694 695 696
    for ($chart=-1 ;
         $chart < 0 || exists $F{"field$chart-0-0"} ;
         $chart++) {
        $chartid = $chart >= 0 ? $chart : "";
697
        for ($row = 0 ;
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
             exists $F{"field$chart-$row-0"} ;
             $row++) {
            my @orlist;
            for (my $col = 0 ;
                 exists $F{"field$chart-$row-$col"} ;
                 $col++) {
                $f = $F{"field$chart-$row-$col"} || "noop";
                $t = $F{"type$chart-$row-$col"} || "noop";
                $v = $F{"value$chart-$row-$col"};
                $v = "" if !defined $v;
                $v = trim($v);
                if ($f eq "noop" || $t eq "noop" || $v eq "") {
                    next;
                }
                $q = SqlQuote($v);
                my $func;
                $term = undef;
                foreach my $key (@funcnames) {
                    if ("$f,$t" =~ m/$key/) {
                        my $ref = $funcsbykey{$key};
                        if ($debug) {
                            print "<P>$key ($f , $t ) => ";
                        }
721 722 723 724
                        $ff = $f;
                        if ($f !~ /\./) {
                            $ff = "bugs.$f";
                        }
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
                        &$ref;
                        if ($debug) {
                            print "$f , $t , $term";
                        }
                        if ($term) {
                            last;
                        }
                    }
                }
                if ($term) {
                    push(@orlist, $term);
                } else {
                    my $errstr = "Can't seem to handle " .
                        qq{'<code>$F{"field$chart-$row-$col"}</code>' and } .
                            qq{'<code>$F{"type$chart-$row-$col"}</code>' } .
                                "together";
                    die "Internal error: $errstr" if $chart < 0;
                    return Error($errstr);
                }
            }
            if (@orlist) {
                push(@andlist, "(" . join(" OR ", @orlist) . ")");
            }
        }
    }
    my %suppseen = ("bugs" => 1);
    my $suppstring = "bugs";
    foreach my $str (@supptables) {
        if (!$suppseen{$str}) {
754
            if ($str !~ /^(LEFT|INNER) JOIN/i) {
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
                $suppstring .= ",";
            }
            $suppstring .= " $str";
            $suppseen{$str} = 1;
        }
    }
    my $query =  ("SELECT " . join(', ', @fields) .
                  " FROM $suppstring" .
                  " WHERE " . join(' AND ', (@wherepart, @andlist)) .
                  " GROUP BY bugs.bug_id");
    if ($debug) {
        print "<P><CODE>" . value_quote($query) . "</CODE><P>\n";
        exit();
    }
    return $query;
}


773

774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
sub LookupNamedQuery {
    my ($name) = (@_);
    confirm_login();
    my $userid = DBNameToIdAndCheck($::COOKIE{"Bugzilla_login"});
    SendSQL("SELECT query FROM namedqueries " .
            "WHERE userid = $userid AND name = " . SqlQuote($name));
    my $result = FetchOneColumn();
    if (!defined $result) {
        print "Content-type: text/html\n\n";
        PutHeader("Something weird happened");
        print qq{The named query $name seems to no longer exist.};
        PutFooter();
        exit;
    }
    return $result;
}


792

793
$::querytitle = "Bug List";
794

795 796
CMD: for ($::FORM{'cmdtype'}) {
    /^runnamed$/ && do {
797
        $::buffer = LookupNamedQuery($::FORM{"namedcmd"});
798
        $::querytitle = "Bug List: $::FORM{'namedcmd'}";
799
        ProcessFormFields($::buffer);
800 801 802
        last CMD;
    };
    /^editnamed$/ && do {
803 804
	my $url = "query.cgi?" . LookupNamedQuery($::FORM{"namedcmd"});
        print qq{Content-type: text/html
805
Refresh: 0; URL=$url
terry%netscape.com's avatar
terry%netscape.com committed
806

807
<TITLE>What a hack.</TITLE>
808 809
<A HREF="$url">Loading your query named <B>$::FORM{'namedcmd'}</B>...</A>
};
810 811 812
        exit;
    };
    /^forgetnamed$/ && do {
813 814 815 816
        confirm_login();
        my $userid = DBNameToIdAndCheck($::COOKIE{"Bugzilla_login"});
        SendSQL("DELETE FROM namedqueries WHERE userid = $userid " .
                "AND name = " . SqlQuote($::FORM{'namedcmd'}));
817

818
        print "Content-type: text/html\n\n";
819
        PutHeader("Query is gone", "");
terry%netscape.com's avatar
terry%netscape.com committed
820

821
        print qq{
822
OK, the <B>$::FORM{'namedcmd'}</B> query is gone.
terry%netscape.com's avatar
terry%netscape.com committed
823
<P>
824 825
<A HREF="query.cgi">Go back to the query page.</A>
};
826
        PutFooter();
827 828 829
        exit;
    };
    /^asdefault$/ && do {
830 831 832 833 834 835 836 837
        confirm_login();
        my $userid = DBNameToIdAndCheck($::COOKIE{"Bugzilla_login"});
        print "Content-type: text/html\n\n";
        SendSQL("REPLACE INTO namedqueries (userid, name, query) VALUES " .
                "($userid, '$::defaultqueryname'," .
                SqlQuote($::buffer) . ")");
        PutHeader("OK, default is set");
        print qq{
838 839
OK, you now have a new default query.  You may also bookmark the result of any
individual query.
terry%netscape.com's avatar
terry%netscape.com committed
840

841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
<P><A HREF="query.cgi">Go back to the query page, using the new default.</A>
};
        PutFooter();
        exit();
    };
    /^asnamed$/ && do {
        confirm_login();
        my $userid = DBNameToIdAndCheck($::COOKIE{"Bugzilla_login"});
        print "Content-type: text/html\n\n";
        my $name = trim($::FORM{'newqueryname'});
        if ($name eq "" || $name =~ /[<>&]/) {
            PutHeader("Please pick a valid name for your new query");
            print "Click the <B>Back</B> button and type in a valid name\n";
            print "for this query.  (Query names should not contain unusual\n";
            print "characters.)\n";
            PutFooter();
            exit();
        }
859 860
        $::buffer =~ s/[\&\?]cmdtype=[a-z]+//;
        my $qname = SqlQuote($name);
cyeh%bluemartini.com's avatar
cyeh%bluemartini.com committed
861
       my $tofooter= ( $::FORM{'tofooter'} ? 1 : 0 );
862 863 864
        SendSQL("SELECT query FROM namedqueries " .
                "WHERE userid = $userid AND name = $qname");
        if (!FetchOneColumn()) {
cyeh%bluemartini.com's avatar
cyeh%bluemartini.com committed
865 866
            SendSQL("REPLACE INTO namedqueries (userid, name, query, linkinfooter) " .
                    "VALUES ($userid, $qname, ". SqlQuote($::buffer) .", ". $tofooter .")");
867
        } else {
cyeh%bluemartini.com's avatar
cyeh%bluemartini.com committed
868 869
            SendSQL("UPDATE namedqueries SET query = " . SqlQuote($::buffer) . "," .
                   " linkinfooter = " . $tofooter .
870 871
                    " WHERE userid = $userid AND name = $qname");
        }
872 873 874 875 876 877
        PutHeader("OK, query saved.");
        print qq{
OK, you have a new query named <code>$name</code>
<P>
<BR><A HREF="query.cgi">Go back to the query page</A>
};
878
        PutFooter();
879 880
        exit;
    };
terry%netscape.com's avatar
terry%netscape.com committed
881 882 883
}


884
if (exists $ENV{'HTTP_USER_AGENT'} && $ENV{'HTTP_USER_AGENT'} =~ /Mozilla.[3-9]/ && $ENV{'HTTP_USER_AGENT'} !~ /[Cc]ompatible/ ) {
885 886 887 888 889 890
    # Search for real Netscape 3 and up.  http://www.browsercaps.org used as source of
    # browsers compatbile with server-push.  It's a Netscape hack, incompatbile
    # with MSIE and Lynx (at least).  Even Communicator 4.51 has bugs with it,
    # especially during page reload.
    $serverpush = 1;

891 892 893 894 895 896 897 898 899
    print qq{Content-type: multipart/x-mixed-replace;boundary=thisrandomstring\n
--thisrandomstring
Content-type: text/html\n
<html><head><title>Bugzilla is pondering your query</title>
<style type="text/css">
    .psb { margin-top: 20%; text-align: center; }
</style></head><body>
<h1 class="psb">Please stand by ...</h1></body></html>
    };
900 901 902
    # Note! HTML header is complete!
} else {
    print "Content-type: text/html\n";
903 904 905
    #Changing attachment to inline to resolve 46897
    #zach@zachlipton.com
    print "Content-disposition: inline; filename=bugzilla_bug_list.html\n";
906 907
    # Note! Don't finish HTML header yet!  Only one newline so far!
}
908 909
sub DefCol {
    my ($name, $k, $t, $s, $q) = (@_);
910

911 912 913 914
    $::key{$name} = $k;
    $::title{$name} = $t;
    if (defined $s && $s ne "") {
        $::sortkey{$name} = $s;
terry%netscape.com's avatar
terry%netscape.com committed
915
    }
916 917 918 919
    if (!defined $q || $q eq "") {
        $q = 0;
    }
    $::needquote{$name} = $q;
terry%netscape.com's avatar
terry%netscape.com committed
920 921
}

922
DefCol("opendate", "unix_timestamp(bugs.creation_ts)", "Opened",
923
       "bugs.creation_ts");
924
DefCol("changeddate", "unix_timestamp(bugs.delta_ts)", "Changed",
925 926 927 928 929 930
       "bugs.delta_ts");
DefCol("severity", "substring(bugs.bug_severity, 1, 3)", "Sev",
       "bugs.bug_severity");
DefCol("priority", "substring(bugs.priority, 1, 3)", "Pri", "bugs.priority");
DefCol("platform", "substring(bugs.rep_platform, 1, 3)", "Plt",
       "bugs.rep_platform");
931 932 933 934 935
DefCol("owner", "map_assigned_to.login_name", "Owner",
       "map_assigned_to.login_name");
DefCol("reporter", "map_reporter.login_name", "Reporter",
       "map_reporter.login_name");
DefCol("qa_contact", "map_qa_contact.login_name", "QAContact", "map_qa_contact.login_name");
936 937 938
DefCol("status", "substring(bugs.bug_status,1,4)", "State", "bugs.bug_status");
DefCol("resolution", "substring(bugs.resolution,1,4)", "Result",
       "bugs.resolution");
939 940
DefCol("summary", "substring(bugs.short_desc, 1, 60)", "Summary", "bugs.short_desc", 1);
DefCol("summaryfull", "bugs.short_desc", "Summary", "bugs.short_desc", 1);
941
DefCol("status_whiteboard", "bugs.status_whiteboard", "StatusSummary", "bugs.status_whiteboard", 1);
942
DefCol("component", "substring(bugs.component, 1, 8)", "Comp",
943
	"bugs.component");
944 945 946
DefCol("product", "substring(bugs.product, 1, 8)", "Product", "bugs.product");
DefCol("version", "substring(bugs.version, 1, 5)", "Vers", "bugs.version");
DefCol("os", "substring(bugs.op_sys, 1, 4)", "OS", "bugs.op_sys");
947 948
DefCol("target_milestone", "bugs.target_milestone", "TargetM",
       "bugs.target_milestone");
949
DefCol("votes", "bugs.votes", "Votes", "bugs.votes desc");
950
DefCol("keywords", "bugs.keywords", "Keywords", "bugs.keywords", 5);
951 952 953 954

my @collist;
if (defined $::COOKIE{'COLUMNLIST'}) {
    @collist = split(/ /, $::COOKIE{'COLUMNLIST'});
terry%netscape.com's avatar
terry%netscape.com committed
955
} else {
956
    @collist = @::default_column_list;
terry%netscape.com's avatar
terry%netscape.com committed
957 958
}

959 960
my $minvotes;
if (defined $::FORM{'votes'}) {
961
    if (trim($::FORM{'votes'}) ne "") {
962 963 964 965 966 967 968
        if (! (grep {/^votes$/} @collist)) {
            push(@collist, 'votes');
        }
    }
}


969
my $dotweak = defined $::FORM{'tweak'};
terry%netscape.com's avatar
terry%netscape.com committed
970

971 972
if ($dotweak) {
    confirm_login();
973
    if (!UserInGroup("editbugs")) {
974
        print qq{
cyeh%bluemartini.com's avatar
cyeh%bluemartini.com committed
975
Sorry; you do not have sufficient privileges to edit a bunch of bugs
976 977 978 979 980
at once.
};
        PutFooter();
        exit();
    }
981 982
} else {
    quietly_check_login();
terry%netscape.com's avatar
terry%netscape.com committed
983 984 985
}


986
my @fields = ("bugs.bug_id", "bugs.groupset");
terry%netscape.com's avatar
terry%netscape.com committed
987

988 989
foreach my $c (@collist) {
    if (exists $::needquote{$c}) {
990
        push(@fields, "$::key{$c}");
991
    }
terry%netscape.com's avatar
terry%netscape.com committed
992 993 994
}


995
if ($dotweak) {
996
    push(@fields, "bugs.product", "bugs.bug_status");
terry%netscape.com's avatar
terry%netscape.com committed
997 998
}

999

terry%netscape.com's avatar
terry%netscape.com committed
1000

1001 1002 1003 1004 1005 1006
if ($::FORM{'regetlastlist'}) {
    if (!$::COOKIE{'BUGLIST'}) {
        print qq{
Sorry, I seem to have lost the cookie that recorded the results of your last
query.  You will have to start over at the <A HREF="query.cgi">query page</A>.
};
1007
        PutFooter();
1008 1009 1010
        exit;
    }
    my @list = split(/:/, $::COOKIE{'BUGLIST'});
1011
    $::FORM{'bug_id'} = join(',', @list);
1012 1013 1014
    if (!$::FORM{'order'}) {
        $::FORM{'order'} = 'reuse last sort';
    }
1015 1016
    $::buffer = "bug_id=" . $::FORM{'bug_id'} . "&order=" .
        url_quote($::FORM{'order'});
1017 1018
}

1019

terry%netscape.com's avatar
terry%netscape.com committed
1020

1021 1022
ReconnectToShadowDatabase();

1023
my $query = GenerateSQL(\@fields, undef, undef, $::buffer);
1024

1025

1026

1027 1028
if ($::COOKIE{'LASTORDER'}) {
    if ((!$::FORM{'order'}) || $::FORM{'order'} =~ /^reuse/i) {
1029 1030
        $::FORM{'order'} = url_decode($::COOKIE{'LASTORDER'});
    }
1031 1032 1033 1034
}


if (defined $::FORM{'order'} && $::FORM{'order'} ne "") {
1035
    $query .= " ORDER BY ";
1036 1037
    $::FORM{'order'} =~ s/votesum/bugs.votes/; # Silly backwards compatability
                                               # hack.
1038 1039
    $::FORM{'order'} =~ s/assign\.login_name/map_assigned_to.login_name/g;
                                # Another backwards compatability hack.
1040

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    ORDER: for ($::FORM{'order'}) {
        /\./ && do {
            # This (hopefully) already has fieldnames in it, so we're done.
            last ORDER;
        };
        /Number/ && do {
            $::FORM{'order'} = "bugs.bug_id";
            last ORDER;
        };
        /Import/ && do {
1051
            $::FORM{'order'} = "bugs.priority, bugs.bug_severity";
1052 1053 1054
            last ORDER;
        };
        /Assign/ && do {
1055
            $::FORM{'order'} = "map_assigned_to.login_name, bugs.bug_status, priority, bugs.bug_id";
1056 1057 1058
            last ORDER;
        };
        # DEFAULT
1059
        $::FORM{'order'} = "bugs.bug_status, bugs.priority, map_assigned_to.login_name, bugs.bug_id";
terry%netscape.com's avatar
terry%netscape.com committed
1060
    }
1061 1062
    die "Invalid order: $::FORM{'order'}" unless
        $::FORM{'order'} =~ /^([a-zA-Z0-9_., ]+)$/;
1063

1064 1065 1066 1067 1068 1069 1070 1071 1072
    # Extra special disgusting hack: if we are ordering by target_milestone,
    # change it to order by the sortkey of the target_milestone first.
    my $order = $::FORM{'order'};
    if ($order =~ /bugs.target_milestone/) {
        $query =~ s/ WHERE / LEFT JOIN milestones ms_order ON ms_order.value = bugs.target_milestone AND ms_order.product = bugs.product WHERE /;
        $order =~ s/bugs.target_milestone/ms_order.sortkey,ms_order.value/;
    }

    $query .= $order;
terry%netscape.com's avatar
terry%netscape.com committed
1073 1074
}

1075

1076
if ($::FORM{'debug'} && $serverpush) {
1077
    print "<P><CODE>" . value_quote($query) . "</CODE><P>\n";
1078 1079 1080
}


1081 1082 1083
if (Param('expectbigqueries')) {
    SendSQL("set option SQL_BIG_TABLES=1");
}
1084 1085 1086 1087 1088 1089 1090
SendSQL($query);

my $count = 0;
$::bugl = "";
sub pnl {
    my ($str) = (@_);
    $::bugl  .= $str;
terry%netscape.com's avatar
terry%netscape.com committed
1091 1092
}

1093 1094 1095
my $fields = $::buffer;
$fields =~ s/[&?]order=[^&]*//g;
$fields =~ s/[&?]cmdtype=[^&]*//g;
terry%netscape.com's avatar
terry%netscape.com committed
1096 1097


1098
my $orderpart;
1099 1100
my $oldorder;

1101
if (defined $::FORM{'order'} && trim($::FORM{'order'}) ne "") {
1102
    $orderpart = "&order=" . url_quote("$::FORM{'order'}");
1103
    $oldorder = url_quote(", $::FORM{'order'}");
terry%netscape.com's avatar
terry%netscape.com committed
1104
} else {
1105
    $orderpart = "";
1106
    $oldorder = "";
terry%netscape.com's avatar
terry%netscape.com committed
1107 1108
}

1109
if ($dotweak) {
1110
    pnl "<FORM NAME=changeform METHOD=POST ACTION=\"process_bug.cgi\">";
terry%netscape.com's avatar
terry%netscape.com committed
1111 1112 1113
}


1114
my @th;
1115 1116
foreach my $c (@collist) {
    if (exists $::needquote{$c}) {
1117
        my $h = "<TH>";
1118
        if (defined $::sortkey{$c}) {
1119
            $h .= "<A HREF=\"buglist.cgi?$fields&order=" . url_quote($::sortkey{$c}) . "$oldorder\">$::title{$c}</A>";
1120
        } else {
1121
            $h .= $::title{$c};
1122
        }
1123 1124
        $h .= "</TH>";
        push(@th, $h);
terry%netscape.com's avatar
terry%netscape.com committed
1125 1126 1127
    }
}

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
my $tablestart = "<TABLE CELLSPACING=0 CELLPADDING=4 WIDTH=100%>
<TR ALIGN=LEFT><TH>
<A HREF=\"buglist.cgi?$fields&order=bugs.bug_id\">ID</A>";

my $splitheader = 0;
if ($::COOKIE{'SPLITHEADER'}) {
    $splitheader = 1;
}

if ($splitheader) {
    $tablestart =~ s/<TH/<TH COLSPAN="2"/;
    for (my $pass=0 ; $pass<2 ; $pass++) {
        if ($pass == 1) {
            $tablestart .= "</TR>\n<TR><TD></TD>";
        }
        for (my $i=1-$pass ; $i<@th ; $i += 2) {
1144
            my $h = $th[$i];
1145 1146 1147 1148 1149 1150 1151 1152 1153
            $h =~ s/TH/TH COLSPAN="2" ALIGN="left"/;
            $tablestart .= $h;
        }
    }
} else {
    $tablestart .= join("", @th);
}


1154
$tablestart .= "\n";
terry%netscape.com's avatar
terry%netscape.com committed
1155 1156


1157 1158 1159 1160 1161
my @row;
my %seen;
my @bugarray;
my %prodhash;
my %statushash;
1162
my %ownerhash;
terry%netscape.com's avatar
terry%netscape.com committed
1163

1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
my $pricol = -1;
my $sevcol = -1;
for (my $colcount = 0 ; $colcount < @collist ; $colcount++) {
    my $colname = $collist[$colcount];
    if ($colname eq "priority") {
        $pricol = $colcount;
    }
    if ($colname eq "severity") {
        $sevcol = $colcount;
    }
}

1176 1177
my @weekday= qw( Sun Mon Tue Wed Thu Fri Sat );

1178 1179
while (@row = FetchSQLData()) {
    my $bug_id = shift @row;
1180
    my $g = shift @row;         # Bug's group set.
1181 1182 1183 1184 1185 1186 1187 1188
    if (!defined $seen{$bug_id}) {
        $seen{$bug_id} = 1;
        $count++;
        if ($count % 200 == 0) {
            # Too big tables take too much browser memory...
            pnl "</TABLE>$tablestart";
        }
        push @bugarray, $bug_id;
1189

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
        # retrieve this bug's priority and severity, if available,
        # by looping through all column names -- gross but functional
        my $priority = "unknown";
        my $severity;
        if ($pricol >= 0) {
            $priority = $row[$pricol];
        }
        if ($sevcol >= 0) {
            $severity = $row[$sevcol];
        }
        my $customstyle = "";
        if ($severity) {
            if ($severity eq "enhan") {
                $customstyle = "style='font-style:italic ! important'";
            }
            if ($severity eq "block") {
                $customstyle = "style='color:red ! important; font-weight:bold ! important'";
            }
            if ($severity eq "criti") {
                $customstyle = "style='color:red; ! important'";
            }
        }
        pnl "<TR VALIGN=TOP ALIGN=LEFT CLASS=$priority $customstyle><TD>";
1213
        if ($dotweak) {
1214
            pnl "<input type=checkbox name=id_$bug_id>";
1215 1216
        }
        pnl "<A HREF=\"show_bug.cgi?id=$bug_id\">";
1217 1218 1219
        pnl "$bug_id</A>";
        if ($g != "0") { pnl "*"; }
        pnl " ";
1220
        foreach my $c (@collist) {
1221 1222 1223
            if (exists $::needquote{$c}) {
                my $value = shift @row;
                if (!defined $value) {
1224
                    pnl "<TD>";
1225 1226
                    next;
                }
1227 1228
                if ($c eq "owner") {
                    $ownerhash{$value} = 1;
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
		}elsif( $c eq 'changeddate' or $c eq 'opendate' ) {
		    my $age= time() - $value;
		    my ($s,$m,$h,$d,$mo,$y,$wd)= localtime $value;
		    if( $age < 18*60*60 ) {
			$value= sprintf "%02d:%02d:%02d", $h,$m,$s;
		    }elsif( $age < 6*24*60*60 ) {
			$value= sprintf "%s %02d:%02d", $weekday[$wd],$h,$m;
		    }else {
			$value= sprintf "%04d-%02d-%02d", 1900+$y,$mo+1,$d;
		    }
1239
		}
1240
                if ($::needquote{$c} || $::needquote{$c} == 5) {
1241 1242 1243 1244
                    $value = html_quote($value);
                } else {
                    $value = "<nobr>$value</nobr>";
                }
1245

1246
                pnl "<td class=$c>$value";
terry%netscape.com's avatar
terry%netscape.com committed
1247 1248
            }
        }
1249 1250 1251 1252 1253 1254 1255
        if ($dotweak) {
            my $value = shift @row;
            $prodhash{$value} = 1;
            $value = shift @row;
            $statushash{$value} = 1;
        }
        pnl "\n";
terry%netscape.com's avatar
terry%netscape.com committed
1256 1257
    }
}
1258 1259 1260
my $buglist = join(":", @bugarray);


1261 1262
# This is stupid.  We really really need to move the quip list into the DB!
my $quip;
1263 1264
if (Param('usequip')){
  if (open (COMMENTS, "<data/comments")) {
1265 1266
    my @cdata;
    while (<COMMENTS>) {
1267
      push @cdata, $_;
1268 1269 1270
    }
    close COMMENTS;
    $quip = $cdata[int(rand($#cdata + 1))];
1271 1272
  }
  $quip ||= "Bugzilla would like to put a random quip here, but nobody has entered any.";
1273 1274
}

1275

1276
# We've done all we can without any output.  If we can server push it is time
1277
# take down the waiting page and put up the real one.
1278 1279 1280
if ($serverpush) {
    print "\n";
    print "--thisrandomstring\n";
1281
    print "Content-type: text/html\n";
1282
    print "Content-disposition: inline; filename=bugzilla_bug_list.html\n";
1283
    # Note! HTML header not yet closed
1284
}
1285
my $toolong = 0;
1286 1287 1288 1289
if ($::FORM{'order'}) {
    my $q = url_quote($::FORM{'order'});
    print "Set-Cookie: LASTORDER=$q ; path=/; expires=Sun, 30-Jun-2029 00:00:00 GMT\n";
}
1290
if (length($buglist) < 4000) {
1291
    print "Set-Cookie: BUGLIST=$buglist\n\n";
1292
} else {
1293
    print "Set-Cookie: BUGLIST=\n\n";
1294
    $toolong = 1;
terry%netscape.com's avatar
terry%netscape.com committed
1295
}
1296
PutHeader($::querytitle);
terry%netscape.com's avatar
terry%netscape.com committed
1297

1298

1299
print "
1300
<CENTER>
1301
<B>" .  time2str("%a %b %e %T %Z %Y", time()) . "</B>";
terry%netscape.com's avatar
terry%netscape.com committed
1302

1303 1304 1305 1306
if (Param('usebuggroups')) {
   print "<BR>* next to a bug number notes a bug not visible to everyone.<BR>";
}

1307
if (defined $::FORM{'debug'}) {
1308
    print "<P><CODE>" . value_quote($query) . "</CODE><P>\n";
terry%netscape.com's avatar
terry%netscape.com committed
1309 1310
}

1311 1312 1313 1314 1315
if ($toolong) {
    print "<h2>This list is too long for bugzilla's little mind; the\n";
    print "Next/Prev/First/Last buttons won't appear.</h2>\n";
}

1316
if (Param('usequip')){
1317
  print "<HR><A HREF=quips.cgi><I>$quip</I></A></CENTER>\n";
1318
}
1319 1320 1321
print "<HR SIZE=10>";
print "$count bugs found." if $count > 9;
print $tablestart, "\n";
1322 1323 1324 1325 1326
print $::bugl;
print "</TABLE>\n";

if ($count == 0) {
    print "Zarro Boogs found.\n";
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
    # I've been asked to explain this ... way back when, when Netscape released
    # version 4.0 of its browser, we had a release party.  Naturally, there
    # had been a big push to try and fix every known bug before the release.
    # Naturally, that hadn't actually happened.  (This is not unique to
    # Netscape or to 4.0; the same thing has happened with every software
    # project I've ever seen.)  Anyway, at the release party, T-shirts were
    # handed out that said something like "Netscape 4.0: Zarro Boogs".
    # Just like the software, the T-shirt had no known bugs.  Uh-huh.
    #
    # So, when you query for a list of bugs, and it gets no results, you
    # can think of this as a friendly reminder.  Of *course* there are bugs
    # matching your query, they just aren't in the bugsystem yet...
1339 1340 1341

    print qq{<p><A HREF="query.cgi">Query Page</A>\n};
    print qq{&nbsp;&nbsp;<A HREF="enter_bug.cgi">Enter New Bug</A>\n};
1342
    print qq{<NOBR><A HREF="query.cgi?$::buffer">Edit this query</A></NOBR>\n};
1343 1344 1345 1346
} elsif ($count == 1) {
    print "One bug found.\n";
} else {
    print "$count bugs found.\n";
terry%netscape.com's avatar
terry%netscape.com committed
1347 1348
}

1349 1350 1351
if ($dotweak) {
    GetVersionTable();
    print "
terry%netscape.com's avatar
terry%netscape.com committed
1352 1353 1354
<SCRIPT>
numelements = document.changeform.elements.length;
function SetCheckboxes(value) {
1355
    var item;
terry%netscape.com's avatar
terry%netscape.com committed
1356 1357 1358 1359 1360
    for (var i=0 ; i<numelements ; i++) {
        item = document.changeform.elements\[i\];
        item.checked = value;
    }
}
1361
document.write(\" <input type=button value=\\\"Uncheck All\\\" onclick=\\\"SetCheckboxes(false);\\\"> <input type=button value=\\\"Check All\\\" onclick=\\\"SetCheckboxes(true);\\\">\");
1362
</SCRIPT>";
1363 1364

    my $resolution_popup = make_options(\@::settable_resolution, "FIXED");
1365 1366 1367 1368
    my @prod_list = keys %prodhash;
    my @list = @prod_list;
    my @legal_versions;
    my @legal_component;
1369
    if (1 == @prod_list) {
1370 1371
        @legal_versions = @{$::versions{$prod_list[0]}};
        @legal_component = @{$::components{$prod_list[0]}};
terry%netscape.com's avatar
terry%netscape.com committed
1372
    }
1373

1374 1375 1376 1377
    my $version_popup = make_options(\@legal_versions, $::dontchange);
    my $platform_popup = make_options(\@::legal_platform, $::dontchange);
    my $priority_popup = make_options(\@::legal_priority, $::dontchange);
    my $sev_popup = make_options(\@::legal_severity, $::dontchange);
1378
    my $component_popup = make_options(\@legal_component, $::dontchange);
1379
    my $product_popup = make_options(\@::legal_product, $::dontchange);
terry%netscape.com's avatar
terry%netscape.com committed
1380 1381


1382
    print "
terry%netscape.com's avatar
terry%netscape.com committed
1383 1384 1385
<hr>
<TABLE>
<TR>
1386 1387 1388 1389
    <TD ALIGN=RIGHT><B>Product:</B></TD>
    <TD><SELECT NAME=product>$product_popup</SELECT></TD>
    <TD ALIGN=RIGHT><B>Version:</B></TD>
    <TD><SELECT NAME=version>$version_popup</SELECT></TD>
terry%netscape.com's avatar
terry%netscape.com committed
1390
<TR>
1391 1392 1393 1394
    <TD ALIGN=RIGHT><B><A HREF=\"bug_status.html#rep_platform\">Platform:</A></B></TD>
    <TD><SELECT NAME=rep_platform>$platform_popup</SELECT></TD>
    <TD ALIGN=RIGHT><B><A HREF=\"bug_status.html#priority\">Priority:</A></B></TD>
    <TD><SELECT NAME=priority>$priority_popup</SELECT></TD>
terry%netscape.com's avatar
terry%netscape.com committed
1395 1396
</TR>
<TR>
1397 1398 1399 1400
    <TD ALIGN=RIGHT><B>Component:</B></TD>
    <TD><SELECT NAME=component>$component_popup</SELECT></TD>
    <TD ALIGN=RIGHT><B><A HREF=\"bug_status.html#severity\">Severity:</A></B></TD>
    <TD><SELECT NAME=bug_severity>$sev_popup</SELECT></TD>
1401 1402 1403
</TR>";

    if (Param("usetargetmilestone")) {
1404 1405 1406 1407 1408
        my @legal_milestone;
        if(1 == @prod_list) {
            @legal_milestone = @{$::target_milestone{$prod_list[0]}};
        }
        my $tfm_popup = make_options(\@legal_milestone, $::dontchange);
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
        print "
    <TR>
    <TD ALIGN=RIGHT><B>Target milestone:</B></TD>
    <TD><SELECT NAME=target_milestone>$tfm_popup</SELECT></TD>
    </TR>";
    }

    if (Param("useqacontact")) {
        print "
<TR>
<TD><B>QA Contact:</B></TD>
<TD COLSPAN=3><INPUT NAME=qa_contact SIZE=32 VALUE=\"" .
            value_quote($::dontchange) . "\"></TD>
</TR>";
    }

1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
   print qq{
<TR><TD ALIGN="RIGHT"><B>CC List:</B></TD>
<TD COLSPAN=3><INPUT NAME="masscc" SIZE=32 VALUE="">
<SELECT NAME="ccaction">
<OPTION VALUE="add">Add these to the CC List
<OPTION VALUE="remove">Remove these from the CC List
</SELECT>
</TD>
</TR>
};

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
    if (@::legal_keywords) {
        print qq{
<TR><TD><B><A HREF="describekeywords.cgi">Keywords</A>:</TD>
<TD COLSPAN=3><INPUT NAME=keywords SIZE=32 VALUE="">
<SELECT NAME="keywordaction">
<OPTION VALUE="add">Add these keywords
<OPTION VALUE="delete">Delete these keywords
<OPTION VALUE="makeexact">Make the keywords be exactly this list
</SELECT>
</TD>
</TR>
};
    }


    print "</TABLE>
terry%netscape.com's avatar
terry%netscape.com committed
1452

1453
<INPUT NAME=multiupdate value=Y TYPE=hidden>
terry%netscape.com's avatar
terry%netscape.com committed
1454 1455 1456

<B>Additional Comments:</B>
<BR>
1457
<TEXTAREA WRAP=HARD NAME=comment ROWS=5 COLS=80></TEXTAREA><BR>";
terry%netscape.com's avatar
terry%netscape.com committed
1458

1459 1460 1461 1462 1463
if($::usergroupset ne '0') {
    SendSQL("select bit, name, description, isactive ".
            "from groups where bit & $::usergroupset != 0 ".
            "and isbuggroup != 0 ".
            "order by description");
1464 1465
    # We only print out a header bit for this section if there are any
    # results.
1466 1467
    my $groupFound = 0;
    my $inactiveFound = 0;
1468
    while (MoreSQLData()) {
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
        my ($bit, $groupname, $description, $isactive) = (FetchSQLData());
        if(($prodhash{$groupname}) || (!defined($::proddesc{$groupname}))) {
          if(!$groupFound) {
            print "<B>Groupset:</B><BR>\n";
            print "<TABLE BORDER=1><TR>\n";
            print "<TH ALIGN=center VALIGN=middle>Don't<br>change<br>this group<br>restriction</TD>\n";
            print "<TH ALIGN=center VALIGN=middle>Remove<br>bugs<br>from this<br>group</TD>\n";
            print "<TH ALIGN=center VALIGN=middle>Add<br>bugs<br>to this<br>group</TD>\n";
            print "<TH ALIGN=left VALIGN=middle>Group name:</TD></TR>\n";
            $groupFound = 1;
          }
          # Modifying this to use radio buttons instead
          print "<TR>";
          print "<TD ALIGN=center><input type=radio name=\"bit-$bit\" value=\"-1\" checked></TD>\n";
          print "<TD ALIGN=center><input type=radio name=\"bit-$bit\" value=\"0\"></TD>\n";
          if ($isactive) {
            print "<TD ALIGN=center><input type=radio name=\"bit-$bit\" value=\"1\"></TD>\n";
          } else {
            $inactiveFound = 1;
            print "<TD>&nbsp;</TD>\n";
          }
          print "<TD>";
          if(!$isactive) {
            print "<I>";
          }
          print "$description";
          if(!$isactive) {
            print "</I>";
          }
          print "</TD></TR>\n";
        }
    }
    # Add in some blank space for legibility
    if($groupFound) {
      print "</TABLE>\n";
      if ($inactiveFound) {
        print "<FONT SIZE=\"-1\">(Note: Bugs may not be added to inactive groups (<I>italicized</I>), only removed)</FONT><BR>\n";
1506
      }
1507 1508
      print "<BR><BR>\n";
    }
1509 1510 1511 1512 1513
}




terry%netscape.com's avatar
terry%netscape.com committed
1514 1515
    # knum is which knob number we're generating, in javascript terms.

1516 1517
    my $knum = 0;
    print "
1518
<INPUT TYPE=radio NAME=knob VALUE=none CHECKED>
1519 1520
        Do nothing else<br>";
    $knum++;
1521
    if ($statushash{$::unconfirmedstate} && 1 == scalar(keys(%statushash))) {
1522 1523 1524 1525 1526
        print "
<INPUT TYPE=radio NAME=knob VALUE=confirm>
        Confirm bugs (change status to <b>NEW</b>)<br>";
    }
    $knum++;
1527
    print "
1528
<INPUT TYPE=radio NAME=knob VALUE=accept>
1529 1530 1531 1532 1533 1534
        Accept bugs (change status to <b>ASSIGNED</b>)<br>";
    $knum++;
    if (!defined $statushash{'CLOSED'} &&
        !defined $statushash{'VERIFIED'} &&
        !defined $statushash{'RESOLVED'}) {
        print "
1535
<INPUT TYPE=radio NAME=knob VALUE=clearresolution>
1536 1537 1538
        Clear the resolution<br>";
        $knum++;
        print "
1539
<INPUT TYPE=radio NAME=knob VALUE=resolve>
terry%netscape.com's avatar
terry%netscape.com committed
1540
        Resolve bugs, changing <A HREF=\"bug_status.html\">resolution</A> to
1541
        <SELECT NAME=resolution
terry%netscape.com's avatar
terry%netscape.com committed
1542
          ONCHANGE=\"document.changeform.knob\[$knum\].checked=true\">
1543 1544
          $resolution_popup</SELECT><br>";
        $knum++;
terry%netscape.com's avatar
terry%netscape.com committed
1545
    }
1546 1547 1548 1549
    if (!defined $statushash{'NEW'} &&
        !defined $statushash{'ASSIGNED'} &&
        !defined $statushash{'REOPENED'}) {
        print "
1550
<INPUT TYPE=radio NAME=knob VALUE=reopen> Reopen bugs<br>";
1551
        $knum++;
terry%netscape.com's avatar
terry%netscape.com committed
1552
    }
1553
    my @statuskeys = keys %statushash;
1554
    if (1 == @statuskeys) {
1555 1556
        if (defined $statushash{'RESOLVED'}) {
            print "
1557
<INPUT TYPE=radio NAME=knob VALUE=verify>
1558 1559
        Mark bugs as <b>VERIFIED</b><br>";
            $knum++;
terry%netscape.com's avatar
terry%netscape.com committed
1560
        }
1561 1562
        if (defined $statushash{'VERIFIED'}) {
            print "
1563
<INPUT TYPE=radio NAME=knob VALUE=close>
1564 1565
        Mark bugs as <b>CLOSED</b><br>";
            $knum++;
terry%netscape.com's avatar
terry%netscape.com committed
1566 1567
        }
    }
1568
    print "
1569
<INPUT TYPE=radio NAME=knob VALUE=reassign>
terry%netscape.com's avatar
terry%netscape.com committed
1570
        <A HREF=\"bug_status.html#assigned_to\">Reassign</A> bugs to
1571
        <INPUT NAME=assigned_to SIZE=32
terry%netscape.com's avatar
terry%netscape.com committed
1572
          ONCHANGE=\"document.changeform.knob\[$knum\].checked=true\"
1573 1574
          VALUE=\"$::COOKIE{'Bugzilla_login'}\"><br>";
    $knum++;
1575
    print "<INPUT TYPE=radio NAME=knob VALUE=reassignbycomponent>
1576 1577
          Reassign bugs to owner of selected component<br>";
    $knum++;
terry%netscape.com's avatar
terry%netscape.com committed
1578

1579
    print "
terry%netscape.com's avatar
terry%netscape.com committed
1580
<p>
1581
<font size=-1>
terry%netscape.com's avatar
terry%netscape.com committed
1582 1583 1584
To make changes to a bunch of bugs at once:
<ol>
<li> Put check boxes next to the bugs you want to change.
1585 1586
<li> Adjust above form elements.  (If the change you are making requires
       an explanation, include it in the comments box).
terry%netscape.com's avatar
terry%netscape.com committed
1587 1588
<li> Click the below \"Commit\" button.
</ol></font>
1589 1590 1591 1592 1593 1594
<INPUT TYPE=SUBMIT VALUE=Commit>";

    my $movers = Param("movers");
    $movers =~ s/\s?,\s?/|/g;
    $movers =~ s/@/\@/g;

1595 1596
    if ( Param("move-enabled")
         && (defined $::COOKIE{"Bugzilla_login"})
1597 1598 1599 1600 1601 1602 1603
         && ($::COOKIE{"Bugzilla_login"} =~ /($movers)/) ){
      print "<P>";
      print "<INPUT TYPE=\"SUBMIT\" NAME=\"action\" VALUE=\"";
      print Param("move-button-text") . "\">";
    }

    print "</FORM><hr>\n";
terry%netscape.com's avatar
terry%netscape.com committed
1604 1605 1606
}


1607
if ($count > 0) {
1608 1609 1610
    print "<FORM METHOD=POST ACTION=\"long_list.cgi\">
<INPUT TYPE=HIDDEN NAME=buglist VALUE=$buglist>
<INPUT TYPE=SUBMIT VALUE=\"Long Format\">
1611 1612 1613 1614 1615
<NOBR><A HREF=\"query.cgi\">Query Page</A></NOBR>
&nbsp;&nbsp;
<NOBR><A HREF=\"enter_bug.cgi\">Enter New Bug</A></NOBR>
&nbsp;&nbsp;
<NOBR><A HREF=\"colchange.cgi?$::buffer\">Change columns</A></NOBR>";
1616
    if (!$dotweak && $count > 1 && UserInGroup("editbugs")) {
1617 1618 1619
        print "&nbsp;&nbsp;\n";
        print "<NOBR><A HREF=\"buglist.cgi?$fields$orderpart&tweak=1\">";
        print "Change several bugs at once</A></NOBR>\n";
terry%netscape.com's avatar
terry%netscape.com committed
1620
    }
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
    my @owners = sort(keys(%ownerhash));
    if (@owners > 1 && UserInGroup("editbugs")) {
        my $suffix = Param('emailsuffix');
        if ($suffix ne "") {
            map(s/$/$suffix/, @owners);
        }
        my $list = join(',', @owners);
        print qq{&nbsp;&nbsp;\n};
        print qq{<NOBR><A HREF="mailto:$list">Send mail to bug owners</A></NOBR>\n};
    }
1631 1632
    print qq{&nbsp;&nbsp;\n};
    print qq{<NOBR><A HREF="query.cgi?$::buffer">Edit this query</A></NOBR>\n};
1633

1634
    print "</FORM>\n";
terry%netscape.com's avatar
terry%netscape.com committed
1635
}
1636

1637 1638 1639 1640 1641 1642
# 2001-06-20, myk@mozilla.org, bug 47914:
# Switch back from the shadow database to the regular database 
# so that PutFooter() can determine the current user even if
# the "logincookies" table is corrupted in the shadow database.
SendSQL("USE $::db_name");

1643 1644
PutFooter();

1645 1646 1647
if ($serverpush) {
    print "\n--thisrandomstring--\n";
}