156 if ( $maxSize instanceof
ApiMain ) {
158 $this->errorFormatter = $maxSize->getErrorFormatter();
159 $this->mainForContinuation =
$maxSize;
160 $maxSize = $maxSize->getConfig()->get(
'APIMaxResultSize' );
164 $this->checkingSize =
true;
174 $this->errorFormatter = $formatter;
196 self::META_TYPE =>
'assoc',
257 return self::applyTransformations( $this->
data, $transforms );
264 } elseif ( is_array(
$ret[$last] ) ) {
265 return self::applyTransformations(
$ret[$last], $transforms );
292 if ( ( $flags & self::NO_VALIDATE ) !== self::NO_VALIDATE ) {
297 if ( $flags & self::ADD_ON_TOP ) {
298 array_unshift( $arr,
$value );
300 array_push( $arr,
$value );
305 $exists = isset( $arr[
$name] );
306 if ( !$exists || ( $flags & self::OVERRIDE ) ) {
307 if ( !$exists && ( $flags & self::ADD_ON_TOP ) ) {
308 $arr = [ $name =>
$value ] + $arr;
312 } elseif ( is_array( $arr[$name] ) && is_array(
$value ) ) {
313 $conflicts = array_intersect_key( $arr[$name],
$value );
317 $keys = implode(
', ', array_keys( $conflicts ) );
319 "Conflicting keys ($keys) when attempting to merge element $name" 324 "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 ) ) {
343 get_class( $oldValue ) .
'::serializeForApiResult() returned an object of class ' .
351 return self::validateValue(
$value );
354 get_class( $oldValue ) .
'::serializeForApiResult() returned an invalid value: ' .
360 } elseif ( is_callable( [
$value,
'__toString' ] ) ) {
366 if ( is_array(
$value ) ) {
371 $tmp[$k] = self::validateValue( $v );
374 } elseif ( is_float(
$value ) && !is_finite(
$value ) ) {
376 } elseif ( is_string(
$value ) ) {
377 $value = MediaWikiServices::getInstance()->getContentLanguage()->normalize(
$value );
380 if ( is_resource(
$value ) ) {
406 $arr = &$this->
path(
$path, ( $flags & self::ADD_ON_TOP ) ?
'prepend' :
'append' );
408 if ( $this->checkingSize && !( $flags & self::NO_SIZE_CHECK ) ) {
412 $flags |= self::NO_VALIDATE;
414 $newsize = $this->
size + self::size(
$value );
415 if ( $this->maxSize !==
false && $newsize > $this->maxSize ) {
416 $this->errorFormatter->addWarning(
421 $this->
size = $newsize;
436 if ( isset( $arr[
$name] ) ) {
438 unset( $arr[$name] );
462 if ( $this->checkingSize && !( $flags & self::NO_SIZE_CHECK ) ) {
463 $newsize = $this->
size - self::size(
$ret );
464 $this->
size = max( $newsize, 0 );
482 self::setContentField( $arr,
$name, $flags );
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 )
538 "Attempting to set content element as $name when " . $arr[self::META_CONTENT] .
539 ' is already set as the content element' 542 $arr[self::META_CONTENT] =
$name;
554 $arr = &$this->
path(
$path, ( $flags & self::ADD_ON_TOP ) ?
'prepend' :
'append' );
555 self::setContentField( $arr,
$name, $flags );
566 if ( !isset( $arr[self::META_SUBELEMENTS] ) ) {
567 $arr[self::META_SUBELEMENTS] = (
array)$names;
569 $arr[self::META_SUBELEMENTS] = array_merge( $arr[self::META_SUBELEMENTS], (
array)$names );
582 self::setSubelementsList( $arr, $names );
593 if ( isset( $arr[self::META_SUBELEMENTS] ) ) {
594 $arr[self::META_SUBELEMENTS] = array_diff( $arr[self::META_SUBELEMENTS], (
array)$names );
607 self::unsetSubelementsList( $arr, $names );
617 if ( !is_string( $tag ) ) {
620 $arr[self::META_INDEXED_TAG_NAME] = $tag;
631 self::setIndexedTagName( $arr, $tag );
642 if ( !is_string( $tag ) ) {
645 $arr[self::META_INDEXED_TAG_NAME] = $tag;
646 foreach ( $arr
as $k => &$v ) {
647 if ( !self::isMetadataKey( $k ) && is_array( $v ) ) {
648 self::setIndexedTagNameRecursive( $v, $tag );
662 self::setIndexedTagNameRecursive( $arr, $tag );
676 if ( !isset( $arr[self::META_PRESERVE_KEYS] ) ) {
677 $arr[self::META_PRESERVE_KEYS] = (
array)$names;
679 $arr[self::META_PRESERVE_KEYS] = array_merge( $arr[self::META_PRESERVE_KEYS], (
array)$names );
692 self::setPreserveKeysList( $arr, $names );
703 if ( isset( $arr[self::META_PRESERVE_KEYS] ) ) {
704 $arr[self::META_PRESERVE_KEYS] = array_diff( $arr[self::META_PRESERVE_KEYS], (
array)$names );
717 self::unsetPreserveKeysList( $arr, $names );
729 if ( !in_array(
$type, [
730 'default',
'array',
'assoc',
'kvp',
'BCarray',
'BCassoc',
'BCkvp' 734 $arr[self::META_TYPE] =
$type;
735 if ( is_string( $kvpKeyName ) ) {
736 $arr[self::META_KVP_KEY_NAME] = $kvpKeyName;
749 self::setArrayType( $arr, $tag, $kvpKeyName );
760 self::setArrayType( $arr,
$type, $kvpKeyName );
761 foreach ( $arr
as $k => &$v ) {
762 if ( !self::isMetadataKey( $k ) && is_array( $v ) ) {
763 self::setArrayTypeRecursive( $v,
$type, $kvpKeyName );
777 self::setArrayTypeRecursive( $arr, $tag, $kvpKeyName );
794 return substr( $key, 0, 1 ) ===
'_';
807 $strip = $transforms[
'Strip'] ??
'none';
808 if ( $strip ===
'base' ) {
809 $transforms[
'Strip'] =
'none';
811 $transformTypes = $transforms[
'Types'] ??
null;
812 if ( $transformTypes !== null && !is_array( $transformTypes ) ) {
817 $data = self::stripMetadataNonRecursive( $dataIn, $metadata );
819 if ( isset( $transforms[
'Custom'] ) ) {
820 if ( !is_callable( $transforms[
'Custom'] ) ) {
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] )
831 'ApiResult::META_KVP_KEY_NAME metadata item' );
836 if ( isset( $transforms[
'BC'] ) ) {
837 if ( !is_array( $transforms[
'BC'] ) ) {
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] !==
'*' 849 $k = $metadata[self::META_CONTENT];
852 $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] ) {
873 $metadata[self::META_TYPE] =
'default';
876 $transformTypes[
'ArmorKVP'] = $metadata[self::META_KVP_KEY_NAME];
883 $defaultType =
'array';
885 foreach (
$data as $k => &$v ) {
886 $v = is_array( $v ) ? self::applyTransformations( $v, $transforms ) : $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,
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' ) {
930 $type = $metadata[self::META_TYPE];
932 if ( (
$type ===
'kvp' ||
$type ===
'BCkvp' ) &&
933 empty( $transformTypes[
'ArmorKVP'] )
936 } elseif (
$type ===
'BCarray' ) {
938 } elseif (
$type ===
'BCassoc' ) {
945 $metadata[self::META_TYPE] =
'assoc';
946 $data += $keepMetadata;
952 $metadata[self::META_TYPE] =
'array';
953 return $data + $keepMetadata;
957 $key = $metadata[self::META_KVP_KEY_NAME] ?? $transformTypes[
'ArmorKVP'];
958 $valKey = isset( $transforms[
'BC'] ) ?
'*' :
'value';
959 $assocAsObject = !empty( $transformTypes[
'AssocAsObject'] );
960 $merge = !empty( $metadata[self::META_KVP_MERGE] );
963 foreach (
$data as $k => $v ) {
964 if ( $merge && ( is_array( $v ) || is_object( $v ) ) ) {
966 if ( isset( $vArr[self::META_TYPE] ) ) {
967 $mergeType = $vArr[self::META_TYPE];
968 } elseif ( is_object( $v ) ) {
969 $mergeType =
'assoc';
971 $keys = array_keys( $vArr );
972 sort(
$keys, SORT_NUMERIC );
973 $mergeType = (
$keys === array_keys(
$keys ) ) ?
'array' :
'assoc';
978 if ( $mergeType ===
'assoc' ) {
982 if ( $strip ===
'none' ) {
983 self::setPreserveKeysList( $item, [ $key ] );
990 if ( $strip ===
'none' ) {
992 self::META_PRESERVE_KEYS => [ $key ],
993 self::META_CONTENT => $valKey,
994 self::META_TYPE =>
'assoc',
998 $ret[] = $assocAsObject ? (
object)$item : $item;
1000 $metadata[self::META_TYPE] =
'array';
1002 return $ret + $keepMetadata;
1008 return $data + $keepMetadata;
1023 if ( is_array(
$data ) || is_object(
$data ) ) {
1024 $isObj = is_object(
$data );
1028 $preserveKeys = isset(
$data[self::META_PRESERVE_KEYS] )
1031 foreach (
$data as $k => $v ) {
1032 if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys,
true ) ) {
1034 } elseif ( is_array( $v ) || is_object( $v ) ) {
1035 $data[$k] = self::stripMetadata( $v );
1057 if ( !is_array( $metadata ) ) {
1060 if ( is_array(
$data ) || is_object(
$data ) ) {
1061 $isObj = is_object(
$data );
1065 $preserveKeys = isset(
$data[self::META_PRESERVE_KEYS] )
1068 foreach (
$data as $k => $v ) {
1069 if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys,
true ) ) {
1089 if ( is_array(
$value ) ) {
1091 if ( !self::isMetadataKey( $k ) ) {
1092 $s += self::size( $v );
1095 } elseif ( is_scalar(
$value ) ) {
1116 foreach (
$path as $i => $k ) {
1117 if ( !isset(
$ret[$k] ) ) {
1118 switch ( $create ) {
1129 $fail = implode(
'.', array_slice(
$path, 0, $i + 1 ) );
1133 if ( !is_array(
$ret[$k] ) ) {
1134 $fail = implode(
'.', array_slice(
$path, 0, $i + 1 ) );
1155 foreach (
$vars as $k => $v ) {
1156 if ( is_array( $v ) || is_object( $v ) ) {
1157 $vars[$k] = self::addMetadataToResultVars( (
array)$v, is_object( $v ) );
1158 } elseif ( is_bool( $v ) ) {
1162 if ( is_string( $k ) ) {
1164 } elseif ( $k > $maxKey ) {
1168 if ( !$hash && $maxKey !== count(
$vars ) - 1 ) {
1177 self::META_TYPE, self::META_PRESERVE_KEYS, self::META_KVP_KEY_NAME,
1178 self::META_INDEXED_TAG_NAME, self::META_BC_BOOLS
1182 self::META_TYPE =>
'kvp',
1183 self::META_KVP_KEY_NAME =>
'key',
1184 self::META_PRESERVE_KEYS =>
$keys,
1185 self::META_BC_BOOLS => $bools,
1186 self::META_INDEXED_TAG_NAME =>
'var',
1190 self::META_TYPE =>
'array',
1191 self::META_BC_BOOLS => $bools,
1192 self::META_INDEXED_TAG_NAME =>
'value',
1207 if ( $dbInfinity ===
null ) {
1211 if ( $expiry ===
'' || $expiry ===
null || $expiry ===
false ||
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of data
setErrorFormatter(ApiErrorFormatter $formatter)
Set the error formatter.
static unsetSubelementsList(array &$arr, $names)
Causes the elements with the specified names to be output as attributes (when possible) rather than a...
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
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
addParsedLimit( $moduleName, $limit)
Add the numeric limit for a limit=max to the result.
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 $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
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
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
wfIsInfinity( $str)
Determine input string is represents as infinity.
const META_TYPE
Key for the 'type' metadata item.
static isMetadataKey( $key)
Test whether a key should be considered metadata.
addArrayTypeRecursive( $path, $tag, $kvpKeyName=null)
Set the array data type for a path recursively.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
addValue( $path, $name, $value, $flags=0)
Add value to the output data at the given path.
getSize()
Get the size of the result, i.e.
const META_PRESERVE_KEYS
Key for the 'preserve keys' metadata item.
const ADD_ON_TOP
For addValue(), setValue() and similar functions, if the value does not exist, add it as the first el...
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
static size( $value)
Get the 'real' size of a result item.
static formatExpiry( $expiry, $infinity='infinity')
Format an expiry timestamp for API output.
serializeForApiResult()
Allow for adding one ApiResult into another.
static unsetValue(array &$arr, $name)
Remove an output value to the array by name.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
addContentValue( $path, $name, $value, $flags=0)
Add value to the output data at the given path and mark as META_CONTENT.
static stripMetadata( $data)
Recursively remove metadata keys from a data array or object.
removeValue( $path, $name, $flags=0)
Remove value from the output data at the given path.
static addMetadataToResultVars( $vars, $forceHash=true)
Add the correct metadata to an array of vars we want to export through the API.
addContentField( $path, $name, $flags=0)
Set the name of the content field name (META_CONTENT)
static setArrayTypeRecursive(array &$arr, $type, $kvpKeyName=null)
Set the array data type recursively.
addPreserveKeysList( $path, $names)
Preserve specified keys.
This is the main API class, used for both external and internal processing.
static applyTransformations(array $dataIn, array $transforms)
Apply transformations to an array, returning the transformed array.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
const META_KVP_MERGE
Key for the metadata item that indicates that the KVP key should be added into an assoc value...
const NO_SIZE_CHECK
For addValue() and similar functions, do not check size while adding a value Don't use this unless yo...
This interface allows for overriding the default conversion applied by ApiResult::validateValue().
This class represents the result of the API operations.
static stripMetadataNonRecursive( $data, &$metadata=null)
Remove metadata keys from a data array or object, non-recursive.
addArrayType( $path, $tag, $kvpKeyName=null)
Set the array data type for a path.
const NO_VALIDATE
For addValue(), setValue() and similar functions, do not validate data.
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
removePreserveKeysList( $path, $names)
Don't preserve specified keys.
static setSubelementsList(array &$arr, $names)
Causes the elements with the specified names to be output as subelements rather than attributes...
static setIndexedTagNameRecursive(array &$arr, $tag)
Set indexed tag name on $arr and all subarrays.
static unsetPreserveKeysList(array &$arr, $names)
Don't preserve specified keys.
addSubelementsList( $path, $names)
Causes the elements with the specified names to be output as subelements rather than attributes...
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
addIndexedTagNameRecursive( $path, $tag)
Set indexed tag name on $path and all subarrays.
const META_INDEXED_TAG_NAME
Key for the 'indexed tag name' metadata item.
static validateValue( $value)
Validate a value for addition to the result.
static setValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name.
const META_SUBELEMENTS
Key for the 'subelements' metadata item.
reset()
Clear the current result data.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
addIndexedTagName( $path, $tag)
Set the tag name for numeric-keyed values in XML format.
const META_BC_BOOLS
Key for the 'BC bools' metadata item.
& path( $path, $create='append')
Return a reference to the internal data at $path.
static setPreserveKeysList(array &$arr, $names)
Preserve specified keys.
static setContentField(array &$arr, $name, $flags=0)
Set the name of the content field name (META_CONTENT)
Allows to change the fields on the form that will be generated $name
const META_CONTENT
Key for the 'content' metadata item.
getResultData( $path=[], $transforms=[])
Get the result data array.
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
const META_BC_SUBELEMENTS
Key for the 'BC subelements' metadata item.
const OVERRIDE
Override existing value in addValue(), setValue(), and similar functions.
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
removeSubelementsList( $path, $names)
Causes the elements with the specified names to be output as attributes (when possible) rather than a...
const META_KVP_KEY_NAME
Key for the metadata item whose value specifies the name used for the kvp key in the alternative outp...