MediaWiki  master
convertExtensionToRegistration.php
Go to the documentation of this file.
1 <?php
2 
3 require_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'] = [
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 ( !str_starts_with( $path, 'tests/parser/' ) || !str_ends_with( $path, '.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  // FIXME: the ParserTestFiles key was deprecated in
259  // MW 1.30 and removed in MW 1.40. If not all entries were filtered
260  // out by the above, we *should* recommend the user move the
261  // parser tests under `tests/parser` *not* generate an extension.json
262  // with a ParserTestFiles key that will no longer validate.
263  }
264 
265  protected function handleCredits( $realName, $value ) {
266  $keys = array_keys( $value );
267  $this->json['type'] = $keys[0];
268  $values = array_values( $value );
269  foreach ( $values[0][0] as $name => $val ) {
270  if ( $name !== 'path' ) {
271  $this->json[$name] = $val;
272  }
273  }
274  }
275 
276  public function handleHooks( $realName, $value ) {
277  foreach ( $value as $hookName => &$handlers ) {
278  if ( $hookName === 'UnitTestsList' ) {
279  $this->output( "Note: the UnitTestsList hook is no longer necessary as " .
280  "long as your tests are located in the \"tests/phpunit/\" directory. " .
281  "Please see <https://www.mediawiki.org/wiki/Manual:PHP_unit_testing/" .
282  "Writing_unit_tests_for_extensions#Register_your_tests> for more details.\n"
283  );
284  }
285  foreach ( $handlers as $func ) {
286  if ( $func instanceof Closure ) {
287  $this->fatalError( "Error: Closures cannot be converted to JSON. " .
288  "Please move the handler for $hookName somewhere else."
289  );
290  } elseif ( function_exists( $func ) ) {
291  // Check if $func exists in the global scope
292  $this->fatalError( "Error: Global functions cannot be converted to JSON. " .
293  "Please move the handler for $hookName inside a class."
294  );
295  }
296  }
297  if ( count( $handlers ) === 1 ) {
298  $handlers = $handlers[0];
299  }
300  }
301  $this->json[$realName] = $value;
302  }
303 
308  protected function handleResourceModules( $realName, $value ) {
309  $defaults = [];
310  $remote = $this->hasOption( 'skin' ) ? 'remoteSkinPath' : 'remoteExtPath';
311  foreach ( $value as $name => $data ) {
312  if ( isset( $data['localBasePath'] ) ) {
313  $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir );
314  if ( !$defaults ) {
315  $defaults['localBasePath'] = $data['localBasePath'];
316  unset( $data['localBasePath'] );
317  if ( isset( $data[$remote] ) ) {
318  $defaults[$remote] = $data[$remote];
319  unset( $data[$remote] );
320  }
321  } else {
322  if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
323  unset( $data['localBasePath'] );
324  }
325  if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
326  && $data[$remote] === $defaults[$remote]
327  ) {
328  unset( $data[$remote] );
329  }
330  }
331  }
332 
333  $this->json[$realName][$name] = $data;
334  }
335  if ( $defaults ) {
336  $this->json['ResourceFileModulePaths'] = $defaults;
337  }
338  }
339 
340  protected function needsComposerAutoloader( $path ) {
341  $path .= '/composer.json';
342  if ( file_exists( $path ) ) {
343  // assume, that the composer.json file is in the root of the extension path
344  $composerJson = new ComposerJson( $path );
345  // check, if there are some dependencies in the require section
346  if ( $composerJson->getRequiredDependencies() ) {
347  return true;
348  }
349  }
350  return false;
351  }
352 }
353 
354 $maintClass = ConvertExtensionToRegistration::class;
355 require_once RUN_MAINTENANCE_IF_MAIN;
Reads a composer.json file and provides accessors to get its hash and the required dependencies.
Definition: ComposerJson.php:9
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)
const MANIFEST_VERSION
Version of the highest supported manifest version Note: Update MANIFEST_VERSION_MW_VERSION when chang...
const MANIFEST_VERSION_MW_VERSION
MediaWiki version constraint representing what the current highest MANIFEST_VERSION is supported in.
const MEDIAWIKI_CORE
"requires" key that applies to MediaWiki core
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:98
const ALL_OK
Skip escaping as many characters as reasonably possible.
Definition: FormatJson.php:57
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
addArg( $arg, $description, $required=true, $multi=false)
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