summaryrefslogtreecommitdiffstats
path: root/showdependencygraph.cgi
blob: 7c59a26db84b402d782f7d8c5ef3b153bc36277a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bonsaitools/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# 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 the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Terry Weissman <terry@mozilla.org>

use diagnostics;
use strict;

use lib qw(.);

require "CGI.pl";

ConnectToDatabase();

quietly_check_login();

# More warning suppression silliness.
$::userid = $::userid;
$::usergroupset = $::usergroupset;

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

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

######################################################################
# End Data/Security Validation
######################################################################

my $id = $::FORM{'id'};

my $urlbase = Param("urlbase");

my %seen;
my %edgesdone;

sub AddLink {
    my ($blocked, $dependson) = (@_);
    my $key = "$blocked,$dependson";
    if (!exists $edgesdone{$key}) {
        $edgesdone{$key} = 1;
        print DOT "$blocked -> $dependson\n";
        $seen{$blocked} = 1;
        $seen{$dependson} = 1;
    }
}


print "Content-type: text/html\n\n";
PutHeader("Dependency graph", "Dependency graph", $id);

$::FORM{'rankdir'} = "LR" if !defined $::FORM{'rankdir'};


if (defined $id) {
    mkdir("data/webdot", 0777);

    my $filename = "data/webdot/$$.dot";
    open(DOT, ">$filename") || die "Can't create $filename";
    print DOT "digraph G {";
    print DOT qq{
graph [URL="${urlbase}query.cgi", rankdir=$::FORM{'rankdir'}, size="64,64"]
node [URL="${urlbase}show_bug.cgi?id=\\N", style=filled, color=lightgrey]
};
    my %baselist;

    foreach my $i (split('[\s,]+', $::FORM{'id'})) {
        $i = trim($i);
        if ($i ne "") {
            $baselist{$i} = 1;
        }
    }
    my @basearray = keys(%baselist);

    if ($::FORM{'doall'}) {
        SendSQL("select blocked, dependson from dependencies");
        
        while (MoreSQLData()) {
            my ($blocked, $dependson) = (FetchSQLData());
            AddLink($blocked, $dependson);
        }
    } else {
        my @stack = @basearray;
        while (@stack) {
            my $id = shift @stack;
            SendSQL("select blocked, dependson from dependencies where blocked = $id or dependson = $id");
            while (MoreSQLData()) {
                my ($blocked, $dependson) = (FetchSQLData());
                if ($blocked != $id && !exists $seen{$blocked}) {
                    push @stack, $blocked;
                }
                if ($dependson != $id && !exists $seen{$dependson}) {
                    push @stack, $dependson;
                }
                AddLink($blocked, $dependson);
            }
        }
    }

    foreach my $k (@basearray) {
        $seen{$k} = 1;
    }
    foreach my $k (keys(%seen)) {
        my $summary = "";
        my $stat;
        if ($::FORM{'showsummary'}) {
            SendSQL(SelectVisible("SELECT bug_status, short_desc FROM bugs " .
                                  "WHERE bugs.bug_id = $k",
                                  $::userid,
                                  $::usergroupset));
            ($stat, $summary) = (FetchSQLData());
            $stat = "NEW" if !defined $stat;
            $summary = "" if !defined $summary;
        } else {
            SendSQL("select bug_status from bugs where bug_id = $k");
            $stat = FetchOneColumn();
        }
        my @params;
#        print DOT "$k [URL" . qq{="${urlbase}show_bug.cgi?id=$k"};
        if ($summary ne "") {
            $summary =~ s/([\\\"])/\\$1/g;
            push(@params, qq{label="$k\\n$summary"});
        }
        if (exists $baselist{$k}) {
            push(@params, "shape=box");
        }
        my $opened = ($stat eq "NEW" || $stat eq "ASSIGNED" ||
                      $stat eq "REOPENED");
        if ($opened) {
            push(@params, "color=green");
        }
        if (@params) {
            print DOT "$k [" . join(',', @params) . "]\n";
        } else {
            print DOT "$k\n";
        }
    }


    print DOT "}\n";
    close DOT;
    chmod 0777, $filename;
    
    my $webdotbase = Param('webdotbase');
    if($webdotbase =~ /^https?:/) {
        my $url = PerformSubsts(Param("webdotbase")) . $filename;
        print qq{<a href="$url.map"> <img src="$url.gif" ismap> </a><hr>\n};
    } else {
        my $pngfilename = "data/webdot/$$.png";
        my $mapfilename = "data/webdot/$$.map";
        system("$webdotbase","-Tpng","-o","$pngfilename","$filename");
        system("$webdotbase","-Timap","-o","$mapfilename","$filename");
        print qq{<a href="$mapfilename"> <img src="$pngfilename" ismap> </a><hr>\n};
    }
  
    # Cleanup any old .dot files created from previous runs.
    my $since = time() - 24 * 60 * 60;
    foreach my $f (glob("data/webdot/*.dot data/webdot/*.png data/webdot/*.map")) {
        # Here we are deleting all old files. All entries are from the
        # data/webdot/ directory. Since we're deleting the file (not following
        # symlinks), this can't escape to delete anything it shouldn't
        trick_taint($f);
        if (ModTime($f) < $since) {
            unlink $f;
        }
    }
} else {
    $::FORM{'id'} = "";
    $::FORM{'doall'} = 0;
    $::FORM{'showsummary'} = 0;
}    

print "
<form>
<table>
<tr>
<th align=right>Bug numbers:</th>
<td><input name=id value=\"" . value_quote($::FORM{'id'}) . "\"></td>
<td><input type=checkbox name=doall" . ($::FORM{'doall'} ? " checked" : "") .
">Show <b>every</b> bug in the system with 
dependencies</td>
</tr>
<tr><td colspan=3><input type=checkbox name=showsummary" .
($::FORM{'showsummary'} ? " checked" : "") . ">Show the summary of all bugs
</tr>
<tr><td colspan=3><select name=rankdir>
<option value=\"TB\"" . ($::FORM{'rankdir'} eq 'TB' ? 'selected' : '') .
">Orient top-to-bottom
<option value=\"LR\"" . ($::FORM{'rankdir'} eq 'LR' ? 'selected' : '') .
">Orient left-to-right
</select></td></tr>
</table>
<input type=submit value=\"Submit\">
</form>
 ";

PutFooter();