showdependencytree.cgi 13 KB
Newer Older
1
#!/usr/bonsaitools/bin/perl -wT
2 3
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
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.
#
14
# The Original Code is the Bugzilla Bug Tracking System.
15
#
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.
#
21
# Contributor(s): Terry Weissman <terry@mozilla.org>
22 23
#                 Andreas Franke <afranke@mathweb.org>
#                 Christian Reis <kiko@async.com.br>
24 25 26 27

use diagnostics;
use strict;

28
use lib qw(.);
29 30 31 32
require "CGI.pl";

use vars %::FORM;

33 34 35 36
ConnectToDatabase();

quietly_check_login();

37 38 39
# More warning suppression silliness.
$::userid = $::userid;
$::usergroupset = $::usergroupset;
40 41 42 43 44 45

######################################################################
# Begin Data/Security Validation
######################################################################

# Make sure the bug ID is a positive integer representing an existing
46
# bug that the user is authorized to access
47 48
ValidateBugID($::FORM{'id'});

49 50 51 52 53 54 55
my $id = $::FORM{'id'};
my $hide_resolved = $::FORM{'hide_resolved'} || 0;
my $maxdepth = $::FORM{'maxdepth'} || 0;

if ($maxdepth !~ /^\d+$/) { $maxdepth = 0 };
if ($hide_resolved !~ /^\d+$/ || $hide_resolved != 1) { $hide_resolved = 0 };

56 57 58
######################################################################
# End Data/Security Validation
######################################################################
59

60 61
#
# Globals
62

63
# A hash to count visited bugs, and also to avoid processing repeated bugs
64 65
my %seen;

66 67 68
# A hash to keep track of the bugs we print for the 'as buglist' links.
my %printed;

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
# HTML output generated in the parse of the dependency tree. This is a
# global only to avoid excessive complication in the recursion invocation
my $html;

# Saves the largest of the two actual depths of the trees
my $realdepth = 0;

# The scriptname for use as FORM ACTION.
my $scriptname = $::ENV{'SCRIPT_NAME'}; # showdependencytree.cgi

#
# Functions

# DumpKids recurses through the bug hierarchy starting at bug i, and
# appends the bug information found to the html global variable. The
# parameters are not straightforward, so look at the examples.
#
# DumpKids(i, target [, depth])
#
# Params
#   i: The bug id to analyze
#   target: The type we are looking for; either "blocked" or "dependson"
# Optional
#   depth: The current dependency depth we are analyzing, used during
#          recursion
# Globals Modified
#   html: Bug descriptions are appended here
#   realdepth: We set the maximum depth of recursion reached
#   seen: We store the bugs analyzed so far
98
#   printed: We store those bugs we actually print, for the "buglist" link
99 100 101 102 103 104 105 106 107 108
# Globals Referenced
#   maxdepth
#   hide_resolved
#
# Examples:
#   DumpKids(163, "blocked");
#       will look for bugs that depend on bug 163
#   DumpKids(163, "dependson");
#       will look for bugs on which bug 163 depends

