Debian Codegraph @

maintenance/generateConfigSchema.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigSchema;
4 use MediaWiki\Settings\Config\ConfigSchemaAggregator;
5 use MediaWiki\Settings\Source\ReflectionSchemaSource;
6 use Symfony\Component\Yaml\Yaml;
7 use Wikimedia\StaticArrayWriter;
8
9 // @codeCoverageIgnoreStart
10 require_once __DIR__ . '/Maintenance.php';
11
12 // Tell Setup.php to load the config schema from MainConfigSchema rather than
13 // any generated file, so we can use this script to re-generate a broken schema file.
14 define( 'MW_USE_CONFIG_SCHEMA_CLASS', 1 );
15 // @codeCoverageIgnoreEnd
16
17 /**
18 * Maintenance script that generates configuration schema files:
19 * - includes/MainConfigNames.php: name constants for config settings
20 * - docs/config-vars.php: dummy variable declarations for config settings
21 * - includes/config-schema.php: an optimized config schema for use by Setup.php
22 * - docs/config-schema.yaml: a JSON Schema of the config settings
23 *
24 * @ingroup Maintenance
25 */
26 class GenerateConfigSchema extends Maintenance {
27
28 private const DEFAULT_NAMES_PATH = __DIR__ . '/../includes/MainConfigNames.php';
29 private const DEFAULT_VARS_PATH = __DIR__ . '/../docs/config-vars.php';
30 private const DEFAULT_ARRAY_PATH = __DIR__ . '/../includes/config-schema.php';
31 private const DEFAULT_SCHEMA_PATH = __DIR__ . '/../docs/config-schema.yaml';
32 private const STDOUT = 'php://stdout';
33
34 /** @var array */
35 private $settingsArray;
36
37 public function __construct() {
38 parent::__construct();
39
40 $this->addDescription( 'Generates various config schema files.' );
41
42 $this->addOption(
43 'vars',
44 'Path to output variable stubs to. ' .
45 'Default if none of the options is given: ' .
46 self::DEFAULT_VARS_PATH,
47 false,
48 true
49 );
50
51 $this->addOption(
52 'schema',
53 'Path to output the schema array to. ' .
54 'Default if none of the options is given: ' .
55 self::DEFAULT_ARRAY_PATH,
56 false,
57 true
58 );
59
60 $this->addOption(
61 'names',
62 'Path to output the name constants to. ' .
63 'Default if none of the options is given: ' .
64 self::DEFAULT_NAMES_PATH,
65 false,
66 true
67 );
68
69 $this->addOption(
70 'yaml',
71 'Path to output the schema YAML to. ' .
72 'Default if none of the options is given: ' .
73 self::DEFAULT_SCHEMA_PATH,
74 false,
75 true
76 );
77 }
78
79 public function canExecuteWithoutLocalSettings(): bool {
80 return true;
81 }
82
83 public function getDbType() {
84 return self::DB_NONE;
85 }
86
87 /**
88 * Loads the config schema from the MainConfigSchema class.
89 *
90 * @return array An associative array with a single key, 'config-schema',
91 * containing the config schema definition.
92 */
93 private function getSettings(): array {
94 if ( !$this->settingsArray ) {
95 $source = new ReflectionSchemaSource( MainConfigSchema::class, true );
96 $this->settingsArray = $source->load();
97 }
98
99 return $this->settingsArray;
100 }
101
102 /**
103 * @param string $path
104 * @param string $content
105 */
106 private function writeOutput( $path, $content ) {
107 // ensure a single line break at the end of the file
108 $content = trim( $content ) . "\n";
109
110 file_put_contents( $path, $content );
111 }
112
113 /**
114 * @param string $name The name of the option
115 *
116 * @return ?string
117 */
118 private function getOutputPath( string $name ): ?string {
119 $outputPath = $this->getOption( $name );
120 if ( $outputPath === '-' ) {
121 $outputPath = self::STDOUT;
122 }
123 return $outputPath;
124 }
125
126 public function execute() {
127 $settings = $this->getSettings();
128 $allSchemas = $settings['config-schema'];
129 $obsolete = $settings['obsolete-config'] ?? [];
130
131 $schemaPath = $this->getOutputPath( 'schema' );
132 $varsPath = $this->getOutputPath( 'vars' );
133 $yamlPath = $this->getOutputPath( 'yaml' );
134 $namesPath = $this->getOutputPath( 'names' );
135
136 if ( $schemaPath === null && $varsPath === null &&
137 $yamlPath === null && $namesPath === null
138 ) {
139 // If no output path is specified explicitly, use the default path for all.
140 $schemaPath = self::DEFAULT_ARRAY_PATH;
141 $varsPath = self::DEFAULT_VARS_PATH;
142 $yamlPath = self::DEFAULT_SCHEMA_PATH;
143 $namesPath = self::DEFAULT_NAMES_PATH;
144 }
145
146 if ( $schemaPath === self::STDOUT || $varsPath === self::STDOUT ||
147 $yamlPath === self::STDOUT || $namesPath === self::STDOUT
148 ) {
149 // If any of the output is stdout, switch to quiet mode.
150 $this->mQuiet = true;
151 }
152
153 if ( $schemaPath !== null ) {
154 $this->output( "Writing schema array to $schemaPath\n" );
155 $this->writeOutput( $schemaPath, $this->generateSchemaArray( $allSchemas, $obsolete ) );
156 }
157
158 if ( $varsPath !== null ) {
159 $this->output( "Writing variable stubs to $varsPath\n" );
160 $this->writeOutput( $varsPath, $this->generateVariableStubs( $allSchemas ) );
161 }
162
163 if ( $yamlPath !== null ) {
164 $this->output( "Writing schema YAML to $yamlPath\n" );
165 $this->writeOutput( $yamlPath, $this->generateSchemaYaml( $allSchemas ) );
166 }
167
168 if ( $namesPath !== null ) {
169 $this->output( "Writing name constants to $namesPath\n" );
170 $this->writeOutput( $namesPath, $this->generateNames( $allSchemas ) );
171 }
172 }
173
174 public function generateSchemaArray( array $allSchemas, array $obsolete ) {
175 $aggregator = new ConfigSchemaAggregator();
176 foreach ( $allSchemas as $key => $schema ) {
177 $aggregator->addSchema( $key, $schema );
178 }
179 $schemaInverse = [
180 'default' => $aggregator->getDefaults(),
181 'type' => $aggregator->getTypes(),
182 'mergeStrategy' => $aggregator->getMergeStrategyNames(),
183 'dynamicDefault' => $aggregator->getDynamicDefaults(),
184 ];
185
186 $keyMask = array_flip( [
187 'default',
188 'type',
189 'mergeStrategy',
190 'dynamicDefault',
191 'description',
192 'properties'
193 ] );
194
195 $schemaExtra = [];
196 foreach ( $aggregator->getDefinedKeys() as $key ) {
197 $sch = $aggregator->getSchemaFor( $key );
198 $sch = array_diff_key( $sch, $keyMask );
199
200 if ( $sch ) {
201 $schemaExtra[ $key ] = $sch;
202 }
203 }
204
205 $content = ( new StaticArrayWriter() )->write(
206 [
207 'config-schema-inverse' => $schemaInverse,
208 'config-schema' => $schemaExtra,
209 'obsolete-config' => $obsolete
210 ],
211 "This file is automatically generated using maintenance/generateConfigSchema.php.\n" .
212 "Do not modify this file manually, edit includes/MainConfigSchema.php instead.\n" .
213 "phpcs:disable Generic.Files.LineLength"
214 );
215
216 return $content;
217 }
218
219 public function generateNames( array $allSchemas ) {
220 $code = "<?php\n";
221 $code .= "/**\n" .
222 " * This file is automatically generated using maintenance/generateConfigSchema.php.\n" .
223 " * Do not modify this file manually, edit includes/MainConfigSchema.php instead.\n" .
224 " * @file\n" .
225 " * @ingroup Config\n" .
226 " */\n\n";
227
228 $code .= "// phpcs:disable Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase\n";
229 $code .= "// phpcs:disable Generic.Files.LineLength.TooLong\n";
230 $code .= "namespace MediaWiki;\n\n";
231
232 $code .= "/**\n" .
233 " * A class containing constants representing the names of configuration variables.\n" .
234 " * These constants can be used in calls to Config::get() or with ServiceOptions,\n" .
235 " * to protect against typos and to make it easier to discover documentation about\n" .
236 " * the respective config setting.\n" .
237 " *\n" .
238 " * @note this class is generated automatically by maintenance/generateConfigSchema.php\n" .
239 " * @since 1.39\n" .
240 " */\n";
241
242 $code .= "class MainConfigNames {\n";
243
244 // Details about each config variable
245 foreach ( $allSchemas as $configKey => $configSchema ) {
246 $code .= "\n";
247 $code .= $this->getConstantDeclaration( $configKey, $configSchema );
248 }
249
250 $code .= "\n}\n";
251
252 return $code;
253 }
254
255 /**
256 * @param string $name
257 * @param array $schema
258 *
259 * @return string
260 */
261 private function getConstantDeclaration( string $name, array $schema ): string {
262 $chunks = [];
263
264 $chunks[] = "Name constant for the $name setting, for use with Config::get()";
265 $chunks[] = "@see MainConfigSchema::$name";
266
267 if ( isset( $schema['since'] ) ) {
268 $chunks[] = "@since {$schema['since']}";
269 }
270
271 if ( isset( $schema['deprecated'] ) ) {
272 $deprecated = str_replace( "\n", "\n\t * ", wordwrap( $schema['deprecated'] ) );
273 $chunks[] = "@deprecated {$deprecated}";
274 }
275
276 $code = "\t/**\n\t * ";
277 $code .= implode( "\n\t * ", $chunks );
278 $code .= "\n\t */\n";
279
280 $code .= "\tpublic const $name = '$name';\n";
281 return $code;
282 }
283
284 public function generateSchemaYaml( array $allSchemas ) {
285 foreach ( $allSchemas as &$sch ) {
286 // Cast empty arrays to objects if they are declared to be of type object.
287 // This ensures they get represented in yaml as {} rather than [].
288 if ( isset( $sch['default'] ) && isset( $sch['type'] ) ) {
289 $types = (array)$sch['type'];
290 if ( $sch['default'] === [] && in_array( 'object', $types ) ) {
291 $sch['default'] = new stdClass();
292 }
293 }
294
295 // Wrap long deprecation messages
296 if ( isset( $sch['deprecated'] ) ) {
297 $sch['deprecated'] = wordwrap( $sch['deprecated'] );
298 }
299 }
300
301 // Dynamic defaults are not relevant to yaml consumers
302 unset( $sch['dynamicDefault'] );
303
304 $yamlFlags = Yaml::DUMP_OBJECT_AS_MAP
305 | Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK
306 | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE;
307
308 $array = [ 'config-schema' => $allSchemas ];
309 $yaml = Yaml::dump( $array, 4, 4, $yamlFlags );
310
311 $header = "# This file is automatically generated using maintenance/generateConfigSchema.php.\n";
312 $header .= "# Do not modify this file manually, edit includes/MainConfigSchema.php instead.\n";
313
314 return $header . $yaml;
315 }
316
317 public function generateVariableStubs( array $allSchemas ) {
318 $content = "<?php\n";
319 $content .= "/**\n" .
320 " * This file is automatically generated using maintenance/generateConfigSchema.php.\n" .
321 " * Do not modify this file manually, edit includes/MainConfigSchema.php instead.\n" .
322 " */\n";
323
324 $content .= "// phpcs:disable\n";
325 $content .= "throw new LogicException( 'Do not load config-vars.php, " .
326 "it exists as a documentation stub only' );\n";
327
328 foreach ( $allSchemas as $name => $schema ) {
329 $content .= "\n";
330 $content .= $this->getVariableDeclaration( $name, $schema );
331 }
332
333 return $content;
334 }
335
336 /**
337 * @param string $name
338 * @param array $schema
339 *
340 * @return string
341 */
342 private function getVariableDeclaration( string $name, array $schema ): string {
343 $chunks = [];
344 $chunks[] = "Config variable stub for the $name setting, for use by phpdoc and IDEs.";
345 $chunks[] = "@see MediaWiki\\MainConfigSchema::$name";
346
347 if ( isset( $schema['since'] ) ) {
348 $chunks[] = "@since {$schema['since']}";
349 }
350
351 if ( isset( $schema['deprecated'] ) ) {
352 $deprecated = str_replace( "\n", "\n * ", wordwrap( $schema['deprecated'] ) );
353 $chunks[] = "@deprecated {$deprecated}";
354 }
355
356 $code = "/**\n * ";
357 $code .= implode( "\n * ", $chunks );
358 $code .= "\n */\n";
359
360 $code .= "\$wg{$name} = null;\n";
361 return $code;
362 }
363 }
364
365 // @codeCoverageIgnoreStart
366 $maintClass = GenerateConfigSchema::class;
367 require_once RUN_MAINTENANCE_IF_MAIN;
368 // @codeCoverageIgnoreEnd