X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=blobdiff_plain;f=maintenance%2FconvertExtensionToRegistration.php;h=f9dd58c299e0dbd1dfec08a7f697033289219e00;hb=3aa7e1d66a0f3d91043c7abf0e10eac15f445a80;hp=608605c015323e2fad9fb603fff1f3d4757c7f65;hpb=d9a34ea8c09f790f557ea3017fe8c6ad1dca68ff;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/convertExtensionToRegistration.php b/maintenance/convertExtensionToRegistration.php old mode 100644 new mode 100755 index 608605c015..f9dd58c299 --- a/maintenance/convertExtensionToRegistration.php +++ b/maintenance/convertExtensionToRegistration.php @@ -4,7 +4,7 @@ require_once __DIR__ . '/Maintenance.php'; class ConvertExtensionToRegistration extends Maintenance { - protected $custom = array( + protected $custom = [ 'MessagesDirs' => 'handleMessagesDirs', 'ExtensionMessagesFiles' => 'handleExtensionMessagesFiles', 'AutoloadClasses' => 'removeAbsolutePath', @@ -14,32 +14,32 @@ class ConvertExtensionToRegistration extends Maintenance { 'Hooks' => 'handleHooks', 'ExtensionFunctions' => 'handleExtensionFunctions', 'ParserTestFiles' => 'removeAbsolutePath', - ); + ]; /** * Things that were formerly globals and should still be converted * * @var array */ - protected $formerGlobals = array( + protected $formerGlobals = [ 'TrackingCategories', - ); + ]; /** * No longer supported globals (with reason) should not be converted and emit a warning * * @var array */ - protected $noLongerSupportedGlobals = array( + protected $noLongerSupportedGlobals = [ 'SpecialPageGroups' => 'deprecated', // Deprecated 1.21, removed in 1.26 - ); + ]; /** * Keys that should be put at the top of the generated JSON file (T86608) * * @var array */ - protected $promote = array( + protected $promote = [ 'name', 'namemsg', 'version', @@ -49,13 +49,13 @@ class ConvertExtensionToRegistration extends Maintenance { 'descriptionmsg', 'license-name', 'type', - ); + ]; private $json, $dir, $hasWarning = false; public function __construct() { parent::__construct(); - $this->mDescription = 'Converts extension entry points to the new JSON registration format'; + $this->addDescription( 'Converts extension entry points to the new JSON registration format' ); $this->addArg( 'path', 'Location to the PHP entry point you wish to convert', /* $required = */ true ); $this->addOption( 'skin', 'Whether to write to skin.json', false, false ); @@ -76,7 +76,7 @@ class ConvertExtensionToRegistration extends Maintenance { $__settings = array_merge( $this->getAllGlobals(), array_keys( $this->custom ) ); foreach ( $__settings as $var ) { $var = 'wg' . $var; - $$var = array(); + $$var = []; } unset( $var ); $arg = $this->getArg( 0 ); @@ -90,7 +90,7 @@ class ConvertExtensionToRegistration extends Maintenance { unset( $vars['this'] ); unset( $vars['__settings'] ); $this->dir = dirname( realpath( $this->getArg( 0 ) ) ); - $this->json = array(); + $this->json = []; $globalSettings = $this->getAllGlobals(); foreach ( $vars as $name => $value ) { $realName = substr( $name, 2 ); // Strip 'wg' @@ -101,8 +101,8 @@ class ConvertExtensionToRegistration extends Maintenance { } if ( isset( $this->custom[$realName] ) ) { - call_user_func_array( array( $this, $this->custom[$realName] ), - array( $realName, $value, $vars ) ); + call_user_func_array( [ $this, $this->custom[$realName] ], + [ $realName, $value, $vars ] ); } elseif ( in_array( $realName, $globalSettings ) ) { $this->json[$realName] = $value; } elseif ( array_key_exists( $realName, $this->noLongerSupportedGlobals ) ) { @@ -116,8 +116,15 @@ class ConvertExtensionToRegistration extends Maintenance { } } + // check, if the extension requires composer libraries + if ( $this->needsComposerAutoloader( dirname( $this->getArg( 0 ) ) ) ) { + // set the load composer autoloader automatically property + $this->output( "Detected composer dependencies, setting 'load_composer_autoloader' to true.\n" ); + $this->json['load_composer_autoloader'] = true; + } + // Move some keys to the top - $out = array(); + $out = []; foreach ( $this->promote as $key ) { if ( isset( $this->json[$key] ) ) { $out[$key] = $this->json[$key]; @@ -144,6 +151,12 @@ class ConvertExtensionToRegistration extends Maintenance { "Please move your extension function somewhere else.", 1 ); } + // check if $func exists in the global scope + if ( function_exists( $func ) ) { + $this->error( "Error: Global functions cannot be converted to JSON. " . + "Please move your extension function ($func) into a class.", 1 + ); + } } $this->json[$realName] = $value; @@ -184,7 +197,7 @@ class ConvertExtensionToRegistration extends Maintenance { } protected function removeAbsolutePath( $realName, $value ) { - $out = array(); + $out = []; foreach ( $value as $key => $val ) { $out[$key] = $this->stripPath( $val, $this->dir ); } @@ -203,20 +216,29 @@ class ConvertExtensionToRegistration extends Maintenance { } public function handleHooks( $realName, $value ) { - foreach ( $value as $hookName => $handlers ) { + foreach ( $value as $hookName => &$handlers ) { foreach ( $handlers as $func ) { if ( $func instanceof Closure ) { $this->error( "Error: Closures cannot be converted to JSON. " . "Please move the handler for $hookName somewhere else.", 1 ); } + // Check if $func exists in the global scope + if ( function_exists( $func ) ) { + $this->error( "Error: Global functions cannot be converted to JSON. " . + "Please move the handler for $hookName inside a class.", 1 + ); + } + } + if ( count( $handlers ) === 1 ) { + $handlers = $handlers[0]; } } $this->json[$realName] = $value; } protected function handleResourceModules( $realName, $value ) { - $defaults = array(); + $defaults = []; $remote = $this->hasOption( 'skin' ) ? 'remoteSkinPath' : 'remoteExtPath'; foreach ( $value as $name => $data ) { if ( isset( $data['localBasePath'] ) ) { @@ -246,6 +268,19 @@ class ConvertExtensionToRegistration extends Maintenance { $this->json['ResourceFileModulePaths'] = $defaults; } } + + protected function needsComposerAutoloader( $path ) { + $path .= '/composer.json'; + if ( file_exists( $path ) ) { + // assume, that the composer.json file is in the root of the extension path + $composerJson = new ComposerJson( $path ); + // check, if there are some dependencies in the require section + if ( $composerJson->getRequiredDependencies() ) { + return true; + } + } + return false; + } } $maintClass = 'ConvertExtensionToRegistration';