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