Merge "Improve some documentation of AuthManager's additions"
[lhc/web/wiklou.git] / maintenance / convertExtensionToRegistration.php
1 <?php
2
3 require_once __DIR__ . '/Maintenance.php';
4
5 class ConvertExtensionToRegistration extends Maintenance {
6
7 protected $custom = [
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',
17 ];
18
19 /**
20 * Things that were formerly globals and should still be converted
21 *
22 * @var array
23 */
24 protected $formerGlobals = [
25 'TrackingCategories',
26 ];
27
28 /**
29 * No longer supported globals (with reason) should not be converted and emit a warning
30 *
31 * @var array
32 */
33 protected $noLongerSupportedGlobals = [
34 'SpecialPageGroups' => 'deprecated', // Deprecated 1.21, removed in 1.26
35 ];
36
37 /**
38 * Keys that should be put at the top of the generated JSON file (T86608)
39 *
40 * @var array
41 */
42 protected $promote = [
43 'name',
44 'namemsg',
45 'version',
46 'author',
47 'url',
48 'description',
49 'descriptionmsg',
50 'license-name',
51 'type',
52 ];
53
54 private $json, $dir, $hasWarning = false;
55
56 public function __construct() {
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',
60 /* $required = */ true );
61 $this->addOption( 'skin', 'Whether to write to skin.json', false, false );
62 }
63
64 protected function getAllGlobals() {
65 $processor = new ReflectionClass( 'ExtensionProcessor' );
66 $settings = $processor->getProperty( 'globalSettings' );
67 $settings->setAccessible( true );
68 return $settings->getValue() + $this->formerGlobals;
69 }
70
71 public function execute() {
72 // Extensions will do stuff like $wgResourceModules += array(...) which is a
73 // fatal unless an array is already set. So set an empty value.
74 // And use the weird $__settings name to avoid any conflicts
75 // with real poorly named settings.
76 $__settings = array_merge( $this->getAllGlobals(), array_keys( $this->custom ) );
77 foreach ( $__settings as $var ) {
78 $var = 'wg' . $var;
79 $$var = [];
80 }
81 unset( $var );
82 $arg = $this->getArg( 0 );
83 if ( !is_file( $arg ) ) {
84 $this->error( "$arg is not a file.", true );
85 }
86 require $arg;
87 unset( $arg );
88 // Try not to create any local variables before this line
89 $vars = get_defined_vars();
90 unset( $vars['this'] );
91 unset( $vars['__settings'] );
92 $this->dir = dirname( realpath( $this->getArg( 0 ) ) );
93 $this->json = [];
94 $globalSettings = $this->getAllGlobals();
95 foreach ( $vars as $name => $value ) {
96 $realName = substr( $name, 2 ); // Strip 'wg'
97 if ( $realName === false ) {
98 continue;
99 }
100
101 // If it's an empty array that we likely set, skip it
102 if ( is_array( $value ) && count( $value ) === 0 && in_array( $realName, $__settings ) ) {
103 continue;
104 }
105
106 if ( isset( $this->custom[$realName] ) ) {
107 call_user_func_array( [ $this, $this->custom[$realName] ],
108 [ $realName, $value, $vars ] );
109 } elseif ( in_array( $realName, $globalSettings ) ) {
110 $this->json[$realName] = $value;
111 } elseif ( array_key_exists( $realName, $this->noLongerSupportedGlobals ) ) {
112 $this->output( 'Warning: Skipped global "' . $name . '" (' .
113 $this->noLongerSupportedGlobals[$realName] . '). ' .
114 "Please update the entry point before convert to registration.\n" );
115 $this->hasWarning = true;
116 } elseif ( strpos( $name, 'wg' ) === 0 ) {
117 // Most likely a config setting
118 $this->json['config'][$realName] = $value;
119 }
120 }
121
122 // check, if the extension requires composer libraries
123 if ( $this->needsComposerAutoloader( dirname( $this->getArg( 0 ) ) ) ) {
124 // set the load composer autoloader automatically property
125 $this->output( "Detected composer dependencies, setting 'load_composer_autoloader' to true.\n" );
126 $this->json['load_composer_autoloader'] = true;
127 }
128
129 // Move some keys to the top
130 $out = [];
131 foreach ( $this->promote as $key ) {
132 if ( isset( $this->json[$key] ) ) {
133 $out[$key] = $this->json[$key];
134 unset( $this->json[$key] );
135 }
136 }
137 $out += $this->json;
138 // Put this at the bottom
139 $out['manifest_version'] = ExtensionRegistry::MANIFEST_VERSION;
140 $type = $this->hasOption( 'skin' ) ? 'skin' : 'extension';
141 $fname = "{$this->dir}/$type.json";
142 $prettyJSON = FormatJson::encode( $out, "\t", FormatJson::ALL_OK );
143 file_put_contents( $fname, $prettyJSON . "\n" );
144 $this->output( "Wrote output to $fname.\n" );
145 if ( $this->hasWarning ) {
146 $this->output( "Found warnings! Please resolve the warnings and rerun this script.\n" );
147 }
148 }
149
150 protected function handleExtensionFunctions( $realName, $value ) {
151 foreach ( $value as $func ) {
152 if ( $func instanceof Closure ) {
153 $this->error( "Error: Closures cannot be converted to JSON. " .
154 "Please move your extension function somewhere else.", 1
155 );
156 }
157 // check if $func exists in the global scope
158 if ( function_exists( $func ) ) {
159 $this->error( "Error: Global functions cannot be converted to JSON. " .
160 "Please move your extension function ($func) into a class.", 1
161 );
162 }
163 }
164
165 $this->json[$realName] = $value;
166 }
167
168 protected function handleMessagesDirs( $realName, $value ) {
169 foreach ( $value as $key => $dirs ) {
170 foreach ( (array)$dirs as $dir ) {
171 $this->json[$realName][$key][] = $this->stripPath( $dir, $this->dir );
172 }
173 }
174 }
175
176 protected function handleExtensionMessagesFiles( $realName, $value, $vars ) {
177 foreach ( $value as $key => $file ) {
178 $strippedFile = $this->stripPath( $file, $this->dir );
179 if ( isset( $vars['wgMessagesDirs'][$key] ) ) {
180 $this->output(
181 "Note: Ignoring PHP shim $strippedFile. " .
182 "If your extension no longer supports versions of MediaWiki " .
183 "older than 1.23.0, you can safely delete it.\n"
184 );
185 } else {
186 $this->json[$realName][$key] = $strippedFile;
187 }
188 }
189 }
190
191 private function stripPath( $val, $dir ) {
192 if ( $val === $dir ) {
193 $val = '';
194 } elseif ( strpos( $val, $dir ) === 0 ) {
195 // +1 is for the trailing / that won't be in $this->dir
196 $val = substr( $val, strlen( $dir ) + 1 );
197 }
198
199 return $val;
200 }
201
202 protected function removeAbsolutePath( $realName, $value ) {
203 $out = [];
204 foreach ( $value as $key => $val ) {
205 $out[$key] = $this->stripPath( $val, $this->dir );
206 }
207 $this->json[$realName] = $out;
208 }
209
210 protected function handleCredits( $realName, $value ) {
211 $keys = array_keys( $value );
212 $this->json['type'] = $keys[0];
213 $values = array_values( $value );
214 foreach ( $values[0][0] as $name => $val ) {
215 if ( $name !== 'path' ) {
216 $this->json[$name] = $val;
217 }
218 }
219 }
220
221 public function handleHooks( $realName, $value ) {
222 foreach ( $value as $hookName => &$handlers ) {
223 foreach ( $handlers as $func ) {
224 if ( $func instanceof Closure ) {
225 $this->error( "Error: Closures cannot be converted to JSON. " .
226 "Please move the handler for $hookName somewhere else.", 1
227 );
228 }
229 // Check if $func exists in the global scope
230 if ( function_exists( $func ) ) {
231 $this->error( "Error: Global functions cannot be converted to JSON. " .
232 "Please move the handler for $hookName inside a class.", 1
233 );
234 }
235 }
236 if ( count( $handlers ) === 1 ) {
237 $handlers = $handlers[0];
238 }
239 }
240 $this->json[$realName] = $value;
241 }
242
243 protected function handleResourceModules( $realName, $value ) {
244 $defaults = [];
245 $remote = $this->hasOption( 'skin' ) ? 'remoteSkinPath' : 'remoteExtPath';
246 foreach ( $value as $name => $data ) {
247 if ( isset( $data['localBasePath'] ) ) {
248 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir );
249 if ( !$defaults ) {
250 $defaults['localBasePath'] = $data['localBasePath'];
251 unset( $data['localBasePath'] );
252 if ( isset( $data[$remote] ) ) {
253 $defaults[$remote] = $data[$remote];
254 unset( $data[$remote] );
255 }
256 } else {
257 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
258 unset( $data['localBasePath'] );
259 }
260 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
261 && $data[$remote] === $defaults[$remote]
262 ) {
263 unset( $data[$remote] );
264 }
265 }
266 }
267
268 $this->json[$realName][$name] = $data;
269 }
270 if ( $defaults ) {
271 $this->json['ResourceFileModulePaths'] = $defaults;
272 }
273 }
274
275 protected function needsComposerAutoloader( $path ) {
276 $path .= '/composer.json';
277 if ( file_exists( $path ) ) {
278 // assume, that the composer.json file is in the root of the extension path
279 $composerJson = new ComposerJson( $path );
280 // check, if there are some dependencies in the require section
281 if ( $composerJson->getRequiredDependencies() ) {
282 return true;
283 }
284 }
285 return false;
286 }
287 }
288
289 $maintClass = 'ConvertExtensionToRegistration';
290 require_once RUN_MAINTENANCE_IF_MAIN;