Merge "Treat phpdbg as run from the command line when checking PHP_SAPI"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 8 Jan 2018 23:03:41 +0000 (23:03 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 8 Jan 2018 23:03:41 +0000 (23:03 +0000)
1  2 
includes/GlobalFunctions.php
includes/libs/rdbms/database/Database.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php
maintenance/Maintenance.php

@@@ -2088,6 -2088,16 +2088,16 @@@ function wfIsHHVM() 
        return defined( 'HHVM_VERSION' );
  }
  
+ /**
+  * Check if we are running from the commandline
+  *
+  * @since 1.31
+  * @return bool
+  */
+ function wfIsCLI() {
+       return PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg';
+ }
  /**
   * Tries to get the system directory for temporary files. First
   * $wgTmpDirectory is checked, and then the TMPDIR, TMP, and TEMP
@@@ -2910,7 -2920,7 +2920,7 @@@ function wfGetLBFactory() 
   * Find a file.
   * Shortcut for RepoGroup::singleton()->findFile()
   *
 - * @param string $title String or Title object
 + * @param string|Title $title String or Title object
   * @param array $options Associative array of options (see RepoGroup::findFile)
   * @return File|bool File, or false if the file does not exist
   */
@@@ -3031,7 -3041,7 +3041,7 @@@ function wfWaitForSlaves
        $ifWritesSince = null, $wiki = false, $cluster = false, $timeout = null
  ) {
        if ( $timeout === null ) {
-               $timeout = ( PHP_SAPI === 'cli' ) ? 86400 : 10;
+               $timeout = wfIsCLI() ? 86400 : 10;
        }
  
        if ( $cluster === '*' ) {
@@@ -394,7 -394,9 +394,9 @@@ abstract class Database implements IDat
                        $p['variables'] = isset( $p['variables'] ) ? $p['variables'] : [];
                        $p['tablePrefix'] = isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : '';
                        $p['schema'] = isset( $p['schema'] ) ? $p['schema'] : '';
-                       $p['cliMode'] = isset( $p['cliMode'] ) ? $p['cliMode'] : ( PHP_SAPI === 'cli' );
+                       $p['cliMode'] = isset( $p['cliMode'] )
+                               ? $p['cliMode']
+                               : ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' );
                        $p['agent'] = isset( $p['agent'] ) ? $p['agent'] : '';
                        if ( !isset( $p['connLogger'] ) ) {
                                $p['connLogger'] = new \Psr\Log\NullLogger();
                $fname = __METHOD__,
                callable $inputCallback = null
        ) {
 +              $delimiterReset = new ScopedCallback(
 +                      function ( $delimiter ) {
 +                              $this->delimiter = $delimiter;
 +                      },
 +                      [ $this->delimiter ]
 +              );
                $cmd = '';
  
                while ( !feof( $fp ) ) {
                        if ( $done || feof( $fp ) ) {
                                $cmd = $this->replaceVars( $cmd );
  
 -                              if ( !$inputCallback || call_user_func( $inputCallback, $cmd ) ) {
 +                              if ( $inputCallback ) {
 +                                      $callbackResult = call_user_func( $inputCallback, $cmd );
 +
 +                                      if ( is_string( $callbackResult ) || !$callbackResult ) {
 +                                              $cmd = $callbackResult;
 +                                      }
 +                              }
 +
 +                              if ( $cmd ) {
                                        $res = $this->query( $cmd, $fname );
  
                                        if ( $resultCallback ) {
                        }
                }
  
 +              ScopedCallback::consume( $delimiterReset );
                return true;
        }
  
@@@ -232,7 -232,9 +232,9 @@@ class LoadBalancer implements ILoadBala
                $this->host = isset( $params['hostname'] )
                        ? $params['hostname']
                        : ( gethostname() ?: 'unknown' );
-               $this->cliMode = isset( $params['cliMode'] ) ? $params['cliMode'] : PHP_SAPI === 'cli';
+               $this->cliMode = isset( $params['cliMode'] )
+                       ? $params['cliMode']
+                       : ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' );
                $this->agent = isset( $params['agent'] ) ? $params['agent'] : '';
  
                if ( isset( $params['chronologyProtector'] ) ) {
                return isset( $this->mServers[$i]['type'] ) ? $this->mServers[$i]['type'] : 'unknown';
        }
  
 -      /**
 -       * @deprecated Since 1.30, no alternative
 -       */
 -      public function getServerInfo( $i ) {
 -              wfDeprecated( __METHOD__, '1.30' );
 -              if ( isset( $this->mServers[$i] ) ) {
 -                      return $this->mServers[$i];
 -              } else {
 -                      return false;
 -              }
 -      }
 -
 -      /**
 -       * @deprecated Since 1.30, construct new object
 -       */
 -      public function setServerInfo( $i, array $serverInfo ) {
 -              wfDeprecated( __METHOD__, '1.30' );
 -              $this->mServers[$i] = $serverInfo;
 -      }
 -
        public function getMasterPos() {
                # If this entire request was served from a replica DB without opening a connection to the
                # master (however unlikely that may be), then we can fetch the position from the replica DB.
@@@ -381,15 -381,11 +381,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;
                }
                        $this->fatalError( $err, intval( $die ) );
                }
                $this->outputChanneled( false );
-               if ( PHP_SAPI == 'cli' ) {
+               if ( PHP_SAPI == 'cli' || PHP_SAPI == 'phpdbg' ) {
                        fwrite( STDERR, $err . "\n" );
                } else {
                        print $err;
                global $IP, $wgCommandLineMode, $wgRequestTime;
  
                # 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' );
                }