| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Json\FormatJson; |
| 4 |
use MediaWiki\Registration\ExtensionProcessor; |
| 5 |
use MediaWiki\Registration\ExtensionRegistry; |
| 6 |
use Wikimedia\Composer\ComposerJson; |
| 7 |
|
| 8 |
|
| 9 |
require_once __DIR__ . '/Maintenance.php'; |
| 10 |
|
| 11 |
|
| 12 |
class ConvertExtensionToRegistration extends Maintenance { |
| 13 |
|
| 14 |
private const CUSTOM_GLOBALS = [ |
| 15 |
'MessagesDirs' => 'handleMessagesDirs', |
| 16 |
'ExtensionMessagesFiles' => 'handleExtensionMessagesFiles', |
| 17 |
'AutoloadClasses' => 'removeAbsolutePath', |
| 18 |
'ExtensionCredits' => 'handleCredits', |
| 19 |
'ResourceModules' => 'handleResourceModules', |
| 20 |
'ResourceModuleSkinStyles' => 'handleResourceModules', |
| 21 |
'Hooks' => 'handleHooks', |
| 22 |
'ExtensionFunctions' => 'handleExtensionFunctions', |
| 23 |
'ParserTestFiles' => 'removeAutodiscoveredParserTestFiles', |
| 24 |
]; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private const FORMER_GLOBALS = [ |
| 30 |
'TrackingCategories', |
| 31 |
]; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private const NO_LONGER_SUPPORTED_GLOBALS = [ |
| 37 |
'SpecialPageGroups' => 'deprecated', |
| 38 |
]; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private const PROMOTE_ATTRIBUTES = [ |
| 44 |
'name', |
| 45 |
'namemsg', |
| 46 |
'version', |
| 47 |
'author', |
| 48 |
'url', |
| 49 |
'description', |
| 50 |
'descriptionmsg', |
| 51 |
'license-name', |
| 52 |
'type', |
| 53 |
]; |
| 54 |
|
| 55 |
private array $json; |
| 56 |
private string $dir; |
| 57 |
private bool $hasWarning = false; |
| 58 |
|
| 59 |
public function __construct() { |
| 60 |
parent::__construct(); |
| 61 |
$this->addDescription( 'Converts extension entry points to the new JSON registration format' ); |
| 62 |
$this->addArg( 'path', 'Location to the PHP entry point you wish to convert', |
| 63 |
true ); |
| 64 |
$this->addOption( 'skin', 'Whether to write to skin.json', false, false ); |
| 65 |
$this->addOption( 'config-prefix', 'Custom prefix for configuration settings', false, true ); |
| 66 |
} |
| 67 |
|
| 68 |
protected function getAllGlobals() { |
| 69 |
$processor = new ReflectionClass( ExtensionProcessor::class ); |
| 70 |
$settings = $processor->getProperty( 'globalSettings' ); |
| 71 |
return array_merge( $settings->getValue(), self::FORMER_GLOBALS ); |
| 72 |
} |
| 73 |
|
| 74 |
public function execute() { |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
$__settings = array_merge( $this->getAllGlobals(), array_keys( self::CUSTOM_GLOBALS ) ); |
| 80 |
foreach ( $__settings as $var ) { |
| 81 |
$var = 'wg' . $var; |
| 82 |
$$var = []; |
| 83 |
} |
| 84 |
unset( $var ); |
| 85 |
$arg = $this->getArg( 0 ); |
| 86 |
if ( !is_file( $arg ) ) { |
| 87 |
$this->fatalError( "$arg is not a file." ); |
| 88 |
} |
| 89 |
require $arg; |
| 90 |
unset( $arg ); |
| 91 |
|
| 92 |
$vars = get_defined_vars(); |
| 93 |
unset( $vars['this'] ); |
| 94 |
unset( $vars['__settings'] ); |
| 95 |
$this->dir = dirname( realpath( $this->getArg( 0 ) ) ); |
| 96 |
$this->json = []; |
| 97 |
$globalSettings = $this->getAllGlobals(); |
| 98 |
$configPrefix = $this->getOption( 'config-prefix', 'wg' ); |
| 99 |
if ( $configPrefix !== 'wg' ) { |
| 100 |
$this->json['config']['_prefix'] = $configPrefix; |
| 101 |
} |
| 102 |
|
| 103 |
foreach ( $vars as $name => $value ) { |
| 104 |
$realName = substr( $name, 2 ); |
| 105 |
if ( $realName === '' ) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
if ( is_array( $value ) && count( $value ) === 0 && in_array( $realName, $__settings ) ) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
if ( isset( self::CUSTOM_GLOBALS[$realName] ) ) { |
| 115 |
call_user_func_array( [ $this, self::CUSTOM_GLOBALS[$realName] ], |
| 116 |
[ $realName, $value, $vars ] ); |
| 117 |
} elseif ( in_array( $realName, $globalSettings ) ) { |
| 118 |
$this->json[$realName] = $value; |
| 119 |
} elseif ( array_key_exists( $realName, self::NO_LONGER_SUPPORTED_GLOBALS ) ) { |
| 120 |
$this->output( 'Warning: Skipped global "' . $name . '" (' . |
| 121 |
self::NO_LONGER_SUPPORTED_GLOBALS[$realName] . '). ' . |
| 122 |
"Please update the entry point before convert to registration.\n" ); |
| 123 |
$this->hasWarning = true; |
| 124 |
} elseif ( strpos( $name, $configPrefix ) === 0 ) { |
| 125 |
$configName = substr( $name, strlen( $configPrefix ) ); |
| 126 |
|
| 127 |
$isPath = false; |
| 128 |
if ( is_array( $value ) ) { |
| 129 |
foreach ( $value as $k => $v ) { |
| 130 |
if ( strpos( $v, $this->dir ) !== false ) { |
| 131 |
$value[$k] = $this->stripPath( $v, $this->dir ); |
| 132 |
$isPath = true; |
| 133 |
} |
| 134 |
} |
| 135 |
} elseif ( is_string( $value ) && strpos( $value, $this->dir ) !== false ) { |
| 136 |
$value = $this->stripPath( $value, $this->dir ); |
| 137 |
$isPath = true; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
$this->json['config'][$configName] = [ 'value' => $value ]; |
| 142 |
|
| 143 |
if ( $isPath ) { |
| 144 |
$this->json['config'][$configName]['path'] = true; |
| 145 |
} |
| 146 |
} elseif ( $configPrefix !== 'wg' && strpos( $name, 'wg' ) === 0 ) { |
| 147 |
|
| 148 |
$this->output( 'Warning: Skipped global "' . $name . '" (' . |
| 149 |
'config prefix is "' . $configPrefix . '"). ' . |
| 150 |
"Please check that this setting isn't needed.\n" ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
if ( $this->needsComposerAutoloader( dirname( $this->getArg( 0 ) ) ) ) { |
| 156 |
|
| 157 |
$this->output( "Detected composer dependencies, setting 'load_composer_autoloader' to true.\n" ); |
| 158 |
$this->json['load_composer_autoloader'] = true; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
$out = []; |
| 163 |
foreach ( self::PROMOTE_ATTRIBUTES as $key ) { |
| 164 |
if ( isset( $this->json[$key] ) ) { |
| 165 |
$out[$key] = $this->json[$key]; |
| 166 |
unset( $this->json[$key] ); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
$out['requires'] = [ |
| 172 |
ExtensionRegistry::MEDIAWIKI_CORE => ExtensionRegistry::MANIFEST_VERSION_MW_VERSION |
| 173 |
]; |
| 174 |
$out += $this->json; |
| 175 |
|
| 176 |
$out['manifest_version'] = ExtensionRegistry::MANIFEST_VERSION; |
| 177 |
$type = $this->hasOption( 'skin' ) ? 'skin' : 'extension'; |
| 178 |
$fname = "{$this->dir}/$type.json"; |
| 179 |
$prettyJSON = FormatJson::encode( $out, "\t", FormatJson::ALL_OK ); |
| 180 |
file_put_contents( $fname, $prettyJSON . "\n" ); |
| 181 |
$this->output( "Wrote output to $fname.\n" ); |
| 182 |
if ( $this->hasWarning ) { |
| 183 |
$this->output( "Found warnings! Please resolve the warnings and rerun this script.\n" ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
protected function handleExtensionFunctions( $realName, $value ) { |
| 188 |
foreach ( $value as $func ) { |
| 189 |
if ( $func instanceof Closure ) { |
| 190 |
$this->fatalError( "Error: Closures cannot be converted to JSON. " . |
| 191 |
"Please move your extension function somewhere else." |
| 192 |
); |
| 193 |
} elseif ( function_exists( $func ) ) { |
| 194 |
|
| 195 |
$this->fatalError( "Error: Global functions cannot be converted to JSON. " . |
| 196 |
"Please move your extension function ($func) into a class." |
| 197 |
); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
$this->json[$realName] = $value; |
| 202 |
} |
| 203 |
|
| 204 |
protected function handleMessagesDirs( $realName, $value ) { |
| 205 |
foreach ( $value as $key => $dirs ) { |
| 206 |
foreach ( (array)$dirs as $dir ) { |
| 207 |
$this->json[$realName][$key][] = $this->stripPath( $dir, $this->dir ); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
protected function handleExtensionMessagesFiles( $realName, $value, $vars ) { |
| 213 |
foreach ( $value as $key => $file ) { |
| 214 |
$strippedFile = $this->stripPath( $file, $this->dir ); |
| 215 |
if ( isset( $vars['wgMessagesDirs'][$key] ) ) { |
| 216 |
$this->output( |
| 217 |
"Note: Ignoring PHP shim $strippedFile. " . |
| 218 |
"If your extension no longer supports versions of MediaWiki " . |
| 219 |
"older than 1.23.0, you can safely delete it.\n" |
| 220 |
); |
| 221 |
} else { |
| 222 |
$this->json[$realName][$key] = $strippedFile; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
private function stripPath( $val, $dir ) { |
| 228 |
if ( $val === $dir ) { |
| 229 |
$val = ''; |
| 230 |
} elseif ( strpos( $val, $dir ) === 0 ) { |
| 231 |
|
| 232 |
$val = substr( $val, strlen( $dir ) + 1 ); |
| 233 |
} |
| 234 |
|
| 235 |
return $val; |
| 236 |
} |
| 237 |
|
| 238 |
protected function removeAbsolutePath( $realName, $value ) { |
| 239 |
$out = []; |
| 240 |
foreach ( $value as $key => $val ) { |
| 241 |
$out[$key] = $this->stripPath( $val, $this->dir ); |
| 242 |
} |
| 243 |
$this->json[$realName] = $out; |
| 244 |
} |
| 245 |
|
| 246 |
protected function removeAutodiscoveredParserTestFiles( $realName, $value ) { |
| 247 |
$out = []; |
| 248 |
foreach ( $value as $key => $val ) { |
| 249 |
$path = $this->stripPath( $val, $this->dir ); |
| 250 |
|
| 251 |
|
| 252 |
if ( !str_starts_with( $path, 'tests/parser/' ) || !str_ends_with( $path, '.txt' ) ) { |
| 253 |
$out[$key] = $path; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
if ( $out ) { |
| 258 |
$this->json[$realName] = $out; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
protected function handleCredits( $realName, $value ) { |
| 268 |
$keys = array_keys( $value ); |
| 269 |
$this->json['type'] = $keys[0]; |
| 270 |
$values = array_values( $value ); |
| 271 |
foreach ( $values[0][0] as $name => $val ) { |
| 272 |
if ( $name !== 'path' ) { |
| 273 |
$this->json[$name] = $val; |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
public function handleHooks( $realName, $value ) { |
| 279 |
foreach ( $value as $hookName => &$handlers ) { |
| 280 |
if ( $hookName === 'UnitTestsList' ) { |
| 281 |
$this->output( "Note: the UnitTestsList hook is no longer necessary as " . |
| 282 |
"long as your tests are located in the \"tests/phpunit/\" directory. " . |
| 283 |
"Please see <https://www.mediawiki.org/wiki/Manual:PHP_unit_testing/" . |
| 284 |
"Writing_unit_tests_for_extensions#Register_your_tests> for more details.\n" |
| 285 |
); |
| 286 |
} |
| 287 |
foreach ( $handlers as $func ) { |
| 288 |
if ( $func instanceof Closure ) { |
| 289 |
$this->fatalError( "Error: Closures cannot be converted to JSON. " . |
| 290 |
"Please move the handler for $hookName somewhere else." |
| 291 |
); |
| 292 |
} elseif ( function_exists( $func ) ) { |
| 293 |
|
| 294 |
$this->fatalError( "Error: Global functions cannot be converted to JSON. " . |
| 295 |
"Please move the handler for $hookName inside a class." |
| 296 |
); |
| 297 |
} |
| 298 |
} |
| 299 |
if ( count( $handlers ) === 1 ) { |
| 300 |
$handlers = $handlers[0]; |
| 301 |
} |
| 302 |
} |
| 303 |
$this->json[$realName] = $value; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
protected function handleResourceModules( $realName, $value ) { |
| 311 |
$defaults = []; |
| 312 |
$remote = $this->hasOption( 'skin' ) ? 'remoteSkinPath' : 'remoteExtPath'; |
| 313 |
foreach ( $value as $name => $data ) { |
| 314 |
if ( isset( $data['localBasePath'] ) ) { |
| 315 |
$data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir ); |
| 316 |
if ( !$defaults ) { |
| 317 |
$defaults['localBasePath'] = $data['localBasePath']; |
| 318 |
unset( $data['localBasePath'] ); |
| 319 |
if ( isset( $data[$remote] ) ) { |
| 320 |
$defaults[$remote] = $data[$remote]; |
| 321 |
unset( $data[$remote] ); |
| 322 |
} |
| 323 |
} else { |
| 324 |
if ( $data['localBasePath'] === $defaults['localBasePath'] ) { |
| 325 |
unset( $data['localBasePath'] ); |
| 326 |
} |
| 327 |
if ( isset( $data[$remote] ) && isset( $defaults[$remote] ) |
| 328 |
&& $data[$remote] === $defaults[$remote] |
| 329 |
) { |
| 330 |
unset( $data[$remote] ); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
$this->json[$realName][$name] = $data; |
| 336 |
} |
| 337 |
if ( $defaults ) { |
| 338 |
$this->json['ResourceFileModulePaths'] = $defaults; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
protected function needsComposerAutoloader( $path ) { |
| 343 |
$path .= '/composer.json'; |
| 344 |
if ( file_exists( $path ) ) { |
| 345 |
|
| 346 |
$composerJson = new ComposerJson( $path ); |
| 347 |
|
| 348 |
if ( $composerJson->getRequiredDependencies() ) { |
| 349 |
return true; |
| 350 |
} |
| 351 |
} |
| 352 |
return false; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
$maintClass = ConvertExtensionToRegistration::class; |
| 358 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 359 |
|