From: Max Semenik Date: Fri, 19 Jan 2018 01:44:03 +0000 (-0800) Subject: Deprecate wfShellWikiCmd() X-Git-Tag: 1.31.0-rc.0~60^2 X-Git-Url: http://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=5cf4575ea34f2dd17a8f318d0412e0d8457db5d5 Deprecate wfShellWikiCmd() Bug: T184339 Change-Id: Ic86a451e0e9d609e06865a4969560d151efa844c --- diff --git a/RELEASE-NOTES-1.31 b/RELEASE-NOTES-1.31 index 052bc8223a..8ec3b7da42 100644 --- a/RELEASE-NOTES-1.31 +++ b/RELEASE-NOTES-1.31 @@ -337,6 +337,8 @@ changes to languages because of Phabricator reports. * Wikimedia\Rdbms\SavepointPostgres is deprecated. * The DO_MAINTENANCE constant is deprecated. RUN_MAINTENANCE_IF_MAIN should be used instead. +* The function wfShellWikiCmd() has been deprecated, use + MediaWiki\Shell::makeScriptCommand(). === Other changes in 1.31 === * Browser support for Internet Explorer 10 was lowered from Grade A to Grade C. diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 513f59346c..cd8ae46907 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -2377,6 +2377,8 @@ function wfInitShellLocale() { * Note that $parameters should be a flat array and an option with an argument * should consist of two consecutive items in the array (do not use "--option value"). * + * @deprecated since 1.31, use Shell::makeScriptCommand() + * * @param string $script MediaWiki cli script path * @param array $parameters Arguments and options to the script * @param array $options Associative array of options: diff --git a/includes/SiteConfiguration.php b/includes/SiteConfiguration.php index 2d1d961dec..6bd179a9da 100644 --- a/includes/SiteConfiguration.php +++ b/includes/SiteConfiguration.php @@ -20,6 +20,8 @@ * @file */ +use MediaWiki\Shell\Shell; + /** * This is a class for holding configuration settings, particularly for * multi-wiki sites. @@ -546,19 +548,21 @@ class SiteConfiguration { } else { $this->cfgCache[$wiki] = []; } - $retVal = 1; - $cmd = wfShellWikiCmd( + $result = Shell::makeScriptCommand( "$IP/maintenance/getConfiguration.php", [ '--wiki', $wiki, '--settings', implode( ' ', $settings ), - '--format', 'PHP' + '--format', 'PHP', ] - ); - // ulimit5.sh breaks this call - $data = trim( wfShellExec( $cmd, $retVal, [], [ 'memory' => 0, 'filesize' => 0 ] ) ); - if ( $retVal != 0 || !strlen( $data ) ) { - throw new MWException( "Failed to run getConfiguration.php." ); + ) + // limit.sh breaks this call + ->limits( [ 'memory' => 0, 'filesize' => 0 ] ) + ->execute(); + + $data = trim( $result->getStdout() ); + if ( $result->getExitCode() != 0 || !strlen( $data ) ) { + throw new MWException( "Failed to run getConfiguration.php: {$result->getStdout()}" ); } $res = unserialize( $data ); if ( !is_array( $res ) ) { diff --git a/includes/shell/Shell.php b/includes/shell/Shell.php index d57bf4fcdc..72107e94e2 100644 --- a/includes/shell/Shell.php +++ b/includes/shell/Shell.php @@ -22,6 +22,7 @@ namespace MediaWiki\Shell; +use Hooks; use MediaWiki\MediaWikiServices; /** @@ -212,4 +213,32 @@ class Shell { } return $retVal; } + + /** + * Generate a Command object to run a MediaWiki CLI script. + * Note that $parameters should be a flat array and an option with an argument + * should consist of two consecutive items in the array (do not use "--option value"). + * + * @param string $script MediaWiki CLI script with full path + * @param string[] $parameters Arguments and options to the script + * @param array $options Associative array of options: + * 'php': The path to the php executable + * 'wrapper': Path to a PHP wrapper to handle the maintenance script + * @return Command + */ + public static function makeScriptCommand( $script, $parameters, $options = [] ) { + global $wgPhpCli; + // Give site config file a chance to run the script in a wrapper. + // The caller may likely want to call wfBasename() on $script. + Hooks::run( 'wfShellWikiCmd', [ &$script, &$parameters, &$options ] ); + $cmd = isset( $options['php'] ) ? [ $options['php'] ] : [ $wgPhpCli ]; + if ( isset( $options['wrapper'] ) ) { + $cmd[] = $options['wrapper']; + } + $cmd[] = $script; + + return self::command( $cmd ) + ->params( $parameters ) + ->restrict( self::RESTRICT_DEFAULT & ~self::NO_LOCALSETTINGS ); + } } diff --git a/tests/phpunit/includes/shell/ShellTest.php b/tests/phpunit/includes/shell/ShellTest.php index 8162bc0299..bf46f44b0c 100644 --- a/tests/phpunit/includes/shell/ShellTest.php +++ b/tests/phpunit/includes/shell/ShellTest.php @@ -1,12 +1,14 @@ [ [ 'ls', null ], "'ls'" ], ]; } + + /** + * @covers \MediaWiki\Shell\Shell::makeScriptCommand + * @dataProvider provideMakeScriptCommand + * + * @param string $expected + * @param string $script + * @param string[] $parameters + * @param string[] $options + * @param callable|null $hook + */ + public function testMakeScriptCommand( $expected, + $script, + $parameters, + $options = [], + $hook = null + ) { + // Running tests under Vagrant involves MWMultiVersion that uses the below hook + $this->setMwGlobals( 'wgHooks', [] ); + + if ( $hook ) { + $this->setTemporaryHook( 'wfShellWikiCmd', $hook ); + } + + $command = Shell::makeScriptCommand( $script, $parameters, $options ); + $command->params( 'safe' ) + ->unsafeParams( 'unsafe' ); + + $this->assertType( Command::class, $command ); + + $wrapper = TestingAccessWrapper::newFromObject( $command ); + $this->assertEquals( $expected, $wrapper->command ); + $this->assertEquals( 0, $wrapper->restrictions & Shell::NO_LOCALSETTINGS ); + } + + public function provideMakeScriptCommand() { + global $wgPhpCli; + + return [ + [ + "'$wgPhpCli' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe", + 'maintenance/foobar.php', + [ 'bar\'"baz' ], + ], + [ + "'$wgPhpCli' 'changed.php' '--wiki=somewiki' 'bar'\\''\"baz' 'safe' unsafe", + 'maintenance/foobar.php', + [ 'bar\'"baz' ], + [], + function ( &$script, array &$parameters ) { + $script = 'changed.php'; + array_unshift( $parameters, '--wiki=somewiki' ); + } + ], + [ + "'/bin/perl' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe", + 'maintenance/foobar.php', + [ 'bar\'"baz' ], + [ 'php' => '/bin/perl' ], + ], + [ + "'$wgPhpCli' 'foobinize' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe", + 'maintenance/foobar.php', + [ 'bar\'"baz' ], + [ 'wrapper' => 'foobinize' ], + ], + ]; + } }