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
|
|
|
* @see SMWResultPrinter::handleParameters() |
77
|
|
|
*/ |
78
|
|
|
protected function handleParameters( array $params, $outputmode ) { |
79
|
|
|
parent::handleParameters( $params, $outputmode ); |
80
|
|
|
|
81
|
|
|
$this->options = new GraphOptions($params); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param SMWQueryResult $res |
86
|
|
|
* @param $outputmode |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
protected function getResultText( SMWQueryResult $res, $outputmode ) { |
91
|
|
|
if ( !class_exists( 'GraphViz' ) |
92
|
|
|
&& !class_exists( '\\MediaWiki\\Extension\\GraphViz\\GraphViz' ) |
93
|
|
|
) { |
94
|
|
|
wfWarn( 'The SRF Graph printer needs the GraphViz extension to be installed.' ); |
95
|
|
|
return ''; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// iterate query result and create SRF\GraphNodes |
99
|
|
|
while ( $row = $res->getNext() ) { |
100
|
|
|
$this->processResultRow( $row ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// use GraphFormater to build the graph |
104
|
|
|
$graphFormatter = new GraphFormatter( $this->options ); |
105
|
|
|
$graphFormatter->buildGraph( $this->nodes ); |
106
|
|
|
|
107
|
|
|
// Calls graphvizParserHook function from MediaWiki GraphViz extension |
108
|
|
|
$result = $GLOBALS['wgParser']->recursiveTagParse( "<graphviz>" . $graphFormatter->getGraph |
109
|
|
|
() . "</graphviz>" ); |
110
|
|
|
|
111
|
|
|
// append legend |
112
|
|
|
$result .= $graphFormatter->getGraphLegend(); |
113
|
|
|
|
114
|
|
|
return $result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Process a result row and create SRF\GraphNodes |
119
|
|
|
* |
120
|
|
|
* @since 3.1 |
121
|
|
|
* |
122
|
|
|
* @param array $row |
123
|
|
|
* |
124
|
|
|
*/ |
125
|
|
|
protected function processResultRow( array /* of SMWResultArray */ $row ) { |
126
|
|
|
|
127
|
|
|
// loop through all row fields |
128
|
|
|
foreach ( $row as $i => $resultArray ) { |
129
|
|
|
|
130
|
|
|
// loop through all values of a multivalue field |
131
|
|
|
while ( ( /* SMWWikiPageValue */ |
132
|
|
|
$object = $resultArray->getNextDataValue() ) !== false ) { |
133
|
|
|
|
134
|
|
|
// create SRF\GraphNode for column 0 |
135
|
|
|
if ( $i == 0 ) { |
136
|
|
|
$node = new GraphNode( $object->getShortWikiText() ); |
137
|
|
|
$node->setLabel( $object->getDisplayTitle() ); |
138
|
|
|
$this->nodes[] = $node; |
139
|
|
|
} else { |
140
|
|
|
$node->addParentNode( $resultArray->getPrintRequest()->getLabel(), $object->getShortWikiText() ); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @see SMWResultPrinter::getParamDefinitions |
148
|
|
|
* |
149
|
|
|
* @since 1.8 |
150
|
|
|
* |
151
|
|
|
* @param $definitions array of IParamDefinition |
152
|
|
|
* |
153
|
|
|
* @return array of IParamDefinition|array |
154
|
|
|
*/ |
155
|
|
|
public function getParamDefinitions( array $definitions ) { |
156
|
|
|
$params = parent::getParamDefinitions( $definitions ); |
157
|
|
|
|
158
|
|
|
$params['graphname'] = [ |
159
|
|
|
'default' => 'QueryResult', |
160
|
|
|
'message' => 'srf-paramdesc-graphname', |
161
|
|
|
]; |
162
|
|
|
|
163
|
|
|
$params['graphsize'] = [ |
164
|
|
|
'type' => 'string', |
165
|
|
|
'default' => '', |
166
|
|
|
'message' => 'srf-paramdesc-graphsize', |
167
|
|
|
'manipulatedefault' => false, |
168
|
|
|
]; |
169
|
|
|
|
170
|
|
|
$params['graphlegend'] = [ |
171
|
|
|
'type' => 'boolean', |
172
|
|
|
'default' => false, |
173
|
|
|
'message' => 'srf-paramdesc-graphlegend', |
174
|
|
|
]; |
175
|
|
|
|
176
|
|
|
$params['graphlabel'] = [ |
177
|
|
|
'type' => 'boolean', |
178
|
|
|
'default' => false, |
179
|
|
|
'message' => 'srf-paramdesc-graphlabel', |
180
|
|
|
]; |
181
|
|
|
|
182
|
|
|
$params['graphlink'] = [ |
183
|
|
|
'type' => 'boolean', |
184
|
|
|
'default' => false, |
185
|
|
|
'message' => 'srf-paramdesc-graphlink', |
186
|
|
|
]; |
187
|
|
|
|
188
|
|
|
$params['graphcolor'] = [ |
189
|
|
|
'type' => 'boolean', |
190
|
|
|
'default' => false, |
191
|
|
|
'message' => 'srf-paramdesc-graphcolor', |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
$params['arrowdirection'] = [ |
195
|
|
|
'aliases' => 'rankdir', |
196
|
|
|
'default' => 'LR', |
197
|
|
|
'message' => 'srf-paramdesc-rankdir', |
198
|
|
|
'values' => [ 'LR', 'RL', 'TB', 'BT' ], |
199
|
|
|
]; |
200
|
|
|
|
201
|
|
|
$params['nodeshape'] = [ |
202
|
|
|
'default' => false, |
203
|
|
|
'message' => 'srf-paramdesc-graph-nodeshape', |
204
|
|
|
'manipulatedefault' => false, |
205
|
|
|
'values' => self::$NODE_SHAPES, |
206
|
|
|
]; |
207
|
|
|
|
208
|
|
|
$params['relation'] = [ |
209
|
|
|
'default' => 'child', |
210
|
|
|
'message' => 'srf-paramdesc-graph-relation', |
211
|
|
|
'manipulatedefault' => false, |
212
|
|
|
'values' => [ 'parent', 'child' ], |
213
|
|
|
]; |
214
|
|
|
|
215
|
|
|
$params['wordwraplimit'] = [ |
216
|
|
|
'type' => 'integer', |
217
|
|
|
'default' => 25, |
218
|
|
|
'message' => 'srf-paramdesc-graph-wwl', |
219
|
|
|
'manipulatedefault' => false, |
220
|
|
|
]; |
221
|
|
|
|
222
|
|
|
$params['nodelabel'] = [ |
223
|
|
|
'default' => '', |
224
|
|
|
'message' => 'srf-paramdesc-nodelabel', |
225
|
|
|
'values' => self::$NODE_LABELS, |
226
|
|
|
]; |
227
|
|
|
|
228
|
|
|
return $params; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
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: