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 );
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;
100 foreach ( $vars as $name => $value ) {
101 $realName = substr( $name, 2 );
102 if ( $realName ===
false ) {
107 if ( is_array( $value ) && count( $value ) === 0 && in_array( $realName, $__settings ) ) {
111 if ( isset( $this->custom[$realName] ) ) {
112 call_user_func_array( [ $this, $this->custom[$realName] ],
113 [ $realName, $value, $vars ] );
114 } elseif ( in_array( $realName, $globalSettings ) ) {
115 $this->json[$realName] = $value;
116 } elseif ( array_key_exists( $realName, $this->noLongerSupportedGlobals ) ) {
117 $this->
output(
'Warning: Skipped global "' . $name .
'" (' .
118 $this->noLongerSupportedGlobals[$realName] .
'). ' .
119 "Please update the entry point before convert to registration.\n" );
120 $this->hasWarning =
true;
121 } elseif ( strpos( $name, $configPrefix ) === 0 ) {
123 $this->json[
'config'][substr( $name, strlen( $configPrefix ) )] = [
'value' => $value ];
124 } elseif ( $configPrefix !==
'wg' && strpos( $name,
'wg' ) === 0 ) {
126 $this->
output(
'Warning: Skipped global "' . $name .
'" (' .
127 'config prefix is "' . $configPrefix .
'"). ' .
128 "Please check that this setting isn't needed.\n" );
135 $this->
output(
"Detected composer dependencies, setting 'load_composer_autoloader' to true.\n" );
136 $this->json[
'load_composer_autoloader'] =
true;
141 foreach ( $this->promote as $key ) {
142 if ( isset( $this->json[$key] ) ) {
143 $out[$key] = $this->json[$key];
144 unset( $this->json[$key] );
156 $fname =
"{$this->dir}/$type.json";
158 file_put_contents( $fname, $prettyJSON .
"\n" );
159 $this->
output(
"Wrote output to $fname.\n" );
160 if ( $this->hasWarning ) {
161 $this->
output(
"Found warnings! Please resolve the warnings and rerun this script.\n" );
166 foreach ( $value as $func ) {
167 if ( $func instanceof Closure ) {
168 $this->
fatalError(
"Error: Closures cannot be converted to JSON. " .
169 "Please move your extension function somewhere else."
171 } elseif ( function_exists( $func ) ) {
173 $this->
fatalError(
"Error: Global functions cannot be converted to JSON. " .
174 "Please move your extension function ($func) into a class."
179 $this->json[$realName] = $value;
183 foreach ( $value as $key =>
$dirs ) {
185 $this->json[$realName][$key][] = $this->
stripPath( $dir, $this->dir );
191 foreach ( $value as $key =>
$file ) {
193 if ( isset( $vars[
'wgMessagesDirs'][$key] ) ) {
195 "Note: Ignoring PHP shim $strippedFile. " .
196 "If your extension no longer supports versions of MediaWiki " .
197 "older than 1.23.0, you can safely delete it.\n"
200 $this->json[$realName][$key] = $strippedFile;
206 if ( $val ===
$dir ) {
208 } elseif ( strpos( $val,
$dir ) === 0 ) {
210 $val = substr( $val, strlen(
$dir ) + 1 );
218 foreach ( $value as $key => $val ) {
219 $out[$key] = $this->
stripPath( $val, $this->dir );
221 $this->json[$realName] = $out;
226 foreach ( $value as $key => $val ) {
230 if ( substr(
$path, 0, 13 ) !==
'tests/parser/' || substr(
$path, -4 ) !==
'.txt' ) {
236 $this->json[$realName] = $out;
241 $keys = array_keys( $value );
242 $this->json[
'type'] =
$keys[0];
243 $values = array_values( $value );
244 foreach ( $values[0][0] as $name => $val ) {
245 if ( $name !==
'path' ) {
246 $this->json[$name] = $val;
252 foreach ( $value as $hookName => &$handlers ) {
253 if ( $hookName ===
'UnitTestsList' ) {
254 $this->
output(
"Note: the UnitTestsList hook is no longer necessary as " .
255 "long as your tests are located in the \"tests/phpunit/\" directory. " .
256 "Please see <https://www.mediawiki.org/wiki/Manual:PHP_unit_testing/" .
257 "Writing_unit_tests_for_extensions#Register_your_tests> for more details.\n"
260 foreach ( $handlers as $func ) {
261 if ( $func instanceof Closure ) {
262 $this->
fatalError(
"Error: Closures cannot be converted to JSON. " .
263 "Please move the handler for $hookName somewhere else."
265 } elseif ( function_exists( $func ) ) {
267 $this->
fatalError(
"Error: Global functions cannot be converted to JSON. " .
268 "Please move the handler for $hookName inside a class."
272 if ( count( $handlers ) === 1 ) {
273 $handlers = $handlers[0];
276 $this->json[$realName] = $value;
285 $remote = $this->
hasOption(
'skin' ) ?
'remoteSkinPath' :
'remoteExtPath';
286 foreach ( $value as $name => $data ) {
287 if ( isset( $data[
'localBasePath'] ) ) {
288 $data[
'localBasePath'] = $this->
stripPath( $data[
'localBasePath'], $this->dir );
290 $defaults[
'localBasePath'] = $data[
'localBasePath'];
291 unset( $data[
'localBasePath'] );
292 if ( isset( $data[$remote] ) ) {
293 $defaults[$remote] = $data[$remote];
294 unset( $data[$remote] );
297 if ( $data[
'localBasePath'] === $defaults[
'localBasePath'] ) {
298 unset( $data[
'localBasePath'] );
300 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
301 && $data[$remote] === $defaults[$remote]
303 unset( $data[$remote] );
308 $this->json[$realName][$name] = $data;
311 $this->json[
'ResourceFileModulePaths'] = $defaults;
316 $path .=
'/composer.json';
317 if ( file_exists(
$path ) ) {
321 if ( $composerJson->getRequiredDependencies() ) {