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