MediaWiki REL1_39
convertExtensionToRegistration.php
Go to the documentation of this file.
1<?php
2
3require_once __DIR__ . '/Maintenance.php';
4
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' => 'removeAutodiscoveredParserTestFiles',
17 ];
18
24 protected $formerGlobals = [
25 'TrackingCategories',
26 ];
27
34 'SpecialPageGroups' => 'deprecated', // Deprecated 1.21, removed in 1.26
35 ];
36
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 $this->addOption( 'config-prefix', 'Custom prefix for configuration settings', false, true );
63 }
64
65 protected function getAllGlobals() {
66 $processor = new ReflectionClass( ExtensionProcessor::class );
67 $settings = $processor->getProperty( 'globalSettings' );
68 $settings->setAccessible( true );
69 return array_merge( $settings->getValue(), $this->formerGlobals );
70 }
71
72 public function execute() {
73 // Extensions will do stuff like $wgResourceModules += array(...) which is a
74 // fatal unless an array is already set. So set an empty value.
75 // And use the weird $__settings name to avoid any conflicts
76 // with real poorly named settings.
77 $__settings = array_merge( $this->getAllGlobals(), array_keys( $this->custom ) );
78 foreach ( $__settings as $var ) {
79 $var = 'wg' . $var;
80 $$var = [];
81 }
82 unset( $var );
83 $arg = $this->getArg( 0 );
84 if ( !is_file( $arg ) ) {
85 $this->fatalError( "$arg is not a file." );
86 }
87 require $arg;
88 unset( $arg );
89 // Try not to create any local variables before this line
90 $vars = get_defined_vars();
91 unset( $vars['this'] );
92 unset( $vars['__settings'] );
93 $this->dir = dirname( realpath( $this->getArg( 0 ) ) );
94 $this->json = [];
95 $globalSettings = $this->getAllGlobals();
96 $configPrefix = $this->getOption( 'config-prefix', 'wg' );
97 if ( $configPrefix !== 'wg' ) {
98 $this->json['config']['_prefix'] = $configPrefix;
99 }
100
101 foreach ( $vars as $name => $value ) {
102 $realName = substr( $name, 2 ); // Strip 'wg'
103 if ( $realName === false ) {
104 continue;
105 }
106
107 // If it's an empty array that we likely set, skip it
108 if ( is_array( $value ) && count( $value ) === 0 && in_array( $realName, $__settings ) ) {
109 continue;
110 }
111
112 if ( isset( $this->custom[$realName] ) ) {
113 call_user_func_array( [ $this, $this->custom[$realName] ],
114 [ $realName, $value, $vars ] );
115 } elseif ( in_array( $realName, $globalSettings ) ) {
116 $this->json[$realName] = $value;
117 } elseif ( array_key_exists( $realName, $this->noLongerSupportedGlobals ) ) {
118 $this->output( 'Warning: Skipped global "' . $name . '" (' .
119 $this->noLongerSupportedGlobals[$realName] . '). ' .
120 "Please update the entry point before convert to registration.\n" );
121 $this->hasWarning = true;
122 } elseif ( strpos( $name, $configPrefix ) === 0 ) {
123 $configName = substr( $name, strlen( $configPrefix ) );
124
125 $isPath = false;
126 if ( is_array( $value ) ) {
127 foreach ( $value as $k => $v ) {
128 if ( strpos( $v, $this->dir ) !== false ) {
129 $value[$k] = $this->stripPath( $v, $this->dir );
130 $isPath = true;
131 }
132 }
133 } elseif ( is_string( $value ) && strpos( $value, $this->dir ) !== false ) {
134 $value = $this->stripPath( $value, $this->dir );
135 $isPath = true;
136 }
137
138 // Most likely a config setting
139 $this->json['config'][$configName] = [ 'value' => $value ];
140
141 if ( $isPath ) {
142 $this->json['config'][$configName]['path'] = true;
143 }
144 } elseif ( $configPrefix !== 'wg' && strpos( $name, 'wg' ) === 0 ) {
145 // Warn about this
146 $this->output( 'Warning: Skipped global "' . $name . '" (' .
147 'config prefix is "' . $configPrefix . '"). ' .
148 "Please check that this setting isn't needed.\n" );
149 }
150 }
151
152 // check, if the extension requires composer libraries
153 if ( $this->needsComposerAutoloader( dirname( $this->getArg( 0 ) ) ) ) {
154 // set the load composer autoloader automatically property
155 $this->output( "Detected composer dependencies, setting 'load_composer_autoloader' to true.\n" );
156 $this->json['load_composer_autoloader'] = true;
157 }
158
159 // Move some keys to the top
160 $out = [];
161 foreach ( $this->promote as $key ) {
162 if ( isset( $this->json[$key] ) ) {
163 $out[$key] = $this->json[$key];
164 unset( $this->json[$key] );
165 }
166 }
167 // Set a requirement on the MediaWiki version that the current MANIFEST_VERSION
168 // was introduced in.
169 $out['requires'] = [
170 ExtensionRegistry::MEDIAWIKI_CORE => ExtensionRegistry::MANIFEST_VERSION_MW_VERSION
171 ];
172 $out += $this->json;
173 // Put this at the bottom
174 $out['manifest_version'] = ExtensionRegistry::MANIFEST_VERSION;
175 $type = $this->hasOption( 'skin' ) ? 'skin' : 'extension';
176 $fname = "{$this->dir}/$type.json";
177 $prettyJSON = FormatJson::encode( $out, "\t", FormatJson::ALL_OK );
178 file_put_contents( $fname, $prettyJSON . "\n" );
179 $this->output( "Wrote output to $fname.\n" );
180 if ( $this->hasWarning ) {
181 $this->output( "Found warnings! Please resolve the warnings and rerun this script.\n" );
182 }
183 }
184
185 protected function handleExtensionFunctions( $realName, $value ) {
186 foreach ( $value as $func ) {
187 if ( $func instanceof Closure ) {
188 $this->fatalError( "Error: Closures cannot be converted to JSON. " .
189 "Please move your extension function somewhere else."
190 );
191 } elseif ( function_exists( $func ) ) {
192 // check if $func exists in the global scope
193 $this->fatalError( "Error: Global functions cannot be converted to JSON. " .
194 "Please move your extension function ($func) into a class."
195 );
196 }
197 }
198
199 $this->json[$realName] = $value;
200 }
201
202 protected function handleMessagesDirs( $realName, $value ) {
203 foreach ( $value as $key => $dirs ) {
204 foreach ( (array)$dirs as $dir ) {
205 $this->json[$realName][$key][] = $this->stripPath( $dir, $this->dir );
206 }
207 }
208 }
209
210 protected function handleExtensionMessagesFiles( $realName, $value, $vars ) {
211 foreach ( $value as $key => $file ) {
212 $strippedFile = $this->stripPath( $file, $this->dir );
213 if ( isset( $vars['wgMessagesDirs'][$key] ) ) {
214 $this->output(
215 "Note: Ignoring PHP shim $strippedFile. " .
216 "If your extension no longer supports versions of MediaWiki " .
217 "older than 1.23.0, you can safely delete it.\n"
218 );
219 } else {
220 $this->json[$realName][$key] = $strippedFile;
221 }
222 }
223 }
224
225 private function stripPath( $val, $dir ) {
226 if ( $val === $dir ) {
227 $val = '';
228 } elseif ( strpos( $val, $dir ) === 0 ) {
229 // +1 is for the trailing / that won't be in $this->dir
230 $val = substr( $val, strlen( $dir ) + 1 );
231 }
232
233 return $val;
234 }
235
236 protected function removeAbsolutePath( $realName, $value ) {
237 $out = [];
238 foreach ( $value as $key => $val ) {
239 $out[$key] = $this->stripPath( $val, $this->dir );
240 }
241 $this->json[$realName] = $out;
242 }
243
244 protected function removeAutodiscoveredParserTestFiles( $realName, $value ) {
245 $out = [];
246 foreach ( $value as $key => $val ) {
247 $path = $this->stripPath( $val, $this->dir );
248 // When path starts with tests/parser/ the file would be autodiscovered with
249 // extension registry, so no need to add it to extension.json
250 if ( substr( $path, 0, 13 ) !== 'tests/parser/' || substr( $path, -4 ) !== '.txt' ) {
251 $out[$key] = $path;
252 }
253 }
254 // in the best case all entries are filtered out
255 if ( $out ) {
256 $this->json[$realName] = $out;
257 }
258 }
259
260 protected function handleCredits( $realName, $value ) {
261 $keys = array_keys( $value );
262 $this->json['type'] = $keys[0];
263 $values = array_values( $value );
264 foreach ( $values[0][0] as $name => $val ) {
265 if ( $name !== 'path' ) {
266 $this->json[$name] = $val;
267 }
268 }
269 }
270
271 public function handleHooks( $realName, $value ) {
272 foreach ( $value as $hookName => &$handlers ) {
273 if ( $hookName === 'UnitTestsList' ) {
274 $this->output( "Note: the UnitTestsList hook is no longer necessary as " .
275 "long as your tests are located in the \"tests/phpunit/\" directory. " .
276 "Please see <https://www.mediawiki.org/wiki/Manual:PHP_unit_testing/" .
277 "Writing_unit_tests_for_extensions#Register_your_tests> for more details.\n"
278 );
279 }
280 foreach ( $handlers as $func ) {
281 if ( $func instanceof Closure ) {
282 $this->fatalError( "Error: Closures cannot be converted to JSON. " .
283 "Please move the handler for $hookName somewhere else."
284 );
285 } elseif ( function_exists( $func ) ) {
286 // Check if $func exists in the global scope
287 $this->fatalError( "Error: Global functions cannot be converted to JSON. " .
288 "Please move the handler for $hookName inside a class."
289 );
290 }
291 }
292 if ( count( $handlers ) === 1 ) {
293 $handlers = $handlers[0];
294 }
295 }
296 $this->json[$realName] = $value;
297 }
298
303 protected function handleResourceModules( $realName, $value ) {
304 $defaults = [];
305 $remote = $this->hasOption( 'skin' ) ? 'remoteSkinPath' : 'remoteExtPath';
306 foreach ( $value as $name => $data ) {
307 if ( isset( $data['localBasePath'] ) ) {
308 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir );
309 if ( !$defaults ) {
310 $defaults['localBasePath'] = $data['localBasePath'];
311 unset( $data['localBasePath'] );
312 if ( isset( $data[$remote] ) ) {
313 $defaults[$remote] = $data[$remote];
314 unset( $data[$remote] );
315 }
316 } else {
317 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
318 unset( $data['localBasePath'] );
319 }
320 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
321 && $data[$remote] === $defaults[$remote]
322 ) {
323 unset( $data[$remote] );
324 }
325 }
326 }
327
328 $this->json[$realName][$name] = $data;
329 }
330 if ( $defaults ) {
331 $this->json['ResourceFileModulePaths'] = $defaults;
332 }
333 }
334
335 protected function needsComposerAutoloader( $path ) {
336 $path .= '/composer.json';
337 if ( file_exists( $path ) ) {
338 // assume, that the composer.json file is in the root of the extension path
339 $composerJson = new ComposerJson( $path );
340 // check, if there are some dependencies in the require section
341 if ( $composerJson->getRequiredDependencies() ) {
342 return true;
343 }
344 }
345 return false;
346 }
347}
348
349$maintClass = ConvertExtensionToRegistration::class;
350require_once RUN_MAINTENANCE_IF_MAIN;
Reads a composer.json file and provides accessors to get its hash and the required dependencies.
string[] $promote
Keys that should be put at the top of the generated JSON file (T86608)
string[] $noLongerSupportedGlobals
No longer supported globals (with reason) should not be converted and emit a warning.
string[] $formerGlobals
Things that were formerly globals and should still be converted.
handleExtensionMessagesFiles( $realName, $value, $vars)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42