154 if ( $maxSize instanceof
ApiMain ) {
156 $this->errorFormatter = $maxSize->getErrorFormatter();
157 $this->mainForContinuation =
$maxSize;
158 $maxSize = $maxSize->getConfig()->get(
'APIMaxResultSize' );
162 $this->checkingSize =
true;
172 $this->errorFormatter = $formatter;
194 self::META_TYPE =>
'assoc',
255 return self::applyTransformations( $this->
data, $transforms );
262 } elseif ( is_array(
$ret[$last] ) ) {
263 return self::applyTransformations(
$ret[$last], $transforms );
294 if (
$name === null ) {
296 array_unshift( $arr,
$value );
298 array_push( $arr,
$value );
303 $exists = isset( $arr[
$name] );
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 ) );
317 "Conflicting keys ($keys) when attempting to merge element $name"
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 ) ) {
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 ) ) {
380 if ( is_resource(
$value ) ) {
414 $newsize = $this->
size + self::size(
$value );
415 if ( $this->maxSize !==
false && $newsize > $this->maxSize ) {
417 $msg =
new ApiRawMessage(
'This result was truncated because it would otherwise ' .
418 'be larger than the limit of $1 bytes',
'truncatedresult' );
419 $msg->numParams( $this->maxSize );
420 $this->errorFormatter->addWarning(
'result', $msg );
423 $this->
size = $newsize;
438 if ( isset( $arr[
$name] ) ) {
440 unset( $arr[$name] );
457 if (
$name === null ) {
465 $newsize = $this->
size - self::size(
$ret );
466 $this->
size = max( $newsize, 0 );
481 if (
$name === null ) {
499 if (
$name === null ) {
535 if ( isset( $arr[self::META_CONTENT] ) &&
536 isset( $arr[$arr[self::META_CONTENT]] ) &&
537 !(
$flags & self::OVERRIDE )
540 "Attempting to set content element as $name when " . $arr[self::META_CONTENT] .
541 ' is already set as the content element'
544 $arr[self::META_CONTENT] =
$name;
568 if ( !isset( $arr[self::META_SUBELEMENTS] ) ) {
569 $arr[self::META_SUBELEMENTS] = (
array)$names;
571 $arr[self::META_SUBELEMENTS] = array_merge( $arr[self::META_SUBELEMENTS], (
array)$names );
584 self::setSubelementsList( $arr, $names );
595 if ( isset( $arr[self::META_SUBELEMENTS] ) ) {
596 $arr[self::META_SUBELEMENTS] = array_diff( $arr[self::META_SUBELEMENTS], (
array)$names );
609 self::unsetSubelementsList( $arr, $names );
619 if ( !is_string(
$tag ) ) {
622 $arr[self::META_INDEXED_TAG_NAME] =
$tag;
633 self::setIndexedTagName( $arr,
$tag );
644 if ( !is_string(
$tag ) ) {
647 $arr[self::META_INDEXED_TAG_NAME] =
$tag;
648 foreach ( $arr
as $k => &$v ) {
649 if ( !self::isMetadataKey( $k ) && is_array( $v ) ) {
650 self::setIndexedTagNameRecursive( $v,
$tag );
664 self::setIndexedTagNameRecursive( $arr,
$tag );
678 if ( !isset( $arr[self::META_PRESERVE_KEYS] ) ) {
679 $arr[self::META_PRESERVE_KEYS] = (
array)$names;
681 $arr[self::META_PRESERVE_KEYS] = array_merge( $arr[self::META_PRESERVE_KEYS], (
array)$names );
694 self::setPreserveKeysList( $arr, $names );
705 if ( isset( $arr[self::META_PRESERVE_KEYS] ) ) {
706 $arr[self::META_PRESERVE_KEYS] = array_diff( $arr[self::META_PRESERVE_KEYS], (
array)$names );
719 self::unsetPreserveKeysList( $arr, $names );
731 if ( !in_array(
$type, [
732 'default',
'array',
'assoc',
'kvp',
'BCarray',
'BCassoc',
'BCkvp'
736 $arr[self::META_TYPE] =
$type;
737 if ( is_string( $kvpKeyName ) ) {
738 $arr[self::META_KVP_KEY_NAME] = $kvpKeyName;
751 self::setArrayType( $arr,
$tag, $kvpKeyName );
762 self::setArrayType( $arr,
$type, $kvpKeyName );
763 foreach ( $arr
as $k => &$v ) {
764 if ( !self::isMetadataKey( $k ) && is_array( $v ) ) {
765 self::setArrayTypeRecursive( $v,
$type, $kvpKeyName );
779 self::setArrayTypeRecursive( $arr,
$tag, $kvpKeyName );
796 return substr( $key, 0, 1 ) ===
'_';
809 $strip = isset( $transforms[
'Strip'] ) ? $transforms[
'Strip'] :
'none';
810 if ( $strip ===
'base' ) {
811 $transforms[
'Strip'] =
'none';
813 $transformTypes = isset( $transforms[
'Types'] ) ? $transforms[
'Types'] : null;
814 if ( $transformTypes !== null && !is_array( $transformTypes ) ) {
819 $data = self::stripMetadataNonRecursive( $dataIn, $metadata );
821 if ( isset( $transforms[
'Custom'] ) ) {
822 if ( !is_callable( $transforms[
'Custom'] ) ) {
825 call_user_func_array( $transforms[
'Custom'], [ &
$data, &$metadata ] );
828 if ( ( isset( $transforms[
'BC'] ) || $transformTypes !== null ) &&
829 isset( $metadata[self::META_TYPE] ) && $metadata[self::META_TYPE] ===
'BCkvp' &&
830 !isset( $metadata[self::META_KVP_KEY_NAME] )
833 'ApiResult::META_KVP_KEY_NAME metadata item' );
838 if ( isset( $transforms[
'BC'] ) ) {
839 if ( !is_array( $transforms[
'BC'] ) ) {
842 if ( !in_array(
'nobool', $transforms[
'BC'],
true ) ) {
843 $boolKeys = isset( $metadata[self::META_BC_BOOLS] )
844 ? array_flip( $metadata[self::META_BC_BOOLS] )
848 if ( !in_array(
'no*', $transforms[
'BC'],
true ) &&
849 isset( $metadata[self::META_CONTENT] ) && $metadata[self::META_CONTENT] !==
'*'
851 $k = $metadata[self::META_CONTENT];
854 $metadata[self::META_CONTENT] =
'*';
857 if ( !in_array(
'nosub', $transforms[
'BC'],
true ) &&
858 isset( $metadata[self::META_BC_SUBELEMENTS] )
860 foreach ( $metadata[self::META_BC_SUBELEMENTS]
as $k ) {
861 if ( isset(
$data[$k] ) ) {
864 self::META_CONTENT =>
'*',
865 self::META_TYPE =>
'assoc',
871 if ( isset( $metadata[self::META_TYPE] ) ) {
872 switch ( $metadata[self::META_TYPE] ) {
875 $metadata[self::META_TYPE] =
'default';
878 $transformTypes[
'ArmorKVP'] = $metadata[self::META_KVP_KEY_NAME];
885 $defaultType =
'array';
887 foreach (
$data as $k => &$v ) {
888 $v = is_array( $v ) ? self::applyTransformations( $v, $transforms ) : $v;
889 if ( $boolKeys !== null && is_bool( $v ) && !isset( $boolKeys[$k] ) ) {
896 if ( is_string( $k ) ) {
897 $defaultType =
'assoc';
898 } elseif ( $k > $maxKey ) {
911 $keepMetadata = &$metadata;
914 $keepMetadata = array_intersect_key( $metadata, [
915 self::META_INDEXED_TAG_NAME => 1,
916 self::META_SUBELEMENTS => 1,
924 if ( $transformTypes !== null ) {
925 if ( $defaultType ===
'array' && $maxKey !== count(
$data ) - 1 ) {
926 $defaultType =
'assoc';
930 $type = $defaultType;
931 if ( isset( $metadata[self::META_TYPE] ) && $metadata[self::META_TYPE] !==
'default' ) {
932 $type = $metadata[self::META_TYPE];
934 if ( (
$type ===
'kvp' ||
$type ===
'BCkvp' ) &&
935 empty( $transformTypes[
'ArmorKVP'] )
938 } elseif (
$type ===
'BCarray' ) {
940 } elseif (
$type ===
'BCassoc' ) {
947 $metadata[self::META_TYPE] =
'assoc';
948 $data += $keepMetadata;
954 $metadata[self::META_TYPE] =
'array';
955 return $data + $keepMetadata;
959 $key = isset( $metadata[self::META_KVP_KEY_NAME] )
960 ? $metadata[self::META_KVP_KEY_NAME]
961 : $transformTypes[
'ArmorKVP'];
962 $valKey = isset( $transforms[
'BC'] ) ?
'*' :
'value';
963 $assocAsObject = !empty( $transformTypes[
'AssocAsObject'] );
964 $merge = !empty( $metadata[self::META_KVP_MERGE] );
967 foreach (
$data as $k => $v ) {
968 if ( $merge && ( is_array( $v ) || is_object( $v ) ) ) {
970 if ( isset( $vArr[self::META_TYPE] ) ) {
971 $mergeType = $vArr[self::META_TYPE];
972 } elseif ( is_object( $v ) ) {
973 $mergeType =
'assoc';
975 $keys = array_keys( $vArr );
976 sort(
$keys, SORT_NUMERIC );
977 $mergeType = (
$keys === array_keys(
$keys ) ) ?
'array' :
'assoc';
982 if ( $mergeType ===
'assoc' ) {
986 if ( $strip ===
'none' ) {
987 self::setPreserveKeysList( $item, [ $key ] );
994 if ( $strip ===
'none' ) {
996 self::META_PRESERVE_KEYS => [ $key ],
997 self::META_CONTENT => $valKey,
998 self::META_TYPE =>
'assoc',
1002 $ret[] = $assocAsObject ? (
object)$item : $item;
1004 $metadata[self::META_TYPE] =
'array';
1006 return $ret + $keepMetadata;
1012 return $data + $keepMetadata;
1027 if ( is_array(
$data ) || is_object(
$data ) ) {
1028 $isObj = is_object(
$data );
1032 $preserveKeys = isset(
$data[self::META_PRESERVE_KEYS] )
1035 foreach (
$data as $k => $v ) {
1036 if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys,
true ) ) {
1038 } elseif ( is_array( $v ) || is_object( $v ) ) {
1039 $data[$k] = self::stripMetadata( $v );
1061 if ( !is_array( $metadata ) ) {
1064 if ( is_array(
$data ) || is_object(
$data ) ) {
1065 $isObj = is_object(
$data );
1069 $preserveKeys = isset(
$data[self::META_PRESERVE_KEYS] )
1072 foreach (
$data as $k => $v ) {
1073 if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys,
true ) ) {
1093 if ( is_array(
$value ) ) {
1095 if ( !self::isMetadataKey( $k ) ) {
1096 $s += self::size( $v );
1099 } elseif ( is_scalar(
$value ) ) {
1120 foreach (
$path as $i => $k ) {
1121 if ( !isset(
$ret[$k] ) ) {
1122 switch ( $create ) {
1133 $fail = implode(
'.', array_slice(
$path, 0, $i + 1 ) );
1137 if ( !is_array(
$ret[$k] ) ) {
1138 $fail = implode(
'.', array_slice(
$path, 0, $i + 1 ) );
1159 foreach (
$vars as $k => $v ) {
1160 if ( is_array( $v ) || is_object( $v ) ) {
1162 } elseif ( is_bool( $v ) ) {
1166 if ( is_string( $k ) ) {
1168 } elseif ( $k > $maxKey ) {
1172 if ( !$hash && $maxKey !== count(
$vars ) - 1 ) {
& path($path, $create= 'append')
Return a reference to the internal data at $path.
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
the array() calling protocol came about after MediaWiki 1.4rc1.
setErrorFormatter(ApiErrorFormatter $formatter)
Set the error formatter.
addSubelementsList($path, $names)
Causes the elements with the specified names to be output as subelements rather than attributes...
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
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
const META_TYPE
Key for the 'type' metadata item.
it s the revision text itself In either if gzip is the revision text is gzipped $flags
removeValue($path, $name, $flags=0)
Remove value from 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...
when a variable name is used in a it is silently declared as a new local masking the global
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.
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
static size($value)
Get the 'real' size of a result item.
addParsedLimit($moduleName, $limit)
Add the numeric limit for a limit=max to the result.
serializeForApiResult()
Allow for adding one ApiResult into another.
static unsetValue(array &$arr, $name)
Remove an output value to the array by name.
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.
addPreserveKeysList($path, $names)
Preserve specified keys.
static stripMetadata($data)
Recursively remove metadata keys from a data array or object.
static setArrayTypeRecursive(array &$arr, $type, $kvpKeyName=null)
Set the array data type recursively.
static validateValue($value)
Validate a value for addition to the result.
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.
wfDeprecated($function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
static stripMetadataNonRecursive($data, &$metadata=null)
Remove metadata keys from a data array or object, non-recursive.
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.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books $tag
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
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.
addArrayType($path, $tag, $kvpKeyName=null)
Set the array data type for a path.
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
addContentValue($path, $name, $value, $flags=0)
Add value to the output data at the given path and mark as META_CONTENT.
removeSubelementsList($path, $names)
Causes the elements with the specified names to be output as attributes (when possible) rather than a...
const META_INDEXED_TAG_NAME
Key for the 'indexed tag name' metadata item.
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.
const META_SUBELEMENTS
Key for the 'subelements' metadata item.
reset()
Clear the current result data.
removePreserveKeysList($path, $names)
Don't preserve specified keys.
addContentField($path, $name, $flags=0)
Set the name of the content field name (META_CONTENT)
const META_BC_BOOLS
Key for the 'BC bools' metadata item.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive $limit
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
addArrayTypeRecursive($path, $tag, $kvpKeyName=null)
Set the array data type for a path recursively.
Extension of RawMessage implementing IApiMessage.
addIndexedTagNameRecursive($path, $tag)
Set indexed tag name on $path and all subarrays.
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)
const META_CONTENT
Key for the 'content' metadata item.
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.
getResultData($path=[], $transforms=[])
Get the result data array.
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
const META_KVP_KEY_NAME
Key for the metadata item whose value specifies the name used for the kvp key in the alternative outp...
Allows to change the fields on the form that will be generated $name
static isMetadataKey($key)
Test whether a key should be considered metadata.