bc225ca77cd5e4ceb2b7ef637fe5759d0d2764c5
3 * API for MediaWiki 1.8+
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiBase.php' );
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.
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.
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.
49 class ApiResult
extends ApiBase
{
51 private $mData, $mIsRawMode, $mSize, $mCheckingSize;
55 * @param $main ApiMain object
57 public function __construct( $main ) {
58 parent
::__construct( $main, 'result' );
59 $this->mIsRawMode
= false;
60 $this->mCheckingSize
= true;
65 * Clear the current result data.
67 public function reset() {
68 $this->mData
= array();
73 * Call this function when special elements such as '_element'
74 * are needed by the formatter, for example in XML printing.
76 public function setRawMode() {
77 $this->mIsRawMode
= true;
81 * Returns true whether the formatter requested raw data.
84 public function getIsRawMode() {
85 return $this->mIsRawMode
;
89 * Get the result's internal data array (read-only)
92 public function getData() {
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.
102 public static function size( $value ) {
104 if ( is_array( $value ) ) {
105 foreach ( $value as $v ) {
106 $s +
= self
::size( $v );
108 } elseif ( !is_object( $value ) ) {
109 // Objects can't always be cast to string
110 $s = strlen( $value );
116 * Get the size of the result, i.e. the amount of bytes in it
119 public function getSize() {
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)
128 public function disableSizeCheck() {
129 $this->mCheckingSize
= false;
133 * Re-enable size checking in addValue()
135 public function enableSizeCheck() {
136 $this->mCheckingSize
= true;
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
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' );
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;
159 ApiBase
::dieDebug( __METHOD__
, "Attempting to merge element $name" );
162 ApiBase
::dieDebug( __METHOD__
, "Attempting to add element $name=$value, existing value is {$arr[$name]}" );
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
175 public static function setContent( &$arr, $value, $subElemName = null ) {
176 if ( is_array( $value ) ) {
177 ApiBase
::dieDebug( __METHOD__
, 'Bad parameter' );
179 if ( is_null( $subElemName ) ) {
180 ApiResult
::setElement( $arr, '*', $value );
182 if ( !isset( $arr[$subElemName] ) ) {
183 $arr[$subElemName] = array();
185 ApiResult
::setElement( $arr[$subElemName], '*', $value );
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.
194 * @param $tag string Tag name
196 public function setIndexedTagName( &$arr, $tag ) {
197 // In raw mode, add the '_element', otherwise just ignore
198 if ( !$this->getIsRawMode() ) {
201 if ( $arr === null ||
$tag === null ||
!is_array( $arr ) ||
is_array( $tag ) )
203 ApiBase
::dieDebug( __METHOD__
, 'Bad parameter' );
205 // Do not use setElement() as it is ok to call this more than once
206 $arr['_element'] = $tag;
210 * Calls setIndexedTagName() on each sub-array of $arr
212 * @param $tag string Tag name
214 public function setIndexedTagName_recursive( &$arr, $tag ) {
215 if ( !is_array( $arr ) ) {
218 foreach ( $arr as &$a ) {
219 if ( !is_array( $a ) ) {
222 $this->setIndexedTagName( $a, $tag );
223 $this->setIndexedTagName_recursive( $a, $tag );
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
234 public function setIndexedTagName_internal( $path, $tag ) {
235 $data = &$this->mData
;
236 foreach ( (array)$path as $p ) {
237 if ( !isset( $data[$p] ) ) {
242 if ( is_null( $data ) ) {
245 $this->setIndexedTagName( $data, $tag );
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
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 ) {
263 $this->mSize
= $newsize;
266 if ( !is_null( $path ) ) {
267 if ( is_array( $path ) ) {
268 foreach ( $path as $p ) {
269 if ( !isset( $data[$p] ) ) {
275 if ( !isset( $data[$path] ) ) {
276 $data[$path] = array();
278 $data = &$data[$path];
283 $data[] = $value; // Add list element
285 self
::setElement( $data, $name, $value, $overwrite ); // Add named element
291 * Add a parsed limit=max to the result.
293 * @param $moduleName string
296 public function setParsedLimit( $moduleName, $limit ) {
297 // Add value, allowing overwriting
298 $this->addValue( 'limits', $moduleName, $limit, true );
302 * Unset a value previously added to the result set.
303 * Fails silently if the value isn't found.
304 * For parameters, see addValue()
306 * @param $name string
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] ) ) {
318 $this->mSize
-= self
::size( $data[$name] );
319 unset( $data[$name] );
323 * Ensure all values in this result are valid UTF-8.
325 public function cleanUpUTF8() {
326 array_walk_recursive( $this->mData
, array( 'ApiResult', 'cleanUp_helper' ) );
330 * Callback function for cleanUpUTF8()
332 private static function cleanUp_helper( &$s ) {
333 if ( !is_string( $s ) ) {
337 $s = $wgContLang->normalize( $s );
340 public function execute() {
341 ApiBase
::dieDebug( __METHOD__
, 'execute() is not supported on Result object' );
344 public function getVersion() {
345 return __CLASS__
. ': $Id$';