156 $this->errorFormatter =
$maxSize->getErrorFormatter();
157 $this->mainForContinuation =
$maxSize;
162 $this->checkingSize =
true;
172 $this->errorFormatter = $formatter;
194 self::META_TYPE =>
'assoc',
253 $path = (
array)$path;
258 $last = array_pop( $path );
259 $ret = &$this->
path( $path,
'dummy' );
290 if ( (
$flags & self::NO_VALIDATE ) !== self::NO_VALIDATE ) {
294 if ( $name ===
null ) {
295 if (
$flags & self::ADD_ON_TOP ) {
296 array_unshift( $arr,
$value );
298 array_push( $arr,
$value );
303 $exists = isset( $arr[$name] );
304 if ( !$exists || (
$flags & self::OVERRIDE ) ) {
305 if ( !$exists && (
$flags & self::ADD_ON_TOP ) ) {
306 $arr = [ $name =>
$value ] + $arr;
310 } elseif ( is_array( $arr[$name] ) && is_array(
$value ) ) {
311 $conflicts = array_intersect_key( $arr[$name],
$value );
315 $keys = implode(
', ', array_keys( $conflicts ) );
316 throw new RuntimeException(
317 "Conflicting keys ($keys) when attempting to merge element $name"
321 throw new RuntimeException(
322 "Attempting to add element $name=$value, existing value is {$arr[$name]}"
335 if ( is_object(
$value ) ) {
338 if ( is_callable( [
$value,
'serializeForApiResult' ] ) ) {
341 if ( is_object(
$value ) ) {
342 throw new UnexpectedValueException(
343 get_class( $oldValue ) .
'::serializeForApiResult() returned an object of class ' .
352 }
catch ( Exception $ex ) {
353 throw new UnexpectedValueException(
354 get_class( $oldValue ) .
'::serializeForApiResult() returned an invalid value: ' .
360 } elseif ( is_callable( [
$value,
'__toString' ] ) ) {
366 if ( is_array(
$value ) ) {
370 foreach (
$value as $k => $v ) {
374 } elseif ( is_float(
$value ) && !is_finite(
$value ) ) {
375 throw new InvalidArgumentException(
'Cannot add non-finite floats to ApiResult' );
376 } elseif ( is_string(
$value ) ) {
380 if ( is_resource(
$value ) ) {
383 throw new InvalidArgumentException(
"Cannot add $type to ApiResult" );
406 $arr = &$this->
path( $path, (
$flags & self::ADD_ON_TOP ) ?
'prepend' :
'append' );
408 if ( $this->checkingSize && !(
$flags & self::NO_SIZE_CHECK ) ) {
415 if ( $this->maxSize !==
false && $newsize > $this->maxSize ) {
416 $this->errorFormatter->addWarning(
417 'result', [
'apiwarn-truncatedresult', Message::numParam( $this->maxSize ) ]
421 $this->
size = $newsize;
436 if ( isset( $arr[$name] ) ) {
438 unset( $arr[$name] );
454 $path = (
array)$path;
455 if ( $name ===
null ) {
457 throw new InvalidArgumentException(
'Cannot remove the data root' );
459 $name = array_pop( $path );
462 if ( $this->checkingSize && !(
$flags & self::NO_SIZE_CHECK ) ) {
464 $this->
size = max( $newsize, 0 );
479 if ( $name ===
null ) {
480 throw new InvalidArgumentException(
'Content value must be named' );
497 if ( $name ===
null ) {
498 throw new InvalidArgumentException(
'Content value must be named' );
513 $this->
addValue(
'limits', $moduleName, $limit,
514 self::OVERRIDE | self::NO_SIZE_CHECK );
533 if ( isset( $arr[self::META_CONTENT] ) &&
534 isset( $arr[$arr[self::META_CONTENT]] ) &&
535 !(
$flags & self::OVERRIDE )
537 throw new RuntimeException(
538 "Attempting to set content element as $name when " . $arr[self::META_CONTENT] .
539 ' is already set as the content element'
554 $arr = &$this->
path( $path, (
$flags & self::ADD_ON_TOP ) ?
'prepend' :
'append' );
566 if ( !isset( $arr[self::META_SUBELEMENTS] ) ) {
581 $arr = &$this->
path( $path );
593 if ( isset( $arr[self::META_SUBELEMENTS] ) ) {
606 $arr = &$this->
path( $path );
617 if ( !is_string( $tag ) ) {
618 throw new InvalidArgumentException(
'Bad tag name' );
630 $arr = &$this->
path( $path );
642 if ( !is_string( $tag ) ) {
643 throw new InvalidArgumentException(
'Bad tag name' );
646 foreach ( $arr as $k => &$v ) {
647 if ( !self::isMetadataKey( $k ) && is_array( $v ) ) {
661 $arr = &$this->
path( $path );
676 if ( !isset( $arr[self::META_PRESERVE_KEYS] ) ) {
691 $arr = &$this->
path( $path );
703 if ( isset( $arr[self::META_PRESERVE_KEYS] ) ) {
716 $arr = &$this->
path( $path );
729 if ( !in_array(
$type, [
730 'default',
'array',
'assoc',
'kvp',
'BCarray',
'BCassoc',
'BCkvp'
732 throw new InvalidArgumentException(
'Bad type' );
735 if ( is_string( $kvpKeyName ) ) {
748 $arr = &$this->
path( $path );
761 foreach ( $arr as $k => &$v ) {
762 if ( !self::isMetadataKey( $k ) && is_array( $v ) ) {
776 $arr = &$this->
path( $path );
794 return substr( $key, 0, 1 ) ===
'_';
807 $strip = isset( $transforms[
'Strip'] ) ? $transforms[
'Strip'] :
'none';
808 if ( $strip ===
'base' ) {
809 $transforms[
'Strip'] =
'none';
811 $transformTypes = isset( $transforms[
'Types'] ) ? $transforms[
'Types'] :
null;
812 if ( $transformTypes !==
null && !is_array( $transformTypes ) ) {
813 throw new InvalidArgumentException( __METHOD__ .
':Value for "Types" must be an array' );
819 if ( isset( $transforms[
'Custom'] ) ) {
820 if ( !is_callable( $transforms[
'Custom'] ) ) {
821 throw new InvalidArgumentException( __METHOD__ .
': Value for "Custom" must be callable' );
823 call_user_func_array( $transforms[
'Custom'], [ &
$data, &$metadata ] );
826 if ( ( isset( $transforms[
'BC'] ) || $transformTypes !==
null ) &&
827 isset( $metadata[self::META_TYPE] ) && $metadata[self::META_TYPE] ===
'BCkvp' &&
828 !isset( $metadata[self::META_KVP_KEY_NAME] )
830 throw new UnexpectedValueException(
'Type "BCkvp" used without setting ' .
831 'ApiResult::META_KVP_KEY_NAME metadata item' );
836 if ( isset( $transforms[
'BC'] ) ) {
837 if ( !is_array( $transforms[
'BC'] ) ) {
838 throw new InvalidArgumentException( __METHOD__ .
':Value for "BC" must be an array' );
840 if ( !in_array(
'nobool', $transforms[
'BC'],
true ) ) {
841 $boolKeys = isset( $metadata[self::META_BC_BOOLS] )
842 ? array_flip( $metadata[self::META_BC_BOOLS] )
846 if ( !in_array(
'no*', $transforms[
'BC'],
true ) &&
847 isset( $metadata[self::META_CONTENT] ) && $metadata[self::META_CONTENT] !==
'*'
855 if ( !in_array(
'nosub', $transforms[
'BC'],
true ) &&
856 isset( $metadata[self::META_BC_SUBELEMENTS] )
858 foreach ( $metadata[self::META_BC_SUBELEMENTS] as $k ) {
859 if ( isset(
$data[$k] ) ) {
862 self::META_CONTENT =>
'*',
863 self::META_TYPE =>
'assoc',
869 if ( isset( $metadata[self::META_TYPE] ) ) {
870 switch ( $metadata[self::META_TYPE] ) {
883 $defaultType =
'array';
885 foreach (
$data as $k => &$v ) {
887 if ( $boolKeys !==
null && is_bool( $v ) && !isset( $boolKeys[$k] ) ) {
894 if ( is_string( $k ) ) {
895 $defaultType =
'assoc';
896 } elseif ( $k > $maxKey ) {
909 $keepMetadata = &$metadata;
912 $keepMetadata = array_intersect_key( $metadata, [
913 self::META_INDEXED_TAG_NAME => 1,
914 self::META_SUBELEMENTS => 1,
918 throw new InvalidArgumentException( __METHOD__ .
': Unknown value for "Strip"' );
922 if ( $transformTypes !==
null ) {
923 if ( $defaultType ===
'array' && $maxKey !== count(
$data ) - 1 ) {
924 $defaultType =
'assoc';
928 $type = $defaultType;
929 if ( isset( $metadata[self::META_TYPE] ) && $metadata[self::META_TYPE] !==
'default' ) {
932 if ( (
$type ===
'kvp' ||
$type ===
'BCkvp' ) &&
933 empty( $transformTypes[
'ArmorKVP'] )
936 } elseif (
$type ===
'BCarray' ) {
938 } elseif (
$type ===
'BCassoc' ) {
946 $data += $keepMetadata;
953 return $data + $keepMetadata;
957 $key = isset( $metadata[self::META_KVP_KEY_NAME] )
959 : $transformTypes[
'ArmorKVP'];
960 $valKey = isset( $transforms[
'BC'] ) ?
'*' :
'value';
961 $assocAsObject = !empty( $transformTypes[
'AssocAsObject'] );
962 $merge = !empty( $metadata[self::META_KVP_MERGE] );
965 foreach (
$data as $k => $v ) {
966 if ( $merge && ( is_array( $v ) || is_object( $v ) ) ) {
968 if ( isset( $vArr[self::META_TYPE] ) ) {
970 } elseif ( is_object( $v ) ) {
971 $mergeType =
'assoc';
973 $keys = array_keys( $vArr );
974 sort(
$keys, SORT_NUMERIC );
975 $mergeType = (
$keys === array_keys(
$keys ) ) ?
'array' :
'assoc';
980 if ( $mergeType ===
'assoc' ) {
984 if ( $strip ===
'none' ) {
992 if ( $strip ===
'none' ) {
994 self::META_PRESERVE_KEYS => [ $key ],
995 self::META_CONTENT => $valKey,
996 self::META_TYPE =>
'assoc',
1000 $ret[] = $assocAsObject ? (
object)$item : $item;
1004 return $ret + $keepMetadata;
1007 throw new UnexpectedValueException(
"Unknown type '$type'" );
1010 return $data + $keepMetadata;
1025 if ( is_array(
$data ) || is_object(
$data ) ) {
1026 $isObj = is_object(
$data );
1030 $preserveKeys = isset(
$data[self::META_PRESERVE_KEYS] )
1033 foreach (
$data as $k => $v ) {
1034 if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys,
true ) ) {
1036 } elseif ( is_array( $v ) || is_object( $v ) ) {
1059 if ( !is_array( $metadata ) ) {
1062 if ( is_array(
$data ) || is_object(
$data ) ) {
1063 $isObj = is_object(
$data );
1067 $preserveKeys = isset(
$data[self::META_PRESERVE_KEYS] )
1070 foreach (
$data as $k => $v ) {
1071 if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys,
true ) ) {
1091 if ( is_array(
$value ) ) {
1092 foreach (
$value as $k => $v ) {
1093 if ( !self::isMetadataKey( $k ) ) {
1097 } elseif ( is_scalar(
$value ) ) {
1115 private function &
path( $path, $create =
'append' ) {
1116 $path = (
array)$path;
1118 foreach ( $path as $i => $k ) {
1119 if ( !isset(
$ret[$k] ) ) {
1120 switch ( $create ) {
1131 $fail = implode(
'.', array_slice( $path, 0, $i + 1 ) );
1132 throw new InvalidArgumentException(
"Path $fail does not exist" );
1135 if ( !is_array(
$ret[$k] ) ) {
1136 $fail = implode(
'.', array_slice( $path, 0, $i + 1 ) );
1137 throw new InvalidArgumentException(
"Path $fail is not an array" );
1157 foreach (
$vars as $k => $v ) {
1158 if ( is_array( $v ) || is_object( $v ) ) {
1160 } elseif ( is_bool( $v ) ) {
1164 if ( is_string( $k ) ) {
1166 } elseif ( $k > $maxKey ) {
1170 if ( !$hash && $maxKey !== count(
$vars ) - 1 ) {
1179 self::META_TYPE, self::META_PRESERVE_KEYS, self::META_KVP_KEY_NAME,
1180 self::META_INDEXED_TAG_NAME, self::META_BC_BOOLS
1184 self::META_TYPE =>
'kvp',
1185 self::META_KVP_KEY_NAME =>
'key',
1186 self::META_PRESERVE_KEYS =>
$keys,
1187 self::META_BC_BOOLS => $bools,
1188 self::META_INDEXED_TAG_NAME =>
'var',
1192 self::META_TYPE =>
'array',
1193 self::META_BC_BOOLS => $bools,
1194 self::META_INDEXED_TAG_NAME =>
'value',
1209 if ( $dbInfinity ===
null ) {
1213 if ( $expiry ===
'' || $expiry ===
null || $expiry ===
false ||
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfIsInfinity( $str)
Determine input string is represents as infinity.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
This is the main API class, used for both external and internal processing.
This class represents the result of the API operations.
addArrayType( $path, $tag, $kvpKeyName=null)
Set the array data type for a path.
static unsetSubelementsList(array &$arr, $names)
Causes the elements with the specified names to be output as attributes (when possible) rather than a...
static unsetPreserveKeysList(array &$arr, $names)
Don't preserve specified keys.
static applyTransformations(array $dataIn, array $transforms)
Apply transformations to an array, returning the transformed array.
const META_TYPE
Key for the 'type' metadata item.
static stripMetadataNonRecursive( $data, &$metadata=null)
Remove metadata keys from a data array or object, non-recursive.
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
serializeForApiResult()
Allow for adding one ApiResult into another.
static addMetadataToResultVars( $vars, $forceHash=true)
Add the correct metadata to an array of vars we want to export through the API.
static setValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name.
static validateValue( $value)
Validate a value for addition to the result.
const META_SUBELEMENTS
Key for the 'subelements' metadata item.
addIndexedTagName( $path, $tag)
Set the tag name for numeric-keyed values in XML format.
addValue( $path, $name, $value, $flags=0)
Add value to the output data at the given path.
const META_BC_BOOLS
Key for the 'BC bools' metadata item.
const META_PRESERVE_KEYS
Key for the 'preserve keys' metadata item.
const NO_SIZE_CHECK
For addValue() and similar functions, do not check size while adding a value Don't use this unless yo...
addArrayTypeRecursive( $path, $tag, $kvpKeyName=null)
Set the array data type for a path recursively.
getSize()
Get the size of the result, i.e.
static unsetValue(array &$arr, $name)
Remove an output value to the array by name.
static setPreserveKeysList(array &$arr, $names)
Preserve specified keys.
addPreserveKeysList( $path, $names)
Preserve specified keys.
addParsedLimit( $moduleName, $limit)
Add the numeric limit for a limit=max to the result.
addSubelementsList( $path, $names)
Causes the elements with the specified names to be output as subelements rather than attributes.
static stripMetadata( $data)
Recursively remove metadata keys from a data array or object.
const META_CONTENT
Key for the 'content' metadata item.
addIndexedTagNameRecursive( $path, $tag)
Set indexed tag name on $path and all subarrays.
removeSubelementsList( $path, $names)
Causes the elements with the specified names to be output as attributes (when possible) rather than a...
setErrorFormatter(ApiErrorFormatter $formatter)
Set the error formatter.
const OVERRIDE
Override existing value in addValue(), setValue(), and similar functions.
& path( $path, $create='append')
Return a reference to the internal data at $path.
static setSubelementsList(array &$arr, $names)
Causes the elements with the specified names to be output as subelements rather than attributes.
static setArrayTypeRecursive(array &$arr, $type, $kvpKeyName=null)
Set the array data type recursively.
const META_KVP_KEY_NAME
Key for the metadata item whose value specifies the name used for the kvp key in the alternative outp...
removeValue( $path, $name, $flags=0)
Remove value from the output data at the given path.
const ADD_ON_TOP
For addValue(), setValue() and similar functions, if the value does not exist, add it as the first el...
getResultData( $path=[], $transforms=[])
Get the result data array.
const META_BC_SUBELEMENTS
Key for the 'BC subelements' metadata item.
removePreserveKeysList( $path, $names)
Don't preserve specified keys.
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
const META_INDEXED_TAG_NAME
Key for the 'indexed tag name' metadata item.
const META_KVP_MERGE
Key for the metadata item that indicates that the KVP key should be added into an assoc value,...
reset()
Clear the current result data.
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
static setIndexedTagNameRecursive(array &$arr, $tag)
Set indexed tag name on $arr and all subarrays.
static setContentField(array &$arr, $name, $flags=0)
Set the name of the content field name (META_CONTENT)
static size( $value)
Get the 'real' size of a result item.
static formatExpiry( $expiry, $infinity='infinity')
Format an expiry timestamp for API output.
const NO_VALIDATE
For addValue(), setValue() and similar functions, do not validate data.
addContentValue( $path, $name, $value, $flags=0)
Add value to the output data at the given path and mark as META_CONTENT.
addContentField( $path, $name, $flags=0)
Set the name of the content field name (META_CONTENT)
static isMetadataKey( $key)
Test whether a key should be considered metadata.
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 local content language as $wgContLang
the array() calling protocol came about after MediaWiki 1.4rc1.
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
it s the revision text itself In either if gzip is the revision text is gzipped $flags
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Allows to change the fields on the form that will be generated $name
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
This interface allows for overriding the default conversion applied by ApiResult::validateValue().