-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSparqlResultFormat_body.php
More file actions
177 lines (153 loc) · 5.66 KB
/
SparqlResultFormat_body.php
File metadata and controls
177 lines (153 loc) · 5.66 KB
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
<?php
use SMW\Exporter\Escaper;
class ExtSparqlResultFormat {
// The prefix and suffix for the widget strip marker.
private static $markerPrefix = "START_SPARQL_II";
private static $markerSuffix = "END_SPARQL_II";
// Must be public for use in anonymous callback function in PHP 5.3
public static $elements = array();
public static function sparql2FormatTemplate( $parser, $options_array, $format ) {
global $wgSparqlEndpointDefinition;
# parsing options array
$options = self::extractOptions( $options_array );
# getting endpoint data defined in localsettings
$endpointData = $wgSparqlEndpointDefinition[$options['sparqlEndpoint']];
# generating code for prefixes array
$prefixes = isset($endpointData['prefixes']) ? $endpointData['prefixes'] : null;
$javascriptPrefixesArray = self::createJavascriptPrefixesArray( $prefixes );
# format specific code
try {
$js = $format->generateJavascriptCode( $options, $javascriptPrefixesArray );
$html = $format->generateHtmlContainerCode( $options );
# composing output code
$output = self::composeOutputScript( $js, $html );
} catch ( Exception $e ) {
$output = "<div class='error'>Error:" . $e->getMessage() . "</div>";
}
// To prevent the widget output from being tampered with, the
// compiled HTML is stored and a strip marker with an index to
// retrieve it later is returned.
$index = array_push( self::$elements, $output ) - 1;
return self::$markerPrefix . '-' . $index . self::$markerSuffix;
}
public static function sparql2table( $parser ) {
$options_array = array_slice( func_get_args(), 1 );
$format = new SparqlResultFormatTable;
return self::sparql2FormatTemplate( $parser, $options_array, $format );
}
public static function sparql2graph( $parser ) {
$options_array = array_slice( func_get_args(), 1 );
$format = new SparqlResultFormatGraph;
return self::sparql2FormatTemplate( $parser, $options_array, $format );
}
public static function sparql2treemap( $parser ) {
$options_array = array_slice( func_get_args(), 1 );
$format = new SparqlResultFormatTreemap;
return self::sparql2FormatTemplate( $parser, $options_array, $format );
}
public static function sparql2donutchart( $parser ) {
$options_array = array_slice( func_get_args(), 1 );
$format = new SparqlResultFormatDonutChart;
return self::sparql2FormatTemplate( $parser, $options_array, $format );
}
public static function sparql2barchart( $parser ) {
$options_array = array_slice( func_get_args(), 1 );
$format = new SparqlResultFormatBarChart;
return self::sparql2FormatTemplate( $parser, $options_array, $format );
}
public static function sparql2piechart( $parser ) {
$options_array = array_slice( func_get_args(), 1 );
$format = new SparqlResultFormatPieChart;
return self::sparql2FormatTemplate( $parser, $options_array, $format );
}
public static function sparql2bubblechart( $parser ) {
$options_array = array_slice( func_get_args(), 1 );
$format = new SparqlResultFormatBubbleChart;
return self::sparql2FormatTemplate( $parser, $options_array, $format );
}
public static function sparql2csv( $parser ) {
$options_array = array_slice( func_get_args(), 1 );
$format = new SparqlResultFormatCSV;
return self::sparql2FormatTemplate( $parser, $options_array, $format );
}
public static function extractOptions( array $options ) {
$results = array();
foreach ( $options as $option ) {
$pair = explode( '=', $option, 2 );
if ( count( $pair ) === 2 ) {
$name = trim( $pair[0] );
$value = trim( $pair[1] );
if ( isset( $results[$name] ) ) {
if ( is_Array( $results[$name] ) ) { // array già creato
array_push( $results[$name], $value );
} else {
$oldVal = $results[$name];
$results[$name] = array();
array_push($results[$name],$oldVal);
array_push($results[$name],$value);
}
} else {
$results[$name] = $value;
}
}
if ( count( $pair ) === 1 ) {
$name = trim( $pair[0] );
$results[$name] = true;
}
}
// Now you've got an array that looks like this:
// [foo] => "bar"
// [apple] => "orange"
// [banana] => true
return $results;
}
public static function page2uri( $parser ) {
global $smwgNamespace;
$options_array = array_slice( func_get_args(), 1 );
if ( is_array( $options_array ) ) {
$pageName = $options_array[0];
} else {
return "<div class='error'>Error: you must specify the page name/div>";
}
if ( class_exists( "SMW\Exporter\Escaper" ) ) {
$esc = Escaper::encodeUri( urlencode( str_replace( ' ', '_', $pageName ) ) );
} else {
return "<div class='error'>Error: Cannot find Escaper class. Is SemanticMediaWiki extension installed?/div>";
}
return $smwgNamespace . $esc;
}
public static function smwSparqlDefaultGraph( $parser ) {
global $smwgSparqlDefaultGraph;
if ( isset( $smwgSparqlDefaultGraph ) ) {
return $smwgSparqlDefaultGraph;
}
return 'Error: no $smwgSparqlDefaultGraph variable definition';
}
public static function outputHtml( &$out, &$text ) {
$text = preg_replace_callback(
'/' . self::$markerPrefix . '-(\d+)' . self::$markerSuffix . '/S',
function ( $matches ) {
// Can't use self:: in an anonymous function pre PHP 5.4
return ExtSparqlResultFormat::$elements[$matches[1]];
},
$text
);
return true;
}
public static function createJavascriptPrefixesArray( $prefixes ) {
$res = "var prefixes = [];";
if (is_array($prefixes)){
foreach ( $prefixes as $key => &$val ) {
$res .= " prefixes.push({pre:'$key',ns:'$val'});";
}
}
return $res;
}
public static function composeOutputScript( $javascript, $html ) {
$output = " <script type=\"text/javascript\">
$javascript
</script>
$html";
return $output;
}
}