From: Kunal Mehta Date: Wed, 3 Jan 2018 06:51:44 +0000 (-0800) Subject: Treat phpdbg as run from the command line when checking PHP_SAPI X-Git-Tag: 1.31.0-rc.0~965^2 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=251a0b97e5bb8711bac1784e90f9b7d61377e7da Treat phpdbg as run from the command line when checking PHP_SAPI 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 --- diff --git a/includes/ForkController.php b/includes/ForkController.php index 2dde17be08..cc16964e1f 100644 --- a/includes/ForkController.php +++ b/includes/ForkController.php @@ -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; diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 523a0f93a6..7b770900fa 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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 === '*' ) { diff --git a/includes/debug/logger/monolog/WikiProcessor.php b/includes/debug/logger/monolog/WikiProcessor.php index e39a2c300a..db5b9bf669 100644 --- a/includes/debug/logger/monolog/WikiProcessor.php +++ b/includes/debug/logger/monolog/WikiProcessor.php @@ -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; diff --git a/includes/exception/MWExceptionHandler.php b/includes/exception/MWExceptionHandler.php index f3d61f0cdd..d863a2bb58 100644 --- a/includes/exception/MWExceptionHandler.php +++ b/includes/exception/MWExceptionHandler.php @@ -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 ); diff --git a/includes/libs/lockmanager/LockManager.php b/includes/libs/lockmanager/LockManager.php index a6257bfda9..6b3cfb44ae 100644 --- a/includes/libs/lockmanager/LockManager.php +++ b/includes/libs/lockmanager/LockManager.php @@ -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 diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php index 15e02ad085..0418dfc078 100644 --- a/includes/libs/rdbms/database/Database.php +++ b/includes/libs/rdbms/database/Database.php @@ -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(); diff --git a/includes/libs/rdbms/loadbalancer/LoadBalancer.php b/includes/libs/rdbms/loadbalancer/LoadBalancer.php index 591e287e0a..9a7f7e1908 100644 --- a/includes/libs/rdbms/loadbalancer/LoadBalancer.php +++ b/includes/libs/rdbms/loadbalancer/LoadBalancer.php @@ -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'] ) ) { diff --git a/includes/profiler/Profiler.php b/includes/profiler/Profiler.php index 4da7976d82..81449be1f6 100644 --- a/includes/profiler/Profiler.php +++ b/includes/profiler/Profiler.php @@ -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'; } diff --git a/includes/profiler/output/ProfilerOutputText.php b/includes/profiler/output/ProfilerOutputText.php index dc24f18102..100304ffb2 100644 --- a/includes/profiler/output/ProfilerOutputText.php +++ b/includes/profiler/output/ProfilerOutputText.php @@ -59,7 +59,7 @@ class ProfilerOutputText extends ProfilerOutput { ); $contentType = $this->collector->getContentType(); - if ( PHP_SAPI === 'cli' ) { + if ( wfIsCLI() ) { print "\n"; } elseif ( $contentType === 'text/html' ) { $visible = isset( $this->params['visible'] ) ? diff --git a/includes/utils/UIDGenerator.php b/includes/utils/UIDGenerator.php index 736109b49a..68ef57ae24 100644 --- a/includes/utils/UIDGenerator.php +++ b/includes/utils/UIDGenerator.php @@ -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 ) { diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index 07f547f48a..68c51a8839 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -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' ); } diff --git a/maintenance/generateLocalAutoload.php b/maintenance/generateLocalAutoload.php index bec11a0de1..189858c5af 100644 --- a/maintenance/generateLocalAutoload.php +++ b/maintenance/generateLocalAutoload.php @@ -1,6 +1,6 @@