3 require_once __DIR__ .
'/Maintenance.php';
8 'MessagesDirs' =>
'handleMessagesDirs',
9 'ExtensionMessagesFiles' =>
'handleExtensionMessagesFiles',
10 'AutoloadClasses' =>
'removeAbsolutePath',
11 'ExtensionCredits' =>
'handleCredits',
12 'ResourceModules' =>
'handleResourceModules',
13 'ResourceModuleSkinStyles' =>
'handleResourceModules',
14 'Hooks' =>
'handleHooks',
15 'ExtensionFunctions' =>
'handleExtensionFunctions',
16 'ParserTestFiles' =>
'removeAutodiscoveredParserTestFiles',
34 'SpecialPageGroups' =>
'deprecated',
57 parent::__construct();
58 $this->
addDescription(
'Converts extension entry points to the new JSON registration format' );
59 $this->
addArg(
'path',
'Location to the PHP entry point you wish to convert',
61 $this->
addOption(
'skin',
'Whether to write to skin.json',
false,
false );
62 $this->
addOption(
'config-prefix',
'Custom prefix for configuration settings',
false,
true );
66 $processor =
new ReflectionClass( ExtensionProcessor::class );
67 $settings = $processor->getProperty(
'globalSettings' );
68 $settings->setAccessible(
true );
69 return array_merge( $settings->getValue(), $this->formerGlobals );
77 $__settings = array_merge( $this->
getAllGlobals(), array_keys( $this->custom ) );
78 foreach ( $__settings as $var ) {
84 if ( !is_file( $arg ) ) {
90 $vars = get_defined_vars();
91 unset( $vars[
'this'] );
92 unset( $vars[
'__settings'] );
93 $this->dir = dirname( realpath( $this->
getArg( 0 ) ) );
96 $configPrefix = $this->
getOption(
'config-prefix',
'wg' );
97 if ( $configPrefix !==
'wg' ) {
98 $this->json[
'config'][
'_prefix'] = $configPrefix;
101 foreach ( $vars as $name => $value ) {
102 $realName = substr( $name, 2 );
103 if ( $realName ===
false ) {
108 if ( is_array( $value ) && count( $value ) === 0 && in_array( $realName, $__settings ) ) {
112 if ( isset( $this->custom[$realName] ) ) {
113 call_user_func_array( [ $this, $this->custom[$realName] ],
114 [ $realName, $value, $vars ] );
115 } elseif ( in_array( $realName, $globalSettings ) ) {
116 $this->json[$realName] = $value;
117 } elseif ( array_key_exists( $realName, $this->noLongerSupportedGlobals ) ) {
118 $this->
output(
'Warning: Skipped global "' . $name .
'" (' .
119 $this->noLongerSupportedGlobals[$realName] .
'). ' .
120 "Please update the entry point before convert to registration.\n" );
121 $this->hasWarning =
true;
122 } elseif ( strpos( $name, $configPrefix ) === 0 ) {
123 $configName = substr( $name, strlen( $configPrefix ) );
126 if ( is_array( $value ) ) {
127 foreach ( $value as $k => $v ) {
128 if ( strpos( $v, $this->dir ) !==
false ) {
129 $value[$k] = $this->stripPath( $v, $this->dir );
133 } elseif ( is_string( $value ) && strpos( $value, $this->dir ) !==
false ) {
134 $value = $this->stripPath( $value, $this->dir );
139 $this->json[
'config'][$configName] = [
'value' => $value ];
142 $this->json[
'config'][$configName][
'path'] =
true;
144 } elseif ( $configPrefix !==
'wg' && strpos( $name,
'wg' ) === 0 ) {
146 $this->
output(
'Warning: Skipped global "' . $name .
'" (' .
147 'config prefix is "' . $configPrefix .
'"). ' .
148 "Please check that this setting isn't needed.\n" );
155 $this->
output(
"Detected composer dependencies, setting 'load_composer_autoloader' to true.\n" );
156 $this->json[
'load_composer_autoloader'] =
true;
161 foreach ( $this->promote as $key ) {
162 if ( isset( $this->json[$key] ) ) {
163 $out[$key] = $this->json[$key];
164 unset( $this->json[$key] );
175 $type = $this->
hasOption(
'skin' ) ?
'skin' :
'extension';
176 $fname =
"{$this->dir}/$type.json";
178 file_put_contents( $fname, $prettyJSON .
"\n" );
179 $this->
output(
"Wrote output to $fname.\n" );
180 if ( $this->hasWarning ) {
181 $this->
output(
"Found warnings! Please resolve the warnings and rerun this script.\n" );
186 foreach ( $value as $func ) {
187 if ( $func instanceof Closure ) {
188 $this->
fatalError(
"Error: Closures cannot be converted to JSON. " .
189 "Please move your extension function somewhere else."
191 } elseif ( function_exists( $func ) ) {
193 $this->
fatalError(
"Error: Global functions cannot be converted to JSON. " .
194 "Please move your extension function ($func) into a class."
199 $this->json[$realName] = $value;
203 foreach ( $value as $key => $dirs ) {
204 foreach ( (array)$dirs as
$dir ) {
205 $this->json[$realName][$key][] = $this->stripPath(
$dir, $this->dir );
211 foreach ( $value as $key =>
$file ) {
212 $strippedFile = $this->stripPath(
$file, $this->dir );
213 if ( isset( $vars[
'wgMessagesDirs'][$key] ) ) {
215 "Note: Ignoring PHP shim $strippedFile. " .
216 "If your extension no longer supports versions of MediaWiki " .
217 "older than 1.23.0, you can safely delete it.\n"
220 $this->json[$realName][$key] = $strippedFile;
225 private function stripPath( $val,
$dir ) {
226 if ( $val ===
$dir ) {
228 } elseif ( strpos( $val,
$dir ) === 0 ) {
230 $val = substr( $val, strlen(
$dir ) + 1 );
238 foreach ( $value as $key => $val ) {
239 $out[$key] = $this->stripPath( $val, $this->dir );
241 $this->json[$realName] = $out;
246 foreach ( $value as $key => $val ) {
247 $path = $this->stripPath( $val, $this->dir );
250 if ( !str_starts_with(
$path,
'tests/parser/' ) || !str_ends_with(
$path,
'.txt' ) ) {
256 $this->json[$realName] = $out;
266 $keys = array_keys( $value );
267 $this->json[
'type'] = $keys[0];
268 $values = array_values( $value );
269 foreach ( $values[0][0] as $name => $val ) {
270 if ( $name !==
'path' ) {
271 $this->json[$name] = $val;
277 foreach ( $value as $hookName => &$handlers ) {
278 if ( $hookName ===
'UnitTestsList' ) {
279 $this->
output(
"Note: the UnitTestsList hook is no longer necessary as " .
280 "long as your tests are located in the \"tests/phpunit/\" directory. " .
281 "Please see <https://www.mediawiki.org/wiki/Manual:PHP_unit_testing/" .
282 "Writing_unit_tests_for_extensions#Register_your_tests> for more details.\n"
285 foreach ( $handlers as $func ) {
286 if ( $func instanceof Closure ) {
287 $this->
fatalError(
"Error: Closures cannot be converted to JSON. " .
288 "Please move the handler for $hookName somewhere else."
290 } elseif ( function_exists( $func ) ) {
292 $this->
fatalError(
"Error: Global functions cannot be converted to JSON. " .
293 "Please move the handler for $hookName inside a class."
297 if ( count( $handlers ) === 1 ) {
298 $handlers = $handlers[0];
301 $this->json[$realName] = $value;
310 $remote = $this->
hasOption(
'skin' ) ?
'remoteSkinPath' :
'remoteExtPath';
311 foreach ( $value as $name => $data ) {
312 if ( isset( $data[
'localBasePath'] ) ) {
313 $data[
'localBasePath'] = $this->stripPath( $data[
'localBasePath'], $this->dir );
315 $defaults[
'localBasePath'] = $data[
'localBasePath'];
316 unset( $data[
'localBasePath'] );
317 if ( isset( $data[$remote] ) ) {
318 $defaults[$remote] = $data[$remote];
319 unset( $data[$remote] );
322 if ( $data[
'localBasePath'] === $defaults[
'localBasePath'] ) {
323 unset( $data[
'localBasePath'] );
325 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
326 && $data[$remote] === $defaults[$remote]
328 unset( $data[$remote] );
333 $this->json[$realName][$name] = $data;
336 $this->json[
'ResourceFileModulePaths'] = $defaults;
341 $path .=
'/composer.json';
342 if ( file_exists(
$path ) ) {
346 if ( $composerJson->getRequiredDependencies() ) {
355 require_once RUN_MAINTENANCE_IF_MAIN;
Reads a composer.json file and provides accessors to get its hash and the required dependencies.
handleMessagesDirs( $realName, $value)
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.
handleResourceModules( $realName, $value)
string[] $formerGlobals
Things that were formerly globals and should still be converted.
handleExtensionFunctions( $realName, $value)
needsComposerAutoloader( $path)
handleHooks( $realName, $value)
handleExtensionMessagesFiles( $realName, $value, $vars)
removeAutodiscoveredParserTestFiles( $realName, $value)
removeAbsolutePath( $realName, $value)
execute()
Do the actual work.
__construct()
Default constructor.
handleCredits( $realName, $value)
const MANIFEST_VERSION
Version of the highest supported manifest version Note: Update MANIFEST_VERSION_MW_VERSION when chang...
const MANIFEST_VERSION_MW_VERSION
MediaWiki version constraint representing what the current highest MANIFEST_VERSION is supported in.
const MEDIAWIKI_CORE
"requires" key that applies to MediaWiki core
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.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.