Merge "SizeFilterWidget: Reduce width"
[lhc/web/wiklou.git] / maintenance / Maintenance.php
index 07f547f..4d0af5f 100644 (file)
@@ -25,6 +25,7 @@
 require_once __DIR__ . '/../includes/PHPVersionCheck.php';
 wfEntryPointCheck( 'cli' );
 
+use MediaWiki\Shell\Shell;
 use Wikimedia\Rdbms\DBReplicationWaitError;
 
 /**
@@ -182,7 +183,7 @@ abstract class Maintenance {
                if ( $count < 2 ) {
                        return false; // sanity
                }
-               if ( $bt[0]['class'] !== 'Maintenance' || $bt[0]['function'] !== 'shouldExecute' ) {
+               if ( $bt[0]['class'] !== self::class || $bt[0]['function'] !== 'shouldExecute' ) {
                        return false; // last call should be to this function
                }
                $includeFuncs = [ 'require_once', 'require', 'include', 'include_once' ];
@@ -381,11 +382,15 @@ abstract class Maintenance {
         * @param mixed $channel Unique identifier for the channel. See function outputChanneled.
         */
        protected function output( $out, $channel = null ) {
-               // Try to periodically flush buffered metrics to avoid OOMs
-               $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
-               if ( $stats->getDataCount() > 1000 ) {
-                       MediaWiki::emitBufferedStatsdData( $stats, $this->getConfig() );
+               // This is sometimes called very early, before Setup.php is included.
+               if ( class_exists( MediaWikiServices::class ) ) {
+                       // Try to periodically flush buffered metrics to avoid OOMs
+                       $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
+                       if ( $stats->getDataCount() > 1000 ) {
+                               MediaWiki::emitBufferedStatsdData( $stats, $this->getConfig() );
+                       }
                }
+
                if ( $this->mQuiet ) {
                        return;
                }
@@ -410,7 +415,10 @@ abstract class Maintenance {
                        $this->fatalError( $err, intval( $die ) );
                }
                $this->outputChanneled( false );
-               if ( PHP_SAPI == 'cli' ) {
+               if (
+                       ( PHP_SAPI == 'cli' || PHP_SAPI == 'phpdbg' ) &&
+                       !defined( 'MW_PHPUNIT_TEST' )
+               ) {
                        fwrite( STDERR, $err . "\n" );
                } else {
                        print $err;
@@ -507,6 +515,8 @@ abstract class Maintenance {
                        "http://en.wikipedia.org. This is sometimes necessary because " .
                        "server name detection may fail in command line scripts.", false, true );
                $this->addOption( 'profiler', 'Profiler output format (usually "text")', false, true );
+               // This is named --mwdebug, because --debug would conflict in the phpunit.php CLI script.
+               $this->addOption( 'mwdebug', 'Enable built-in MediaWiki development settings', false, true );
 
                # Save generic options to display them separately in help
                $this->mGenericParameters = $this->mParams;
@@ -669,10 +679,11 @@ abstract class Maintenance {
         * Do some sanity checking and basic setup
         */
        public function setup() {
-               global $IP, $wgCommandLineMode, $wgRequestTime;
+               global $IP, $wgCommandLineMode;
 
                # Abort if called from a web server
-               if ( PHP_SAPI !== 'cli' ) {
+               # wfIsCLI() is not available yet
+               if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) {
                        $this->fatalError( 'This script must be run from the command line' );
                }
 
@@ -705,8 +716,6 @@ abstract class Maintenance {
                # But sometimes this doesn't seem to be the case.
                ini_set( 'max_execution_time', 0 );
 
-               $wgRequestTime = microtime( true );
-
                # Define us as being in MediaWiki
                define( 'MEDIAWIKI', true );
 
@@ -1001,7 +1010,7 @@ abstract class Maintenance {
 
                // ... append parameters ...
                if ( $this->mParams ) {
-                       $output .= " [--" . implode( array_keys( $this->mParams ), "|--" ) . "]";
+                       $output .= " [--" . implode( "|--", array_keys( $this->mParams ) ) . "]";
                }
 
                // ... and append arguments.
@@ -1141,6 +1150,11 @@ abstract class Maintenance {
                        MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->destroy();
                }
 
+               # Apply debug settings
+               if ( $this->hasOption( 'mwdebug' ) ) {
+                       require __DIR__ . '/../includes/DevelopmentSettings.php';
+               }
+
                // Per-script profiling; useful for debugging
                $this->activateProfiler();
 
@@ -1148,9 +1162,9 @@ abstract class Maintenance {
 
                $wgShowSQLErrors = true;
 
-               MediaWiki\suppressWarnings();
+               Wikimedia\suppressWarnings();
                set_time_limit( 0 );
-               MediaWiki\restoreWarnings();
+               Wikimedia\restoreWarnings();
 
                $this->adjustMemoryLimit();
        }
@@ -1202,6 +1216,9 @@ abstract class Maintenance {
                                "must exist and be readable in the source directory.\n" .
                                "Use --conf to specify it." );
                }
+               if ( isset( $this->mOptions['server'] ) ) {
+                       $_SERVER['SERVER_NAME'] = $this->mOptions['server'];
+               }
                $wgCommandLineMode = true;
 
                return $settingsFile;
@@ -1276,7 +1293,7 @@ abstract class Maintenance {
         * This function has the same parameters as wfGetDB()
         *
         * @param int $db DB index (DB_REPLICA/DB_MASTER)
-        * @param array $groups default: empty array
+        * @param string|string[] $groups default: empty array
         * @param string|bool $wiki default: current wiki
         * @return IMaintainableDatabase
         */
@@ -1576,12 +1593,12 @@ abstract class Maintenance {
                // something that can do the relevant syscalls. There are a few
                // options. Linux and Mac OS X both have "stty size" which does the
                // job directly.
-               $retval = false;
-               $size = wfShellExec( 'stty size', $retval );
-               if ( $retval !== 0 ) {
+               $result = Shell::command( 'stty', 'size' )
+                       ->execute();
+               if ( $result->getExitCode() !== 0 ) {
                        return $default;
                }
-               if ( !preg_match( '/^(\d+) (\d+)$/', $size, $m ) ) {
+               if ( !preg_match( '/^(\d+) (\d+)$/', $result->getStdout(), $m ) ) {
                        return $default;
                }
                return [ intval( $m[2] ), intval( $m[1] ) ];