-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathciChart.php
More file actions
193 lines (154 loc) · 5.81 KB
/
ciChart.php
File metadata and controls
193 lines (154 loc) · 5.81 KB
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
<?php
require_once 'Image/GraphViz.php';
/** Chart creation object. Walks around the entire dependency graph
and renders the parts it likes. */
class ciChart
{
var $root;
var $level_count;
var $level_width;
var $highlight;
var $steps;
var $reverse;
/**
Creates a new graph object with the settings that we use
*/
function graph($name) {
return new Image_GraphViz(true,
array('nodesep'=>'0.1',
'fontname'=>'sans-serif',
'bgcolor'=>'white'),
$name);
}
/**
Renders the graph to 'standard output'.
*/
function imageKludge($graph, $format)
{
if( $format=='svg') {
/* Ugly, ugly workaround for graphviz/firefox bug. It
seems that font size requests are ignored by some
graphviz versions, and that firefox does not understand
font sizes with no unit specified. This string manually
overrides the font style in the svg, so long as the exact
formating of the font specification doesn't change in a
future graphviz version...
Not sure if Firefox or Graphviz is more to blame here,
but I could not find any other way to fix it than this.
Let's hope nobody creates a graph containing css
font-size markup as node text, or this will confuse the
hell out of somebody..
*/
$res = $graph->fetch($format);
$res = preg_replace('/font-size:[ 0-9.]*;/','font-size:8pt;', $res);
$res = preg_replace('/width=["\']([0-9]*)pt["\']/','width="\1px"', $res);
$res = preg_replace('/height=["\']([0-9]*)pt["\']/','height="\1px"', $res);
return $res;
} else {
return $graph->fetch($format);
}
}
function __construct($root, $reverse, $highlight, $steps)
{
$this->root = $root;
$this->reverse = $reverse;
$this->highlight = $highlight;
$this->steps = $steps;
}
function getName()
{
if($this->root == 'full') {
$name = "all".($this->reverse?'_reverse':'');
} else {
$name = "ci_".$this->root->id.($this->reverse?'_reverse':'');
}
$revision_id = param('revision_id');
if ($revision_id) {
$name .= "_revision" . $revision_id;
}
return $name;
}
function render($format)
{
$name = $this->getName();
$graph = self::graph($name);
if($this->root == 'full') {
$this->renderAll($graph);
} else {
$this->renderInternal($graph, $this->root);
}
$key = sha1(file_get_contents($graph->saveParsedGraph()).$format);
$res = CiGraphCache::get($key);
if ($res) {
return ($format=='png'?base64_decode($res):$res);
}
$res = self::imageKludge($graph, $format);
CiGraphCache::set($key, ($format=='png'?base64_encode($res):$res));
return $res;
}
function renderInternal($graph, $node)
{
$this->renderNode($graph, $node, array(), true);
}
function renderAll($graph)
{
$done = array();
$ci_list = ci::fetch();
foreach($ci_list as $ci) {
$this->renderNode($graph, $ci, $done, false);
}
}
function renderNode($graph, $node, $done, $is_root, $depth=0)
{
if (array_key_exists($node->id, $done)) {
return;
}
$max_depth = Property::get("chart.maxDepth");
if ($max_depth > 0 && $depth >= $max_depth)
{
return;
}
$revision_id = param('revision_id');
$revision_str = $revision_id !== null? "&revision_id=$revision_id":"";
util::$path = Property::get("core.baseUrl","");
$graph->addNode($node->getDescription(true),
array('URL' => makeUrl(array('controller'=>'ci','task'=>'view','id'=>$node->id,'revision_id'=>$revision_id)),
'target' => '_parent',
'shape' => ciType::getShape($node->ci_type_id),
'fontsize' => '8pt',
'fontname' => 'sans-serif', // Re-add font name attribute on every node, since GraphViz seems to ignore the main graph attribute, even though the docs say it should be inherited.
'label'=>str_replace(' ','\n',$node->getDescription(true)),
'color' => ($is_root?'green':(array_search($node->id, $this->highlight)?'green':'black'))
)
);
$func = (!$this->reverse)?"getDirectDependencies":"getDirectDependants";
$children = $node->$func();
foreach($children as $child) {
$done[$node->id] = $node;
$dt = $this->reverse ? $child->getDependencyType($node) : $node->getDependencyType($child);
$arrow = $dt->isDirected()?($this->reverse?'normal':'inv'):'none';
$color = $dt->color;
if($color == "invisible")
{
continue;
}
$this->renderNode($graph, $child, $done, false, $depth+1);
$graph->addEdge(array($node->getDescription(true) => $child->getDescription(true)),
array ( 'arrowhead'=>$arrow,
'color' => $color ));
}
}
/**
Create a legend of all node types.
*/
function renderLegend($format)
{
$graph = self::graph();
foreach(ciType::getTypes() as $type_id => $type_name) {
$graph->addNode($type_name,array('shape'=>ciType::getShape($type_id),
'fontsize' => '10',));
}
return ciChart::imageKludge($graph, $format);
}
}
?>