Refactor array_intersect_key from ApiResult.php to GlobalFunctions.php
[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 (C) 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 else if ( !is_object( $value ) )
107 // Objects can't always be cast to string
108 $s = strlen( $value );
109 return $s;
110 }
111
112 /**
113 * Get the size of the result, i.e. the amount of bytes in it
114 * @return int
115 */
116 public function getSize() {
117 return $this->mSize;
118 }
119
120 /**
121 * Disable size checking in addValue(). Don't use this unless you
122 * REALLY know what you're doing. Values added while size checking
123 * was disabled will not be counted (ever)
124 */
125 public function disableSizeCheck() {
126 $this->mCheckingSize = false;
127 }
128
129 /**
130 * Re-enable size checking in addValue()
131 */
132 public function enableSizeCheck() {
133 $this->mCheckingSize = true;
134 }
135
136 /**
137 * Add an output value to the array by name.
138 * Verifies that value with the same name has not been added before.
139 * @param $arr array to add $value to
140 * @param $name string Index of $arr to add $value at
141 * @param $value mixed
142 */
143 public static function setElement( & $arr, $name, $value ) {
144 if ( $arr === null || $name === null || $value === null || !is_array( $arr ) || is_array( $name ) )
145 ApiBase :: dieDebug( __METHOD__, 'Bad parameter' );
146
147 if ( !isset ( $arr[$name] ) ) {
148 $arr[$name] = $value;
149 }
150 elseif ( is_array( $arr[$name] ) && is_array( $value ) ) {
151 $merged = array_intersect_key( $arr[$name], $value );
152 if ( !count( $merged ) )
153 $arr[$name] += $value;
154 else
155 ApiBase :: dieDebug( __METHOD__, "Attempting to merge element $name" );
156 } else
157 ApiBase :: dieDebug( __METHOD__, "Attempting to add element $name=$value, existing value is {$arr[$name]}" );
158 }
159
160 /**
161 * Adds a content element to an array.
162 * Use this function instead of hardcoding the '*' element.
163 * @param $arr array to add the content element to
164 * @param $subElemName string when present, content element is created
165 * as a sub item of $arr. Use this parameter to create elements in
166 * format <elem>text</elem> without attributes
167 */
168 public static function setContent( & $arr, $value, $subElemName = null ) {
169 if ( is_array( $value ) )
170 ApiBase :: dieDebug( __METHOD__, 'Bad parameter' );
171 if ( is_null( $subElemName ) ) {
172 ApiResult :: setElement( $arr, '*', $value );
173 } else {
174 if ( !isset ( $arr[$subElemName] ) )
175 $arr[$subElemName] = array ();
176 ApiResult :: setElement( $arr[$subElemName], '*', $value );
177 }
178 }
179
180 /**
181 * In case the array contains indexed values (in addition to named),
182 * give all indexed values the given tag name. This function MUST be
183 * called on every arrray that has numerical indexes.
184 * @param $arr array
185 * @param $tag string Tag name
186 */
187 public function setIndexedTagName( & $arr, $tag ) {
188 // In raw mode, add the '_element', otherwise just ignore
189 if ( !$this->getIsRawMode() )
190 return;
191 if ( $arr === null || $tag === null || !is_array( $arr ) || is_array( $tag ) )
192 ApiBase :: dieDebug( __METHOD__, 'Bad parameter' );
193 // Do not use setElement() as it is ok to call this more than once
194 $arr['_element'] = $tag;
195 }
196
197 /**
198 * Calls setIndexedTagName() on each sub-array of $arr
199 * @param $arr array
200 * @param $tag string Tag name
201 */
202 public function setIndexedTagName_recursive( &$arr, $tag ) {
203 if ( !is_array( $arr ) )
204 return;
205 foreach ( $arr as &$a )
206 {
207 if ( !is_array( $a ) )
208 continue;
209 $this->setIndexedTagName( $a, $tag );
210 $this->setIndexedTagName_recursive( $a, $tag );
211 }
212 }
213
214 /**
215 * Calls setIndexedTagName() on an array already in the result.
216 * Don't specify a path to a value that's not in the result, or
217 * you'll get nasty errors.
218 * @param $path array Path to the array, like addValue()'s $path
219 * @param $tag string
220 */
221 public function setIndexedTagName_internal( $path, $tag ) {
222 $data = & $this->mData;
223 foreach ( (array)$path as $p ) {
224 if ( !isset( $data[$p] ) ) {
225 $data[$p] = array();
226 }
227 $data = & $data[$p];
228 }
229 if ( is_null( $data ) )
230 return;
231 $this->setIndexedTagName( $data, $tag );
232 }
233
234 /**
235 * Add value to the output data at the given path.
236 * Path is an indexed array, each element specifing the branch at which to add the new value
237 * Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value
238 * If $name is empty, the $value is added as a next list element data[] = $value
239 * @return bool True if $value fits in the result, false if not
240 */
241 public function addValue( $path, $name, $value ) {
242 global $wgAPIMaxResultSize;
243 $data = & $this->mData;
244 if ( $this->mCheckingSize ) {
245 $newsize = $this->mSize + self::size( $value );
246 if ( $newsize > $wgAPIMaxResultSize )
247 return false;
248 $this->mSize = $newsize;
249 }
250
251 if ( !is_null( $path ) ) {
252 if ( is_array( $path ) ) {
253 foreach ( $path as $p ) {
254 if ( !isset ( $data[$p] ) )
255 $data[$p] = array ();
256 $data = & $data[$p];
257 }
258 } else {
259 if ( !isset ( $data[$path] ) )
260 $data[$path] = array ();
261 $data = & $data[$path];
262 }
263 }
264
265 if ( !$name )
266 $data[] = $value; // Add list element
267 else
268 ApiResult :: setElement( $data, $name, $value ); // Add named element
269 return true;
270 }
271
272 /**
273 * Unset a value previously added to the result set.
274 * Fails silently if the value isn't found.
275 * For parameters, see addValue()
276 * @param $path array
277 * @param $name string
278 */
279 public function unsetValue( $path, $name ) {
280 $data = & $this->mData;
281 if ( !is_null( $path ) )
282 foreach ( (array)$path as $p ) {
283 if ( !isset( $data[$p] ) )
284 return;
285 $data = & $data[$p];
286 }
287 $this->mSize -= self::size( $data[$name] );
288 unset( $data[$name] );
289 }
290
291 /**
292 * Ensure all values in this result are valid UTF-8.
293 */
294 public function cleanUpUTF8()
295 {
296 array_walk_recursive( $this->mData, array( 'ApiResult', 'cleanUp_helper' ) );
297 }
298
299 /**
300 * Callback function for cleanUpUTF8()
301 */
302 private static function cleanUp_helper( &$s )
303 {
304 if ( !is_string( $s ) )
305 return;
306 global $wgContLang;
307 $s = $wgContLang->normalize( $s );
308 }
309
310 public function execute() {
311 ApiBase :: dieDebug( __METHOD__, 'execute() is not supported on Result object' );
312 }
313
314 public function getVersion() {
315 return __CLASS__ . ': $Id$';
316 }
317 }