00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ('ApiBase.php');
00029 }
00030
00048 class ApiResult extends ApiBase {
00049
00050 private $mData, $mIsRawMode, $mSize, $mCheckingSize;
00051
00056 public function __construct($main) {
00057 parent :: __construct($main, 'result');
00058 $this->mIsRawMode = false;
00059 $this->mCheckingSize = true;
00060 $this->reset();
00061 }
00062
00066 public function reset() {
00067 $this->mData = array ();
00068 $this->mSize = 0;
00069 }
00070
00075 public function setRawMode() {
00076 $this->mIsRawMode = true;
00077 }
00078
00083 public function getIsRawMode() {
00084 return $this->mIsRawMode;
00085 }
00086
00091 public function getData() {
00092 return $this->mData;
00093 }
00094
00101 public static function size($value) {
00102 $s = 0;
00103 if(is_array($value))
00104 foreach($value as $v)
00105 $s += self::size($v);
00106 else if(!is_object($value))
00107
00108 $s = strlen($value);
00109 return $s;
00110 }
00111
00116 public function getSize() {
00117 return $this->mSize;
00118 }
00119
00125 public function disableSizeCheck() {
00126 $this->mCheckingSize = false;
00127 }
00128
00132 public function enableSizeCheck() {
00133 $this->mCheckingSize = true;
00134 }
00135
00143 public static function setElement(& $arr, $name, $value) {
00144 if ($arr === null || $name === null || $value === null || !is_array($arr) || is_array($name))
00145 ApiBase :: dieDebug(__METHOD__, 'Bad parameter');
00146
00147 if (!isset ($arr[$name])) {
00148 $arr[$name] = $value;
00149 }
00150 elseif (is_array($arr[$name]) && is_array($value)) {
00151 $merged = array_intersect_key($arr[$name], $value);
00152 if (!count($merged))
00153 $arr[$name] += $value;
00154 else
00155 ApiBase :: dieDebug(__METHOD__, "Attempting to merge element $name");
00156 } else
00157 ApiBase :: dieDebug(__METHOD__, "Attempting to add element $name=$value, existing value is {$arr[$name]}");
00158 }
00159
00168 public static function setContent(& $arr, $value, $subElemName = null) {
00169 if (is_array($value))
00170 ApiBase :: dieDebug(__METHOD__, 'Bad parameter');
00171 if (is_null($subElemName)) {
00172 ApiResult :: setElement($arr, '*', $value);
00173 } else {
00174 if (!isset ($arr[$subElemName]))
00175 $arr[$subElemName] = array ();
00176 ApiResult :: setElement($arr[$subElemName], '*', $value);
00177 }
00178 }
00179
00187 public function setIndexedTagName(& $arr, $tag) {
00188
00189 if (!$this->getIsRawMode())
00190 return;
00191 if ($arr === null || $tag === null || !is_array($arr) || is_array($tag))
00192 ApiBase :: dieDebug(__METHOD__, 'Bad parameter');
00193
00194 $arr['_element'] = $tag;
00195 }
00196
00202 public function setIndexedTagName_recursive(&$arr, $tag)
00203 {
00204 if(!is_array($arr))
00205 return;
00206 foreach($arr as &$a)
00207 {
00208 if(!is_array($a))
00209 continue;
00210 $this->setIndexedTagName($a, $tag);
00211 $this->setIndexedTagName_recursive($a, $tag);
00212 }
00213 }
00214
00222 public function setIndexedTagName_internal( $path, $tag ) {
00223 $data = & $this->mData;
00224 foreach((array)$path as $p) {
00225 if ( !isset( $data[$p] ) ) {
00226 $data[$p] = array();
00227 }
00228 $data = & $data[$p];
00229 }
00230 if(is_null($data))
00231 return;
00232 $this->setIndexedTagName($data, $tag);
00233 }
00234
00242 public function addValue($path, $name, $value) {
00243 global $wgAPIMaxResultSize;
00244 $data = & $this->mData;
00245 if( $this->mCheckingSize ) {
00246 $newsize = $this->mSize + self::size($value);
00247 if($newsize > $wgAPIMaxResultSize)
00248 return false;
00249 $this->mSize = $newsize;
00250 }
00251
00252 if (!is_null($path)) {
00253 if (is_array($path)) {
00254 foreach ($path as $p) {
00255 if (!isset ($data[$p]))
00256 $data[$p] = array ();
00257 $data = & $data[$p];
00258 }
00259 } else {
00260 if (!isset ($data[$path]))
00261 $data[$path] = array ();
00262 $data = & $data[$path];
00263 }
00264 }
00265
00266 if (!$name)
00267 $data[] = $value;
00268 else
00269 ApiResult :: setElement($data, $name, $value);
00270 return true;
00271 }
00272
00280 public function unsetValue($path, $name) {
00281 $data = & $this->mData;
00282 if(!is_null($path))
00283 foreach((array)$path as $p) {
00284 if(!isset($data[$p]))
00285 return;
00286 $data = & $data[$p];
00287 }
00288 $this->mSize -= self::size($data[$name]);
00289 unset($data[$name]);
00290 }
00291
00295 public function cleanUpUTF8()
00296 {
00297 array_walk_recursive($this->mData, array('ApiResult', 'cleanUp_helper'));
00298 }
00299
00303 private static function cleanUp_helper(&$s)
00304 {
00305 if(!is_string($s))
00306 return;
00307 $s = UtfNormal::cleanUp($s);
00308 }
00309
00310 public function execute() {
00311 ApiBase :: dieDebug(__METHOD__, 'execute() is not supported on Result object');
00312 }
00313
00314 public function getVersion() {
00315 return __CLASS__ . ': $Id: ApiResult.php 47447 2009-02-18 12:41:28Z tstarling $';
00316 }
00317 }
00318
00319
00320 if (!function_exists('array_intersect_key')) {
00321 function array_intersect_key($isec, $keys) {
00322 $argc = func_num_args();
00323
00324 if ($argc > 2) {
00325 for ($i = 1; $isec && $i < $argc; $i++) {
00326 $arr = func_get_arg($i);
00327
00328 foreach (array_keys($isec) as $key) {
00329 if (!isset($arr[$key]))
00330 unset($isec[$key]);
00331 }
00332 }
00333
00334 return $isec;
00335 } else {
00336 $res = array();
00337 foreach (array_keys($isec) as $key) {
00338 if (isset($keys[$key]))
00339 $res[$key] = $isec[$key];
00340 }
00341
00342 return $res;
00343 }
00344 }
00345 }