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
This commit is contained in:
Kunal Mehta 2018-01-02 22:51:44 -08:00
parent 2e248f0bb2
commit 251a0b97e5
13 changed files with 30 additions and 14 deletions

View 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;

View 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 === '*' ) {

View 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;

View 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 );

View 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

View 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();

View 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'] ) ) {

View 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';
}

View 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'] ) ?

View 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 ) {

View 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' );
}

View 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" );
}

View 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" );
}