158 $this->errorFormatter =
$maxSize->getErrorFormatter();
159 $this->mainForContinuation =
$maxSize;
164 $this->checkingSize =
true;
174 $this->errorFormatter = $formatter;
196 self::META_TYPE =>
'assoc',
262 if ( !isset( $ret[
$last] ) ) {
264 } elseif ( is_array( $ret[
$last] ) ) {
291 public static function setValue( array &$arr, $name, $value, $flags = 0 ) {
292 if ( ( $flags & self::NO_VALIDATE ) !== self::NO_VALIDATE ) {
296 if ( $name ===
null ) {
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;
310 $arr[$name] = $value;
312 } elseif ( is_array( $arr[$name] ) && is_array( $value ) ) {
313 $conflicts = array_intersect_key( $arr[$name], $value );
315 $arr[$name] += $value;
317 $keys = implode(
', ', array_keys( $conflicts ) );
318 throw new RuntimeException(
319 "Conflicting keys ($keys) when attempting to merge element $name"
323 throw new RuntimeException(
324 "Attempting to add element $name=$value, existing value is {$arr[$name]}"
335 if ( is_object( $value ) ) {
338 if ( is_callable( [ $value,
'serializeForApiResult' ] ) ) {
340 $value = $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' ] ) ) {
361 $value = (string)$value;
363 $value = (array)$value + [ self::META_TYPE =>
'assoc' ];
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 ) ) {
377 $value = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $value );
378 } elseif ( $value !==
null && !is_scalar( $value ) ) {
379 $type = gettype( $value );
380 if ( is_resource( $value ) ) {
381 $type .=
'(' . get_resource_type( $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] );
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] ) ) {
593 if ( isset( $arr[self::META_SUBELEMENTS] ) ) {
617 if ( !is_string( $tag ) ) {
618 throw new InvalidArgumentException(
'Bad tag name' );
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 ) ) {
676 if ( !isset( $arr[self::META_PRESERVE_KEYS] ) ) {
703 if ( isset( $arr[self::META_PRESERVE_KEYS] ) ) {
729 if ( !in_array(
$type, [
730 'default',
'array',
'assoc',
'kvp',
'BCarray',
'BCassoc',
'BCkvp'
732 throw new InvalidArgumentException(
'Bad type' );
735 if ( is_string( $kvpKeyName ) ) {
761 foreach ( $arr as $k => &$v ) {
762 if ( !self::isMetadataKey( $k ) && is_array( $v ) ) {
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 ) ) {
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;
947 return empty( $transformTypes[
'AssocAsObject'] ) ?
$data : (object)
$data;
953 return $data + $keepMetadata;
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] ) ) {
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' ) {
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;
1002 return $ret + $keepMetadata;
1005 throw new UnexpectedValueException(
"Unknown type '$type'" );
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] )
1029 ? (array)
$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 ) ) {
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] )
1066 ? (array)
$data[self::META_PRESERVE_KEYS]
1068 foreach (
$data as $k => $v ) {
1069 if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys,
true ) ) {
1087 private static function size( $value ) {
1089 if ( is_array( $value ) ) {
1090 foreach ( $value as $k => $v ) {
1091 if ( !self::isMetadataKey( $k ) ) {
1095 } elseif ( is_scalar( $value ) ) {
1096 $s = strlen( $value );
1116 foreach (
$path as $i => $k ) {
1117 if ( !isset( $ret[$k] ) ) {
1118 switch ( $create ) {
1123 $ret = [ $k => [] ] + $ret;
1129 $fail = implode(
'.', array_slice(
$path, 0, $i + 1 ) );
1130 throw new InvalidArgumentException(
"Path $fail does not exist" );
1133 if ( !is_array( $ret[$k] ) ) {
1134 $fail = implode(
'.', array_slice(
$path, 0, $i + 1 ) );
1135 throw new InvalidArgumentException(
"Path $fail is not an array" );
1155 foreach ( $vars as $k => $v ) {
1156 if ( is_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 ) {
1176 $keys = array_diff( array_keys( $vars ), [
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 ||