1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SRF\Graph; |
4
|
|
|
|
5
|
|
|
use SMW\ResultPrinter; |
6
|
|
|
use SMWQueryResult; |
7
|
|
|
use SMWWikiPageValue; |
8
|
|
|
use GraphViz; |
9
|
|
|
use Html; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* SMW result printer for graphs using graphViz. |
13
|
|
|
* In order to use this printer you need to have both |
14
|
|
|
* the graphViz library installed on your system and |
15
|
|
|
* have the graphViz MediaWiki extension installed. |
16
|
|
|
* |
17
|
|
|
* @file SRF_Graph.php |
18
|
|
|
* @ingroup SemanticResultFormats |
19
|
|
|
* |
20
|
|
|
* @licence GNU GPL v2+ |
21
|
|
|
* @author Frank Dengler |
22
|
|
|
* @author Jeroen De Dauw < [email protected] > |
23
|
|
|
* @author Sebastian Schmid |
24
|
|
|
*/ |
25
|
|
|
class GraphPrinter extends ResultPrinter { |
26
|
|
|
|
27
|
|
|
const NODELABEL_DISPLAYTITLE = 'displaytitle'; |
28
|
|
|
public static $NODE_LABELS = [ |
29
|
|
|
self::NODELABEL_DISPLAYTITLE, |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
public static $NODE_SHAPES = [ |
33
|
|
|
'box', |
34
|
|
|
'box3d', |
35
|
|
|
'circle', |
36
|
|
|
'component', |
37
|
|
|
'diamond', |
38
|
|
|
'doublecircle', |
39
|
|
|
'doubleoctagon', |
40
|
|
|
'egg', |
41
|
|
|
'ellipse', |
42
|
|
|
'folder', |
43
|
|
|
'hexagon', |
44
|
|
|
'house', |
45
|
|
|
'invhouse', |
46
|
|
|
'invtrapezium', |
47
|
|
|
'invtriangle', |
48
|
|
|
'Mcircle', |
49
|
|
|
'Mdiamond', |
50
|
|
|
'Msquare', |
51
|
|
|
'none', |
52
|
|
|
'note', |
53
|
|
|
'octagon', |
54
|
|
|
'parallelogram', |
55
|
|
|
'pentagon ', |
56
|
|
|
'plaintext', |
57
|
|
|
'point', |
58
|
|
|
'polygon', |
59
|
|
|
'rect', |
60
|
|
|
'rectangle', |
61
|
|
|
'septagon', |
62
|
|
|
'square', |
63
|
|
|
'tab', |
64
|
|
|
'trapezium', |
65
|
|
|
'triangle', |
66
|
|
|
'tripleoctagon', |
67
|
|
|
]; |
68
|
|
|
private $nodes = []; |
69
|
|
|
private $options = []; |
70
|
|
|
|
71
|
|
|
public function getName() { |
72
|
|
|
return $this->msg( 'srf-printername-graph' )->text(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @see SMWResultPrinter::handleParameters() |
78
|
|
|
*/ |
79
|
|
|
protected function handleParameters( array $params, $outputmode ) { |
80
|
|
|
parent::handleParameters( $params, $outputmode ); |
81
|
|
|
|
82
|
|
|
$this->options = [ |
83
|
|
|
'graphName' => trim( $params['graphname'] ), |
84
|
|
|
'graphSize' => trim( $params['graphsize'] ), |
85
|
|
|
'nodeShape' => $params['nodeshape'], |
86
|
|
|
'nodeLabel' => $params['nodelabel'], |
87
|
|
|
'rankDir' => strtoupper( trim( $params['arrowdirection'] ) ), |
88
|
|
|
'wordWrapLimit' => $params['wordwraplimit'], |
89
|
|
|
'parentRelation' => strtolower( trim( $params['relation'] ) ) == 'parent', |
90
|
|
|
'enableGraphLink' => $params['graphlink'], |
91
|
|
|
'showGraphLabel' => $params['graphlabel'], |
92
|
|
|
'showGraphColor' => $params['graphcolor'], |
93
|
|
|
'showGraphLegend' => $params['graphlegend'] |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param SMWQueryResult $res |
100
|
|
|
* @param $outputmode |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
protected function getResultText( SMWQueryResult $res, $outputmode ) { |
104
|
|
|
if ( !class_exists( 'GraphViz' ) |
105
|
|
|
&& !class_exists( '\\MediaWiki\\Extension\\GraphViz\\GraphViz' ) |
106
|
|
|
) { |
107
|
|
|
wfWarn( 'The SRF Graph printer needs the GraphViz extension to be installed.' ); |
108
|
|
|
return ''; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// iterate query result and create SRF\GraphNodes |
112
|
|
|
while ( $row = $res->getNext() ) { |
113
|
|
|
$this->processResultRow( $row ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// use GraphFormater to build the graph |
117
|
|
|
$graphFormatter = new GraphFormatter( $this->options ); |
118
|
|
|
$graphFormatter->buildGraph( $this->nodes ); |
119
|
|
|
|
120
|
|
|
// Calls graphvizParserHook function from MediaWiki GraphViz extension |
121
|
|
|
$result = $GLOBALS['wgParser']->recursiveTagParse( "<graphviz>" . $graphFormatter->getGraph |
122
|
|
|
() . "</graphviz>" ); |
123
|
|
|
|
124
|
|
|
// append legend |
125
|
|
|
$result .= $graphFormatter->getGraphLegend(); |
126
|
|
|
|
127
|
|
|
return $result; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Process a result row and create SRF\GraphNodes |
133
|
|
|
* |
134
|
|
|
* @since 3.1 |
135
|
|
|
* |
136
|
|
|
* @param array $row |
137
|
|
|
* |
138
|
|
|
*/ |
139
|
|
|
protected function processResultRow( array /* of SMWResultArray */ $row ) { |
140
|
|
|
|
141
|
|
|
// loop through all row fields |
142
|
|
|
foreach ( $row as $i => $resultArray ) { |
143
|
|
|
|
144
|
|
|
// loop through all values of a multivalue field |
145
|
|
|
while ( ( /* SMWWikiPageValue */ |
146
|
|
|
$object = $resultArray->getNextDataValue() ) !== false ) { |
147
|
|
|
|
148
|
|
|
// create SRF\GraphNode for column 0 |
149
|
|
|
if ( $i == 0 ) { |
150
|
|
|
$node = new GraphNode( $object->getShortWikiText() ); |
151
|
|
|
$node->setLabel( $object->getDisplayTitle() ); |
152
|
|
|
$this->nodes[] = $node; |
153
|
|
|
} else { |
154
|
|
|
$node->addParentNode( $resultArray->getPrintRequest()->getLabel(), $object->getShortWikiText() ); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @see SMWResultPrinter::getParamDefinitions |
163
|
|
|
* |
164
|
|
|
* @since 1.8 |
165
|
|
|
* |
166
|
|
|
* @param $definitions array of IParamDefinition |
167
|
|
|
* |
168
|
|
|
* @return array of IParamDefinition|array |
169
|
|
|
*/ |
170
|
|
|
public function getParamDefinitions( array $definitions ) { |
171
|
|
|
$params = parent::getParamDefinitions( $definitions ); |
172
|
|
|
|
173
|
|
|
$params['graphname'] = [ |
174
|
|
|
'default' => 'QueryResult', |
175
|
|
|
'message' => 'srf-paramdesc-graphname', |
176
|
|
|
]; |
177
|
|
|
|
178
|
|
|
$params['graphsize'] = [ |
179
|
|
|
'type' => 'string', |
180
|
|
|
'default' => '', |
181
|
|
|
'message' => 'srf-paramdesc-graphsize', |
182
|
|
|
'manipulatedefault' => false, |
183
|
|
|
]; |
184
|
|
|
|
185
|
|
|
$params['graphlegend'] = [ |
186
|
|
|
'type' => 'boolean', |
187
|
|
|
'default' => false, |
188
|
|
|
'message' => 'srf-paramdesc-graphlegend', |
189
|
|
|
]; |
190
|
|
|
|
191
|
|
|
$params['graphlabel'] = [ |
192
|
|
|
'type' => 'boolean', |
193
|
|
|
'default' => false, |
194
|
|
|
'message' => 'srf-paramdesc-graphlabel', |
195
|
|
|
]; |
196
|
|
|
|
197
|
|
|
$params['graphlink'] = [ |
198
|
|
|
'type' => 'boolean', |
199
|
|
|
'default' => false, |
200
|
|
|
'message' => 'srf-paramdesc-graphlink', |
201
|
|
|
]; |
202
|
|
|
|
203
|
|
|
$params['graphcolor'] = [ |
204
|
|
|
'type' => 'boolean', |
205
|
|
|
'default' => false, |
206
|
|
|
'message' => 'srf-paramdesc-graphcolor', |
207
|
|
|
]; |
208
|
|
|
|
209
|
|
|
$params['arrowdirection'] = [ |
210
|
|
|
'aliases' => 'rankdir', |
211
|
|
|
'default' => 'LR', |
212
|
|
|
'message' => 'srf-paramdesc-rankdir', |
213
|
|
|
'values' => [ 'LR', 'RL', 'TB', 'BT' ], |
214
|
|
|
]; |
215
|
|
|
|
216
|
|
|
$params['nodeshape'] = [ |
217
|
|
|
'default' => false, |
218
|
|
|
'message' => 'srf-paramdesc-graph-nodeshape', |
219
|
|
|
'manipulatedefault' => false, |
220
|
|
|
'values' => self::$NODE_SHAPES, |
221
|
|
|
]; |
222
|
|
|
|
223
|
|
|
$params['relation'] = [ |
224
|
|
|
'default' => 'child', |
225
|
|
|
'message' => 'srf-paramdesc-graph-relation', |
226
|
|
|
'manipulatedefault' => false, |
227
|
|
|
'values' => [ 'parent', 'child' ], |
228
|
|
|
]; |
229
|
|
|
|
230
|
|
|
$params['wordwraplimit'] = [ |
231
|
|
|
'type' => 'integer', |
232
|
|
|
'default' => 25, |
233
|
|
|
'message' => 'srf-paramdesc-graph-wwl', |
234
|
|
|
'manipulatedefault' => false, |
235
|
|
|
]; |
236
|
|
|
|
237
|
|
|
$params['nodelabel'] = [ |
238
|
|
|
'default' => '', |
239
|
|
|
'message' => 'srf-paramdesc-nodelabel', |
240
|
|
|
'values' => self::$NODE_LABELS, |
241
|
|
|
]; |
242
|
|
|
|
243
|
|
|
return $params; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: