5 * Created on Sep 4, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
28 * This class represents the result of the API operations.
29 * It simply wraps a nested array() structure, adding some functions to simplify
30 * array's modifications. As various modules execute, they add different pieces
31 * of information to this result, structuring it as it will be given to the client.
33 * Each subarray may either be a dictionary - key-value pairs with unique keys,
34 * or lists, where the items are added using $data[] = $value notation.
36 * There are two special key values that change how XML output is generated:
37 * '_element' This key sets the tag name for the rest of the elements in the current array.
38 * It is only inserted if the formatter returned true for getNeedsRawData()
39 * '*' This key has special meaning only to the XML formatter, and is outputted as is
40 * for all others. In XML it becomes the content of the current element.
44 class ApiResult
extends ApiBase
{
47 * override existing value in addValue() and setElement()
53 * For addValue() and setElement(), if the value does not exist, add it as the first element.
54 * In case the new value has no name (numerical index), all indexes will be renumbered.
59 private $mData, $mIsRawMode, $mSize, $mCheckingSize;
62 * @param ApiMain $main
64 public function __construct( ApiMain
$main ) {
65 parent
::__construct( $main, 'result' );
66 $this->mIsRawMode
= false;
67 $this->mCheckingSize
= true;
72 * Clear the current result data.
74 public function reset() {
75 $this->mData
= array();
80 * Call this function when special elements such as '_element'
81 * are needed by the formatter, for example in XML printing.
82 * @since 1.23 $flag parameter added
83 * @param bool $flag Set the raw mode flag to this state
85 public function setRawMode( $flag = true ) {
86 $this->mIsRawMode
= $flag;
90 * Returns true whether the formatter requested raw data.
93 public function getIsRawMode() {
94 return $this->mIsRawMode
;
98 * Get the result's internal data array (read-only)
101 public function getData() {
106 * Get the 'real' size of a result item. This means the strlen() of the item,
107 * or the sum of the strlen()s of the elements if the item is an array.
108 * @param mixed $value
111 public static function size( $value ) {
113 if ( is_array( $value ) ) {
114 foreach ( $value as $v ) {
115 $s +
= self
::size( $v );
117 } elseif ( !is_object( $value ) ) {
118 // Objects can't always be cast to string
119 $s = strlen( $value );
126 * Get the size of the result, i.e. the amount of bytes in it
129 public function getSize() {
134 * Disable size checking in addValue(). Don't use this unless you
135 * REALLY know what you're doing. Values added while size checking
136 * was disabled will not be counted (ever)
138 public function disableSizeCheck() {
139 $this->mCheckingSize
= false;
143 * Re-enable size checking in addValue()
145 public function enableSizeCheck() {
146 $this->mCheckingSize
= true;
150 * Add an output value to the array by name.
151 * Verifies that value with the same name has not been added before.
152 * @param array $arr To add $value to
153 * @param string $name Index of $arr to add $value at
154 * @param mixed $value
155 * @param int $flags Zero or more OR-ed flags like OVERRIDE | ADD_ON_TOP.
156 * This parameter used to be boolean, and the value of OVERRIDE=1 was
157 * specifically chosen so that it would be backwards compatible with the
158 * new method signature.
160 * @since 1.21 int $flags replaced boolean $override
162 public static function setElement( &$arr, $name, $value, $flags = 0 ) {
163 if ( $arr === null ||
$name === null ||
$value === null
164 ||
!is_array( $arr ) ||
is_array( $name )
166 ApiBase
::dieDebug( __METHOD__
, 'Bad parameter' );
169 $exists = isset( $arr[$name] );
170 if ( !$exists ||
( $flags & ApiResult
::OVERRIDE
) ) {
171 if ( !$exists && ( $flags & ApiResult
::ADD_ON_TOP
) ) {
172 $arr = array( $name => $value ) +
$arr;
174 $arr[$name] = $value;
176 } elseif ( is_array( $arr[$name] ) && is_array( $value ) ) {
177 $merged = array_intersect_key( $arr[$name], $value );
178 if ( !count( $merged ) ) {
179 $arr[$name] +
= $value;
181 ApiBase
::dieDebug( __METHOD__
, "Attempting to merge element $name" );
186 "Attempting to add element $name=$value, existing value is {$arr[$name]}"
192 * Adds a content element to an array.
193 * Use this function instead of hardcoding the '*' element.
194 * @param array $arr To add the content element to
195 * @param mixed $value
196 * @param string $subElemName When present, content element is created
197 * as a sub item of $arr. Use this parameter to create elements in
198 * format "<elem>text</elem>" without attributes.
200 public static function setContent( &$arr, $value, $subElemName = null ) {
201 if ( is_array( $value ) ) {
202 ApiBase
::dieDebug( __METHOD__
, 'Bad parameter' );
204 if ( is_null( $subElemName ) ) {
205 ApiResult
::setElement( $arr, '*', $value );
207 if ( !isset( $arr[$subElemName] ) ) {
208 $arr[$subElemName] = array();
210 ApiResult
::setElement( $arr[$subElemName], '*', $value );
215 * In case the array contains indexed values (in addition to named),
216 * give all indexed values the given tag name. This function MUST be
217 * called on every array that has numerical indexes.
219 * @param string $tag Tag name
221 public function setIndexedTagName( &$arr, $tag ) {
222 // In raw mode, add the '_element', otherwise just ignore
223 if ( !$this->getIsRawMode() ) {
226 if ( $arr === null ||
$tag === null ||
!is_array( $arr ) ||
is_array( $tag ) ) {
227 ApiBase
::dieDebug( __METHOD__
, 'Bad parameter' );
229 // Do not use setElement() as it is ok to call this more than once
230 $arr['_element'] = $tag;
234 * Calls setIndexedTagName() on each sub-array of $arr
236 * @param string $tag Tag name
238 public function setIndexedTagName_recursive( &$arr, $tag ) {
239 if ( !is_array( $arr ) ) {
242 foreach ( $arr as &$a ) {
243 if ( !is_array( $a ) ) {
246 $this->setIndexedTagName( $a, $tag );
247 $this->setIndexedTagName_recursive( $a, $tag );
252 * Calls setIndexedTagName() on an array already in the result.
253 * Don't specify a path to a value that's not in the result, or
254 * you'll get nasty errors.
255 * @param array $path Path to the array, like addValue()'s $path
258 public function setIndexedTagName_internal( $path, $tag ) {
259 $data = &$this->mData
;
260 foreach ( (array)$path as $p ) {
261 if ( !isset( $data[$p] ) ) {
266 if ( is_null( $data ) ) {
269 $this->setIndexedTagName( $data, $tag );
273 * Add value to the output data at the given path.
274 * Path can be an indexed array, each element specifying the branch at which to add the new
275 * value. Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value.
276 * If $path is null, the value will be inserted at the data root.
277 * If $name is empty, the $value is added as a next list element data[] = $value.
279 * @param array|string|null $path
280 * @param string $name
281 * @param mixed $value
282 * @param int $flags Zero or more OR-ed flags like OVERRIDE | ADD_ON_TOP. This
283 * parameter used to be boolean, and the value of OVERRIDE=1 was specifically
284 * chosen so that it would be backwards compatible with the new method
286 * @return bool True if $value fits in the result, false if not
288 * @since 1.21 int $flags replaced boolean $override
290 public function addValue( $path, $name, $value, $flags = 0 ) {
291 global $wgAPIMaxResultSize;
293 $data = &$this->mData
;
294 if ( $this->mCheckingSize
) {
295 $newsize = $this->mSize + self
::size( $value );
296 if ( $newsize > $wgAPIMaxResultSize ) {
298 "This result was truncated because it would otherwise be larger than the " .
299 "limit of {$wgAPIMaxResultSize} bytes" );
303 $this->mSize
= $newsize;
306 $addOnTop = $flags & ApiResult
::ADD_ON_TOP
;
307 if ( $path !== null ) {
308 foreach ( (array)$path as $p ) {
309 if ( !isset( $data[$p] ) ) {
311 $data = array( $p => array() ) +
$data;
324 // This element needs to be inserted in the beginning
325 // Numerical indexes will be renumbered
326 array_unshift( $data, $value );
328 // Add new value at the end
333 self
::setElement( $data, $name, $value, $flags );
340 * Add a parsed limit=max to the result.
342 * @param string $moduleName
345 public function setParsedLimit( $moduleName, $limit ) {
346 // Add value, allowing overwriting
347 $this->addValue( 'limits', $moduleName, $limit, ApiResult
::OVERRIDE
);
351 * Unset a value previously added to the result set.
352 * Fails silently if the value isn't found.
353 * For parameters, see addValue()
354 * @param array|null $path
355 * @param string $name
357 public function unsetValue( $path, $name ) {
358 $data = &$this->mData
;
359 if ( $path !== null ) {
360 foreach ( (array)$path as $p ) {
361 if ( !isset( $data[$p] ) ) {
367 $this->mSize
-= self
::size( $data[$name] );
368 unset( $data[$name] );
372 * Ensure all values in this result are valid UTF-8.
374 public function cleanUpUTF8() {
375 array_walk_recursive( $this->mData
, array( 'ApiResult', 'cleanUp_helper' ) );
379 * Callback function for cleanUpUTF8()
383 private static function cleanUp_helper( &$s ) {
384 if ( !is_string( $s ) ) {
388 $s = $wgContLang->normalize( $s );
392 * Converts a Status object to an array suitable for addValue
393 * @param Status $status
394 * @param string $errorType
397 public function convertStatusToArray( $status, $errorType = 'error' ) {
398 if ( $status->isGood() ) {
403 foreach ( $status->getErrorsByType( $errorType ) as $error ) {
404 $this->setIndexedTagName( $error['params'], 'param' );
407 $this->setIndexedTagName( $result, $errorType );
412 public function execute() {
413 ApiBase
::dieDebug( __METHOD__
, 'execute() is not supported on Result object' );