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 * No longer supported globals (with reason) should not be converted and emit a warning
33 protected $noLongerSupportedGlobals = array(
34 'SpecialPageGroups' => 'deprecated',
38 * Keys that should be put at the top of the generated JSON file (T86608)
42 protected $promote = array(
54 private $json, $dir, $hasWarning = false;
56 public function __construct() {
57 parent
::__construct();
58 $this->mDescription
= 'Converts extension entry points to the new JSON registration format';
59 $this->addArg( 'path', 'Location to the PHP entry point you wish to convert', /* $required = */ true );
60 $this->addOption( 'skin', 'Whether to write to skin.json', false, false );
63 protected function getAllGlobals() {
64 $processor = new ReflectionClass( 'ExtensionProcessor' );
65 $settings = $processor->getProperty( 'globalSettings' );
66 $settings->setAccessible( true );
67 return $settings->getValue() +
$this->formerGlobals
;
70 public function execute() {
71 // Extensions will do stuff like $wgResourceModules += array(...) which is a
72 // fatal unless an array is already set. So set an empty value.
73 // And use the weird $__settings name to avoid any conflicts
74 // with real poorly named settings.
75 $__settings = array_merge( $this->getAllGlobals(), array_keys( $this->custom
) );
76 foreach ( $__settings as $var ) {
81 require $this->getArg( 0 );
82 // Try not to create any local variables before this line
83 $vars = get_defined_vars();
84 unset( $vars['this'] );
85 unset( $vars['__settings'] );
86 $this->dir
= dirname( realpath( $this->getArg( 0 ) ) );
87 $this->json
= array();
88 $globalSettings = $this->getAllGlobals();
89 foreach ( $vars as $name => $value ) {
90 $realName = substr( $name, 2 ); // Strip 'wg'
92 // If it's an empty array that we likely set, skip it
93 if ( is_array( $value ) && count( $value ) === 0 && in_array( $realName, $__settings ) ) {
97 if ( isset( $this->custom
[$realName] ) ) {
98 call_user_func_array( array( $this, $this->custom
[$realName] ), array( $realName, $value, $vars ) );
99 } elseif ( in_array( $realName, $globalSettings ) ) {
100 $this->json
[$realName] = $value;
101 } elseif ( array_key_exists( $realName, $this->noLongerSupportedGlobals
) ) {
102 $this->output( 'Warning: Skipped global "' . $name . '" (' .
103 $this->noLongerSupportedGlobals
[$realName] . '). ' .
104 "Please update the entry point before convert to registration.\n" );
105 $this->hasWarning
= true;
106 } elseif ( strpos( $name, 'wg' ) === 0 ) {
107 // Most likely a config setting
108 $this->json
['config'][$realName] = $value;
112 // Move some keys to the top
114 foreach ( $this->promote
as $key ) {
115 if ( isset( $this->json
[$key] ) ) {
116 $out[$key] = $this->json
[$key];
117 unset( $this->json
[$key] );
122 $type = $this->hasOption( 'skin' ) ?
'skin' : 'extension';
123 $fname = "{$this->dir}/$type.json";
124 $prettyJSON = FormatJson
::encode( $out, "\t", FormatJson
::ALL_OK
);
125 file_put_contents( $fname, $prettyJSON . "\n" );
126 $this->output( "Wrote output to $fname.\n" );
127 if ( $this->hasWarning
) {
128 $this->output( "Found warnings! Please resolve the warnings and rerun this script.\n" );
132 protected function handleExtensionFunctions( $realName, $value ) {
133 foreach ( $value as $func ) {
134 if ( $func instanceof Closure
) {
135 $this->error( "Error: Closures cannot be converted to JSON. Please move your extension function somewhere else.", 1 );
139 $this->json
[$realName] = $value;
142 protected function handleMessagesDirs( $realName, $value ) {
143 foreach ( $value as $key => $dirs ) {
144 foreach ( (array)$dirs as $dir ) {
145 $this->json
[$realName][$key][] = $this->stripPath( $dir, $this->dir
);
150 protected function handleExtensionMessagesFiles( $realName, $value, $vars ) {
151 foreach ( $value as $key => $file ) {
152 $strippedFile = $this->stripPath( $file, $this->dir
);
153 if ( isset( $vars['wgMessagesDirs'][$key] ) ) {
155 "Note: Ignoring PHP shim $strippedFile. " .
156 "If your extension no longer supports versions of MediaWiki " .
157 "older than 1.23.0, you can safely delete it.\n"
160 $this->json
[$realName][$key] = $strippedFile;
165 private function stripPath( $val, $dir ) {
166 if ( $val === $dir ) {
168 } elseif ( strpos( $val, $dir ) === 0 ) {
169 // +1 is for the trailing / that won't be in $this->dir
170 $val = substr( $val, strlen( $dir ) +
1 );
176 protected function removeAbsolutePath( $realName, $value ) {
178 foreach ( $value as $key => $val ) {
179 $out[$key] = $this->stripPath( $val, $this->dir
);
181 $this->json
[$realName] = $out;
184 protected function handleCredits( $realName, $value) {
185 $keys = array_keys( $value );
186 $this->json
['type'] = $keys[0];
187 $values = array_values( $value );
188 foreach ( $values[0][0] as $name => $val ) {
189 if ( $name !== 'path' ) {
190 $this->json
[$name] = $val;
195 public function handleHooks( $realName, $value ) {
196 foreach ( $value as $hookName => $handlers ) {
197 foreach ( $handlers as $func ) {
198 if ( $func instanceof Closure
) {
199 $this->error( "Error: Closures cannot be converted to JSON. Please move the handler for $hookName somewhere else.", 1 );
203 $this->json
[$realName] = $value;
206 protected function handleResourceModules( $realName, $value ) {
208 $remote = $this->hasOption( 'skin' ) ?
'remoteSkinPath' : 'remoteExtPath';
209 foreach ( $value as $name => $data ) {
210 if ( isset( $data['localBasePath'] ) ) {
211 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir
);
213 $defaults['localBasePath'] = $data['localBasePath'];
214 unset( $data['localBasePath'] );
215 if ( isset( $data[$remote] ) ) {
216 $defaults[$remote] = $data[$remote];
217 unset( $data[$remote] );
220 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
221 unset( $data['localBasePath'] );
223 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
224 && $data[$remote] === $defaults[$remote]
226 unset( $data[$remote] );
232 $this->json
[$realName][$name] = $data;
235 $this->json
['ResourceFileModulePaths'] = $defaults;
240 $maintClass = 'ConvertExtensionToRegistration';
241 require_once RUN_MAINTENANCE_IF_MAIN
;