bc225ca77cd5e4ceb2b7ef637fe5759d0d2764c5
[lhc/web/wiklou.git] / includes / api / ApiResult.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on Sep 4, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiBase.php' );
30 }
31
32 /**
33 * This class represents the result of the API operations.
34 * It simply wraps a nested array() structure, adding some functions to simplify array's modifications.
35 * As various modules execute, they add different pieces of information to this result,
36 * structuring it as it will be given to the client.
37 *
38 * Each subarray may either be a dictionary - key-value pairs with unique keys,
39 * or lists, where the items are added using $data[] = $value notation.
40 *
41 * There are two special key values that change how XML output is generated:
42 * '_element' This key sets the tag name for the rest of the elements in the current array.
43 * It is only inserted if the formatter returned true for getNeedsRawData()
44 * '*' This key has special meaning only to the XML formatter, and is outputed as is
45 * for all others. In XML it becomes the content of the current element.
46 *
47 * @ingroup API
48 */
49 class ApiResult extends ApiBase {
50
51 private $mData, $mIsRawMode, $mSize, $mCheckingSize;
52
53 /**
54 * Constructor
55 * @param $main ApiMain object
56 */
57 public function __construct( $main ) {
58 parent::__construct( $main, 'result' );
59 $this->mIsRawMode = false;
60 $this->mCheckingSize = true;
61 $this->reset();
62 }
63
64 /**
65 * Clear the current result data.
66 */
67 public function reset() {
68 $this->mData = array();
69 $this->mSize = 0;
70 }
71
72 /**
73 * Call this function when special elements such as '_element'
74 * are needed by the formatter, for example in XML printing.
75 */
76 public function setRawMode() {
77 $this->mIsRawMode = true;
78 }
79
80 /**
81 * Returns true whether the formatter requested raw data.
82 * @return bool
83 */
84 public function getIsRawMode() {
85 return $this->mIsRawMode;
86 }
87
88 /**
89 * Get the result's internal data array (read-only)
90 * @return array
91 */
92 public function getData() {
93 return $this->mData;
94 }
95
96 /**
97 * Get the 'real' size of a result item. This means the strlen() of the item,
98 * or the sum of the strlen()s of the elements if the item is an array.
99 * @param $value mixed
100 * @return int
101 */
102 public static function size( $value ) {
103 $s = 0;
104 if ( is_array( $value ) ) {
105 foreach ( $value as $v ) {
106 $s += self::size( $v );
107 }
108 } elseif ( !is_object( $value ) ) {
109 // Objects can't always be cast to string
110 $s = strlen( $value );
111 }
112 return $s;
113 }
114
115 /**
116 * Get the size of the result, i.e. the amount of bytes in it
117 * @return int
118 */
119 public function getSize() {
120 return $this->mSize;
121 }
122
123 /**
124 * Disable size checking in addValue(). Don't use this unless you
125 * REALLY know what you're doing. Values added while size checking
126 * was disabled will not be counted (ever)
127 */
128 public function disableSizeCheck() {
129 $this->mCheckingSize = false;
130 }
131
132 /**
133 * Re-enable size checking in addValue()
134 */
135 public function enableSizeCheck() {
136 $this->mCheckingSize = true;
137 }
138
139 /**
140 * Add an output value to the array by name.
141 * Verifies that value with the same name has not been added before.
142 * @param $arr array to add $value to
143 * @param $name string Index of $arr to add $value at
144 * @param $value mixed
145 * @param $overwrite bool Whether overwriting an existing element is allowed
146 */
147 public static function setElement( &$arr, $name, $value, $overwrite = false ) {
148 if ( $arr === null || $name === null || $value === null || !is_array( $arr ) || is_array( $name ) ) {
149 ApiBase::dieDebug( __METHOD__, 'Bad parameter' );
150 }
151
152 if ( !isset ( $arr[$name] ) || $overwrite ) {
153 $arr[$name] = $value;
154 } elseif ( is_array( $arr[$name] ) && is_array( $value ) ) {
155 $merged = array_intersect_key( $arr[$name], $value );
156 if ( !count( $merged ) ) {
157 $arr[$name] += $value;
158 } else {
159 ApiBase::dieDebug( __METHOD__, "Attempting to merge element $name" );
160 }
161 } else {
162 ApiBase::dieDebug( __METHOD__, "Attempting to add element $name=$value, existing value is {$arr[$name]}" );
163 }
164 }
165
166 /**
167 * Adds a content element to an array.
168 * Use this function instead of hardcoding the '*' element.
169 * @param $arr array to add the content element to
170 * @param $value Mixed
171 * @param $subElemName string when present, content element is created
172 * as a sub item of $arr. Use this parameter to create elements in
173 * format <elem>text</elem> without attributes
174 */
175 public static function setContent( &$arr, $value, $subElemName = null ) {
176 if ( is_array( $value ) ) {
177 ApiBase::dieDebug( __METHOD__, 'Bad parameter' );
178 }
179 if ( is_null( $subElemName ) ) {
180 ApiResult::setElement( $arr, '*', $value );
181 } else {
182 if ( !isset( $arr[$subElemName] ) ) {
183 $arr[$subElemName] = array();
184 }
185 ApiResult::setElement( $arr[$subElemName], '*', $value );
186 }
187 }
188
189 /**
190 * In case the array contains indexed values (in addition to named),
191 * give all indexed values the given tag name. This function MUST be
192 * called on every arrray that has numerical indexes.
193 * @param $arr array
194 * @param $tag string Tag name
195 */
196 public function setIndexedTagName( &$arr, $tag ) {
197 // In raw mode, add the '_element', otherwise just ignore
198 if ( !$this->getIsRawMode() ) {
199 return;
200 }
201 if ( $arr === null || $tag === null || !is_array( $arr ) || is_array( $tag ) )
202 {
203 ApiBase::dieDebug( __METHOD__, 'Bad parameter' );
204 }
205 // Do not use setElement() as it is ok to call this more than once
206 $arr['_element'] = $tag;
207 }
208
209 /**
210 * Calls setIndexedTagName() on each sub-array of $arr
211 * @param $arr array
212 * @param $tag string Tag name
213 */
214 public function setIndexedTagName_recursive( &$arr, $tag ) {
215 if ( !is_array( $arr ) ) {
216 return;
217 }
218 foreach ( $arr as &$a ) {
219 if ( !is_array( $a ) ) {
220 continue;
221 }
222 $this->setIndexedTagName( $a, $tag );
223 $this->setIndexedTagName_recursive( $a, $tag );
224 }
225 }
226
227 /**
228 * Calls setIndexedTagName() on an array already in the result.
229 * Don't specify a path to a value that's not in the result, or
230 * you'll get nasty errors.
231 * @param $path array Path to the array, like addValue()'s $path
232 * @param $tag string
233 */
234 public function setIndexedTagName_internal( $path, $tag ) {
235 $data = &$this->mData;
236 foreach ( (array)$path as $p ) {
237 if ( !isset( $data[$p] ) ) {
238 $data[$p] = array();
239 }
240 $data = &$data[$p];
241 }
242 if ( is_null( $data ) ) {
243 return;
244 }
245 $this->setIndexedTagName( $data, $tag );
246 }
247
248 /**
249 * Add value to the output data at the given path.
250 * Path is an indexed array, each element specifing the branch at which to add the new value
251 * Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value
252 * If $name is empty, the $value is added as a next list element data[] = $value
253 * @return bool True if $value fits in the result, false if not
254 */
255 public function addValue( $path, $name, $value, $overwrite = false ) {
256 global $wgAPIMaxResultSize;
257 $data = &$this->mData;
258 if ( $this->mCheckingSize ) {
259 $newsize = $this->mSize + self::size( $value );
260 if ( $newsize > $wgAPIMaxResultSize ) {
261 return false;
262 }
263 $this->mSize = $newsize;
264 }
265
266 if ( !is_null( $path ) ) {
267 if ( is_array( $path ) ) {
268 foreach ( $path as $p ) {
269 if ( !isset( $data[$p] ) ) {
270 $data[$p] = array();
271 }
272 $data = &$data[$p];
273 }
274 } else {
275 if ( !isset( $data[$path] ) ) {
276 $data[$path] = array();
277 }
278 $data = &$data[$path];
279 }
280 }
281
282 if ( !$name ) {
283 $data[] = $value; // Add list element
284 } else {
285 self::setElement( $data, $name, $value, $overwrite ); // Add named element
286 }
287 return true;
288 }
289
290 /**
291 * Add a parsed limit=max to the result.
292 *
293 * @param $moduleName string
294 * @param $limit int
295 */
296 public function setParsedLimit( $moduleName, $limit ) {
297 // Add value, allowing overwriting
298 $this->addValue( 'limits', $moduleName, $limit, true );
299 }
300
301 /**
302 * Unset a value previously added to the result set.
303 * Fails silently if the value isn't found.
304 * For parameters, see addValue()
305 * @param $path array
306 * @param $name string
307 */
308 public function unsetValue( $path, $name ) {
309 $data = &$this->mData;
310 if ( !is_null( $path ) ) {
311 foreach ( (array)$path as $p ) {
312 if ( !isset( $data[$p] ) ) {
313 return;
314 }
315 $data = &$data[$p];
316 }
317 }
318 $this->mSize -= self::size( $data[$name] );
319 unset( $data[$name] );
320 }
321
322 /**
323 * Ensure all values in this result are valid UTF-8.
324 */
325 public function cleanUpUTF8() {
326 array_walk_recursive( $this->mData, array( 'ApiResult', 'cleanUp_helper' ) );
327 }
328
329 /**
330 * Callback function for cleanUpUTF8()
331 */
332 private static function cleanUp_helper( &$s ) {
333 if ( !is_string( $s ) ) {
334 return;
335 }
336 global $wgContLang;
337 $s = $wgContLang->normalize( $s );
338 }
339
340 public function execute() {
341 ApiBase::dieDebug( __METHOD__, 'execute() is not supported on Result object' );
342 }
343
344 public function getVersion() {
345 return __CLASS__ . ': $Id$';
346 }
347 }