1 | <?php
|
---|
2 | require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngineDb/ResultRenderer.php';
|
---|
3 | require_once RDFAPI_INCLUDE_DIR . 'util/RdfUtil.php';
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * XML result renderer for SparqlEngine
|
---|
7 | *
|
---|
8 | * @author Tobias Gauà <tobias.gauss@web.de>
|
---|
9 | * @author Christian Weiske <cweiske@cweiske.de>
|
---|
10 | * @license http://www.gnu.org/licenses/lgpl.html LGPL
|
---|
11 | *
|
---|
12 | * @package sparql
|
---|
13 | */
|
---|
14 | class SparqlEngine_ResultRenderer_HTML implements SparqlEngine_ResultRenderer
|
---|
15 | {
|
---|
16 | /**
|
---|
17 | * If the result HTML should be wrapped in a div
|
---|
18 | * @var boolean
|
---|
19 | */
|
---|
20 | protected $bWrap = true;
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Converts the database results into the output format
|
---|
26 | * and returns the result.
|
---|
27 | *
|
---|
28 | * @param array $arVartable Variable table
|
---|
29 | * @param Query $query SPARQL query object
|
---|
30 | * @param SparqlEngine $engine Sparql Engine to query the database
|
---|
31 | * @return string HTML result
|
---|
32 | */
|
---|
33 | public function convertFromResult($arVartable, Query $query, SparqlEngine $engine)
|
---|
34 | {
|
---|
35 | $this->query = $query;
|
---|
36 | $this->engine = $engine;
|
---|
37 | $this->dataset = $engine->getDataset();
|
---|
38 |
|
---|
39 | $strCode = '';
|
---|
40 |
|
---|
41 | $strResultForm = $query->getResultForm();
|
---|
42 | switch ($strResultForm) {
|
---|
43 | case 'select':
|
---|
44 | case 'select distinct':
|
---|
45 | $strCode = $this->createTableFromRecords($arVartable);
|
---|
46 | break;
|
---|
47 |
|
---|
48 | case 'construct':
|
---|
49 | case 'describe':
|
---|
50 | throw new Exception(
|
---|
51 | 'Construct and describe are currently not supported by the'
|
---|
52 | . ' HTML renderer'
|
---|
53 | );
|
---|
54 |
|
---|
55 | case 'count':
|
---|
56 | case 'ask':
|
---|
57 | $nCount = count($arVartable);
|
---|
58 |
|
---|
59 | if ($strResultForm == 'ask') {
|
---|
60 | $strCode = 'There were results.';
|
---|
61 | } else {
|
---|
62 | $strCode = 'There are ' . $nCount . ' results.';
|
---|
63 | }
|
---|
64 | break;
|
---|
65 |
|
---|
66 | default:
|
---|
67 | throw new Exception('Unsupported result form: ' . $strResultForm);
|
---|
68 | }
|
---|
69 |
|
---|
70 | return $this->wrapCode($strCode);
|
---|
71 | }//public function convertFromResult($arVartable, Query $query, SparqlEngine $engine)
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 | protected function wrapCode($strCode)
|
---|
76 | {
|
---|
77 | if (!$this->bWrap) {
|
---|
78 | return $strCode;
|
---|
79 | }
|
---|
80 |
|
---|
81 | return
|
---|
82 | '<div class="SparqlEngine_ResultRenderer_HTML_result">' . "\n"
|
---|
83 | . $strCode . "\n"
|
---|
84 | . "</div>\n";
|
---|
85 | }//protected function wrapCode($strCode)
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 | protected function createTableFromRecords($arVartable)
|
---|
90 | {
|
---|
91 | if (count($arVartable) == 0) {
|
---|
92 | return 'No result rows.';
|
---|
93 | }
|
---|
94 |
|
---|
95 | $arResult = array();
|
---|
96 | foreach ($arVartable as $row) {
|
---|
97 | $arResultRow = array();
|
---|
98 | foreach ($row as $strVarName => $value) {
|
---|
99 | $arResultRow[$strVarName] = $this->createValue($value);
|
---|
100 | }
|
---|
101 | $arResult[] = $arResultRow;
|
---|
102 | }
|
---|
103 |
|
---|
104 | //I always wanted to to this :)
|
---|
105 | return
|
---|
106 | "<table border='1'>\n"
|
---|
107 | . " <caption>SPARQL result with " . count($arResult) . " rows</caption>\n"
|
---|
108 | . " <thead><th>"
|
---|
109 | . implode('</th><th>', array_keys(reset($arResult)))
|
---|
110 | . "</th></thead>\n"
|
---|
111 | . " <tbody>\n <tr>"
|
---|
112 | . implode(
|
---|
113 | "</tr>\n <tr>",
|
---|
114 | array_map(
|
---|
115 | create_function(
|
---|
116 | '$ar',
|
---|
117 | 'return implode("", $ar);'
|
---|
118 | ),
|
---|
119 | $arResult
|
---|
120 | )
|
---|
121 | )
|
---|
122 | . "</tr>\n </tbody>\n"
|
---|
123 | . "</table>\n";
|
---|
124 | }//protected function createTableFromRecords($arRecordSets)
|
---|
125 |
|
---|
126 |
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Creates an RDF object object
|
---|
130 | * contained in the given $dbRecordSet object.
|
---|
131 | *
|
---|
132 | * @see convertFromDbResult() to understand $strVarBase necessity
|
---|
133 | *
|
---|
134 | * @param ADORecordSet $dbRecordSet Record set returned from ADOConnection::Execute()
|
---|
135 | * @param string $strVarBase Prefix of the columns the recordset fields have.
|
---|
136 | *
|
---|
137 | * @return string HTML code
|
---|
138 | */
|
---|
139 | protected function createValue($value)
|
---|
140 | {
|
---|
141 | if ($value === null) {
|
---|
142 | $strCode = $this->getHtmlNull();
|
---|
143 | }
|
---|
144 | if ($value instanceof Literal) {
|
---|
145 | $strCode = $this->getHtmlLiteral(
|
---|
146 | $value->getLabel(),
|
---|
147 | $value->getLanguage(),
|
---|
148 | $value->getDatatype()
|
---|
149 | );
|
---|
150 | } else if ($value instanceof Resource) {
|
---|
151 | $strCode = $this->getHtmlResource($value->getURI());
|
---|
152 | } else {
|
---|
153 | $strCode = $this->getHtmlBlank();
|
---|
154 | }
|
---|
155 |
|
---|
156 | $rdfUtil = new RdfUtil();
|
---|
157 |
|
---|
158 | return '<td style="background-color: '
|
---|
159 | . $rdfUtil->chooseColor($value) . '">'
|
---|
160 | . $strCode
|
---|
161 | . '</td>';
|
---|
162 | }//protected function createObjectFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVarName)
|
---|
163 |
|
---|
164 |
|
---|
165 |
|
---|
166 | protected function getHtmlNull()
|
---|
167 | {
|
---|
168 | return '<pre>NULL</pre>';
|
---|
169 | }//protected function getHtmlNull()
|
---|
170 |
|
---|
171 |
|
---|
172 |
|
---|
173 | protected function getHtmlBlank($value)
|
---|
174 | {
|
---|
175 | return '<i>Blank node</i>';
|
---|
176 | }//protected function getHtmlBlank($value)
|
---|
177 |
|
---|
178 |
|
---|
179 |
|
---|
180 | protected function getHtmlResource($value)
|
---|
181 | {
|
---|
182 | return htmlspecialchars($value);
|
---|
183 | }//protected function getHtmlResource($value)
|
---|
184 |
|
---|
185 |
|
---|
186 |
|
---|
187 | protected function getHtmlLiteral($value, $language, $datatype)
|
---|
188 | {
|
---|
189 | $strCode = htmlspecialchars($value);
|
---|
190 | if ($language) {
|
---|
191 | $strCode .= '<br/> <i>xml:lang</i>=' . $language;
|
---|
192 | }
|
---|
193 | if ($datatype) {
|
---|
194 | $strCode .= '<br/> <i>rdf:type</i>=' . $datatype;
|
---|
195 | }
|
---|
196 | return $strCode;
|
---|
197 | }//protected function getHtmlLiteral($value, $language, $datatype)
|
---|
198 |
|
---|
199 | }//class SparqlEngine_ResultRenderer_HTML implements SparqlEngine_ResultRenderer
|
---|
200 |
|
---|
201 | ?> |
---|