MediaWiki
REL1_19
|
00001 <?php 00044 class ApiResult extends ApiBase { 00045 00046 private $mData, $mIsRawMode, $mSize, $mCheckingSize; 00047 00052 public function __construct( $main ) { 00053 parent::__construct( $main, 'result' ); 00054 $this->mIsRawMode = false; 00055 $this->mCheckingSize = true; 00056 $this->reset(); 00057 } 00058 00062 public function reset() { 00063 $this->mData = array(); 00064 $this->mSize = 0; 00065 } 00066 00071 public function setRawMode() { 00072 $this->mIsRawMode = true; 00073 } 00074 00079 public function getIsRawMode() { 00080 return $this->mIsRawMode; 00081 } 00082 00087 public function getData() { 00088 return $this->mData; 00089 } 00090 00097 public static function size( $value ) { 00098 $s = 0; 00099 if ( is_array( $value ) ) { 00100 foreach ( $value as $v ) { 00101 $s += self::size( $v ); 00102 } 00103 } elseif ( !is_object( $value ) ) { 00104 // Objects can't always be cast to string 00105 $s = strlen( $value ); 00106 } 00107 return $s; 00108 } 00109 00114 public function getSize() { 00115 return $this->mSize; 00116 } 00117 00123 public function disableSizeCheck() { 00124 $this->mCheckingSize = false; 00125 } 00126 00130 public function enableSizeCheck() { 00131 $this->mCheckingSize = true; 00132 } 00133 00142 public static function setElement( &$arr, $name, $value, $overwrite = false ) { 00143 if ( $arr === null || $name === null || $value === null || !is_array( $arr ) || is_array( $name ) ) { 00144 ApiBase::dieDebug( __METHOD__, 'Bad parameter' ); 00145 } 00146 00147 if ( !isset ( $arr[$name] ) || $overwrite ) { 00148 $arr[$name] = $value; 00149 } elseif ( is_array( $arr[$name] ) && is_array( $value ) ) { 00150 $merged = array_intersect_key( $arr[$name], $value ); 00151 if ( !count( $merged ) ) { 00152 $arr[$name] += $value; 00153 } else { 00154 ApiBase::dieDebug( __METHOD__, "Attempting to merge element $name" ); 00155 } 00156 } else { 00157 ApiBase::dieDebug( __METHOD__, "Attempting to add element $name=$value, existing value is {$arr[$name]}" ); 00158 } 00159 } 00160 00170 public static function setContent( &$arr, $value, $subElemName = null ) { 00171 if ( is_array( $value ) ) { 00172 ApiBase::dieDebug( __METHOD__, 'Bad parameter' ); 00173 } 00174 if ( is_null( $subElemName ) ) { 00175 ApiResult::setElement( $arr, '*', $value ); 00176 } else { 00177 if ( !isset( $arr[$subElemName] ) ) { 00178 $arr[$subElemName] = array(); 00179 } 00180 ApiResult::setElement( $arr[$subElemName], '*', $value ); 00181 } 00182 } 00183 00191 public function setIndexedTagName( &$arr, $tag ) { 00192 // In raw mode, add the '_element', otherwise just ignore 00193 if ( !$this->getIsRawMode() ) { 00194 return; 00195 } 00196 if ( $arr === null || $tag === null || !is_array( $arr ) || is_array( $tag ) ) { 00197 ApiBase::dieDebug( __METHOD__, 'Bad parameter' ); 00198 } 00199 // Do not use setElement() as it is ok to call this more than once 00200 $arr['_element'] = $tag; 00201 } 00202 00208 public function setIndexedTagName_recursive( &$arr, $tag ) { 00209 if ( !is_array( $arr ) ) { 00210 return; 00211 } 00212 foreach ( $arr as &$a ) { 00213 if ( !is_array( $a ) ) { 00214 continue; 00215 } 00216 $this->setIndexedTagName( $a, $tag ); 00217 $this->setIndexedTagName_recursive( $a, $tag ); 00218 } 00219 } 00220 00228 public function setIndexedTagName_internal( $path, $tag ) { 00229 $data = &$this->mData; 00230 foreach ( (array)$path as $p ) { 00231 if ( !isset( $data[$p] ) ) { 00232 $data[$p] = array(); 00233 } 00234 $data = &$data[$p]; 00235 } 00236 if ( is_null( $data ) ) { 00237 return; 00238 } 00239 $this->setIndexedTagName( $data, $tag ); 00240 } 00241 00256 public function addValue( $path, $name, $value, $overwrite = false ) { 00257 global $wgAPIMaxResultSize; 00258 00259 $data = &$this->mData; 00260 if ( $this->mCheckingSize ) { 00261 $newsize = $this->mSize + self::size( $value ); 00262 if ( $newsize > $wgAPIMaxResultSize ) { 00263 $this->setWarning( 00264 "This result was truncated because it would otherwise be larger than the " . 00265 "limit of {$wgAPIMaxResultSize} bytes" ); 00266 return false; 00267 } 00268 $this->mSize = $newsize; 00269 } 00270 00271 if ( !is_null( $path ) ) { 00272 if ( is_array( $path ) ) { 00273 foreach ( $path as $p ) { 00274 if ( !isset( $data[$p] ) ) { 00275 $data[$p] = array(); 00276 } 00277 $data = &$data[$p]; 00278 } 00279 } else { 00280 if ( !isset( $data[$path] ) ) { 00281 $data[$path] = array(); 00282 } 00283 $data = &$data[$path]; 00284 } 00285 } 00286 00287 if ( !$name ) { 00288 $data[] = $value; // Add list element 00289 } else { 00290 self::setElement( $data, $name, $value, $overwrite ); // Add named element 00291 } 00292 return true; 00293 } 00294 00301 public function setParsedLimit( $moduleName, $limit ) { 00302 // Add value, allowing overwriting 00303 $this->addValue( 'limits', $moduleName, $limit, true ); 00304 } 00305 00313 public function unsetValue( $path, $name ) { 00314 $data = &$this->mData; 00315 if ( !is_null( $path ) ) { 00316 foreach ( (array)$path as $p ) { 00317 if ( !isset( $data[$p] ) ) { 00318 return; 00319 } 00320 $data = &$data[$p]; 00321 } 00322 } 00323 $this->mSize -= self::size( $data[$name] ); 00324 unset( $data[$name] ); 00325 } 00326 00330 public function cleanUpUTF8() { 00331 array_walk_recursive( $this->mData, array( 'ApiResult', 'cleanUp_helper' ) ); 00332 } 00333 00339 private static function cleanUp_helper( &$s ) { 00340 if ( !is_string( $s ) ) { 00341 return; 00342 } 00343 global $wgContLang; 00344 $s = $wgContLang->normalize( $s ); 00345 } 00346 00353 public function convertStatusToArray( $status, $errorType = 'error' ) { 00354 if ( $status->isGood() ) { 00355 return array(); 00356 } 00357 00358 $result = array(); 00359 foreach ( $status->getErrorsByType( $errorType ) as $error ) { 00360 $this->setIndexedTagName( $error['params'], 'param' ); 00361 $result[] = $error; 00362 } 00363 $this->setIndexedTagName( $result, $errorType ); 00364 return $result; 00365 } 00366 00367 public function execute() { 00368 ApiBase::dieDebug( __METHOD__, 'execute() is not supported on Result object' ); 00369 } 00370 00371 public function getVersion() { 00372 return __CLASS__ . ': $Id$'; 00373 } 00374 }