MediaWiki master
convertExtensionToRegistration.php
Go to the documentation of this file.
1<?php
2
5
6require_once __DIR__ . '/Maintenance.php';
7
9
10 protected $custom = [
11 'MessagesDirs' => 'handleMessagesDirs',
12 'ExtensionMessagesFiles' => 'handleExtensionMessagesFiles',
13 'AutoloadClasses' => 'removeAbsolutePath',
14 'ExtensionCredits' => 'handleCredits',
15 'ResourceModules' => 'handleResourceModules',
16 'ResourceModuleSkinStyles' => 'handleResourceModules',
17 'Hooks' => 'handleHooks',
18 'ExtensionFunctions' => 'handleExtensionFunctions',
19 'ParserTestFiles' => 'removeAutodiscoveredParserTestFiles',
20 ];
21
27 protected $formerGlobals = [
28 'TrackingCategories',
29 ];
30
37 'SpecialPageGroups' => 'deprecated', // Deprecated 1.21, removed in 1.26
38 ];
39
45 protected $promote = [
46 'name',
47 'namemsg',
48 'version',
49 'author',
50 'url',
51 'description',
52 'descriptionmsg',
53 'license-name',
54 'type',
55 ];
56
57 private array $json;
58 private string $dir;
59 private bool $hasWarning = false;
60
61 public function __construct() {
62 parent::__construct();
63 $this->addDescription( 'Converts extension entry points to the new JSON registration format' );
64 $this->addArg( 'path', 'Location to the PHP entry point you wish to convert',
65 /* $required = */ true );
66 $this->addOption( 'skin', 'Whether to write to skin.json', false, false );
67 $this->addOption( 'config-prefix', 'Custom prefix for configuration settings', false, true );
68 }
69
70 protected function getAllGlobals() {
71 $processor = new ReflectionClass( ExtensionProcessor::class );
72 $settings = $processor->getProperty( 'globalSettings' );
73 $settings->setAccessible( true );
74 return array_merge( $settings->getValue(), $this->formerGlobals );
75 }
76
77 public function execute() {
78 // Extensions will do stuff like $wgResourceModules += array(...) which is a
79 // fatal unless an array is already set. So set an empty value.
80 // And use the weird $__settings name to avoid any conflicts
81 // with real poorly named settings.
82 $__settings = array_merge( $this->getAllGlobals(), array_keys( $this->custom ) );
83 foreach ( $__settings as $var ) {
84 $var = 'wg' . $var;
85 $$var = [];
86 }
87 unset( $var );
88 $arg = $this->getArg( 0 );
89 if ( !is_file( $arg ) ) {
90 $this->fatalError( "$arg is not a file." );
91 }
92 require $arg;
93 unset( $arg );
94 // Try not to create any local variables before this line
95 $vars = get_defined_vars();
96 unset( $vars['this'] );
97 unset( $vars['__settings'] );
98 $this->dir = dirname( realpath( $this->getArg( 0 ) ) );
99 $this->json = [];
100 $globalSettings = $this->getAllGlobals();
101 $configPrefix = $this->getOption( 'config-prefix', 'wg' );
102 if ( $configPrefix !== 'wg' ) {
103 $this->json['config']['_prefix'] = $configPrefix;
104 }
105
106 foreach ( $vars as $name => $value ) {
107 $realName = substr( $name, 2 ); // Strip 'wg'
108 if ( $realName === false ) {
109 continue;
110 }
111
112 // If it's an empty array that we likely set, skip it
113 if ( is_array( $value ) && count( $value ) === 0 && in_array( $realName, $__settings ) ) {
114 continue;
115 }
116
117 if ( isset( $this->custom[$realName] ) ) {
118 call_user_func_array( [ $this, $this->custom[$realName] ],
119 [ $realName, $value, $vars ] );
120 } elseif ( in_array( $realName, $globalSettings ) ) {
121 $this->json[$realName] = $value;
122 } elseif ( array_key_exists( $realName, $this->noLongerSupportedGlobals ) ) {
123 $this->output( 'Warning: Skipped global "' . $name . '" (' .
124 $this->noLongerSupportedGlobals[$realName] . '). ' .
125 "Please update the entry point before convert to registration.\n" );
126 $this->hasWarning = true;
127 } elseif ( strpos( $name, $configPrefix ) === 0 ) {
128 $configName = substr( $name, strlen( $configPrefix ) );
129
130 $isPath = false;
131 if ( is_array( $value ) ) {
132 foreach ( $value as $k => $v ) {
133 if ( strpos( $v, $this->dir ) !== false ) {
134 $value[$k] = $this->stripPath( $v, $this->dir );
135 $isPath = true;
136 }
137 }
138 } elseif ( is_string( $value ) && strpos( $value, $this->dir ) !== false ) {
139 $value = $this->stripPath( $value, $this->dir );
140 $isPath = true;
141 }
142
143 // Most likely a config setting
144 $this->json['config'][$configName] = [ 'value' => $value ];
145
146 if ( $isPath ) {
147 $this->json['config'][$configName]['path'] = true;
148 }
149 } elseif ( $configPrefix !== 'wg' && strpos( $name, 'wg' ) === 0 ) {
150 // Warn about this
151 $this->output( 'Warning: Skipped global "' . $name . '" (' .
152 'config prefix is "' . $configPrefix . '"). ' .
153 "Please check that this setting isn't needed.\n" );
154 }
155 }
156
157 // check, if the extension requires composer libraries
158 if ( $this->needsComposerAutoloader( dirname( $this->getArg( 0 ) ) ) ) {
159 // set the load composer autoloader automatically property
160 $this->output( "Detected composer dependencies, setting 'load_composer_autoloader' to true.\n" );
161 $this->json['load_composer_autoloader'] = true;
162 }
163
164 // Move some keys to the top
165 $out = [];
166 foreach ( $this->promote as $key ) {
167 if ( isset( $this->json[$key] ) ) {
168 $out[$key] = $this->json[$key];
169 unset( $this->json[$key] );
170 }
171 }
172 // Set a requirement on the MediaWiki version that the current MANIFEST_VERSION
173 // was introduced in.
174 $out['requires'] = [
175 ExtensionRegistry::MEDIAWIKI_CORE => ExtensionRegistry::MANIFEST_VERSION_MW_VERSION
176 ];
177 $out += $this->json;
178 // Put this at the bottom
179 $out['manifest_version'] = ExtensionRegistry::MANIFEST_VERSION;
180 $type = $this->hasOption( 'skin' ) ? 'skin' : 'extension';
181 $fname = "{$this->dir}/$type.json";
182 $prettyJSON = FormatJson::encode( $out, "\t", FormatJson::ALL_OK );
183 file_put_contents( $fname, $prettyJSON . "\n" );
184 $this->output( "Wrote output to $fname.\n" );
185 if ( $this->hasWarning ) {
186 $this->output( "Found warnings! Please resolve the warnings and rerun this script.\n" );
187 }
188 }
189
190 protected function handleExtensionFunctions( $realName, $value ) {
191 foreach ( $value as $func ) {
192 if ( $func instanceof Closure ) {
193 $this->fatalError( "Error: Closures cannot be converted to JSON. " .
194 "Please move your extension function somewhere else."
195 );
196 } elseif ( function_exists( $func ) ) {
197 // check if $func exists in the global scope
198 $this->fatalError( "Error: Global functions cannot be converted to JSON. " .
199 "Please move your extension function ($func) into a class."
200 );
201 }
202 }
203
204 $this->json[$realName] = $value;
205 }
206
207 protected function handleMessagesDirs( $realName, $value ) {
208 foreach ( $value as $key => $dirs ) {
209 foreach ( (array)$dirs as $dir ) {
210 $this->json[$realName][$key][] = $this->stripPath( $dir, $this->dir );
211 }
212 }
213 }
214
215 protected function handleExtensionMessagesFiles( $realName, $value, $vars ) {
216 foreach ( $value as $key => $file ) {
217 $strippedFile = $this->stripPath( $file, $this->dir );
218 if ( isset( $vars['wgMessagesDirs'][$key] ) ) {
219 $this->output(
220 "Note: Ignoring PHP shim $strippedFile. " .
221 "If your extension no longer supports versions of MediaWiki " .
222 "older than 1.23.0, you can safely delete it.\n"
223 );
224 } else {
225 $this->json[$realName][$key] = $strippedFile;
226 }
227 }
228 }
229
230 private function stripPath( $val, $dir ) {
231 if ( $val === $dir ) {
232 $val = '';
233 } elseif ( strpos( $val, $dir ) === 0 ) {
234 // +1 is for the trailing / that won't be in $this->dir
235 $val = substr( $val, strlen( $dir ) + 1 );
236 }
237
238 return $val;
239 }
240
241 protected function removeAbsolutePath( $realName, $value ) {
242 $out = [];
243 foreach ( $value as $key => $val ) {
244 $out[$key] = $this->stripPath( $val, $this->dir );
245 }
246 $this->json[$realName] = $out;
247 }
248
249 protected function removeAutodiscoveredParserTestFiles( $realName, $value ) {
250 $out = [];
251 foreach ( $value as $key => $val ) {
252 $path = $this->stripPath( $val, $this->dir );
253 // When path starts with tests/parser/ the file would be autodiscovered with
254 // extension registry, so no need to add it to extension.json
255 if ( !str_starts_with( $path, 'tests/parser/' ) || !str_ends_with( $path, '.txt' ) ) {
256 $out[$key] = $path;
257 }
258 }
259 // in the best case all entries are filtered out
260 if ( $out ) {
261 $this->json[$realName] = $out;
262 }
263 // FIXME: the ParserTestFiles key was deprecated in
264 // MW 1.30 and removed in MW 1.40. If not all entries were filtered
265 // out by the above, we *should* recommend the user move the
266 // parser tests under `tests/parser` *not* generate an extension.json
267 // with a ParserTestFiles key that will no longer validate.
268 }
269
270 protected function handleCredits( $realName, $value ) {
271 $keys = array_keys( $value );
272 $this->json['type'] = $keys[0];
273 $values = array_values( $value );
274 foreach ( $values[0][0] as $name => $val ) {
275 if ( $name !== 'path' ) {
276 $this->json[$name] = $val;
277 }
278 }
279 }
280
281 public function handleHooks( $realName, $value ) {
282 foreach ( $value as $hookName => &$handlers ) {
283 if ( $hookName === 'UnitTestsList' ) {
284 $this->output( "Note: the UnitTestsList hook is no longer necessary as " .
285 "long as your tests are located in the \"tests/phpunit/\" directory. " .
286 "Please see <https://www.mediawiki.org/wiki/Manual:PHP_unit_testing/" .
287 "Writing_unit_tests_for_extensions#Register_your_tests> for more details.\n"
288 );
289 }
290 foreach ( $handlers as $func ) {
291 if ( $func instanceof Closure ) {
292 $this->fatalError( "Error: Closures cannot be converted to JSON. " .
293 "Please move the handler for $hookName somewhere else."
294 );
295 } elseif ( function_exists( $func ) ) {
296 // Check if $func exists in the global scope
297 $this->fatalError( "Error: Global functions cannot be converted to JSON. " .
298 "Please move the handler for $hookName inside a class."
299 );
300 }
301 }
302 if ( count( $handlers ) === 1 ) {
303 $handlers = $handlers[0];
304 }
305 }
306 $this->json[$realName] = $value;
307 }
308
313 protected function handleResourceModules( $realName, $value ) {
314 $defaults = [];
315 $remote = $this->hasOption( 'skin' ) ? 'remoteSkinPath' : 'remoteExtPath';
316 foreach ( $value as $name => $data ) {
317 if ( isset( $data['localBasePath'] ) ) {
318 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir );
319 if ( !$defaults ) {
320 $defaults['localBasePath'] = $data['localBasePath'];
321 unset( $data['localBasePath'] );
322 if ( isset( $data[$remote] ) ) {
323 $defaults[$remote] = $data[$remote];
324 unset( $data[$remote] );
325 }
326 } else {
327 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
328 unset( $data['localBasePath'] );
329 }
330 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
331 && $data[$remote] === $defaults[$remote]
332 ) {
333 unset( $data[$remote] );
334 }
335 }
336 }
337
338 $this->json[$realName][$name] = $data;
339 }
340 if ( $defaults ) {
341 $this->json['ResourceFileModulePaths'] = $defaults;
342 }
343 }
344
345 protected function needsComposerAutoloader( $path ) {
346 $path .= '/composer.json';
347 if ( file_exists( $path ) ) {
348 // assume that the composer.json file is in the root of the extension path
349 $composerJson = new ComposerJson( $path );
350 // check if there are some dependencies in the require section
351 if ( $composerJson->getRequiredDependencies() ) {
352 return true;
353 }
354 }
355 return false;
356 }
357}
358
359$maintClass = ConvertExtensionToRegistration::class;
360require_once RUN_MAINTENANCE_IF_MAIN;
string[] $promote
Keys that should be put at the top of the generated JSON file (T86608)
string[] $noLongerSupportedGlobals
No longer supported globals (with reason) should not be converted and emit a warning.
string[] $formerGlobals
Things that were formerly globals and should still be converted.
handleExtensionMessagesFiles( $realName, $value, $vars)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
JSON formatter wrapper class.
Reads a composer.json file and provides accessors to get its hash and the required dependencies.