Merge "Made getMaxLag() use caching to reduce connection spam"
[lhc/web/wiklou.git] / includes / api / ApiResult.php
1 <?php
2 /**
3 *
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 /**
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.
32 *
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.
35 *
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.
43 *
44 * @ingroup API
45 */
46 class ApiResult extends ApiBase {
47
48 /**
49 * override existing value in addValue() and setElement()
50 * @since 1.21
51 */
52 const OVERRIDE = 1;
53
54 /**
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.
57 * @since 1.21
58 */
59 const ADD_ON_TOP = 2;
60
61 private $mData, $mIsRawMode, $mSize, $mCheckingSize;
62
63 private $continueAllModules = array();
64 private $continueGeneratedModules = array();
65 private $continuationData = array();
66 private $generatorContinuationData = array();
67 private $generatorParams = array();
68 private $generatorDone = false;
69
70 /**
71 * @param ApiMain $main
72 */
73 public function __construct( ApiMain $main ) {
74 parent::__construct( $main, 'result' );
75 $this->mIsRawMode = false;
76 $this->mCheckingSize = true;
77 $this->reset();
78 }
79
80 /**
81 * Clear the current result data.
82 */
83 public function reset() {
84 $this->mData = array();
85 $this->mSize = 0;
86 }
87
88 /**
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
93 */
94 public function setRawMode( $flag = true ) {
95 $this->mIsRawMode = $flag;
96 }
97
98 /**
99 * Returns true whether the formatter requested raw data.
100 * @return bool
101 */
102 public function getIsRawMode() {
103 return $this->mIsRawMode;
104 }
105
106 /**
107 * Get the result's internal data array (read-only)
108 * @return array
109 */
110 public function getData() {
111 return $this->mData;
112 }
113
114 /**
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
118 * @return int
119 */
120 public static function size( $value ) {
121 $s = 0;
122 if ( is_array( $value ) ) {
123 foreach ( $value as $v ) {
124 $s += self::size( $v );
125 }
126 } elseif ( !is_object( $value ) ) {
127 // Objects can't always be cast to string
128 $s = strlen( $value );
129 }
130
131 return $s;
132 }
133
134 /**
135 * Get the size of the result, i.e. the amount of bytes in it
136 * @return int
137 */
138 public function getSize() {
139 return $this->mSize;
140 }
141
142 /**
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)
146 */
147 public function disableSizeCheck() {
148 $this->mCheckingSize = false;
149 }
150
151 /**
152 * Re-enable size checking in addValue()
153 */
154 public function enableSizeCheck() {
155 $this->mCheckingSize = true;
156 }
157
158 /**
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.
168 *
169 * @since 1.21 int $flags replaced boolean $override
170 */
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 )
174 ) {
175 ApiBase::dieDebug( __METHOD__, 'Bad parameter' );
176 }
177
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;
182 } else {
183 $arr[$name] = $value;
184 }
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;
189 } else {
190 ApiBase::dieDebug( __METHOD__, "Attempting to merge element $name" );
191 }
192 } else {
193 ApiBase::dieDebug(
194 __METHOD__,
195 "Attempting to add element $name=$value, existing value is {$arr[$name]}"
196 );
197 }
198 }
199
200 /**
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.
208 */
209 public static function setContent( &$arr, $value, $subElemName = null ) {
210 if ( is_array( $value ) ) {
211 ApiBase::dieDebug( __METHOD__, 'Bad parameter' );
212 }
213 if ( is_null( $subElemName ) ) {
214 ApiResult::setElement( $arr, '*', $value );
215 } else {
216 if ( !isset( $arr[$subElemName] ) ) {
217 $arr[$subElemName] = array();
218 }
219 ApiResult::setElement( $arr[$subElemName], '*', $value );
220 }
221 }
222
223 /**
224 * Causes the elements with the specified names to be output as
225 * subelements rather than attributes.
226 * @param array $arr
227 * @param array|string $names The element name(s) to be output as subelements
228 */
229 public function setSubelements( &$arr, $names ) {
230 // In raw mode, add the '_subelements', otherwise just ignore
231 if ( !$this->getIsRawMode() ) {
232 return;
233 }
234 if ( $arr === null || $names === null || !is_array( $arr ) ) {
235 ApiBase::dieDebug( __METHOD__, 'Bad parameter' );
236 }
237 if ( !is_array( $names ) ) {
238 $names = array( $names );
239 }
240 if ( !isset( $arr['_subelements'] ) ) {
241 $arr['_subelements'] = $names;
242 } else {
243 $arr['_subelements'] = array_merge( $arr['_subelements'], $names );
244 }
245 }
246
247 /**
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.
251 * @param array $arr
252 * @param string $tag Tag name
253 */
254 public function setIndexedTagName( &$arr, $tag ) {
255 // In raw mode, add the '_element', otherwise just ignore
256 if ( !$this->getIsRawMode() ) {
257 return;
258 }
259 if ( $arr === null || $tag === null || !is_array( $arr ) || is_array( $tag ) ) {
260 ApiBase::dieDebug( __METHOD__, 'Bad parameter' );
261 }
262 // Do not use setElement() as it is ok to call this more than once
263 $arr['_element'] = $tag;
264 }
265
266 /**
267 * Calls setIndexedTagName() on each sub-array of $arr
268 * @param array $arr
269 * @param string $tag Tag name
270 */
271 public function setIndexedTagName_recursive( &$arr, $tag ) {
272 if ( !is_array( $arr ) ) {
273 return;
274 }
275 foreach ( $arr as &$a ) {
276 if ( !is_array( $a ) ) {
277 continue;
278 }
279 $this->setIndexedTagName( $a, $tag );
280 $this->setIndexedTagName_recursive( $a, $tag );
281 }
282 }
283
284 /**
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
289 * @param string $tag
290 */
291 public function setIndexedTagName_internal( $path, $tag ) {
292 $data = &$this->mData;
293 foreach ( (array)$path as $p ) {
294 if ( !isset( $data[$p] ) ) {
295 $data[$p] = array();
296 }
297 $data = &$data[$p];
298 }
299 if ( is_null( $data ) ) {
300 return;
301 }
302 $this->setIndexedTagName( $data, $tag );
303 }
304
305 /**
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.
311 *
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
318 * signature.
319 * @return bool True if $value fits in the result, false if not
320 *
321 * @since 1.21 int $flags replaced boolean $override
322 */
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 ) {
329 $this->setWarning(
330 "This result was truncated because it would otherwise be larger than the " .
331 "limit of {$maxResultSize} bytes" );
332
333 return false;
334 }
335 $this->mSize = $newsize;
336 }
337
338 $addOnTop = $flags & ApiResult::ADD_ON_TOP;
339 if ( $path !== null ) {
340 foreach ( (array)$path as $p ) {
341 if ( !isset( $data[$p] ) ) {
342 if ( $addOnTop ) {
343 $data = array( $p => array() ) + $data;
344 $addOnTop = false;
345 } else {
346 $data[$p] = array();
347 }
348 }
349 $data = &$data[$p];
350 }
351 }
352
353 if ( !$name ) {
354 // Add list element
355 if ( $addOnTop ) {
356 // This element needs to be inserted in the beginning
357 // Numerical indexes will be renumbered
358 array_unshift( $data, $value );
359 } else {
360 // Add new value at the end
361 $data[] = $value;
362 }
363 } else {
364 // Add named element
365 self::setElement( $data, $name, $value, $flags );
366 }
367
368 return true;
369 }
370
371 /**
372 * Add a parsed limit=max to the result.
373 *
374 * @param string $moduleName
375 * @param int $limit
376 */
377 public function setParsedLimit( $moduleName, $limit ) {
378 // Add value, allowing overwriting
379 $this->addValue( 'limits', $moduleName, $limit, ApiResult::OVERRIDE );
380 }
381
382 /**
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
388 */
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] ) ) {
394 return;
395 }
396 $data = &$data[$p];
397 }
398 }
399 $this->mSize -= self::size( $data[$name] );
400 unset( $data[$name] );
401 }
402
403 /**
404 * Ensure all values in this result are valid UTF-8.
405 */
406 public function cleanUpUTF8() {
407 array_walk_recursive( $this->mData, array( 'ApiResult', 'cleanUp_helper' ) );
408 }
409
410 /**
411 * Callback function for cleanUpUTF8()
412 *
413 * @param string $s
414 */
415 private static function cleanUp_helper( &$s ) {
416 if ( !is_string( $s ) ) {
417 return;
418 }
419 global $wgContLang;
420 $s = $wgContLang->normalize( $s );
421 }
422
423 /**
424 * Converts a Status object to an array suitable for addValue
425 * @param Status $status
426 * @param string $errorType
427 * @return array
428 */
429 public function convertStatusToArray( $status, $errorType = 'error' ) {
430 if ( $status->isGood() ) {
431 return array();
432 }
433
434 $result = array();
435 foreach ( $status->getErrorsByType( $errorType ) as $error ) {
436 $this->setIndexedTagName( $error['params'], 'param' );
437 $result[] = $error;
438 }
439 $this->setIndexedTagName( $result, $errorType );
440
441 return $result;
442 }
443
444 public function execute() {
445 ApiBase::dieDebug( __METHOD__, 'execute() is not supported on Result object' );
446 }
447
448 /**
449 * Parse a 'continue' parameter and return status information.
450 *
451 * This must be balanced by a call to endContinuation().
452 *
453 * @since 1.24
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.
459 */
460 public function beginContinuation(
461 $continue, array $allModules = array(), array $generatedModules = array()
462 ) {
463 $this->continueGeneratedModules = $generatedModules
464 ? array_combine( $generatedModules, $generatedModules )
465 : array();
466 $this->continuationData = array();
467 $this->generatorContinuationData = array();
468 $this->generatorParams = array();
469
470 $skip = 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] );
477 }
478 $skip = explode( '|', $continue[1] );
479 }
480
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();
489 } else {
490 $this->continueAllModules[$name] = true;
491 $runModules[] = $module;
492 }
493 }
494
495 return array(
496 $this->generatorDone,
497 $runModules,
498 );
499 }
500
501 /**
502 * Set the continuation parameter for a module
503 *
504 * @since 1.24
505 * @param ApiBase $module
506 * @param string $paramName
507 * @param string|array $paramValue
508 */
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'
515 );
516 }
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'
521 );
522 }
523 $paramName = $module->encodeParamName( $paramName );
524 if ( is_array( $paramValue ) ) {
525 $paramValue = join( '|', $paramValue );
526 }
527 $this->continuationData[$name][$paramName] = $paramValue;
528 }
529
530 /**
531 * Set the continuation parameter for the generator module
532 *
533 * @since 1.24
534 * @param ApiBase $module
535 * @param string $paramName
536 * @param string|array $paramValue
537 */
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 );
543 }
544 $this->generatorContinuationData[$name][$paramName] = $paramValue;
545 }
546
547 /**
548 * Close continuation, writing the data into the result
549 *
550 * @since 1.24
551 * @param string $style 'standard' for the new style since 1.21, 'raw' for
552 * the style used in 1.20 and earlier.
553 */
554 public function endContinuation( $style = 'standard' ) {
555 if ( $style === 'raw' ) {
556 $key = 'query-continue';
557 $data = array_merge_recursive(
558 $this->continuationData, $this->generatorContinuationData
559 );
560 } else {
561 $key = 'continue';
562 $data = array();
563
564 $finishedModules = array_diff(
565 array_keys( $this->continueAllModules ),
566 array_keys( $this->continuationData )
567 );
568
569 // First, grab the non-generator-using continuation data
570 $continuationData = array_diff_key(
571 $this->continuationData, $this->continueGeneratedModules
572 );
573 foreach ( $continuationData as $module => $kvp ) {
574 $data += $kvp;
575 }
576
577 // Next, handle the generator-using continuation data
578 $continuationData = array_intersect_key(
579 $this->continuationData, $this->continueGeneratedModules
580 );
581 if ( $continuationData ) {
582 // Some modules are unfinished: include those params, and copy
583 // the generator params.
584 foreach ( $continuationData as $module => $kvp ) {
585 $data += $kvp;
586 }
587 $data += array_intersect_key(
588 $this->getMain()->getRequest()->getValues(),
589 array_flip( $this->generatorParams )
590 );
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 )
599 );
600 $data += $kvp;
601 }
602 $finishedModules = array_diff(
603 $finishedModules, $this->continueGeneratedModules
604 );
605 } else {
606 // Generator and prop modules are all done. Mark it so.
607 $this->generatorDone = true;
608 }
609
610 // Set 'continue' if any continuation data is set or if the generator
611 // still needs to run
612 if ( $data || !$this->generatorDone ) {
613 $data['continue'] =
614 ( $this->generatorDone ? '-' : join( '|', $this->generatorParams ) ) .
615 '||' . join( '|', $finishedModules );
616 }
617 }
618 if ( $data ) {
619 $this->disableSizeCheck();
620 $this->addValue( null, $key, $data, ApiResult::ADD_ON_TOP );
621 $this->enableSizeCheck();
622 }
623 }
624 }