1 | <?php
|
---|
2 | require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngineDb/ResultRenderer.php';
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Sparql DB HTML result renderer.
|
---|
6 | *
|
---|
7 | * @author Christian Weiske <cweiske@cweiske.de>
|
---|
8 | * @license http://www.gnu.org/licenses/lgpl.html LGPL
|
---|
9 | *
|
---|
10 | * @package sparql
|
---|
11 | */
|
---|
12 | class SparqlEngineDb_ResultRenderer_HTML implements SparqlEngineDb_ResultRenderer
|
---|
13 | {
|
---|
14 | /**
|
---|
15 | * If the result HTML should be wrapped in a div
|
---|
16 | * @var boolean
|
---|
17 | */
|
---|
18 | protected $bWrap = true;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Defines the methods needed to create the types
|
---|
22 | * in $arVarAssignments.
|
---|
23 | * Key is the type (e.g. "s" for subject), and
|
---|
24 | * value the method's name.
|
---|
25 | *
|
---|
26 | * @see $arVarAssignments
|
---|
27 | *
|
---|
28 | * @var array
|
---|
29 | */
|
---|
30 | protected $arCreationMethods = array(
|
---|
31 | 's' => 'createSubjectFromDbRecordSetPart',
|
---|
32 | 'p' => 'createPredicateFromDbRecordSetPart',
|
---|
33 | 'o' => 'createObjectFromDbRecordSetPart'
|
---|
34 | );
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Converts the database results into nice HTML.
|
---|
40 | *
|
---|
41 | * @param array $arRecordSets Array of (possibly several) SQL query results.
|
---|
42 | * @param Query $query SPARQL query object
|
---|
43 | * @param SparqlEngineDb $engine Sparql Engine to query the database
|
---|
44 | * @return mixed HTML code
|
---|
45 | */
|
---|
46 | public function convertFromDbResults($arRecordSets, Query $query, SparqlEngineDb $engine)
|
---|
47 | {
|
---|
48 | $this->query = $query;
|
---|
49 | $this->sg = $engine->getSqlGenerator();
|
---|
50 | $strCode = '';
|
---|
51 |
|
---|
52 | $strResultForm = $query->getResultForm();
|
---|
53 | switch ($strResultForm) {
|
---|
54 | case 'select':
|
---|
55 | case 'select distinct':
|
---|
56 | $strCode = $this->createTableFromRecords($arRecordSets);
|
---|
57 | break;
|
---|
58 |
|
---|
59 | case 'construct':
|
---|
60 | case 'describe':
|
---|
61 | throw new Exception(
|
---|
62 | 'Construct and describe are currently not supported by the'
|
---|
63 | . ' HTML renderer'
|
---|
64 | );
|
---|
65 |
|
---|
66 | case 'count':
|
---|
67 | case 'ask':
|
---|
68 | if (count($arRecordSets) > 1) {
|
---|
69 | throw new Exception(
|
---|
70 | 'More than one result set for a '
|
---|
71 | . $strResultForm . ' query!'
|
---|
72 | );
|
---|
73 | }
|
---|
74 |
|
---|
75 | $nCount = 0;
|
---|
76 | $dbRecordSet = reset($arRecordSets);
|
---|
77 | foreach ($dbRecordSet as $row) {
|
---|
78 | $nCount += intval($row['count']);
|
---|
79 | break;
|
---|
80 | }
|
---|
81 |
|
---|
82 | if ($strResultForm == 'ask') {
|
---|
83 | $strCode = 'There were results.';
|
---|
84 | } else {
|
---|
85 | $strCode = 'There are ' . $nCount . ' results.';
|
---|
86 | }
|
---|
87 | break;
|
---|
88 |
|
---|
89 | default:
|
---|
90 | throw new Exception('Unsupported result form: ' . $strResultForm);
|
---|
91 | }
|
---|
92 |
|
---|
93 | return $this->wrapCode($strCode);
|
---|
94 | }//public function convertFromDbResults($arRecordSets, Query $query, SparqlEngineDb $engine)
|
---|
95 |
|
---|
96 |
|
---|
97 |
|
---|
98 | protected function wrapCode($strCode)
|
---|
99 | {
|
---|
100 | if (!$this->bWrap) {
|
---|
101 | return $strCode;
|
---|
102 | }
|
---|
103 |
|
---|
104 | return
|
---|
105 | '<div class="SparqlEngineDb_ResultRenderer_HTML_result">' . "\n"
|
---|
106 | . $strCode . "\n"
|
---|
107 | . "</div>\n";
|
---|
108 | }//protected function wrapCode($strCode)
|
---|
109 |
|
---|
110 |
|
---|
111 |
|
---|
112 | protected function createTableFromRecords($arRecordSets)
|
---|
113 | {
|
---|
114 | $arResultVars = $this->query->getResultVars();
|
---|
115 |
|
---|
116 | if (in_array('*', $arResultVars)) {
|
---|
117 | $arResultVars = array_keys($this->sg->arVarAssignments);
|
---|
118 | }
|
---|
119 |
|
---|
120 | $arResult = array();
|
---|
121 | foreach ($arRecordSets as $dbRecordSet) {
|
---|
122 | //work around bug in adodb:
|
---|
123 | // ADORecordSet_empty does not implement php5 iterators
|
---|
124 | if ($dbRecordSet->RowCount() <= 0) {
|
---|
125 | return array();
|
---|
126 | }
|
---|
127 |
|
---|
128 | foreach ($dbRecordSet as $row) {
|
---|
129 | $arResultRow = array();
|
---|
130 | foreach ($arResultVars as $strVar) {
|
---|
131 | $strVarName = (string)$strVar;
|
---|
132 | if (!isset($this->sg->arVarAssignments[$strVarName])) {
|
---|
133 | //variable is in select, but not in result (test: q-select-2)
|
---|
134 | $arResultRow[$strVarName] = '';
|
---|
135 | } else {
|
---|
136 | $arVarSettings = $this->sg->arVarAssignments[$strVarName];
|
---|
137 | $strMethod = $this->arCreationMethods[$arVarSettings[1]];
|
---|
138 | list($strCode, $strColor) = $this->$strMethod($dbRecordSet, $arVarSettings[0], $strVar);
|
---|
139 | $arResultRow[$strVarName] = '<td style="background-color: '
|
---|
140 | . $strColor . '">' . $strCode . '</td>';
|
---|
141 | }
|
---|
142 | }
|
---|
143 | $arResult[] = $arResultRow;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | //I always wanted to to this :)
|
---|
148 | return
|
---|
149 | "<table border='1'>\n"
|
---|
150 | . " <caption>SPARQL result with " . count($arResult) . " rows</caption>\n"
|
---|
151 | . " <thead><th>"
|
---|
152 | . implode('</th><th>', array_keys(reset($arResult)))
|
---|
153 | . "</th></thead>\n"
|
---|
154 | . " <tbody>\n <tr>"
|
---|
155 | . implode(
|
---|
156 | "</tr>\n <tr>",
|
---|
157 | array_map(
|
---|
158 | create_function(
|
---|
159 | '$ar',
|
---|
160 | 'return implode("", $ar);'
|
---|
161 | ),
|
---|
162 | $arResult
|
---|
163 | )
|
---|
164 | )
|
---|
165 | . "</tr>\n </tbody>\n"
|
---|
166 | . "</table>\n";
|
---|
167 | }//protected function createTableFromRecords($arRecordSets)
|
---|
168 |
|
---|
169 |
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Creates an RDF subject object
|
---|
173 | * contained in the given $dbRecordSet object.
|
---|
174 | *
|
---|
175 | * @see convertFromDbResult() to understand $strVarBase necessity
|
---|
176 | *
|
---|
177 | * @param ADORecordSet $dbRecordSet Record set returned from ADOConnection::Execute()
|
---|
178 | * @param string $strVarBase Prefix of the columns the recordset fields have.
|
---|
179 | *
|
---|
180 | * @return string HTML code
|
---|
181 | */
|
---|
182 | protected function createSubjectFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVar)
|
---|
183 | {
|
---|
184 | $strVarName = (string)$strVar;
|
---|
185 | if ($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']] === null) {
|
---|
186 | return $this->getHtmlNull();
|
---|
187 | }
|
---|
188 | if ($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_is']] == 'r'
|
---|
189 | //null should be predicate which is always a resource
|
---|
190 | || $dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_is']] === null
|
---|
191 | ) {
|
---|
192 | return $this->getHtmlResource($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']]);
|
---|
193 | } else {
|
---|
194 | return $this->getHtmlBlank($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']]);
|
---|
195 | }
|
---|
196 | }//protected function createSubjectFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVar)
|
---|
197 |
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Creates an RDF predicate object
|
---|
202 | * contained in the given $dbRecordSet object.
|
---|
203 | *
|
---|
204 | * @see convertFromDbResult() to understand $strVarBase necessity
|
---|
205 | *
|
---|
206 | * @param ADORecordSet $dbRecordSet Record set returned from ADOConnection::Execute()
|
---|
207 | * @param string $strVarBase Prefix of the columns the recordset fields have.
|
---|
208 | *
|
---|
209 | * @return string HTML code
|
---|
210 | */
|
---|
211 | protected function createPredicateFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVar)
|
---|
212 | {
|
---|
213 | $strVarName = (string)$strVar;
|
---|
214 | if ($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']] === null) {
|
---|
215 | return $this->getHtmlNull();
|
---|
216 | }
|
---|
217 |
|
---|
218 | return $this->getHtmlResource($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']]);
|
---|
219 | }//protected function createPredicateFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVar)
|
---|
220 |
|
---|
221 |
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Creates an RDF object object
|
---|
225 | * contained in the given $dbRecordSet object.
|
---|
226 | *
|
---|
227 | * @see convertFromDbResult() to understand $strVarBase necessity
|
---|
228 | *
|
---|
229 | * @param ADORecordSet $dbRecordSet Record set returned from ADOConnection::Execute()
|
---|
230 | * @param string $strVarBase Prefix of the columns the recordset fields have.
|
---|
231 | *
|
---|
232 | * @return string HTML code
|
---|
233 | */
|
---|
234 | protected function createObjectFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVar)
|
---|
235 | {
|
---|
236 | $strVarName = (string)$strVar;
|
---|
237 | if ($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']] === null) {
|
---|
238 | return $this->getHtmlNull();
|
---|
239 | }
|
---|
240 | switch ($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_is']]) {
|
---|
241 | case 'r':
|
---|
242 | return $this->getHtmlResource($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']]);
|
---|
243 | break;
|
---|
244 | case 'b':
|
---|
245 | return $this->getHtmlBlank($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']]);
|
---|
246 | break;
|
---|
247 | default:
|
---|
248 | return $this->getHtmlLiteral(
|
---|
249 | $dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']],
|
---|
250 | $dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_lang']],
|
---|
251 | $dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_type']]
|
---|
252 | );
|
---|
253 | }
|
---|
254 | }//protected function createObjectFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVar)
|
---|
255 |
|
---|
256 |
|
---|
257 |
|
---|
258 | protected function getHtmlNull()
|
---|
259 | {
|
---|
260 | return array('<pre>NULL</pre>', '#FFF');
|
---|
261 | }//protected function getHtmlNull()
|
---|
262 |
|
---|
263 |
|
---|
264 |
|
---|
265 | protected function getHtmlBlank($value)
|
---|
266 | {
|
---|
267 | return array('<i>Blank node</i>', HTML_TABLE_BNODE_COLOR);
|
---|
268 | }//protected function getHtmlBlank($value)
|
---|
269 |
|
---|
270 |
|
---|
271 |
|
---|
272 | protected function getHtmlResource($value)
|
---|
273 | {
|
---|
274 | return array(
|
---|
275 | htmlspecialchars($value),
|
---|
276 | HTML_TABLE_RESOURCE_COLOR
|
---|
277 | );
|
---|
278 | }//protected function getHtmlResource($value)
|
---|
279 |
|
---|
280 |
|
---|
281 |
|
---|
282 | protected function getHtmlLiteral($value, $language, $datatype)
|
---|
283 | {
|
---|
284 | $strCode = htmlspecialchars($value);
|
---|
285 | if ($language) {
|
---|
286 | $strCode . ' <i>xml:lang</i>=' . $language;
|
---|
287 | }
|
---|
288 | if ($datatype) {
|
---|
289 | $strCode . ' <i>rdf:type</i>=' . $datatype;
|
---|
290 | }
|
---|
291 | return array($strCode, HTML_TABLE_LITERAL_COLOR);
|
---|
292 | }//protected function getHtmlLiteral($value, $language, $datatype)
|
---|
293 |
|
---|
294 |
|
---|
295 | }//class SparqlEngineDb_ResultRenderer_HTML implements SparqlEngineDb_ResultRenderer
|
---|
296 |
|
---|
297 | ?> |
---|