|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Print query results in interactive graph using the |
|
4
|
|
|
* JavaScript InfoVis Toolkit (http://thejit.org) |
|
5
|
|
|
* |
|
6
|
|
|
* @since 1.7.1 |
|
7
|
|
|
* |
|
8
|
|
|
* @file SRF_JitGraph.php |
|
9
|
|
|
* @ingroup SemanticResultFormats |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Result printer for timeline data. |
|
14
|
|
|
* @ingroup SemanticResultFormats |
|
15
|
|
|
*/ |
|
16
|
|
|
class SRFJitGraph extends SMWResultPrinter { |
|
17
|
|
|
|
|
18
|
|
|
public static $NODE_SHAPES = ['circle', 'rectangle', 'square', 'ellipse', 'triangle', 'star']; |
|
19
|
|
|
|
|
20
|
|
|
protected $m_graphName = '227'; |
|
21
|
|
|
protected $m_graphLabel = false; |
|
22
|
|
|
protected $m_graphColor = false; |
|
23
|
|
|
protected $m_graphLegend = false; |
|
24
|
|
|
protected $m_graphLink = false; |
|
25
|
|
|
protected $m_rankdir = "LR"; |
|
26
|
|
|
protected $m_graphSize = ""; |
|
27
|
|
|
protected $m_labelArray = []; |
|
28
|
|
|
protected $m_graphNodeType = 'circle'; |
|
29
|
|
|
protected $m_graphNodeSize = 12; |
|
30
|
|
|
protected $m_graphRootNode = false; |
|
31
|
|
|
protected $m_nodeTypes = ['circle', 'rectangle', 'square', 'ellipse', 'triangle', 'star']; |
|
32
|
|
|
|
|
33
|
|
|
protected $m_nodeColorArray = ['black'=>'#00FF00', 'red' => '#CF2A2A', 'green' => '#558800', 'blue' => '#005588']; |
|
34
|
|
|
protected $m_rootNodeColor = '#CF2A2A'; //Red |
|
35
|
|
|
protected $m_graphNodeColor = '#005588'; //Blue |
|
36
|
|
|
|
|
37
|
|
|
protected $m_settings = [ |
|
38
|
|
|
"divID" => "infovis", |
|
39
|
|
|
"edgeColor" => "#23A4FF", |
|
40
|
|
|
"edgeWidth" => 2, |
|
41
|
|
|
"edgeLength" => 150, |
|
42
|
|
|
"navigation" => true, |
|
43
|
|
|
"zooming" => false, |
|
44
|
|
|
"panning" => "avoid nodes", |
|
45
|
|
|
"labelColor" => "#000000" |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
protected $m_edgeColors = []; |
|
49
|
|
|
protected $m_edgeNames = []; |
|
50
|
|
|
|
|
51
|
|
|
protected $debug_out = ''; |
|
52
|
|
|
|
|
53
|
|
|
protected function handleParameters( array $params, $outputmode ) { |
|
54
|
|
|
parent::handleParameters( $params, $outputmode ); |
|
55
|
|
|
|
|
56
|
|
|
if ( array_key_exists( 'graphname', $params ) ) { |
|
57
|
|
|
$this->m_graphName = trim( $params['graphname'] ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->m_graphNodeType = $params['graphnodetype']; |
|
61
|
|
|
|
|
62
|
|
|
// if ( array_key_exists( 'graphnodetype', $params ) ) { |
|
63
|
|
|
// $userType = strtolower( trim( $params['graphnodetype'] ) ); |
|
64
|
|
|
// if ( in_array($userType, $this->m_nodeTypes) ) { |
|
65
|
|
|
// $this->m_graphNodeType = $userType; |
|
66
|
|
|
// } |
|
67
|
|
|
// |
|
68
|
|
|
// } |
|
69
|
|
|
|
|
70
|
|
|
if ( array_key_exists( 'graphnodesize', $params ) ) { |
|
71
|
|
|
|
|
72
|
|
|
$userSize = intval(trim( $params['graphnodesize'] )); |
|
73
|
|
|
if($userSize > 0) { |
|
74
|
|
|
$this->m_graphNodeSize = $userSize; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
if ( array_key_exists( 'graphsize', $params ) ) { |
|
78
|
|
|
|
|
79
|
|
|
$this->m_graphSize = trim( $params['graphsize'] ); |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
if ( array_key_exists( 'graphrootnode', $params ) ) { |
|
83
|
|
|
|
|
84
|
|
|
if ( strtolower( trim( $params['graphrootnode'] ) ) == 'yes' ) $this->m_graphRootNode = true; |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
if ( array_key_exists( 'graphlegend', $params ) ) { |
|
88
|
|
|
|
|
89
|
|
|
if ( strtolower( trim( $params['graphlegend'] ) ) == 'yes' ) $this->m_graphLegend = true; |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ( array_key_exists( 'graphlabel', $params ) ) { |
|
94
|
|
|
|
|
95
|
|
|
if ( strtolower( trim( $params['graphlabel'] ) ) == 'yes' ) $this->m_graphLabel = true; |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
if ( array_key_exists( 'graphnodecolor', $params ) ) { |
|
99
|
|
|
$userNodeColor = strtolower( trim( $params['graphnodecolor'] ) ); |
|
100
|
|
|
if ( array_key_exists($userNodeColor, $this->m_nodeColorArray) ) { |
|
101
|
|
|
$this->m_graphNodeColor = $this->m_nodeColorArray[$userNodeColor]; |
|
102
|
|
|
$this->debug_out .= "graphNodeColor: " . $this->m_graphNodeColor . " | "; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
if ( array_key_exists( 'rootnodecolor', $params ) ) { |
|
106
|
|
|
$userRootNodeColor = strtolower( trim( $params['rootnodecolor'] ) ); |
|
107
|
|
|
if ( array_key_exists($userRootNodeColor, $this->m_nodeColorArray) ) { |
|
108
|
|
|
$this->m_rootNodeColor = $this->m_nodeColorArray[$userRootNodeColor]; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
if ( array_key_exists( 'graphlink', $params ) ) { |
|
112
|
|
|
|
|
113
|
|
|
if ( strtolower( trim( $params['graphlink'] ) ) == 'yes' ) $this->m_graphLink = true; |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
if ( array_key_exists( 'graphcolor', $params ) ) { |
|
117
|
|
|
|
|
118
|
|
|
if ( strtolower( trim( $params['graphcolor'] ) ) == 'yes' ) $this->m_graphColor = true; |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getName() { |
|
125
|
|
|
// Give grep a chance to find the usages: |
|
126
|
|
|
// srf_printername_outline, srf_printername_sum, srf_printername_average, srf_printername_max |
|
127
|
|
|
return wfMessage( 'srf_printername_' . $this->mFormat )->text(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
protected function getResultText( SMWQueryResult $res, $outputmode ) { |
|
131
|
|
|
global $smwgIQRunningNumber; |
|
132
|
|
|
|
|
133
|
|
|
global $wgTitle, $wgOut; |
|
134
|
|
|
|
|
135
|
|
|
if ( class_exists( 'ResourceLoader' ) ) { |
|
136
|
|
|
$wgOut->addModules( 'ext.srf.jitgraph' ); |
|
137
|
|
|
} |
|
138
|
|
|
else { |
|
139
|
|
|
//Include javascript files in the HTML header |
|
140
|
|
|
$this->includeJS(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$key = 0; |
|
144
|
|
|
// Create text graph |
|
145
|
|
|
$legendInput = ''; |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
$json = "["; |
|
148
|
|
|
$debug = ""; |
|
|
|
|
|
|
149
|
|
|
$jsonLeafs = ""; |
|
150
|
|
|
|
|
151
|
|
|
while ( $row = $res->getNext() ) { |
|
152
|
|
|
|
|
153
|
|
|
$firstcol = true; |
|
154
|
|
|
|
|
155
|
|
|
foreach ( $row as $field ) { |
|
156
|
|
|
while ( ( $object = $field->getNextObject() ) !== false ) { |
|
157
|
|
|
|
|
158
|
|
|
$text = $object->getShortText( $outputmode ); |
|
159
|
|
|
|
|
160
|
|
|
$nodeLinkTitle = Title::newFromText( $text ); |
|
161
|
|
|
$nodeLinkURL = $nodeLinkTitle->getFullURL(); |
|
162
|
|
|
|
|
163
|
|
|
if ( $firstcol ) { |
|
164
|
|
|
$firstcolvalue = $object->getShortText( $outputmode ); |
|
165
|
|
|
|
|
166
|
|
|
//Title of the page where the result format is being displayed |
|
167
|
|
|
$thisPageTitle = $wgTitle->getPrefixedText(); |
|
168
|
|
|
|
|
169
|
|
|
//This little block adds the name of the current edge to the list later used to compile the graph legend |
|
170
|
|
|
$req = $field->getPrintRequest(); |
|
171
|
|
|
$labelName = trim( $req->getLabel()); |
|
172
|
|
|
|
|
173
|
|
|
//Different color options and formatting for the page currently on. |
|
174
|
|
|
if( strcmp($thisPageTitle, $text) == 0 && $this->m_graphRootNode) { |
|
175
|
|
|
$rootNodeSize = $this->m_graphNodeSize + 5; |
|
|
|
|
|
|
176
|
|
|
$json .= "{ \"id\":\"$text\", "; |
|
177
|
|
|
$json .= "\"name\":\"$text\", "; |
|
178
|
|
|
$json .= "\"data\":{\"\$color\": \"$this->m_rootNodeColor\", "; |
|
179
|
|
|
$json .= "\"\$type\":\"$this->m_graphNodeType\", "; |
|
180
|
|
|
$json .= "\"\$dim\":\"17\", "; |
|
181
|
|
|
$json .= "\"\$url\":\"$nodeLinkURL\", "; |
|
182
|
|
|
$json .= "\"\$edgeType\":\"$labelName\" "; |
|
183
|
|
|
} else { |
|
184
|
|
|
$json .= "{ \"id\":\"$text\", "; |
|
185
|
|
|
$json .= "\"name\":\"$text\", "; |
|
186
|
|
|
$json .= "\"data\":{\"\$color\": \"$this->m_graphNodeColor\", "; |
|
187
|
|
|
$json .= "\"\$type\":\"$this->m_graphNodeType\", "; |
|
188
|
|
|
$json .= "\"\$dim\":\"$this->m_graphNodeSize\", "; |
|
189
|
|
|
$json .= "\"\$url\":\"$nodeLinkURL\", "; |
|
190
|
|
|
$json .= "\"\$edgeType\":\"$labelName\" "; |
|
191
|
|
|
|
|
192
|
|
|
if(!in_array($labelName, $this->m_edgeNames) && strlen($labelName) > 0) { |
|
193
|
|
|
$this->m_edgeNames[] = $labelName; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$json .= "}, "; |
|
198
|
|
|
$json .= "\"adjacencies\":[ "; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
if ( !$firstcol ) { |
|
203
|
|
|
|
|
204
|
|
|
$json .= "{ \"nodeTo\":\"$text\", "; |
|
205
|
|
|
$json .= "\"nodeFrom\":\"$firstcolvalue\", "; |
|
|
|
|
|
|
206
|
|
|
$json .= "\"data\":{\"\$color\":\"#$this->m_rootNodeColor\",\"\$url\":\"$nodeLinkURL\"}},"; |
|
207
|
|
|
if ( ( $this->m_graphLabel == true ) || ( $this->m_graphColor == true ) ) { |
|
|
|
|
|
|
208
|
|
|
$req = $field->getPrintRequest(); |
|
209
|
|
|
$labelName = $req->getLabel(); |
|
210
|
|
|
|
|
211
|
|
|
if ( array_search( $labelName, $this->m_labelArray, true ) === false ) { |
|
212
|
|
|
$this->m_labelArray[] = $labelName; |
|
213
|
|
|
} |
|
214
|
|
|
$key = array_search( $labelName, $this->m_labelArray, true ); |
|
215
|
|
|
$color = $this->m_graphColors[$key]; |
|
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
if ( $this->m_graphLabel == true ) { |
|
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
if ( $this->m_graphColor == true ) { |
|
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
//Create an explicit node for each leaf. |
|
227
|
|
|
$jsonLeafs .= "{ \"id\":\"$text\", "; |
|
228
|
|
|
$jsonLeafs .= "\"name\":\"$text\", "; |
|
229
|
|
|
|
|
230
|
|
|
if( strcmp($thisPageTitle, $text) == 0 ) { |
|
|
|
|
|
|
231
|
|
|
$rootNodeSize = $this->m_graphNodeSize + 5; |
|
232
|
|
|
$jsonLeafs .= "\"data\":{\"\$color\": \"$this->m_rootNodeColor\", "; |
|
233
|
|
|
$jsonLeafs .= "\"\$dim\":\"$rootNodeSize\", "; |
|
234
|
|
|
} else { |
|
235
|
|
|
$jsonLeafs .= "\"data\":{\"\$color\": \"$this->m_graphNodeColor\", "; |
|
236
|
|
|
$jsonLeafs .= "\"\$dim\":\"$this->m_graphNodeSize\", "; |
|
237
|
|
|
} |
|
238
|
|
|
$jsonLeafs .= "\"\$type\":\"$this->m_graphNodeType\", "; |
|
239
|
|
|
$jsonLeafs .= "\"\$url\":\"$nodeLinkURL\", "; |
|
240
|
|
|
$jsonLeafs .= "\"\$edgeType\":\"$labelName\" "; |
|
|
|
|
|
|
241
|
|
|
$jsonLeafs .= "}, "; |
|
242
|
|
|
$jsonLeafs .= "\"adjacencies\":[]},"; |
|
243
|
|
|
|
|
244
|
|
|
//This little block adds the name of the current edge to the list later used to compile the graph legend |
|
245
|
|
|
$req = $field->getPrintRequest(); |
|
246
|
|
|
$labelName = trim( $req->getLabel()); |
|
247
|
|
|
if(!in_array($labelName, $this->m_edgeNames) && strlen($labelName) > 0) { |
|
248
|
|
|
$this->m_edgeNames[] = $labelName; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
$firstcol = false; |
|
254
|
|
|
} |
|
255
|
|
|
$json = substr($json,0,-1); // Trim the comma after the last item in the list |
|
256
|
|
|
$json .= "]},"; //close adjacencies array |
|
257
|
|
|
|
|
258
|
|
|
//Append the leaf nodes. |
|
259
|
|
|
//$jsonLeafs = substr($jsonLeafs,0,-1); // Trim the comma after the last item in the list |
|
260
|
|
|
$json .= $jsonLeafs; |
|
261
|
|
|
$jsonLeafs = ""; |
|
262
|
|
|
} |
|
263
|
|
|
$json = substr($json,0,-1); // Trim the comma after the last item in the list |
|
264
|
|
|
$json .= "]"; //close the json object array |
|
265
|
|
|
|
|
266
|
|
|
$result = ''; |
|
267
|
|
|
|
|
268
|
|
|
if($this->m_graphLabel || true) { |
|
269
|
|
|
$result .= '<h3>'. $this->m_graphName .'</h3>'; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
$d_id = rand(1000,9999); |
|
273
|
|
|
$divID = 'infovis-' . $d_id; //generate a random id to have the ability to display multiple graphs on a single page. |
|
274
|
|
|
$this->m_settings['d_id'] = $d_id; |
|
275
|
|
|
$this->m_settings['divID'] = $divID; |
|
276
|
|
|
|
|
277
|
|
|
//User Settings |
|
278
|
|
|
$userSettings = "var graphSettings = {"; |
|
279
|
|
|
foreach ($this->m_settings as $key => $value) { |
|
280
|
|
|
$userSettings .= "\"$key\": \"$value\","; |
|
281
|
|
|
} |
|
282
|
|
|
substr($userSettings,0,-1); |
|
283
|
|
|
$userSettings .= "};"; |
|
284
|
|
|
|
|
285
|
|
|
$result .= '<div id="center-container" class="className"><span class="progressBar" id="progress-'.$d_id.'">0%</span><div id="'. $this->m_settings['divID'] .'" class="infovis"></div>'.''.'</div>'; |
|
286
|
|
|
|
|
287
|
|
|
$result .= "<script>"; |
|
288
|
|
|
$result .= $userSettings; |
|
289
|
|
|
// FIXME: init function cannot be called here, use JS in a separate file and bind to the onload event. |
|
290
|
|
|
$result .= "var json=" . $json . "; this.init(json, graphSettings);" . 'jQuery("#progress-'.$d_id.'").progressBar();'; |
|
291
|
|
|
$result .= 'jQuery(document).load(function() {});'; |
|
292
|
|
|
//$result .= '$("#progress1").progressBar();'; |
|
293
|
|
|
$result .= "</script>"; |
|
294
|
|
|
|
|
295
|
|
|
// yes, our code can be viewed as HTML if requested, no more parsing needed |
|
296
|
|
|
//$this->isHTML = $outputmode == SMW_OUTPUT_HTML; |
|
297
|
|
|
|
|
298
|
|
|
$this->isHTML = true; |
|
299
|
|
|
|
|
300
|
|
|
return $result; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
protected function includeJS() { |
|
304
|
|
|
SMWOutputs::requireHeadItem( SMW_HEADER_STYLE ); |
|
305
|
|
|
|
|
306
|
|
|
//$wgOut->addModules( 'ext.srf.jitgraph' ); |
|
307
|
|
|
|
|
308
|
|
|
global $srfgScriptPath; |
|
309
|
|
|
|
|
310
|
|
|
SMWOutputs::requireHeadItem( |
|
311
|
|
|
'smw_jgcss', |
|
312
|
|
|
'<link rel="stylesheet" type="text/css" href="' . $srfgScriptPath . |
|
313
|
|
|
'/JitGraph/base.css"></link>' |
|
314
|
|
|
); |
|
315
|
|
|
SMWOutputs::requireHeadItem( |
|
316
|
|
|
'smw_jgloader', |
|
317
|
|
|
'<script type="text/javascript" src="' . $srfgScriptPath . |
|
318
|
|
|
'/JitGraph/jquery.progressbar.js"></script>' |
|
319
|
|
|
); |
|
320
|
|
|
SMWOutputs::requireHeadItem( |
|
321
|
|
|
'smw_jg', |
|
322
|
|
|
'<script type="text/javascript" src="' . $srfgScriptPath . |
|
323
|
|
|
'/JitGraph/Jit/jit.js"></script>' |
|
324
|
|
|
); |
|
325
|
|
|
SMWOutputs::requireHeadItem( |
|
326
|
|
|
'smw_jghelper', |
|
327
|
|
|
'<script type="text/javascript" src="' . $srfgScriptPath . |
|
328
|
|
|
'/JitGraph/SRF_JitGraph.js"></script>' |
|
329
|
|
|
); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @see SMWResultPrinter::getParamDefinitions |
|
334
|
|
|
* |
|
335
|
|
|
* @since 1.8 |
|
336
|
|
|
* |
|
337
|
|
|
* @param $definitions array of IParamDefinition |
|
338
|
|
|
* |
|
339
|
|
|
* @return array of IParamDefinition|array |
|
340
|
|
|
*/ |
|
341
|
|
|
public function getParamDefinitions( array $definitions ) { |
|
342
|
|
|
$params = parent::getParamDefinitions( $definitions ); |
|
343
|
|
|
|
|
344
|
|
|
$params['graphname'] = [ |
|
345
|
|
|
'default' => 'GraphName', |
|
346
|
|
|
'message' => 'srf_paramdesc_graphname', |
|
347
|
|
|
]; |
|
348
|
|
|
|
|
349
|
|
|
$params['graphnodetype'] = [ |
|
350
|
|
|
'default' => false, |
|
351
|
|
|
'message' => 'srf-paramdesc-graph-graphnodetype', |
|
352
|
|
|
'values' => self::$NODE_SHAPES, |
|
353
|
|
|
]; |
|
354
|
|
|
|
|
355
|
|
|
$params['graphsize'] = [ |
|
356
|
|
|
'type' => 'integer', |
|
357
|
|
|
'default' => '', |
|
358
|
|
|
'manipulatedefault' => false, |
|
359
|
|
|
'message' => 'srf_paramdesc_graphsize', |
|
360
|
|
|
]; |
|
361
|
|
|
|
|
362
|
|
|
$params['graphlegend'] = [ |
|
363
|
|
|
'type' => 'boolean', |
|
364
|
|
|
'default' => false, |
|
365
|
|
|
'message' => 'srf_paramdesc_graphlegend', |
|
366
|
|
|
]; |
|
367
|
|
|
|
|
368
|
|
|
$params['graphlabel'] = [ |
|
369
|
|
|
'type' => 'boolean', |
|
370
|
|
|
'default' => false, |
|
371
|
|
|
'message' => 'srf_paramdesc_graphlabel', |
|
372
|
|
|
]; |
|
373
|
|
|
|
|
374
|
|
|
$params['graphcolor'] = [ |
|
375
|
|
|
'type' => 'boolean', |
|
376
|
|
|
'default' => false, |
|
377
|
|
|
'message' => 'srf_paramdesc_graphcolor', |
|
378
|
|
|
]; |
|
379
|
|
|
|
|
380
|
|
|
return $params; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
} |
|
384
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.