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 three 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 * '_subelements' This key causes the specified elements to be returned as subelements rather than attributes.
40 * It is only inserted if the formatter returned true for getNeedsRawData()
41 * '*' This key has special meaning only to the XML formatter, and is outputted as is
42 * for all others. In XML it becomes the content of the current element.
46 class ApiResult
extends ApiBase
{
49 * override existing value in addValue() and setElement()
55 * For addValue() and setElement(), if the value does not exist, add it as the first element.
56 * In case the new value has no name (numerical index), all indexes will be renumbered.
61 private $mData, $mIsRawMode, $mSize, $mCheckingSize;
63 private $continueAllModules = array();
64 private $continueGeneratedModules = array();
65 private $continuationData = array();
66 private $generatorContinuationData = array();
67 private $generatorParams = array();
68 private $generatorDone = false;
71 * @param ApiMain $main
73 public function __construct( ApiMain
$main ) {
74 parent
::__construct( $main, 'result' );
75 $this->mIsRawMode
= false;
76 $this->mCheckingSize
= true;
81 * Clear the current result data.
83 public function reset() {
84 $this->mData
= array();
89 * Call this function when special elements such as '_element'
90 * are needed by the formatter, for example in XML printing.
91 * @since 1.23 $flag parameter added
92 * @param bool $flag Set the raw mode flag to this state
94 public function setRawMode( $flag = true ) {
95 $this->mIsRawMode
= $flag;
99 * Returns true whether the formatter requested raw data.
102 public function getIsRawMode() {
103 return $this->mIsRawMode
;
107 * Get the result's internal data array (read-only)
110 public function getData() {
115 * Get the 'real' size of a result item. This means the strlen() of the item,
116 * or the sum of the strlen()s of the elements if the item is an array.
117 * @param mixed $value
120 public static function size( $value ) {
122 if ( is_array( $value ) ) {
123 foreach ( $value as $v ) {
124 $s +
= self
::size( $v );
126 } elseif ( !is_object( $value ) ) {
127 // Objects can't always be cast to string
128 $s = strlen( $value );
135 * Get the size of the result, i.e. the amount of bytes in it
138 public function getSize() {
143 * Disable size checking in addValue(). Don't use this unless you
144 * REALLY know what you're doing. Values added while size checking
145 * was disabled will not be counted (ever)
147 public function disableSizeCheck() {
148 $this->mCheckingSize
= false;
152 * Re-enable size checking in addValue()
154 public function enableSizeCheck() {
155 $this->mCheckingSize
= true;
159 * Add an output value to the array by name.
160 * Verifies that value with the same name has not been added before.
161 * @param array $arr To add $value to
162 * @param string $name Index of $arr to add $value at
163 * @param mixed $value
164 * @param int $flags Zero or more OR-ed flags like OVERRIDE | ADD_ON_TOP.
165 * This parameter used to be boolean, and the value of OVERRIDE=1 was
166 * specifically chosen so that it would be backwards compatible with the
167 * new method signature.
169 * @since 1.21 int $flags replaced boolean $override
171 public static function setElement( &$arr, $name, $value, $flags = 0 ) {
172 if ( $arr === null ||
$name === null ||
$value === null
173 ||
!is_array( $arr ) ||
is_array( $name )
175 ApiBase
::dieDebug( __METHOD__
, 'Bad parameter' );
178 $exists = isset( $arr[$name] );
179 if ( !$exists ||
( $flags & ApiResult
::OVERRIDE
) ) {
180 if ( !$exists && ( $flags & ApiResult
::ADD_ON_TOP
) ) {
181 $arr = array( $name => $value ) +
$arr;
183 $arr[$name] = $value;
185 } elseif ( is_array( $arr[$name] ) && is_array( $value ) ) {
186 $merged = array_intersect_key( $arr[$name], $value );
187 if ( !count( $merged ) ) {
188 $arr[$name] +
= $value;
190 ApiBase
::dieDebug( __METHOD__
, "Attempting to merge element $name" );
195 "Attempting to add element $name=$value, existing value is {$arr[$name]}"
201 * Adds a content element to an array.
202 * Use this function instead of hardcoding the '*' element.
203 * @param array $arr To add the content element to
204 * @param mixed $value
205 * @param string $subElemName When present, content element is created
206 * as a sub item of $arr. Use this parameter to create elements in
207 * format "<elem>text</elem>" without attributes.
209 public static function setContent( &$arr, $value, $subElemName = null ) {
210 if ( is_array( $value ) ) {
211 ApiBase
::dieDebug( __METHOD__
, 'Bad parameter' );
213 if ( is_null( $subElemName ) ) {
214 ApiResult
::setElement( $arr, '*', $value );
216 if ( !isset( $arr[$subElemName] ) ) {
217 $arr[$subElemName] = array();
219 ApiResult
::setElement( $arr[$subElemName], '*', $value );
224 * Causes the elements with the specified names to be output as
225 * subelements rather than attributes.
227 * @param array|string $names The element name(s) to be output as subelements
229 public function setSubelements( &$arr, $names ) {
230 // In raw mode, add the '_subelements', otherwise just ignore
231 if ( !$this->getIsRawMode() ) {
234 if ( $arr === null ||
$names === null ||
!is_array( $arr ) ) {
235 ApiBase
::dieDebug( __METHOD__
, 'Bad parameter' );
237 if ( !is_array( $names ) ) {
238 $names = array( $names );
240 if ( !isset( $arr['_subelements'] ) ) {
241 $arr['_subelements'] = $names;
243 $arr['_subelements'] = array_merge( $arr['_subelements'], $names );
248 * In case the array contains indexed values (in addition to named),
249 * give all indexed values the given tag name. This function MUST be
250 * called on every array that has numerical indexes.
252 * @param string $tag Tag name
254 public function setIndexedTagName( &$arr, $tag ) {
255 // In raw mode, add the '_element', otherwise just ignore
256 if ( !$this->getIsRawMode() ) {
259 if ( $arr === null ||
$tag === null ||
!is_array( $arr ) ||
is_array( $tag ) ) {
260 ApiBase
::dieDebug( __METHOD__
, 'Bad parameter' );
262 // Do not use setElement() as it is ok to call this more than once
263 $arr['_element'] = $tag;
267 * Calls setIndexedTagName() on each sub-array of $arr
269 * @param string $tag Tag name
271 public function setIndexedTagName_recursive( &$arr, $tag ) {
272 if ( !is_array( $arr ) ) {
275 foreach ( $arr as &$a ) {
276 if ( !is_array( $a ) ) {
279 $this->setIndexedTagName( $a, $tag );
280 $this->setIndexedTagName_recursive( $a, $tag );
285 * Calls setIndexedTagName() on an array already in the result.
286 * Don't specify a path to a value that's not in the result, or
287 * you'll get nasty errors.
288 * @param array $path Path to the array, like addValue()'s $path
291 public function setIndexedTagName_internal( $path, $tag ) {
292 $data = &$this->mData
;
293 foreach ( (array)$path as $p ) {
294 if ( !isset( $data[$p] ) ) {
299 if ( is_null( $data ) ) {
302 $this->setIndexedTagName( $data, $tag );
306 * Add value to the output data at the given path.
307 * Path can be an indexed array, each element specifying the branch at which to add the new
308 * value. Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value.
309 * If $path is null, the value will be inserted at the data root.
310 * If $name is empty, the $value is added as a next list element data[] = $value.
312 * @param array|string|null $path
313 * @param string $name
314 * @param mixed $value
315 * @param int $flags Zero or more OR-ed flags like OVERRIDE | ADD_ON_TOP. This
316 * parameter used to be boolean, and the value of OVERRIDE=1 was specifically
317 * chosen so that it would be backwards compatible with the new method
319 * @return bool True if $value fits in the result, false if not
321 * @since 1.21 int $flags replaced boolean $override
323 public function addValue( $path, $name, $value, $flags = 0 ) {
324 $data = &$this->mData
;
325 if ( $this->mCheckingSize
) {
326 $newsize = $this->mSize + self
::size( $value );
327 $maxResultSize = $this->getConfig()->get( 'APIMaxResultSize' );
328 if ( $newsize > $maxResultSize ) {
330 "This result was truncated because it would otherwise be larger than the " .
331 "limit of {$maxResultSize} bytes" );
335 $this->mSize
= $newsize;
338 $addOnTop = $flags & ApiResult
::ADD_ON_TOP
;
339 if ( $path !== null ) {
340 foreach ( (array)$path as $p ) {
341 if ( !isset( $data[$p] ) ) {
343 $data = array( $p => array() ) +
$data;
356 // This element needs to be inserted in the beginning
357 // Numerical indexes will be renumbered
358 array_unshift( $data, $value );
360 // Add new value at the end
365 self
::setElement( $data, $name, $value, $flags );
372 * Add a parsed limit=max to the result.
374 * @param string $moduleName
377 public function setParsedLimit( $moduleName, $limit ) {
378 // Add value, allowing overwriting
379 $this->addValue( 'limits', $moduleName, $limit, ApiResult
::OVERRIDE
);
383 * Unset a value previously added to the result set.
384 * Fails silently if the value isn't found.
385 * For parameters, see addValue()
386 * @param array|null $path
387 * @param string $name
389 public function unsetValue( $path, $name ) {
390 $data = &$this->mData
;
391 if ( $path !== null ) {
392 foreach ( (array)$path as $p ) {
393 if ( !isset( $data[$p] ) ) {
399 $this->mSize
-= self
::size( $data[$name] );
400 unset( $data[$name] );
404 * Ensure all values in this result are valid UTF-8.
406 public function cleanUpUTF8() {
407 array_walk_recursive( $this->mData
, array( 'ApiResult', 'cleanUp_helper' ) );
411 * Callback function for cleanUpUTF8()
415 private static function cleanUp_helper( &$s ) {
416 if ( !is_string( $s ) ) {
420 $s = $wgContLang->normalize( $s );
424 * Converts a Status object to an array suitable for addValue
425 * @param Status $status
426 * @param string $errorType
429 public function convertStatusToArray( $status, $errorType = 'error' ) {
430 if ( $status->isGood() ) {
435 foreach ( $status->getErrorsByType( $errorType ) as $error ) {
436 $this->setIndexedTagName( $error['params'], 'param' );
439 $this->setIndexedTagName( $result, $errorType );
444 public function execute() {
445 ApiBase
::dieDebug( __METHOD__
, 'execute() is not supported on Result object' );
449 * Parse a 'continue' parameter and return status information.
451 * This must be balanced by a call to endContinuation().
454 * @param string|null $continue The "continue" parameter, if any
455 * @param array $allModules Contains ApiBase instances that will be executed
456 * @param array $generatedModules Names of modules that depend on the generator
457 * @return array Two elements: a boolean indicating if the generator is done,
458 * and an array of modules to actually execute.
460 public function beginContinuation(
461 $continue, array $allModules = array(), array $generatedModules = array()
463 $this->continueGeneratedModules
= $generatedModules
464 ?
array_combine( $generatedModules, $generatedModules )
466 $this->continuationData
= array();
467 $this->generatorContinuationData
= array();
468 $this->generatorParams
= array();
471 if ( is_string( $continue ) && $continue !== '' ) {
472 $continue = explode( '||', $continue );
473 $this->dieContinueUsageIf( count( $continue ) !== 2 );
474 $this->generatorDone
= ( $continue[0] === '-' );
475 if ( !$this->generatorDone
) {
476 $this->generatorParams
= explode( '|', $continue[0] );
478 $skip = explode( '|', $continue[1] );
481 $this->continueAllModules
= array();
482 $runModules = array();
483 foreach ( $allModules as $module ) {
484 $name = $module->getModuleName();
485 if ( in_array( $name, $skip ) ) {
486 $this->continueAllModules
[$name] = false;
487 // Prevent spurious "unused parameter" warnings
488 $module->extractRequestParams();
490 $this->continueAllModules
[$name] = true;
491 $runModules[] = $module;
496 $this->generatorDone
,
502 * Set the continuation parameter for a module
505 * @param ApiBase $module
506 * @param string $paramName
507 * @param string|array $paramValue
509 public function setContinueParam( ApiBase
$module, $paramName, $paramValue ) {
510 $name = $module->getModuleName();
511 if ( !isset( $this->continueAllModules
[$name] ) ) {
512 throw new MWException(
513 "Module '$name' called ApiResult::setContinueParam but was not " .
514 'passed to ApiResult::beginContinuation'
517 if ( !$this->continueAllModules
[$name] ) {
518 throw new MWException(
519 "Module '$name' was not supposed to have been executed, but " .
520 'it was executed anyway'
523 $paramName = $module->encodeParamName( $paramName );
524 if ( is_array( $paramValue ) ) {
525 $paramValue = join( '|', $paramValue );
527 $this->continuationData
[$name][$paramName] = $paramValue;
531 * Set the continuation parameter for the generator module
534 * @param ApiBase $module
535 * @param string $paramName
536 * @param string|array $paramValue
538 public function setGeneratorContinueParam( ApiBase
$module, $paramName, $paramValue ) {
539 $name = $module->getModuleName();
540 $paramName = $module->encodeParamName( $paramName );
541 if ( is_array( $paramValue ) ) {
542 $paramValue = join( '|', $paramValue );
544 $this->generatorContinuationData
[$name][$paramName] = $paramValue;
548 * Close continuation, writing the data into the result
551 * @param string $style 'standard' for the new style since 1.21, 'raw' for
552 * the style used in 1.20 and earlier.
554 public function endContinuation( $style = 'standard' ) {
555 if ( $style === 'raw' ) {
556 $key = 'query-continue';
557 $data = array_merge_recursive(
558 $this->continuationData
, $this->generatorContinuationData
564 $finishedModules = array_diff(
565 array_keys( $this->continueAllModules
),
566 array_keys( $this->continuationData
)
569 // First, grab the non-generator-using continuation data
570 $continuationData = array_diff_key(
571 $this->continuationData
, $this->continueGeneratedModules
573 foreach ( $continuationData as $module => $kvp ) {
577 // Next, handle the generator-using continuation data
578 $continuationData = array_intersect_key(
579 $this->continuationData
, $this->continueGeneratedModules
581 if ( $continuationData ) {
582 // Some modules are unfinished: include those params, and copy
583 // the generator params.
584 foreach ( $continuationData as $module => $kvp ) {
587 $data +
= array_intersect_key(
588 $this->getMain()->getRequest()->getValues(),
589 array_flip( $this->generatorParams
)
591 } else if ( $this->generatorContinuationData
) {
592 // All the generator-using modules are complete, but the
593 // generator isn't. Continue the generator and restart the
594 // generator-using modules
595 $this->generatorParams
= array();
596 foreach ( $this->generatorContinuationData
as $kvp ) {
597 $this->generatorParams
= array_merge(
598 $this->generatorParams
, array_keys( $kvp )
602 $finishedModules = array_diff(
603 $finishedModules, $this->continueGeneratedModules
606 // Generator and prop modules are all done. Mark it so.
607 $this->generatorDone
= true;
610 // Set 'continue' if any continuation data is set or if the generator
611 // still needs to run
612 if ( $data ||
!$this->generatorDone
) {
614 ( $this->generatorDone ?
'-' : join( '|', $this->generatorParams
) ) .
615 '||' . join( '|', $finishedModules );
619 $this->disableSizeCheck();
620 $this->addValue( null, $key, $data, ApiResult
::ADD_ON_TOP
);
621 $this->enableSizeCheck();