Merge "Clean up array() syntax in docs, part IV"
[lhc/web/wiklou.git] / maintenance / Maintenance.php
index a08297a..ab316c0 100644 (file)
  */
 
 // Bail on old versions of PHP, or if composer has not been run yet to install
-// dependencies. Using dirname( __FILE__ ) here because __DIR__ is PHP5.3+.
-// @codingStandardsIgnoreStart MediaWiki.Usage.DirUsage.FunctionFound
-require_once dirname( __FILE__ ) . '/../includes/PHPVersionCheck.php';
-// @codingStandardsIgnoreEnd
+// dependencies.
+require_once __DIR__ . '/../includes/PHPVersionCheck.php';
 wfEntryPointCheck( 'cli' );
 
 /**
@@ -125,6 +123,12 @@ abstract class Maintenance {
         */
        private $config;
 
+       /**
+        * @see Maintenance::requireExtension
+        * @var array
+        */
+       private $requiredExtensions = [];
+
        /**
         * Used to read the options in the order they were passed.
         * Useful for option chaining (Ex. dumpBackup.php). It will
@@ -508,6 +512,42 @@ abstract class Maintenance {
                $this->config = $config;
        }
 
+       /**
+        * Indicate that the specified extension must be
+        * loaded before the script can run.
+        *
+        * This *must* be called in the constructor.
+        *
+        * @since 1.28
+        * @param string $name
+        */
+       protected function requireExtension( $name ) {
+               $this->requiredExtensions[] = $name;
+       }
+
+       /**
+        * Verify that the required extensions are installed
+        *
+        * @since 1.28
+        */
+       public function checkRequiredExtensions() {
+               $registry = ExtensionRegistry::getInstance();
+               $missing = [];
+               foreach ( $this->requiredExtensions as $name ) {
+                       if ( !$registry->isLoaded( $name ) ) {
+                               $missing[] = $name;
+                       }
+               }
+
+               if ( $missing ) {
+                       $joined = implode( ', ', $missing );
+                       $msg = "The following extensions are required to be installed "
+                               . "for this script to run: $joined. Please enable them and then try again.";
+                       $this->error( $msg, 1 );
+               }
+
+       }
+
        /**
         * Run a child maintenance script. Pass all of the current arguments
         * to it.
@@ -867,7 +907,7 @@ abstract class Maintenance {
 
                // Description ...
                if ( $this->mDescription ) {
-                       $this->output( "\n" . $this->mDescription . "\n" );
+                       $this->output( "\n" . wordwrap( $this->mDescription, $screenWidth ) . "\n" );
                }
                $output = "\nUsage: php " . basename( $this->mSelf );