summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SPARQLStore/QueryEngine/XmlResponseParser.php
blob: 24182051f551d0b2b3f77943a9122cc250bb4fb2 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php

namespace SMW\SPARQLStore\QueryEngine;

use SMW\SPARQLStore\Exception\XmlParserException;
use SMW\SPARQLStore\HttpResponseParser;
use SMWExpLiteral as ExpLiteral;
use SMWExpResource as ExpResource;

/**
 * Class for parsing SPARQL results in XML format
 *
 * @license GNU GPL v2+
 * @since 1.6
 *
 * @author Markus Krötzsch
 */
class XmlResponseParser implements HttpResponseParser {

	/**
	 * @var resource
	 */
	private $parser;

	/**
	 * Associative array mapping SPARQL variable names to column indices.
	 * @var array of integer
	 */
	private $header;

	/**
	 * List of result rows. Individual entries can be null if a cell in the
	 * SPARQL result table is empty (this is different from finding a blank
	 * node).
	 * @var array of array of (SMWExpElement or null)
	 */
	private $data;

	/**
	 * List of comment strings found in the XML file (without surrounding
	 * markup, i.e. the actual string only).
	 * @var array of string
	 */
	private $comments;

	/**
	 * Stack of open XML tags during parsing.
	 * @var array of string
	 */
	private $xmlOpenTags;

	/**
	 * Integer index of the column that the current result binding fills.
	 * @var integer
	 */
	private $xmlBindIndex;

	/**
	 * Datatype URI for the current literal, or empty if none.
	 * @var string
	 */
	private $currentDataType;

	/**
	 * @since 2.0
	 */
	public function __construct() {
		$this->parser = xml_parser_create();

		xml_parser_set_option( $this->parser, XML_OPTION_SKIP_WHITE, 0 );
		xml_parser_set_option( $this->parser, XML_OPTION_TARGET_ENCODING, 'UTF-8' );
		xml_parser_set_option( $this->parser, XML_OPTION_CASE_FOLDING, 0 );
		xml_set_object( $this->parser, $this );
		xml_set_element_handler( $this->parser, 'handleOpenElement', 'handleCloseElement' );
		xml_set_character_data_handler( $this->parser, 'handleCharacterData' );
		xml_set_default_handler( $this->parser, 'handleDefault' );
		//xml_set_start_namespace_decl_handler($parser, 'handleNsDeclaration' );
	}

	/**
	 * @since 2.0
	 */
	public function __destruct() {
		xml_parser_free( $this->parser );
	}

	/**
	 * Parse the given XML result and return an RepositoryResult for
	 * the contained data.
	 *
	 * @param string $response
	 *
	 * @return RepositoryResult
	 * @throws XmlParserException
	 */
	public function parse( $response ) {

		$this->xmlOpenTags = [];
		$this->header = [];
		$this->data = [];
		$this->comments = [];

		// Sesame can return "" result
		if ( $response === '' ) {
			$this->data = [ [ new ExpLiteral( 'false', 'http://www.w3.org/2001/XMLSchema#boolean' ) ] ];
		}

		// #626 Virtuoso
		if ( $response == 'true' ) {
			$this->data = [ [ new ExpLiteral( 'true', 'http://www.w3.org/2001/XMLSchema#boolean' ) ] ];
		}

		// #474 Virtuoso allows `false` to be a valid raw result
		if ( $response === '' || $response == 'false' || $response == 'true' || is_bool( $response ) || $this->parseXml( $response ) ) {
			return new RepositoryResult(
				$this->header,
				$this->data,
				$this->comments
			);
		}

		throw new XmlParserException(
			$this->getLastError(),
			$this->getLastLineNumber(),
			$this->getLastColumnNumber()
		);
	}

	private function parseXml( $xmlResultData ) {
		return xml_parse( $this->parser, $xmlResultData, true );
	}

	private function getLastError() {
		return xml_error_string( xml_get_error_code( $this->parser ) );
	}

	private function getLastLineNumber() {
		return xml_get_current_line_number( $this->parser );
	}

	private function getLastColumnNumber() {
		return xml_get_current_column_number ( $this->parser );
	}

	private function handleDefault( $parser, $data ) {
		if ( substr( $data, 0, 4 ) == '<!--' ) {
			$comment = substr( $data, 4, strlen( $data ) - 7 );
			$this->comments[] = trim( $comment );
		}
	}

	/**
	 * @see xml_set_element_handler
	 */
	private function handleOpenElement( $parser, $elementTag, $attributes ) {

		$this->currentDataType = '';

		$prevTag = end( $this->xmlOpenTags );
		$this->xmlOpenTags[] = $elementTag;

		switch ( $elementTag ) {
			case 'binding' && ( $prevTag == 'result' ):
					if ( ( array_key_exists( 'name', $attributes ) ) &&
					     ( array_key_exists( $attributes['name'], $this->header ) ) ) {
						 $this->xmlBindIndex = $this->header[$attributes['name']];
					}
				break;
			case 'result' && ( $prevTag == 'results' ):
				$this->data[] = array_fill( 0, count( $this->header ), null );
				break;
			case  'literal' && ( $prevTag == 'binding' ):
				if ( array_key_exists( 'datatype', $attributes ) ) {
					$this->currentDataType = $attributes['datatype'];
				}
				/// TODO handle xml:lang attributes here as well?
				break;
			case  'variable' && ( $prevTag == 'head' ):
				if ( array_key_exists( 'name', $attributes ) ) {
					$this->header[$attributes['name']] = count( $this->header );
				}
				break;
		}
	}

	/**
	 * @see xml_set_element_handler
	 */
	private function handleCloseElement( $parser, $elementTag ) {
		array_pop( $this->xmlOpenTags );
	}

	/**
	 * @see xml_set_character_data_handler
	 */
	private function handleCharacterData( $parser, $characterData ) {

		$prevTag = end( $this->xmlOpenTags );
		$rowcount = count( $this->data ) - 1;

		// UTF-8 is being split therefore concatenate the string (use row as indicator
		// to detect a sliced string)
		if ( isset( $this->data[$rowcount] ) && ( $element = end( $this->data[$rowcount] ) ) !== null ) {
			switch ( $prevTag ) {
				case 'uri':
					$characterData = $element->getUri() . $characterData;
					break;
				case 'literal':
					$characterData = $element->getLexicalForm() . $characterData;
			}
		}

		switch ( $prevTag ) {
			case 'uri':
				$this->data[$rowcount][$this->xmlBindIndex] = new ExpResource( $characterData );
				break;
			case 'literal':
				$this->data[$rowcount][$this->xmlBindIndex] = new ExpLiteral( $characterData, $this->currentDataType );
				break;
			case 'bnode':
				$this->data[$rowcount][$this->xmlBindIndex] = new ExpResource( '_' . $characterData );
				break;
			case 'boolean':
				// no "results" in this case
				$literal = new ExpLiteral( $characterData, 'http://www.w3.org/2001/XMLSchema#boolean' );

				// ?? Really !!
				$this->data = [ [ $literal ] ];
				$this->header = [ '' => 0 ];
				break;
		}
	}

}