MediaWiki  1.23.8
ApiResult.php
Go to the documentation of this file.
1 <?php
44 class ApiResult extends ApiBase {
45 
50  const OVERRIDE = 1;
51 
57  const ADD_ON_TOP = 2;
58 
60 
65  public function __construct( $main ) {
66  parent::__construct( $main, 'result' );
67  $this->mIsRawMode = false;
68  $this->mCheckingSize = true;
69  $this->reset();
70  }
71 
75  public function reset() {
76  $this->mData = array();
77  $this->mSize = 0;
78  }
79 
86  public function setRawMode( $flag = true ) {
87  $this->mIsRawMode = $flag;
88  }
89 
94  public function getIsRawMode() {
95  return $this->mIsRawMode;
96  }
97 
102  public function getData() {
103  return $this->mData;
104  }
105 
112  public static function size( $value ) {
113  $s = 0;
114  if ( is_array( $value ) ) {
115  foreach ( $value as $v ) {
116  $s += self::size( $v );
117  }
118  } elseif ( !is_object( $value ) ) {
119  // Objects can't always be cast to string
120  $s = strlen( $value );
121  }
122 
123  return $s;
124  }
125 
130  public function getSize() {
131  return $this->mSize;
132  }
133 
139  public function disableSizeCheck() {
140  $this->mCheckingSize = false;
141  }
142 
146  public function enableSizeCheck() {
147  $this->mCheckingSize = true;
148  }
149 
163  public static function setElement( &$arr, $name, $value, $flags = 0 ) {
164  if ( $arr === null || $name === null || $value === null
165  || !is_array( $arr ) || is_array( $name )
166  ) {
167  ApiBase::dieDebug( __METHOD__, 'Bad parameter' );
168  }
169 
170  $exists = isset( $arr[$name] );
171  if ( !$exists || ( $flags & ApiResult::OVERRIDE ) ) {
172  if ( !$exists && ( $flags & ApiResult::ADD_ON_TOP ) ) {
173  $arr = array( $name => $value ) + $arr;
174  } else {
175  $arr[$name] = $value;
176  }
177  } elseif ( is_array( $arr[$name] ) && is_array( $value ) ) {
178  $merged = array_intersect_key( $arr[$name], $value );
179  if ( !count( $merged ) ) {
180  $arr[$name] += $value;
181  } else {
182  ApiBase::dieDebug( __METHOD__, "Attempting to merge element $name" );
183  }
184  } else {
186  __METHOD__,
187  "Attempting to add element $name=$value, existing value is {$arr[$name]}"
188  );
189  }
190  }
191 
201  public static function setContent( &$arr, $value, $subElemName = null ) {
202  if ( is_array( $value ) ) {
203  ApiBase::dieDebug( __METHOD__, 'Bad parameter' );
204  }
205  if ( is_null( $subElemName ) ) {
206  ApiResult::setElement( $arr, '*', $value );
207  } else {
208  if ( !isset( $arr[$subElemName] ) ) {
209  $arr[$subElemName] = array();
210  }
211  ApiResult::setElement( $arr[$subElemName], '*', $value );
212  }
213  }
214 
222  public function setIndexedTagName( &$arr, $tag ) {
223  // In raw mode, add the '_element', otherwise just ignore
224  if ( !$this->getIsRawMode() ) {
225  return;
226  }
227  if ( $arr === null || $tag === null || !is_array( $arr ) || is_array( $tag ) ) {
228  ApiBase::dieDebug( __METHOD__, 'Bad parameter' );
229  }
230  // Do not use setElement() as it is ok to call this more than once
231  $arr['_element'] = $tag;
232  }
233 
239  public function setIndexedTagName_recursive( &$arr, $tag ) {
240  if ( !is_array( $arr ) ) {
241  return;
242  }
243  foreach ( $arr as &$a ) {
244  if ( !is_array( $a ) ) {
245  continue;
246  }
247  $this->setIndexedTagName( $a, $tag );
248  $this->setIndexedTagName_recursive( $a, $tag );
249  }
250  }
251 
259  public function setIndexedTagName_internal( $path, $tag ) {
260  $data = &$this->mData;
261  foreach ( (array)$path as $p ) {
262  if ( !isset( $data[$p] ) ) {
263  $data[$p] = array();
264  }
265  $data = &$data[$p];
266  }
267  if ( is_null( $data ) ) {
268  return;
269  }
270  $this->setIndexedTagName( $data, $tag );
271  }
272 
291  public function addValue( $path, $name, $value, $flags = 0 ) {
292  global $wgAPIMaxResultSize;
293 
294  $data = &$this->mData;
295  if ( $this->mCheckingSize ) {
296  $newsize = $this->mSize + self::size( $value );
297  if ( $newsize > $wgAPIMaxResultSize ) {
298  $this->setWarning(
299  "This result was truncated because it would otherwise be larger than the " .
300  "limit of {$wgAPIMaxResultSize} bytes" );
301 
302  return false;
303  }
304  $this->mSize = $newsize;
305  }
306 
307  $addOnTop = $flags & ApiResult::ADD_ON_TOP;
308  if ( $path !== null ) {
309  foreach ( (array)$path as $p ) {
310  if ( !isset( $data[$p] ) ) {
311  if ( $addOnTop ) {
312  $data = array( $p => array() ) + $data;
313  $addOnTop = false;
314  } else {
315  $data[$p] = array();
316  }
317  }
318  $data = &$data[$p];
319  }
320  }
321 
322  if ( !$name ) {
323  // Add list element
324  if ( $addOnTop ) {
325  // This element needs to be inserted in the beginning
326  // Numerical indexes will be renumbered
327  array_unshift( $data, $value );
328  } else {
329  // Add new value at the end
330  $data[] = $value;
331  }
332  } else {
333  // Add named element
334  self::setElement( $data, $name, $value, $flags );
335  }
336 
337  return true;
338  }
339 
346  public function setParsedLimit( $moduleName, $limit ) {
347  // Add value, allowing overwriting
348  $this->addValue( 'limits', $moduleName, $limit, ApiResult::OVERRIDE );
349  }
350 
358  public function unsetValue( $path, $name ) {
359  $data = &$this->mData;
360  if ( $path !== null ) {
361  foreach ( (array)$path as $p ) {
362  if ( !isset( $data[$p] ) ) {
363  return;
364  }
365  $data = &$data[$p];
366  }
367  }
368  $this->mSize -= self::size( $data[$name] );
369  unset( $data[$name] );
370  }
371 
375  public function cleanUpUTF8() {
376  array_walk_recursive( $this->mData, array( 'ApiResult', 'cleanUp_helper' ) );
377  }
378 
384  private static function cleanUp_helper( &$s ) {
385  if ( !is_string( $s ) ) {
386  return;
387  }
389  $s = $wgContLang->normalize( $s );
390  }
391 
398  public function convertStatusToArray( $status, $errorType = 'error' ) {
399  if ( $status->isGood() ) {
400  return array();
401  }
402 
403  $result = array();
404  foreach ( $status->getErrorsByType( $errorType ) as $error ) {
405  $this->setIndexedTagName( $error['params'], 'param' );
406  $result[] = $error;
407  }
408  $this->setIndexedTagName( $result, $errorType );
409 
410  return $result;
411  }
412 
413  public function execute() {
414  ApiBase::dieDebug( __METHOD__, 'execute() is not supported on Result object' );
415  }
416 }
ApiResult\getData
getData()
Get the result's internal data array (read-only)
Definition: ApiResult.php:102
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. $reader:XMLReader object $logInfo:Array of information Return false to stop further processing of the tag 'ImportHandlePageXMLTag':When parsing a XML tag in a page. $reader:XMLReader object $pageInfo:Array of information Return false to stop further processing of the tag 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information Return false to stop further processing of the tag 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. $reader:XMLReader object Return false to stop further processing of the tag 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. $reader:XMLReader object $revisionInfo:Array of information Return false to stop further processing of the tag 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. $title:Title object for the current page $request:WebRequest $ignoreRedirect:boolean to skip redirect check $target:Title/string of redirect target $article:Article object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) $article:article(object) being checked 'IsTrustedProxy':Override the result of wfIsTrustedProxy() $ip:IP being check $result:Change this value to override the result of wfIsTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of User::isValidEmailAddr(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetMagic':DEPRECATED, use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetSpecialPageAliases':DEPRECATED, use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Associative array mapping language codes to prefixed links of the form "language:title". & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1528
ApiResult\setIndexedTagName_internal
setIndexedTagName_internal( $path, $tag)
Calls setIndexedTagName() on an array already in the result.
Definition: ApiResult.php:259
ApiResult\setParsedLimit
setParsedLimit( $moduleName, $limit)
Add a parsed limit=max to the result.
Definition: ApiResult.php:346
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiResult\setRawMode
setRawMode( $flag=true)
Call this function when special elements such as '_element' are needed by the formatter,...
Definition: ApiResult.php:86
ApiResult\setIndexedTagName_recursive
setIndexedTagName_recursive(&$arr, $tag)
Calls setIndexedTagName() on each sub-array of $arr.
Definition: ApiResult.php:239
ApiResult\setContent
static setContent(&$arr, $value, $subElemName=null)
Adds a content element to an array.
Definition: ApiResult.php:201
$limit
if( $sleep) $limit
Definition: importImages.php:99
$s
$s
Definition: mergeMessageFileList.php:156
ApiResult\convertStatusToArray
convertStatusToArray( $status, $errorType='error')
Converts a Status object to an array suitable for addValue.
Definition: ApiResult.php:398
ApiResult\__construct
__construct( $main)
Constructor.
Definition: ApiResult.php:65
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2113
ApiResult\$mData
$mData
Definition: ApiResult.php:59
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiResult\getSize
getSize()
Get the size of the result, i.e.
Definition: ApiResult.php:130
ApiResult\enableSizeCheck
enableSizeCheck()
Re-enable size checking in addValue()
Definition: ApiResult.php:146
ApiResult
This class represents the result of the API operations.
Definition: ApiResult.php:44
ApiResult\$mCheckingSize
$mCheckingSize
Definition: ApiResult.php:59
ApiResult\reset
reset()
Clear the current result data.
Definition: ApiResult.php:75
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
ApiResult\$mSize
$mSize
Definition: ApiResult.php:59
ApiResult\addValue
addValue( $path, $name, $value, $flags=0)
Add value to the output data at the given path.
Definition: ApiResult.php:291
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
$value
$value
Definition: styleTest.css.php:45
ApiResult\unsetValue
unsetValue( $path, $name)
Unset a value previously added to the result set.
Definition: ApiResult.php:358
ApiResult\size
static size( $value)
Get the 'real' size of a result item.
Definition: ApiResult.php:112
ApiResult\disableSizeCheck
disableSizeCheck()
Disable size checking in addValue().
Definition: ApiResult.php:139
ApiResult\setIndexedTagName
setIndexedTagName(&$arr, $tag)
In case the array contains indexed values (in addition to named), give all indexed values the given t...
Definition: ApiResult.php:222
ApiResult\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiResult.php:413
ApiBase\setWarning
setWarning( $warning)
Set warning section for this module.
Definition: ApiBase.php:245
ApiResult\ADD_ON_TOP
const ADD_ON_TOP
For addValue() and setElement(), if the value does not exist, add it as the first element.
Definition: ApiResult.php:57
$path
$path
Definition: NoLocalSettings.php:35
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiResult\cleanUp_helper
static cleanUp_helper(&$s)
Callback function for cleanUpUTF8()
Definition: ApiResult.php:384
ApiResult\OVERRIDE
const OVERRIDE
override existing value in addValue() and setElement()
Definition: ApiResult.php:50
ApiResult\getIsRawMode
getIsRawMode()
Returns true whether the formatter requested raw data.
Definition: ApiResult.php:94
$error
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead where the first element is the message key and the remaining elements are used as parameters to the message based on mime etc Preferred in most cases over UploadVerification object with all info about the upload string as detected by MediaWiki Handlers will typically only apply for specific mime types object & $error
Definition: hooks.txt:2573
ApiResult\cleanUpUTF8
cleanUpUTF8()
Ensure all values in this result are valid UTF-8.
Definition: ApiResult.php:375
ApiResult\setElement
static setElement(&$arr, $name, $value, $flags=0)
Add an output value to the array by name.
Definition: ApiResult.php:163
ApiResult\$mIsRawMode
$mIsRawMode
Definition: ApiResult.php:59
ApiBase\dieDebug
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:2010