Merge "mediawiki.widgets: Remove use of bind() for lexical 'this' binding"
[lhc/web/wiklou.git] / maintenance / convertExtensionToRegistration.php
index 608605c..8993146 100644 (file)
@@ -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 );
                }
@@ -210,13 +223,19 @@ class ConvertExtensionToRegistration extends Maintenance {
                                                "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
+                                       );
+                               }
                        }
                }
                $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 +265,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';