<?php
-
/**
- * Created on Sep 4, 2006
*
- * API for MediaWiki 1.8+
*
- * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
+ * Created on Sep 4, 2006
+ *
+ * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
*/
-if ( !defined( 'MEDIAWIKI' ) ) {
- // Eclipse helper - will be ignored in production
- require_once( 'ApiBase.php' );
-}
-
/**
* This class represents the result of the API operations.
* It simply wraps a nested array() structure, adding some functions to simplify array's modifications.
* @param $arr array to add $value to
* @param $name string Index of $arr to add $value at
* @param $value mixed
+ * @param $overwrite bool Whether overwriting an existing element is allowed
*/
- public static function setElement( &$arr, $name, $value ) {
+ public static function setElement( &$arr, $name, $value, $overwrite = false ) {
if ( $arr === null || $name === null || $value === null || !is_array( $arr ) || is_array( $name ) ) {
ApiBase::dieDebug( __METHOD__, 'Bad parameter' );
}
- if ( !isset ( $arr[$name] ) ) {
+ if ( !isset ( $arr[$name] ) || $overwrite ) {
$arr[$name] = $value;
} elseif ( is_array( $arr[$name] ) && is_array( $value ) ) {
$merged = array_intersect_key( $arr[$name], $value );
* Adds a content element to an array.
* Use this function instead of hardcoding the '*' element.
* @param $arr array to add the content element to
+ * @param $value Mixed
* @param $subElemName string when present, content element is created
* as a sub item of $arr. Use this parameter to create elements in
- * format <elem>text</elem> without attributes
+ * format "<elem>text</elem>" without attributes.
*/
public static function setContent( &$arr, $value, $subElemName = null ) {
if ( is_array( $value ) ) {
/**
* In case the array contains indexed values (in addition to named),
* give all indexed values the given tag name. This function MUST be
- * called on every arrray that has numerical indexes.
+ * called on every array that has numerical indexes.
* @param $arr array
* @param $tag string Tag name
*/
if ( !$this->getIsRawMode() ) {
return;
}
- if ( $arr === null || $tag === null || !is_array( $arr ) || is_array( $tag ) )
- {
+ if ( $arr === null || $tag === null || !is_array( $arr ) || is_array( $tag ) ) {
ApiBase::dieDebug( __METHOD__, 'Bad parameter' );
}
// Do not use setElement() as it is ok to call this more than once
/**
* Add value to the output data at the given path.
- * Path is an indexed array, each element specifing the branch at which to add the new value
- * Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value
- * If $name is empty, the $value is added as a next list element data[] = $value
+ * Path can be an indexed array, each element specifying the branch at which to add the new
+ * value. Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value.
+ * If $path is null, the value will be inserted at the data root.
+ * If $name is empty, the $value is added as a next list element data[] = $value.
+ *
+ * @param $path array|string|null
+ * @param $name string
+ * @param $value mixed
+ * @param $overwrite bool
+ *
* @return bool True if $value fits in the result, false if not
*/
- public function addValue( $path, $name, $value ) {
+ public function addValue( $path, $name, $value, $overwrite = false ) {
global $wgAPIMaxResultSize;
+
$data = &$this->mData;
if ( $this->mCheckingSize ) {
$newsize = $this->mSize + self::size( $value );
if ( $newsize > $wgAPIMaxResultSize ) {
+ $this->setWarning(
+ "This result was truncated because it would otherwise be larger than the " .
+ "limit of {$wgAPIMaxResultSize} bytes" );
return false;
}
$this->mSize = $newsize;
if ( !$name ) {
$data[] = $value; // Add list element
} else {
- ApiResult::setElement( $data, $name, $value ); // Add named element
+ self::setElement( $data, $name, $value, $overwrite ); // Add named element
}
return true;
}
+ /**
+ * Add a parsed limit=max to the result.
+ *
+ * @param $moduleName string
+ * @param $limit int
+ */
+ public function setParsedLimit( $moduleName, $limit ) {
+ // Add value, allowing overwriting
+ $this->addValue( 'limits', $moduleName, $limit, true );
+ }
+
/**
* Unset a value previously added to the result set.
* Fails silently if the value isn't found.
/**
* Callback function for cleanUpUTF8()
+ *
+ * @param $s string
*/
private static function cleanUp_helper( &$s ) {
if ( !is_string( $s ) ) {
$s = $wgContLang->normalize( $s );
}
+ /**
+ * Converts a Status object to an array suitable for addValue
+ * @param Status $status
+ * @param string $errorType
+ * @return array
+ */
+ public function convertStatusToArray( $status, $errorType = 'error' ) {
+ if ( $status->isGood() ) {
+ return array();
+ }
+
+ $result = array();
+ foreach ( $status->getErrorsByType( $errorType ) as $error ) {
+ $this->setIndexedTagName( $error['params'], 'param' );
+ $result[] = $error;
+ }
+ $this->setIndexedTagName( $result, $errorType );
+ return $result;
+ }
+
public function execute() {
ApiBase::dieDebug( __METHOD__, 'execute() is not supported on Result object' );
}