MediaWiki master
SubmoduleDef.php
Go to the documentation of this file.
1<?php
2
4
9
17class SubmoduleDef extends EnumDef {
18
25 public const PARAM_SUBMODULE_MAP = 'param-submodule-map';
26
31 public const PARAM_SUBMODULE_PARAM_PREFIX = 'param-submodule-param-prefix';
32
34 public function checkSettings( string $name, $settings, array $options, array $ret ): array {
35 $map = $settings[self::PARAM_SUBMODULE_MAP] ?? [];
36 if ( !is_array( $map ) ) {
37 $ret['issues'][self::PARAM_SUBMODULE_MAP] = 'PARAM_SUBMODULE_MAP must be an array, got '
38 . gettype( $map );
39 // Prevent errors in parent::checkSettings()
40 $settings[self::PARAM_SUBMODULE_MAP] = null;
41 }
42
43 $ret = parent::checkSettings( $name, $settings, $options, $ret );
44
45 $ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [
46 self::PARAM_SUBMODULE_MAP, self::PARAM_SUBMODULE_PARAM_PREFIX,
47 ] );
48
49 if ( is_array( $map ) ) {
50 $module = $options['module'];
51 foreach ( $map as $k => $v ) {
52 if ( !is_string( $v ) ) {
53 $ret['issues'][] = 'Values for PARAM_SUBMODULE_MAP must be strings, '
54 . "but value for \"$k\" is " . gettype( $v );
55 continue;
56 }
57
58 try {
59 $submod = $module->getModuleFromPath( $v );
60 } catch ( ApiUsageException ) {
61 $submod = null;
62 }
63 if ( !$submod ) {
64 $ret['issues'][] = "PARAM_SUBMODULE_MAP contains \"$v\", which is not a valid module path";
65 }
66 }
67 }
68
69 if ( !is_string( $settings[self::PARAM_SUBMODULE_PARAM_PREFIX] ?? '' ) ) {
70 $ret['issues'][self::PARAM_SUBMODULE_PARAM_PREFIX] = 'PARAM_SUBMODULE_PARAM_PREFIX must be '
71 . 'a string, got ' . gettype( $settings[self::PARAM_SUBMODULE_PARAM_PREFIX] );
72 }
73
74 return $ret;
75 }
76
78 public function getEnumValues( $name, array $settings, array $options ) {
79 if ( isset( $settings[self::PARAM_SUBMODULE_MAP] ) ) {
80 $modules = array_keys( $settings[self::PARAM_SUBMODULE_MAP] );
81 } else {
82 $modules = $options['module']->getModuleManager()->getNames( $name );
83 }
84
85 return $modules;
86 }
87
89 public function getParamInfo( $name, array $settings, array $options ) {
90 $info = parent::getParamInfo( $name, $settings, $options );
92 $module = $options['module'];
93
94 if ( isset( $settings[self::PARAM_SUBMODULE_MAP] ) ) {
95 $info['type'] = array_keys( $settings[self::PARAM_SUBMODULE_MAP] );
96 $info['submodules'] = $settings[self::PARAM_SUBMODULE_MAP];
97 } else {
98 $info['type'] = $module->getModuleManager()->getNames( $name );
99 $prefix = $module->isMain() ? '' : ( $module->getModulePath() . '+' );
100 $info['submodules'] = [];
101 foreach ( $info['type'] as $v ) {
102 $info['submodules'][$v] = $prefix . $v;
103 }
104 }
105 if ( isset( $settings[self::PARAM_SUBMODULE_PARAM_PREFIX] ) ) {
106 $info['submoduleparamprefix'] = $settings[self::PARAM_SUBMODULE_PARAM_PREFIX];
107 }
108
109 $submoduleFlags = []; // for sorting: higher flags are sorted later
110 $submoduleNames = []; // for sorting: lexicographical, ascending
111 foreach ( $info['submodules'] as $v => $submodulePath ) {
112 try {
113 $submod = $module->getModuleFromPath( $submodulePath );
114 } catch ( ApiUsageException ) {
115 $submoduleFlags[] = 0;
116 $submoduleNames[] = $v;
117 continue;
118 }
119 $flags = 0;
120 if ( $submod && $submod->isDeprecated() ) {
121 $info['deprecatedvalues'][] = $v;
122 $flags |= 1;
123 }
124 if ( $submod && $submod->isInternal() ) {
125 $info['internalvalues'][] = $v;
126 $flags |= 2;
127 }
128 $submoduleFlags[] = $flags;
129 $submoduleNames[] = $v;
130 }
131 // sort $info['submodules'] and $info['type'] by $submoduleFlags and $submoduleNames
132 array_multisort( $submoduleFlags, $submoduleNames, $info['submodules'], $info['type'] );
133 if ( isset( $info['deprecatedvalues'] ) ) {
134 sort( $info['deprecatedvalues'] );
135 }
136 if ( isset( $info['internalvalues'] ) ) {
137 sort( $info['internalvalues'] );
138 }
139
140 return $info;
141 }
142
143 private function getSubmoduleMap( ApiBase $module, string $name, array $settings ): array {
144 if ( isset( $settings[self::PARAM_SUBMODULE_MAP] ) ) {
145 $map = $settings[self::PARAM_SUBMODULE_MAP];
146 } else {
147 $prefix = $module->isMain() ? '' : ( $module->getModulePath() . '+' );
148 $map = [];
149 foreach ( $module->getModuleManager()->getNames( $name ) as $submoduleName ) {
150 $map[$submoduleName] = $prefix . $submoduleName;
151 }
152 }
153
154 return $map;
155 }
156
157 protected function sortEnumValues(
158 string $name, array $values, array $settings, array $options
159 ): array {
160 $module = $options['module'];
161 $map = $this->getSubmoduleMap( $module, $name, $settings );
162
163 $submoduleFlags = []; // for sorting: higher flags are sorted later
164 foreach ( $values as $k => $v ) {
165 $flags = 0;
166 try {
167 $submod = isset( $map[$v] ) ? $module->getModuleFromPath( $map[$v] ) : null;
168 if ( $submod && $submod->isDeprecated() ) {
169 $flags |= 1;
170 }
171 if ( $submod && $submod->isInternal() ) {
172 $flags |= 2;
173 }
174 } catch ( ApiUsageException ) {
175 // Ignore
176 }
177 $submoduleFlags[$k] = $flags;
178 }
179 array_multisort( $submoduleFlags, $values, SORT_NATURAL );
180
181 return $values;
182 }
183
185 protected function getEnumValuesForHelp( $name, array $settings, array $options ) {
186 $module = $options['module'];
187 $map = $this->getSubmoduleMap( $module, $name, $settings );
188 $defaultAttrs = [ 'dir' => 'ltr', 'lang' => 'en' ];
189
190 $values = [];
191 $submoduleFlags = []; // for sorting: higher flags are sorted later
192 $submoduleNames = []; // for sorting: lexicographical, ascending
193 foreach ( $map as $v => $m ) {
194 $attrs = $defaultAttrs;
195 $flags = 0;
196 try {
197 $submod = $module->getModuleFromPath( $m );
198 if ( $submod && $submod->isDeprecated() ) {
199 $attrs['class'][] = 'apihelp-deprecated-value';
200 $flags |= 1;
201 }
202 if ( $submod && $submod->isInternal() ) {
203 $attrs['class'][] = 'apihelp-internal-value';
204 $flags |= 2;
205 }
206 } catch ( ApiUsageException ) {
207 // Ignore
208 }
209 $v = Html::element( 'span', $attrs, $v );
210 $values[] = "[[Special:ApiHelp/{$m}|{$v}]]";
211 $submoduleFlags[] = $flags;
212 $submoduleNames[] = $v;
213 }
214 // sort $values by $submoduleFlags and $submoduleNames
215 array_multisort( $submoduleFlags, $submoduleNames, SORT_NATURAL, $values, SORT_NATURAL );
216
217 return $values;
218 }
219
220}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:61
Exception used to abort API execution with an error.
Type definition for submodule types.
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.This is intended for validation of parameter settings during unit...
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.Keys should be short strings using lowercase...
const PARAM_SUBMODULE_MAP
(string[]) Map parameter values to submodule paths.
getEnumValues( $name, array $settings, array $options)
Get the values for enum-like parameters.This is primarily intended for documentation and implementati...
const PARAM_SUBMODULE_PARAM_PREFIX
(string) Used to indicate the 'g' prefix added by ApiQueryGeneratorBase (and similar if anything else...
getEnumValuesForHelp( $name, array $settings, array $options)
Return enum values formatted for the help message.(MessageParam|string)[]
sortEnumValues(string $name, array $values, array $settings, array $options)
Sort enum values for help/param info output.
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
Type definition for enumeration types.
Definition EnumDef.php:32