3 require_once __DIR__
. '/Maintenance.php';
5 class ConvertExtensionToRegistration
extends Maintenance
{
7 protected $custom = array(
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' => 'removeAbsolutePath',
20 * Things that were formerly globals and should still be converted
24 protected $formerGlobals = array(
29 * Keys that should be put at the top of the generated JSON file (T86608)
33 protected $promote = array(
47 public function __construct() {
48 parent
::__construct();
49 $this->mDescription
= 'Converts extension entry points to the new JSON registration format';
50 $this->addArg( 'path', 'Location to the PHP entry point you wish to convert', /* $required = */ true );
51 $this->addOption( 'skin', 'Whether to write to skin.json', false, false );
54 protected function getAllGlobals() {
55 $processor = new ReflectionClass( 'ExtensionProcessor' );
56 $settings = $processor->getProperty( 'globalSettings' );
57 $settings->setAccessible( true );
58 return $settings->getValue() +
$this->formerGlobals
;
61 public function execute() {
62 // Extensions will do stuff like $wgResourceModules += array(...) which is a
63 // fatal unless an array is already set. So set an empty value.
64 // And use the weird $__settings name to avoid any conflicts
65 // with real poorly named settings.
66 $__settings = array_merge( $this->getAllGlobals(), array_keys( $this->custom
) );
67 foreach ( $__settings as $var ) {
72 require $this->getArg( 0 );
73 // Try not to create any local variables before this line
74 $vars = get_defined_vars();
75 unset( $vars['this'] );
76 unset( $vars['__settings'] );
77 $this->dir
= dirname( realpath( $this->getArg( 0 ) ) );
78 $this->json
= array();
79 $globalSettings = $this->getAllGlobals();
80 foreach ( $vars as $name => $value ) {
81 $realName = substr( $name, 2 ); // Strip 'wg'
83 // If it's an empty array that we likely set, skip it
84 if ( is_array( $value ) && count( $value ) === 0 && in_array( $realName, $__settings ) ) {
88 if ( isset( $this->custom
[$realName] ) ) {
89 call_user_func_array( array( $this, $this->custom
[$realName] ), array( $realName, $value, $vars ) );
90 } elseif ( in_array( $realName, $globalSettings ) ) {
91 $this->json
[$realName] = $value;
92 } elseif ( strpos( $name, 'wg' ) === 0 ) {
93 // Most likely a config setting
94 $this->json
['config'][$realName] = $value;
98 // Move some keys to the top
100 foreach ( $this->promote
as $key ) {
101 if ( isset( $this->json
[$key] ) ) {
102 $out[$key] = $this->json
[$key];
103 unset( $this->json
[$key] );
108 $type = $this->hasOption( 'skin' ) ?
'skin' : 'extension';
109 $fname = "{$this->dir}/$type.json";
110 $prettyJSON = FormatJson
::encode( $out, "\t", FormatJson
::ALL_OK
);
111 file_put_contents( $fname, $prettyJSON . "\n" );
112 $this->output( "Wrote output to $fname.\n" );
115 protected function handleExtensionFunctions( $realName, $value ) {
116 foreach ( $value as $func ) {
117 if ( $func instanceof Closure
) {
118 $this->error( "Error: Closures cannot be converted to JSON. Please move your extension function somewhere else.", 1 );
122 $this->json
[$realName] = $value;
125 protected function handleMessagesDirs( $realName, $value ) {
126 foreach ( $value as $key => $dirs ) {
127 foreach ( (array)$dirs as $dir ) {
128 $this->json
[$realName][$key][] = $this->stripPath( $dir, $this->dir
);
133 protected function handleExtensionMessagesFiles( $realName, $value, $vars ) {
134 foreach ( $value as $key => $file ) {
135 $strippedFile = $this->stripPath( $file, $this->dir
);
136 if ( isset( $vars['wgMessagesDirs'][$key] ) ) {
138 "Note: Ignoring PHP shim $strippedFile. " .
139 "If your extension no longer supports versions of MediaWiki " .
140 "older than 1.23.0, you can safely delete it.\n"
143 $this->json
[$realName][$key] = $strippedFile;
148 private function stripPath( $val, $dir ) {
149 if ( $val === $dir ) {
151 } elseif ( strpos( $val, $dir ) === 0 ) {
152 // +1 is for the trailing / that won't be in $this->dir
153 $val = substr( $val, strlen( $dir ) +
1 );
159 protected function removeAbsolutePath( $realName, $value ) {
161 foreach ( $value as $key => $val ) {
162 $out[$key] = $this->stripPath( $val, $this->dir
);
164 $this->json
[$realName] = $out;
167 protected function handleCredits( $realName, $value) {
168 $keys = array_keys( $value );
169 $this->json
['type'] = $keys[0];
170 $values = array_values( $value );
171 foreach ( $values[0][0] as $name => $val ) {
172 if ( $name !== 'path' ) {
173 $this->json
[$name] = $val;
178 public function handleHooks( $realName, $value ) {
179 foreach ( $value as $hookName => $handlers ) {
180 foreach ( $handlers as $func ) {
181 if ( $func instanceof Closure
) {
182 $this->error( "Error: Closures cannot be converted to JSON. Please move the handler for $hookName somewhere else.", 1 );
186 $this->json
[$realName] = $value;
189 protected function handleResourceModules( $realName, $value ) {
191 $remote = $this->hasOption( 'skin' ) ?
'remoteSkinPath' : 'remoteExtPath';
192 foreach ( $value as $name => $data ) {
193 if ( isset( $data['localBasePath'] ) ) {
194 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir
);
196 $defaults['localBasePath'] = $data['localBasePath'];
197 unset( $data['localBasePath'] );
198 if ( isset( $data[$remote] ) ) {
199 $defaults[$remote] = $data[$remote];
200 unset( $data[$remote] );
203 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
204 unset( $data['localBasePath'] );
206 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
207 && $data[$remote] === $defaults[$remote]
209 unset( $data[$remote] );
215 $this->json
[$realName][$name] = $data;
218 $this->json
['ResourceFileModulePaths'] = $defaults;
223 $maintClass = 'ConvertExtensionToRegistration';
224 require_once RUN_MAINTENANCE_IF_MAIN
;