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 if ( !is_array( $arr ) )
00204 return;
00205 foreach ( $arr as &$a )
00206 {
00207 if ( !is_array( $a ) )
00208 continue;
00209 $this->setIndexedTagName( $a, $tag );
00210 $this->setIndexedTagName_recursive( $a, $tag );
00211 }
00212 }
00213
00221 public function setIndexedTagName_internal( $path, $tag ) {
00222 $data = & $this->mData;
00223 foreach ( (array)$path as $p ) {
00224 if ( !isset( $data[$p] ) ) {
00225 $data[$p] = array();
00226 }
00227 $data = & $data[$p];
00228 }
00229 if ( is_null( $data ) )
00230 return;
00231 $this->setIndexedTagName( $data, $tag );
00232 }
00233
00241 public function addValue( $path, $name, $value ) {
00242 global $wgAPIMaxResultSize;
00243 $data = & $this->mData;
00244 if ( $this->mCheckingSize ) {
00245 $newsize = $this->mSize + self::size( $value );
00246 if ( $newsize > $wgAPIMaxResultSize )
00247 return false;
00248 $this->mSize = $newsize;
00249 }
00250
00251 if ( !is_null( $path ) ) {
00252 if ( is_array( $path ) ) {
00253 foreach ( $path as $p ) {
00254 if ( !isset ( $data[$p] ) )
00255 $data[$p] = array ();
00256 $data = & $data[$p];
00257 }
00258 } else {
00259 if ( !isset ( $data[$path] ) )
00260 $data[$path] = array ();
00261 $data = & $data[$path];
00262 }
00263 }
00264
00265 if ( !$name )
00266 $data[] = $value;
00267 else
00268 ApiResult :: setElement( $data, $name, $value );
00269 return true;
00270 }
00271
00279 public function unsetValue( $path, $name ) {
00280 $data = & $this->mData;
00281 if ( !is_null( $path ) )
00282 foreach ( (array)$path as $p ) {
00283 if ( !isset( $data[$p] ) )
00284 return;
00285 $data = & $data[$p];
00286 }
00287 $this->mSize -= self::size( $data[$name] );
00288 unset( $data[$name] );
00289 }
00290
00294 public function cleanUpUTF8()
00295 {
00296 array_walk_recursive( $this->mData, array( 'ApiResult', 'cleanUp_helper' ) );
00297 }
00298
00302 private static function cleanUp_helper( &$s )
00303 {
00304 if ( !is_string( $s ) )
00305 return;
00306 global $wgContLang;
00307 $s = $wgContLang->normalize( $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 62354 2010-02-12 06:44:16Z mah $';
00316 }
00317 }