forked from kjmkznr/blockdiag-mediawiki-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NwdiagResultPrinter.php
247 lines (213 loc) · 7.25 KB
/
NwdiagResultPrinter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/**
* Blockdiag Extension for MediaWiki
*
* @since 1.1.0
* @version 1.1.0
*
* @author Gilles Magalhaes <gilles@tentwentyfour.lu>
*
**/
class NwdiagResultPrinter extends SMWResultPrinter
{
public function getName()
{
return wfMessage('nwdiag')->text();
}
protected function getResultText(SMWQueryResult $result, $outputMode)
{
$data = $this->getResultData($result);
if (empty($data)) {
return $result->addErrors(
[
wfMessage('srf-no-results')->inContentLanguage()->text(),
]
);
} else {
return $this->getFormatOutput($data);
}
}
/**
* Extracts the data we need to build a networkdiag DSL from the
* semantic result
*
* @param SMWQueryResult $result Result of the query
*
* @return array Array of data to be fed to generateDiagCode()
*/
protected function getResultData(SMWQueryResult $result)
{
$data = array();
while ($rows = $result->getNext()) {
foreach ($rows as $field) {
$propertyLabel = $field->getPrintRequest()->getLabel();
$server_name = $field->getResultSubject()->getTitle();
$server_name = strtolower($server_name->getFullText());
while (($dataValue = $field->getNextDataValue()) !== false) {
if ($propertyLabel === "ipv4") {
$ip_address = $dataValue->getWikiValue();
$data[$server_name]["ip_address"] = $ip_address;
} elseif ($propertyLabel === "fqdn") {
$fqdn = strtolower($dataValue->getWikiValue());
if (isset($this->params['domain'])) {
$fqdn = str_replace($this->params['domain'], '', $fqdn);
}
$domain_parts = explode('.', $fqdn);
$hostname = $domain_parts[0];
$group ='';
if (count($domain_parts) >= 1) {
$group = $domain_parts[1];
$node_host = sprintf('%s.%s', $hostname, $group);
}
$data[$server_name]["fqdn"] = $fqdn;
$data[$server_name]["node_host"] = !empty($group) ? $node_host : $hostname;
$data[$server_name]["group"] = $group;
}
}
// If a row does not have a value for the FQDN property label, it will not be returned
// by $field->getNextDataValue()
if (!isset($data[$server_name]["fqdn"])) {
$data[$server_name]["fqdn"] = $server_name;
$data[$server_name]["node_host"] = $server_name;
$data[$server_name]["group"] = $server_name;
}
}
}
return $data;
}
/**
* @see SMWResultPrinter::getParamDefinitions
*
* @since 1.1.0
*
* @param $definitions array of IParamDefinition
*
* @return array of IParamDefinition|array
*/
public function getParamDefinitions(array $definitions)
{
$params = parent::getParamDefinitions($definitions);
$params['gateway'] = [
'message'=>'The node that will be used as a gateway',
'default'=>'',
];
$params['domain'] = [
'message'=>'Specify which part of the fqdn is the domain name and should be stripped',
'default'=>'localhost.lan',
];
return $params;
}
/**
* Prepare data for the output
*
* @since 1.1.2
*
* @param array $data
* @param array $options
*
* @return string
*/
protected function getFormatOutput($data)
{
$this->isHTML = true;
$wgBlockdiagDirectory = $GLOBALS['wgUploadDirectory'] . "/blockdiag";
$wgBlockdiagUrl = $GLOBALS['wgUploadPath'] . "/blockdiag";
$nwdiagCode = $this->generateDiagCode($data, $this->params['gateway']);
$newBlockdiag = new BlockdiagGenerator(
$wgBlockdiagDirectory,
$wgBlockdiagUrl,
$GLOBALS['wgTmpDirectory'],
$nwdiagCode,
$GLOBALS['wgBlockdiagPath']
);
return $newBlockdiag->showImage();
}
/**
* Generate the blockdiag DSL to be rendered by our BlockdiagGenerator
*
* @param array $nodes Array of nodes to be added to the diagram
* @param string $gateway Node that will be used as a gateway
*
* @return string String to be rendered
*/
private function generateDiagCode($nodes, $gateway)
{
$diagCode ='nwdiag {'. PHP_EOL;
// draw gateway
$match = $this->findGateway($nodes, $gateway);
if ($match != "") {
$diagCode .= "\t" . $match[1] . " [shape = ellipse];" . PHP_EOL;
$diagCode .= "\t" . $match[1] . " -- " . implode(".", $match) . "\n\n";
}
// draw network
$diagCode .= "\tnetwork vpn {\n";
foreach ($nodes as $node) {
$ip = $node['ip_address'];
$node_host = $node['node_host'];
$diagCode .= "\t\t\"$node_host\" [address=\"$ip\"];" . PHP_EOL;
}
$diagCode .= "\t}" . PHP_EOL;
// draw groups
$groups = $this->findNodeGroups($nodes);
foreach (array_keys($groups) as $group) {
// don't create a group if there is only one node inside it.
if ($group ==''|| $groups[$group] == 1) {
continue;
}
$diagCode .= "\tgroup $group {\n";
$diagCode .= "\t\tcolor = \"" . $this->randomColor() . "\";\n";
$diagCode .= "\n";
foreach ($nodes as $node) {
if ($node['group'] === $group) {
$diagCode .= "\t\t" . $node['node_host'] . ";" . PHP_EOL;
}
}
$diagCode .= "\t}\n\n";
}
$diagCode .= "}\n";
return $diagCode;
}
/**
* Walk through the $nodes array to find all groups with
* @param [type] $nodes [description]
* @return [type] [description]
*/
private function findNodeGroups($nodes)
{
$groups = array();
foreach ($nodes as $node) {
$nodeGroup = $node['group'];
if (!array_key_exists($nodeGroup, $groups)) {
$groups[$nodeGroup] = 1;
} else {
$groups[$nodeGroup]++;
}
}
return $groups;
}
private function findGateway($nodes, $gateway)
{
$match = null;
foreach ($nodes as $node) {
$node_host = $node['node_host'];
if (strpos($node_host, $gateway) !== false) {
$match = explode(".", $node_host);
break;
}
}
return $match;
}
/**
* Generate a random, but light color
*
* @return string Hexadecimal rgb color definition
*/
public function randomColor()
{
$color ='#';
for ($i = 0; $i <= 2; $i++) {
$color .= dechex(mt_rand(0xAA, 0xFF));
}
return $color;
}
}