summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Api/ApiQueryResultFormatter.php
blob: 684499a37fd98fc5543c79608d4aedfeb62a7784 (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
<?php

namespace SMW\MediaWiki\Api;

use InvalidArgumentException;
use SMW\ProcessingErrorMsgHandler;
use SMWQueryResult;

/**
 * This class handles the Api related query result formatting
 *
 * @ingroup SMW
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class ApiQueryResultFormatter {

	/**
	 * @var Integer|boolean
	 */
	protected $continueOffset = false;

	/**
	 * @var String
	 */
	protected $type;

	/**
	 * @var Boolean
	 */
	protected $isRawMode = false;

	/**
	 * @since 1.9
	 *
	 * @param SMWQueryResult $queryResult
	 */
	public function __construct( SMWQueryResult $queryResult ) {
		$this->queryResult = $queryResult;
	}

	/**
	 * Sets whether the formatter requested raw data and is used in connection
	 * with ApiQueryResultFormatter::setIndexedTagName
	 *
	 * @see ApiResult::getIsRawMode
	 *
	 * @since 1.9
	 *
	 * @param boolean $isRawMode
	 */
	public function setIsRawMode( $isRawMode ) {
		$this->isRawMode = $isRawMode;
	}

	/**
	 * Returns an offset used for continuation support
	 *
	 * @since 1.9
	 *
	 * @return integer
	 */
	public function getContinueOffset() {
		return $this->continueOffset;
	}

	/**
	 * Returns the result type
	 *
	 * @since 1.9
	 *
	 * @return string
	 */
	public function getType() {
		return $this->type;
	}

	/**
	 * Returns formatted result
	 *
	 * @since 1.9
	 *
	 * @return array
	 */
	public function getResult() {
		return $this->result;
	}

	/**
	 * Result formatting
	 *
	 * @since 1.9
	 */
	public function doFormat() {

		if ( $this->queryResult->getErrors() !== [] ) {
			$this->result = $this->formatErrors(
				ProcessingErrorMsgHandler::normalizeAndDecodeMessages( $this->queryResult->getErrors() )
			);
		} else {
			$this->result = $this->formatResults( $this->queryResult->toArray() );

			if ( $this->queryResult->hasFurtherResults() ) {
				$this->continueOffset = $this->result['meta']['count'] + $this->result['meta']['offset'];
			}
		}
	}

	/**
	 * Formatting a result array to support JSON/XML standards
	 *
	 * @since 1.9
	 *
	 * @param array $queryResult
	 *
	 * @return array
	 */
	protected function formatResults( array $queryResult ) {

		$this->type = 'query';
		$results    = [];

		if ( !$this->isRawMode ) {
			return $queryResult;
		}

		foreach ( $queryResult['results'] as $subjectName => $subject ) {
			$serialized = [];

			foreach ( $subject as $key => $value ) {

				if ( $key === 'printouts' ) {
					$printouts = [];

					foreach ( $subject['printouts'] as $property => $values ) {

						if ( (array)$values === $values ) {
							$this->setIndexedTagName( $values, 'value' );
							$printouts[] = array_merge( [ 'label' => $property ], $values );
						}

					}

					$serialized['printouts'] = $printouts;
					$this->setIndexedTagName( $serialized['printouts'], 'property' );

				} else {
					$serialized[$key] = $value;
				}
			}

			$results[] = $serialized;
		}

		if ( $results !== [] ) {
			$queryResult['results'] = $results;
			$this->setIndexedTagName( $queryResult['results'], 'subject' );
		}

		$this->setIndexedTagName( $queryResult['printrequests'], 'printrequest' );
		$this->setIndexedTagName( $queryResult['meta'], 'meta' );

		return $queryResult;
	}

	/**
	 * Formatting an error array in order to support JSON/XML
	 *
	 * @since 1.9
	 *
	 * @param array $errors
	 *
	 * @return array
	 */
	protected function formatErrors( array $errors ) {

		$this->type      = 'error';
		$result['query'] = $errors;

		$this->setIndexedTagName( $result['query'], 'info' );

		return $result;
	}

	/**
	 * Add '_element' to an array
	 *
	 * @note Copied from ApiResult::setIndexedTagName to avoid having a
	 * constructor injection in order to be able to access this method
	 *
	 * @see ApiResult::setIndexedTagName
	 *
	 * @since 1.9
	 *
	 * @param array &$arr
	 * @param string $tag
	 */
	public function setIndexedTagName( &$arr, $tag = null ) {

		if ( !$this->isRawMode ) {
			return;
		}

		if ( $arr === null || $tag === null || !is_array( $arr ) || is_array( $tag ) ) {
			throw new InvalidArgumentException( "{$tag} was incompatible with the requirements" );
		}

		$arr['_element'] = $tag;
	}

}