MediaWiki  master
NamespaceDef.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use ApiResult;
6 use NamespaceInfo;
10 
18 class NamespaceDef extends EnumDef {
19 
25  public const PARAM_EXTRA_NAMESPACES = 'param-extra-namespaces';
26 
28  private $nsInfo;
29 
31  parent::__construct( $callbacks );
32  $this->nsInfo = $nsInfo;
33  }
34 
35  public function validate( $name, $value, array $settings, array $options ) {
36  if ( !is_int( $value ) && preg_match( '/^[+-]?\d+$/D', $value ) ) {
37  // Convert to int since that's what getEnumValues() returns.
38  $value = (int)$value;
39  }
40 
41  return parent::validate( $name, $value, $settings, $options );
42  }
43 
44  public function getEnumValues( $name, array $settings, array $options ) {
45  $namespaces = $this->nsInfo->getValidNamespaces();
46  $extra = $settings[self::PARAM_EXTRA_NAMESPACES] ?? [];
47  if ( is_array( $extra ) && $extra !== [] ) {
48  $namespaces = array_merge( $namespaces, $extra );
49  }
50  sort( $namespaces );
51  return $namespaces;
52  }
53 
54  public function normalizeSettings( array $settings ) {
55  // Force PARAM_ALL
56  if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) ) {
57  $settings[ParamValidator::PARAM_ALL] = true;
58  }
59  return parent::normalizeSettings( $settings );
60  }
61 
62  public function checkSettings( string $name, $settings, array $options, array $ret ) : array {
63  $ret = parent::checkSettings( $name, $settings, $options, $ret );
64 
65  $ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [
66  self::PARAM_EXTRA_NAMESPACES,
67  ] );
68 
69  if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
70  ( $settings[ParamValidator::PARAM_ALL] ?? true ) !== true &&
71  !isset( $ret['issues'][ParamValidator::PARAM_ALL] )
72  ) {
73  $ret['issues'][ParamValidator::PARAM_ALL] =
74  'PARAM_ALL cannot be false or a string for namespace-type parameters';
75  }
76 
77  $ns = $settings[self::PARAM_EXTRA_NAMESPACES] ?? [];
78  if ( !is_array( $ns ) ) {
79  $type = gettype( $ns );
80  } elseif ( $ns === [] ) {
81  $type = 'integer[]';
82  } else {
83  $types = array_unique( array_map( 'gettype', $ns ) );
84  $type = implode( '|', $types );
85  $type = count( $types ) > 1 ? "($type)[]" : "{$type}[]";
86  }
87  if ( $type !== 'integer[]' ) {
88  $ret['issues'][self::PARAM_EXTRA_NAMESPACES] =
89  "PARAM_EXTRA_NAMESPACES must be an integer[], got $type";
90  }
91 
92  return $ret;
93  }
94 
95  public function getParamInfo( $name, array $settings, array $options ) {
96  $info = parent::getParamInfo( $name, $settings, $options );
97 
98  $info['type'] = 'namespace';
99  $extra = $settings[self::PARAM_EXTRA_NAMESPACES] ?? [];
100  if ( is_array( $extra ) && $extra !== [] ) {
101  $info['extranamespaces'] = array_values( $extra );
102  if ( isset( $options['module'] ) ) {
103  // ApiResult metadata when used with the Action API.
104  ApiResult::setIndexedTagName( $info['extranamespaces'], 'ns' );
105  }
106  }
107 
108  return $info;
109  }
110 
111 }
Wikimedia\ParamValidator\Callbacks
Interface defining callbacks needed by ParamValidator.
Definition: Callbacks.php:21
Wikimedia\ParamValidator\TypeDef\EnumDef
Type definition for enumeration types.
Definition: EnumDef.php:32
MediaWiki\ParamValidator\TypeDef
Definition: NamespaceDef.php:3
Wikimedia\ParamValidator\ParamValidator::TypeDef\NamespaceDef\$nsInfo
NamespaceInfo $nsInfo
Definition: NamespaceDef.php:28
Wikimedia\ParamValidator\ParamValidator::TypeDef\NamespaceDef\getEnumValues
getEnumValues( $name, array $settings, array $options)
Get the values for enum-like parameters.
Definition: NamespaceDef.php:44
Wikimedia\ParamValidator\ParamValidator\PARAM_ALL
const PARAM_ALL
(bool|string) Whether a magic "all values" value exists for multi-valued enumerated types,...
Definition: ParamValidator.php:137
ApiResult
This class represents the result of the API operations.
Definition: ApiResult.php:35
Wikimedia\ParamValidator\ParamValidator::TypeDef\NamespaceDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition: NamespaceDef.php:35
Wikimedia\ParamValidator\ParamValidator::TypeDef\NamespaceDef\normalizeSettings
normalizeSettings(array $settings)
Normalize a settings array.
Definition: NamespaceDef.php:54
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:604
Wikimedia\ParamValidator\ParamValidator\PARAM_ISMULTI
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
Definition: ParamValidator.php:112
Wikimedia\ParamValidator\ParamValidator::TypeDef\NamespaceDef\getParamInfo
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.
Definition: NamespaceDef.php:95
Wikimedia\ParamValidator\ParamValidator::TypeDef\NamespaceDef\checkSettings
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.
Definition: NamespaceDef.php:62
Wikimedia\ParamValidator\ParamValidator::TypeDef\NamespaceDef\PARAM_EXTRA_NAMESPACES
const PARAM_EXTRA_NAMESPACES
(int[]) Additional namespace IDs to recognize.
Definition: NamespaceDef.php:25
Wikimedia\ParamValidator\ParamValidator::TypeDef\NamespaceDef
Type definition for namespace types.
Definition: NamespaceDef.php:18
NamespaceInfo
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Definition: NamespaceInfo.php:35
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
Wikimedia\ParamValidator\TypeDef\$callbacks
Callbacks $callbacks
Definition: TypeDef.php:21
Wikimedia\ParamValidator\ParamValidator::TypeDef\NamespaceDef\__construct
__construct(Callbacks $callbacks, NamespaceInfo $nsInfo)
Definition: NamespaceDef.php:30
$type
$type
Definition: testCompression.php:52