109
sub DumpKids {
110 111 112 113 114
    my ($i, $target, $depth) = (@_);
    my $bgcolor = "#d9d9d9";
    my $fgcolor = "#000000";
    my $me;
    if (! defined $depth) { $depth = 1; }
115

116 117 118 119 120 121
    if ($target eq "blocked") {
        $me = "dependson";
    } else {
        $me = "blocked";
    }
    SendSQL("select $target from dependencies where $me=$i order by $target");
122 123 124 125 126
    my @list;
    while (MoreSQLData()) {
        push(@list, FetchOneColumn());
    }
    if (@list) {
127
        my $list_started = 0;
128
        foreach my $kid (@list) {
129 130 131
            my ($bugid, $stat, $milestone) = ("", "", "");
            my ($userid, $short_desc) = ("", "");
            if (Param('usetargetmilestone')) {
132
                SendSQL(SelectVisible("select bugs.bug_id, bug_status, target_milestone, assigned_to, short_desc from bugs where bugs.bug_id = $kid", $::userid, $::usergroupset));
133 134
                ($bugid, $stat, $milestone, $userid, $short_desc) = (FetchSQLData());
            } else {
135
                SendSQL(SelectVisible("select bugs.bug_id, bug_status, assigned_to, short_desc from bugs where bugs.bug_id = $kid", $::userid, $::usergroupset));
136 137 138
                ($bugid, $stat, $userid, $short_desc) = (FetchSQLData());

            }
139 140 141 142 143 144 145 146 147 148 149 150 151 152
            if (! defined $bugid) { next; }
            my $opened = IsOpenedState($stat);
            if ($hide_resolved && ! $opened) { next; }

            # If we specify a maximum depth, we hide the output when
            # that depth has occured, but continue recursing so we know
            # the real maximum depth of the tree.
            if (! $maxdepth || $depth <= $maxdepth) {
                if (! $list_started) { $html .= "<ul>"; $list_started = 1 }
                $html .= "<li>"; 
                if (! $opened) {
                    $html .= qq|<strike><span style="color: $fgcolor; background-color: $bgcolor;">
                    |;
                }
153 154 155 156 157 158 159 160

                if (exists $seen{$kid}) {     
                    $short_desc = "&lt;<em>This bug appears elsewhere in this tree</em>&gt;";
                }
                else {
                    $short_desc = html_quote($short_desc);
                }

161 162 163 164 165 166 167 168 169 170 171 172 173
                SendSQL("select login_name from profiles where userid = $userid");
                my ($owner) = (FetchSQLData());
                if ((Param('usetargetmilestone')) && ($milestone)) {
                    $html .= qq|
                    <a href="show_bug.cgi?id=$kid">$kid [$milestone, $owner]
                        - $short_desc.</a>
                    |;  
                } else {
                    $html .= qq|
                    <a href="show_bug.cgi?id=$kid">$kid [$owner] -
                        $short_desc.</a>\n|; 
                }
                if (! $opened) { $html .= "</span></strike>"; }
174 175
                
                $printed{$kid} = 1;
176 177 178 179
            } # End hideable output

            # Store the maximum depth so far
            $realdepth = $realdepth < $depth ? $depth : $realdepth;
180 181 182 183
            if (!(exists $seen{$kid})) { 
                $seen{$kid} = 1;
                DumpKids($kid, $target, $depth + 1);
            }
184
        }
185
        if ($list_started) { $html .= "</ul>"; }
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
# makeTreeHTML calls DumpKids and generates the HTML output for a
# dependency/blocker section.
#
# makeTreeHTML(i, linked_id, target);
#
# Params
#   i: Bug id
#   linked_id: Linkified bug_id used to linkify the bug
#   target: The type we are looking for; either "blocked" or "dependson"
# Globals modified
#   html [Also modified in our call to DumpKids]
# Globals referenced
#   seen [Also modified by DumpKids]
#   maxdepth
#   realdepth
#
# Example:
#   $depend_html = makeTreeHTML(83058, <A HREF="...">83058</A>, "dependson");
#       Will generate HTML for bugs that depend on bug 83058

sub makeTreeHTML {
    my ($i, $linked_id, $target) = @_;

    # Clean up globals for this run
    $html = "";
    %seen = ();
215
    %printed = ();
216 217 218 219 220 221 222 223

    DumpKids($i, $target);
    my $tmphtml = $html;

    # Output correct heading
    $html = "<h3>Bugs that bug $linked_id ".($target eq "blocked" ? 
        "blocks" : "depends on");

224 225 226 227 228
    if ((scalar keys %printed) > 0) {
        $html .= ' (<a href="buglist.cgi?bug_id=' . join(',', keys %printed) . 
                 '">view as bug list</a>)';        
    }
    
229 230 231 232 233 234 235 236 237 238 239
    # Provide feedback for omitted bugs
    if ($maxdepth || $hide_resolved) {
        $html .= " <small><b>(Only ";
        if ($hide_resolved) { $html .= "open "; }
        $html .= "bugs ";
        if ($maxdepth) { $html .= "whose depth is less than $maxdepth "; }
        $html .= "will be shown)</b></small>";
    }

    $html .= "</h3>";
    $html .= $tmphtml;
240

241
    # If no bugs were found, say so
242
    if ((scalar keys %printed) == 0) {
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
        $html .= "&nbsp;&nbsp;&nbsp;&nbsp;None<p>\n";
    }

    return $html;
}

# Draw the actual form controls that make up the hide/show resolved and
# depth control toolbar. 
#
# drawDepForm()
#
# Params
#   none
# Globals modified
#   none
# Globals referenced
#   hide_resolved
#   maxdepth
#   realdepth

sub drawDepForm {
    my $bgcolor = "#d0d0d0";
    my ($hide_msg, $hide_input);

    # Set the text and action for the hide resolved button.
    if ($hide_resolved) {
        $hide_input = '<input type="hidden" name="hide_resolved" value="0">';
        $hide_msg = "Show Resolved";
    } else {
        $hide_input = '<input type="hidden" name="hide_resolved" value="1">';
        $hide_msg = "Hide Resolved";
    }

    print qq|
    <table cellpadding="3" border="0" cellspacing="0">
    <tr>

    <!-- Hide/show resolved button 
         Swaps text depending on the state of hide_resolved -->
    <td bgcolor="$bgcolor" align="center"> 
    <form method="get" action="$scriptname" 
        style="display: inline; margin: 0px;">
    <input name="id" type="hidden" value="$id">
    | . ( $maxdepth ? 
        qq|<input name="maxdepth" type="hidden" value="$maxdepth">|
        : "" ) . qq|
    $hide_input
    <input type="submit" value="$hide_msg">

    </form>
    </td>
    <td bgcolor="$bgcolor">

    <!-- depth section -->
297
    Max Depth:
298
    
299 300 301
    </td>
    <td bgcolor="$bgcolor">

302 303 304 305 306
    </td>
    <td bgcolor="$bgcolor">
    <form method="get" action="$scriptname" 
        style="display: inline; margin: 0px;">
    
307 308 309
    <!-- set to one form -->
    <input type="submit" value="&nbsp;1&nbsp;" | 
        . ( $realdepth < 2 || $maxdepth == 1 ? "disabled" : "" ) . qq|>
310
    <input name="id" type="hidden" value="$id">
311
    <input name="maxdepth" type="hidden" value="1">
312 313 314 315
    <input name="hide_resolved" type="hidden" value="$hide_resolved">
    </form>
    </td>
    <td bgcolor="$bgcolor">
316 317
    <form method="get" action="$scriptname" 
        style="display: inline; margin: 0px;">
318

319 320 321 322 323 324 325 326 327 328 329 330
    <!-- Minus one form  
         Allow subtracting only when realdepth and maxdepth > 1 -->
    <input name="id" type="hidden" value="$id">
    <input name="maxdepth" type="hidden" value="| 
        .  ( $maxdepth == 1 ? 1 : ( $maxdepth ? 
            $maxdepth-1 : $realdepth-1 ) ) . qq|">
    <input name="hide_resolved" type="hidden" value="$hide_resolved">
    <input type="submit" value="&nbsp;&lt;&nbsp;" | 
        . ( $realdepth < 2 || ( $maxdepth && $maxdepth < 2 ) ?
            "disabled" : "" ) . qq|>

    </form>
331 332 333 334 335
    </td>
    <td bgcolor="$bgcolor">
    <form method="get" action="$scriptname" 
        style="display: inline; margin: 0px;">

336
    <!-- Limit entry form: the button can not do anything when total depth
337 338 339 340 341 342
         is less than two, so disable it -->
    <input name="maxdepth" size="4" maxlength="4" value="| 
        . ( $maxdepth > 0 ? $maxdepth : "" ) . qq|">
    <input name="id" type="hidden" value="$id">
    <input name="hide_resolved" type="hidden"
        value="$hide_resolved">
343
    <noscript>
344 345
    <input type="submit" value="Change" | 
        . ( $realdepth < 2 ? "disabled" : "" ) . qq|>
346
    </noscript>
347 348 349 350 351 352 353

    </form>
    </td>
    <td bgcolor="$bgcolor">
    <form method="get" action="$scriptname" 
        style="display: inline; margin: 0px;">
    
354
    <!-- plus one form 
355 356 357 358 359 360
        Disable button if total depth < 2, or if depth set to unlimited -->
    <input name="id" type="hidden" value="$id">
    | . ( $maxdepth ? qq|
    <input name="maxdepth" type="hidden" value="|.($maxdepth+1).qq|">| : "" ) 
    . qq| 
    <input name="hide_resolved" type="hidden" value="$hide_resolved">
361
    <input type="submit" value="&nbsp;&gt;&nbsp;" | 
362 363 364 365 366 367 368 369 370
        .  ( $realdepth < 2 || ! $maxdepth || $maxdepth >= $realdepth ?
            "disabled" : "" ) . qq|>

    </form>
    </td>
    <td bgcolor="$bgcolor">
    <form method="get" action="$scriptname" 
        style="display: inline; margin: 0px;">
    
371
    <!-- Unlimited button -->
372 373
    <input name="id" type="hidden" value="$id">
    <input name="hide_resolved" type="hidden" value="$hide_resolved">
374 375
    <input type="submit" value="&nbsp;Unlimited&nbsp;">

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
    </form>
    </td>
    </tr></table>
    |;
}

######################################################################
# Main Section
######################################################################

my $linked_id = qq|<a href="show_bug.cgi?id=$id">$id</a>|;

# Start the tree walk and save results. The tree walk generates HTML but
# needs to be called before the page output starts so we have the
# realdepth, which is necessary for generating the control toolbar.

# Get bugs we depend on
my $depend_html = makeTreeHTML($id, $linked_id, "dependson");

my $tmpdepth = $realdepth; 
$realdepth = 0;

# Get bugs we block
my $block_html = makeTreeHTML($id, $linked_id, "blocked");

# Select maximum depth found for use in the toolbar
$realdepth = $realdepth < $tmpdepth ? $tmpdepth : $realdepth;

#
# Actual page output happens here

print "Content-type: text/html\n\n";
PutHeader("Dependency tree for Bug $id", "Dependency tree for Bug $linked_id");
drawDepForm();
print $depend_html;
print $block_html;
drawDepForm();
413
PutFooter();