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 foreach ( array_merge( $this->getAllGlobals(), array_keys( $this->custom
) ) as $var ) {
78 require $this->getArg( 0 );
79 // Try not to create any local variables before this line
80 $vars = get_defined_vars();
81 unset( $vars['this'] );
82 $this->dir
= dirname( realpath( $this->getArg( 0 ) ) );
83 $this->json
= array();
84 $globalSettings = $this->getAllGlobals();
85 foreach ( $vars as $name => $value ) {
86 // If an empty array, assume it's the default we set, so skip it
87 if ( is_array( $value ) && count( $value ) === 0 ) {
90 $realName = substr( $name, 2 ); // Strip 'wg'
91 if ( isset( $this->custom
[$realName] ) ) {
92 call_user_func_array( array( $this, $this->custom
[$realName] ), array( $realName, $value, $vars ) );
93 } elseif ( in_array( $realName, $globalSettings ) ) {
94 $this->json
[$realName] = $value;
95 } elseif ( array_key_exists( $realName, $this->noLongerSupportedGlobals
) ) {
96 $this->output( 'Warning: Skipped global "' . $name . '" (' .
97 $this->noLongerSupportedGlobals
[$realName] . '). ' .
98 "Please update the entry point before convert to registration.\n" );
99 $this->hasWarning
= true;
100 } elseif ( strpos( $name, 'wg' ) === 0 ) {
101 // Most likely a config setting
102 $this->json
['config'][$realName] = $value;
106 // Move some keys to the top
108 foreach ( $this->promote
as $key ) {
109 if ( isset( $this->json
[$key] ) ) {
110 $out[$key] = $this->json
[$key];
111 unset( $this->json
[$key] );
116 $type = $this->hasOption( 'skin' ) ?
'skin' : 'extension';
117 $fname = "{$this->dir}/$type.json";
118 $prettyJSON = FormatJson
::encode( $out, "\t", FormatJson
::ALL_OK
);
119 file_put_contents( $fname, $prettyJSON . "\n" );
120 $this->output( "Wrote output to $fname.\n" );
121 if ( $this->hasWarning
) {
122 $this->output( "Found warnings! Please resolve the warnings and rerun this script.\n" );
126 protected function handleExtensionFunctions( $realName, $value ) {
127 foreach ( $value as $func ) {
128 if ( $func instanceof Closure
) {
129 $this->error( "Error: Closures cannot be converted to JSON. Please move your extension function somewhere else.", 1 );
133 $this->json
[$realName] = $value;
136 protected function handleMessagesDirs( $realName, $value ) {
137 foreach ( $value as $key => $dirs ) {
138 foreach ( (array)$dirs as $dir ) {
139 $this->json
[$realName][$key][] = $this->stripPath( $dir, $this->dir
);
144 protected function handleExtensionMessagesFiles( $realName, $value, $vars ) {
145 foreach ( $value as $key => $file ) {
146 $strippedFile = $this->stripPath( $file, $this->dir
);
147 if ( isset( $vars['wgMessagesDirs'][$key] ) ) {
149 "Note: Ignoring PHP shim $strippedFile. " .
150 "If your extension no longer supports versions of MediaWiki " .
151 "older than 1.23.0, you can safely delete it.\n"
154 $this->json
[$realName][$key] = $strippedFile;
159 private function stripPath( $val, $dir ) {
160 if ( $val === $dir ) {
162 } elseif ( strpos( $val, $dir ) === 0 ) {
163 // +1 is for the trailing / that won't be in $this->dir
164 $val = substr( $val, strlen( $dir ) +
1 );
170 protected function removeAbsolutePath( $realName, $value ) {
172 foreach ( $value as $key => $val ) {
173 $out[$key] = $this->stripPath( $val, $this->dir
);
175 $this->json
[$realName] = $out;
178 protected function handleCredits( $realName, $value) {
179 $keys = array_keys( $value );
180 $this->json
['type'] = $keys[0];
181 $values = array_values( $value );
182 foreach ( $values[0][0] as $name => $val ) {
183 if ( $name !== 'path' ) {
184 $this->json
[$name] = $val;
189 public function handleHooks( $realName, $value ) {
190 foreach ( $value as $hookName => $handlers ) {
191 foreach ( $handlers as $func ) {
192 if ( $func instanceof Closure
) {
193 $this->error( "Error: Closures cannot be converted to JSON. Please move the handler for $hookName somewhere else.", 1 );
197 $this->json
[$realName] = $value;
200 protected function handleResourceModules( $realName, $value ) {
202 $remote = $this->hasOption( 'skin' ) ?
'remoteSkinPath' : 'remoteExtPath';
203 foreach ( $value as $name => $data ) {
204 if ( isset( $data['localBasePath'] ) ) {
205 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir
);
207 $defaults['localBasePath'] = $data['localBasePath'];
208 unset( $data['localBasePath'] );
209 if ( isset( $data[$remote] ) ) {
210 $defaults[$remote] = $data[$remote];
211 unset( $data[$remote] );
214 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
215 unset( $data['localBasePath'] );
217 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
218 && $data[$remote] === $defaults[$remote]
220 unset( $data[$remote] );
226 $this->json
[$realName][$name] = $data;
229 $this->json
['ResourceFileModulePaths'] = $defaults;
234 $maintClass = 'ConvertExtensionToRegistration';
235 require_once RUN_MAINTENANCE_IF_MAIN
;