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
|
|
|
//@see https://github.com/SemanticMediaWiki/SemanticMediaWiki/pull/4273 |
28
|
|
|
// Implement `ResultPrinterDependency` once SMW 3.1 becomes mandatory |
29
|
|
|
|
30
|
|
|
const NODELABEL_DISPLAYTITLE = 'displaytitle'; |
31
|
|
|
public static $NODE_LABELS = [ |
32
|
|
|
self::NODELABEL_DISPLAYTITLE, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
public static $NODE_SHAPES = [ |
36
|
|
|
'box', |
37
|
|
|
'box3d', |
38
|
|
|
'circle', |
39
|
|
|
'component', |
40
|
|
|
'diamond', |
41
|
|
|
'doublecircle', |
42
|
|
|
'doubleoctagon', |
43
|
|
|
'egg', |
44
|
|
|
'ellipse', |
45
|
|
|
'folder', |
46
|
|
|
'hexagon', |
47
|
|
|
'house', |
48
|
|
|
'invhouse', |
49
|
|
|
'invtrapezium', |
50
|
|
|
'invtriangle', |
51
|
|
|
'Mcircle', |
52
|
|
|
'Mdiamond', |
53
|
|
|
'Msquare', |
54
|
|
|
'none', |
55
|
|
|
'note', |
56
|
|
|
'octagon', |
57
|
|
|
'parallelogram', |
58
|
|
|
'pentagon ', |
59
|
|
|
'plaintext', |
60
|
|
|
'point', |
61
|
|
|
'polygon', |
62
|
|
|
'rect', |
63
|
|
|
'rectangle', |
64
|
|
|
'septagon', |
65
|
|
|
'square', |
66
|
|
|
'tab', |
67
|
|
|
'trapezium', |
68
|
|
|
'triangle', |
69
|
|
|
'tripleoctagon', |
70
|
|
|
]; |
71
|
|
|
private $nodes = []; |
72
|
|
|
private $options; |
73
|
|
|
|
74
|
|
|
public function getName() { |
75
|
|
|
return $this->msg( 'srf-printername-graph' )->text(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @see SMWResultPrinter::handleParameters() |
80
|
|
|
*/ |
81
|
|
|
protected function handleParameters( array $params, $outputmode ) { |
82
|
|
|
parent::handleParameters( $params, $outputmode ); |
83
|
|
|
|
84
|
|
|
$this->options = new GraphOptions($params); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @see ResultPrinterDependency::hasMissingDependency |
89
|
|
|
* |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
|
|
public function hasMissingDependency() { |
93
|
|
|
return !class_exists( 'GraphViz' ) || !class_exists( '\\MediaWiki\\Extension\\GraphViz\\GraphViz' ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @see ResultPrinterDependency::getDependencyError |
98
|
|
|
* |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
|
|
public function getDependencyError() { |
102
|
|
|
return Html::rawElement( |
103
|
|
|
'div', |
104
|
|
|
[ |
105
|
|
|
'class' => 'smw-callout smw-callout-error' |
106
|
|
|
], |
107
|
|
|
'The SRF Graph printer requires the GraphViz extension to be installed.' |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param SMWQueryResult $res |
113
|
|
|
* @param $outputmode |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
protected function getResultText( SMWQueryResult $res, $outputmode ) { |
118
|
|
|
|
119
|
|
|
// Remove this once SRF requires 3.1+ |
120
|
|
|
if ( $this->hasMissingDependency() ) { |
121
|
|
|
return $this->getDependencyError(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// iterate query result and create SRF\GraphNodes |
125
|
|
|
while ( $row = $res->getNext() ) { |
126
|
|
|
$this->processResultRow( $row ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// use GraphFormater to build the graph |
130
|
|
|
$graphFormatter = new GraphFormatter( $this->options ); |
131
|
|
|
$graphFormatter->buildGraph( $this->nodes ); |
132
|
|
|
|
133
|
|
|
// Calls graphvizParserHook function from MediaWiki GraphViz extension |
134
|
|
|
$result = $GLOBALS['wgParser']->recursiveTagParse( "<graphviz>" . $graphFormatter->getGraph |
135
|
|
|
() . "</graphviz>" ); |
136
|
|
|
|
137
|
|
|
// append legend |
138
|
|
|
$result .= $graphFormatter->getGraphLegend(); |
139
|
|
|
|
140
|
|
|
return $result; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Process a result row and create SRF\GraphNodes |
145
|
|
|
* |
146
|
|
|
* @since 3.1 |
147
|
|
|
* |
148
|
|
|
* @param array $row |
149
|
|
|
* |
150
|
|
|
*/ |
151
|
|
|
protected function processResultRow( array /* of SMWResultArray */ $row ) { |
152
|
|
|
|
153
|
|
|
// loop through all row fields |
154
|
|
|
foreach ( $row as $i => $resultArray ) { |
155
|
|
|
|
156
|
|
|
// loop through all values of a multivalue field |
157
|
|
|
while ( ( /* SMWWikiPageValue */ |
158
|
|
|
$object = $resultArray->getNextDataValue() ) !== false ) { |
159
|
|
|
|
160
|
|
|
// create SRF\GraphNode for column 0 |
161
|
|
|
if ( $i == 0 ) { |
162
|
|
|
$node = new GraphNode( $object->getShortWikiText() ); |
163
|
|
|
$node->setLabel( $object->getDisplayTitle() ); |
164
|
|
|
$this->nodes[] = $node; |
165
|
|
|
} else { |
166
|
|
|
$node->addParentNode( $resultArray->getPrintRequest()->getLabel(), $object->getShortWikiText() ); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @see SMWResultPrinter::getParamDefinitions |
174
|
|
|
* |
175
|
|
|
* @since 1.8 |
176
|
|
|
* |
177
|
|
|
* @param $definitions array of IParamDefinition |
178
|
|
|
* |
179
|
|
|
* @return array of IParamDefinition|array |
180
|
|
|
*/ |
181
|
|
|
public function getParamDefinitions( array $definitions ) { |
182
|
|
|
$params = parent::getParamDefinitions( $definitions ); |
183
|
|
|
|
184
|
|
|
$params['graphname'] = [ |
185
|
|
|
'default' => 'QueryResult', |
186
|
|
|
'message' => 'srf-paramdesc-graphname', |
187
|
|
|
]; |
188
|
|
|
|
189
|
|
|
$params['graphsize'] = [ |
190
|
|
|
'type' => 'string', |
191
|
|
|
'default' => '', |
192
|
|
|
'message' => 'srf-paramdesc-graphsize', |
193
|
|
|
'manipulatedefault' => false, |
194
|
|
|
]; |
195
|
|
|
|
196
|
|
|
$params['graphlegend'] = [ |
197
|
|
|
'type' => 'boolean', |
198
|
|
|
'default' => false, |
199
|
|
|
'message' => 'srf-paramdesc-graphlegend', |
200
|
|
|
]; |
201
|
|
|
|
202
|
|
|
$params['graphlabel'] = [ |
203
|
|
|
'type' => 'boolean', |
204
|
|
|
'default' => false, |
205
|
|
|
'message' => 'srf-paramdesc-graphlabel', |
206
|
|
|
]; |
207
|
|
|
|
208
|
|
|
$params['graphlink'] = [ |
209
|
|
|
'type' => 'boolean', |
210
|
|
|
'default' => false, |
211
|
|
|
'message' => 'srf-paramdesc-graphlink', |
212
|
|
|
]; |
213
|
|
|
|
214
|
|
|
$params['graphcolor'] = [ |
215
|
|
|
'type' => 'boolean', |
216
|
|
|
'default' => false, |
217
|
|
|
'message' => 'srf-paramdesc-graphcolor', |
218
|
|
|
]; |
219
|
|
|
|
220
|
|
|
$params['arrowdirection'] = [ |
221
|
|
|
'aliases' => 'rankdir', |
222
|
|
|
'default' => 'LR', |
223
|
|
|
'message' => 'srf-paramdesc-rankdir', |
224
|
|
|
'values' => [ 'LR', 'RL', 'TB', 'BT' ], |
225
|
|
|
]; |
226
|
|
|
|
227
|
|
|
$params['nodeshape'] = [ |
228
|
|
|
'default' => false, |
229
|
|
|
'message' => 'srf-paramdesc-graph-nodeshape', |
230
|
|
|
'manipulatedefault' => false, |
231
|
|
|
'values' => self::$NODE_SHAPES, |
232
|
|
|
]; |
233
|
|
|
|
234
|
|
|
$params['relation'] = [ |
235
|
|
|
'default' => 'child', |
236
|
|
|
'message' => 'srf-paramdesc-graph-relation', |
237
|
|
|
'manipulatedefault' => false, |
238
|
|
|
'values' => [ 'parent', 'child' ], |
239
|
|
|
]; |
240
|
|
|
|
241
|
|
|
$params['wordwraplimit'] = [ |
242
|
|
|
'type' => 'integer', |
243
|
|
|
'default' => 25, |
244
|
|
|
'message' => 'srf-paramdesc-graph-wwl', |
245
|
|
|
'manipulatedefault' => false, |
246
|
|
|
]; |
247
|
|
|
|
248
|
|
|
$params['nodelabel'] = [ |
249
|
|
|
'default' => '', |
250
|
|
|
'message' => 'srf-paramdesc-nodelabel', |
251
|
|
|
'values' => self::$NODE_LABELS, |
252
|
|
|
]; |
253
|
|
|
|
254
|
|
|
return $params; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
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: