Treat phpdbg as run from the command line when checking PHP_SAPI
authorKunal Mehta <legoktm@member.fsf.org>
Wed, 3 Jan 2018 06:51:44 +0000 (22:51 -0800)
committerKunal Mehta <legoktm@member.fsf.org>
Thu, 4 Jan 2018 07:00:37 +0000 (23:00 -0800)
phpdbg is a gdb-style debugger for PHP that is run from the command
line. However, it has a different PHP_SAPI value, so it was impossible
to run maintenance scripts with it (until now).

To avoid having to check both PHP_SAPI values in a bunch of places,
introduce wfIsCLI() to easily check whether running from the
command-line or not.

We're (CI team) interested in generating code coverage with phpdbg
instead of xdebug, hence this patch.

Bug: T184043
Change-Id: Id1f994ca146d7858cd8bb6ab6cdbb7718ff524fb

13 files changed:
includes/ForkController.php
includes/GlobalFunctions.php
includes/debug/logger/monolog/WikiProcessor.php
includes/exception/MWExceptionHandler.php
includes/libs/lockmanager/LockManager.php
includes/libs/rdbms/database/Database.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php
includes/profiler/Profiler.php
includes/profiler/output/ProfilerOutputText.php
includes/utils/UIDGenerator.php
maintenance/Maintenance.php
maintenance/generateLocalAutoload.php
maintenance/mwdoc-filter.php

index 2dde17b..cc16964 100644 (file)
@@ -54,7 +54,7 @@ class ForkController {
        const RESTART_ON_ERROR = 1;
 
        public function __construct( $numProcs, $flags = 0 ) {
-               if ( PHP_SAPI != 'cli' ) {
+               if ( !wfIsCLI() ) {
                        throw new MWException( "ForkController cannot be used from the web." );
                }
                $this->procsToStart = $numProcs;
index 523a0f9..7b77090 100644 (file)
@@ -2088,6 +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
@@ -3031,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 === '*' ) {
index e39a2c3..db5b9bf 100644 (file)
@@ -39,7 +39,7 @@ class WikiProcessor {
                $record['extra']['wiki'] = wfWikiID();
                $record['extra']['mwversion'] = $wgVersion;
                $record['extra']['reqId'] = \WebRequest::getRequestId();
-               if ( PHP_SAPI === 'cli' && isset( $_SERVER['argv'] ) ) {
+               if ( wfIsCLI() && isset( $_SERVER['argv'] ) ) {
                        $record['extra']['cli_argv'] = implode( ' ', $_SERVER['argv'] );
                }
                return $record;
index f3d61f0..d863a2b 100644 (file)
@@ -121,7 +121,7 @@ class MWExceptionHandler {
                self::handleException( $e );
 
                // Make sure we don't claim success on exit for CLI scripts (T177414)
-               if ( PHP_SAPI === 'cli' ) {
+               if ( wfIsCLI() ) {
                        register_shutdown_function(
                                function () {
                                        exit( 255 );
index a6257bf..6b3cfb4 100644 (file)
@@ -83,7 +83,7 @@ abstract class LockManager {
                $this->domain = isset( $config['domain'] ) ? $config['domain'] : 'global';
                if ( isset( $config['lockTTL'] ) ) {
                        $this->lockTTL = max( 5, $config['lockTTL'] );
-               } elseif ( PHP_SAPI === 'cli' ) {
+               } elseif ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
                        $this->lockTTL = 3600;
                } else {
                        $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
index 15e02ad..0418dfc 100644 (file)
@@ -394,7 +394,9 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        $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();
index 591e287..9a7f7e1 100644 (file)
@@ -232,7 +232,9 @@ class LoadBalancer implements ILoadBalancer {
                $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'] ) ) {
index 4da7976..81449be 100644 (file)
@@ -74,7 +74,8 @@ abstract class Profiler {
                        }
 
                        $inSample = mt_rand( 0, $params['sampling'] - 1 ) === 0;
-                       if ( PHP_SAPI === 'cli' || !$inSample ) {
+                       // wfIsCLI() is not available yet
+                       if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' || !$inSample ) {
                                $params['class'] = 'ProfilerStub';
                        }
 
index dc24f18..100304f 100644 (file)
@@ -59,7 +59,7 @@ class ProfilerOutputText extends ProfilerOutput {
                        );
 
                        $contentType = $this->collector->getContentType();
-                       if ( PHP_SAPI === 'cli' ) {
+                       if ( wfIsCLI() ) {
                                print "<!--\n{$out}\n-->\n";
                        } elseif ( $contentType === 'text/html' ) {
                                $visible = isset( $this->params['visible'] ) ?
index 736109b..68ef57a 100644 (file)
@@ -367,7 +367,7 @@ class UIDGenerator {
                // Use APC/eAccelerator/xcache if requested, available, and not in CLI mode;
                // Counter values would not survive accross script instances in CLI mode.
                $cache = null;
-               if ( ( $flags & self::QUICK_VOLATILE ) && PHP_SAPI !== 'cli' ) {
+               if ( ( $flags & self::QUICK_VOLATILE ) && !wfIsCLI() ) {
                        $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
                }
                if ( $cache ) {
index 07f547f..68c51a8 100644 (file)
@@ -410,7 +410,7 @@ abstract class Maintenance {
                        $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;
@@ -672,7 +672,8 @@ abstract class Maintenance {
                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' );
                }
 
index bec11a0..189858c 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-if ( PHP_SAPI != 'cli' ) {
+if ( PHP_SAPI != 'cli' && PHP_SAPI != 'phpdbg' ) {
        die( "This script can only be run from the command line.\n" );
 }
 
index feaad12..89fc44b 100644 (file)
@@ -39,7 +39,7 @@
  */
 
 // Warning: Converting this to a Maintenance script may reduce performance.
-if ( PHP_SAPI != 'cli' ) {
+if ( PHP_SAPI != 'cli' && PHP_SAPI != 'phpdbg' ) {
        die( "This filter can only be run from the command line.\n" );
 }