Seb35 has uploaded a new change for review. https://gerrit.wikimedia.org/r/302200
Change subject: Improve support for CLI and enforce the need of $_SERVER['HTTP_HOST'] ...................................................................... Improve support for CLI and enforce the need of $_SERVER['HTTP_HOST'] * Removed all possibility to change the host in MediaWikiFarm::getInstance($host): it was a bit inconsistent because after initialisation it was not possible to change the host, so remove any hope to do so. * Reorder operations in scripts/mwscript.php to be more consistent in both installations; fixed a bug when reading the parameters; fixed a bug because $argv and $_SERVER['argv'] were desynchronised. * More consistent unit tests with a single object creation during setUp instead of using MediaWikiFarm::getInstance(), which is also used in main MediaWiki. As a result it seems all combinaisons Web/CLI + Mono/Multi work, and all CLI programs (maintenance/*.php and tests/phpunit/phpunit.php). Change-Id: I22bef39cb8b05822e23b4e58773b0a64a1ba8a95 --- M docs/internals.rst M scripts/mwscript.php M src/MediaWikiFarm.php M tests/phpunit/MediaWikiFarmTest.php 4 files changed, 71 insertions(+), 59 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MediaWikiFarm refs/changes/00/302200/1 diff --git a/docs/internals.rst b/docs/internals.rst index a4ca3a8..3a1559a 100644 --- a/docs/internals.rst +++ b/docs/internals.rst @@ -16,9 +16,7 @@ MediaWikiFarm has an experimental support of unit tests with `PHPUnit`_. -Because of the very specific use case of PHPUnit called in CLI, it is recommended to set up a classical wiki installation with a classical LocalSettings.php with its own database and (fakely) load MediaWikiFarm with :code:`wfLoadExtension( 'MediaWikiFarm' )`. In fact this will not load the MediaWikiFarm executable code due to a lack of of support of extension registration (difficult problem in this case), so this will only load the class MediaWikiFarm needed to execute unit tests. - -Then, to execute PHPUnit, go to root MediaWiki directory and run :command:`php tests/phpunit/phpunit.php --group MediaWikiFarm`. You can add :command:`--debug` if you want more details. +Executing PHPUnit is like executing any other script in the farm: :command:`php extensions/MediaWikiFarm/scripts/mwscript.php --wiki=your.wiki.example.org tests/phpunit/phpunit.php --group MediaWikiFarm`. You can add :command:`--debug` if you want more details. .. _PHPUnit: http://www.phpunit.de diff --git a/scripts/mwscript.php b/scripts/mwscript.php index 08b4fa5..76b8e82 100644 --- a/scripts/mwscript.php +++ b/scripts/mwscript.php @@ -10,33 +10,10 @@ # Protect against web entry if( PHP_SAPI != 'cli' ) exit; - -# Configuration of the MediaWiki Farm -# The config file is in different location depending if it is a mono- or multi-version installation -if( is_file( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . '/includes/DefaultSettings.php' ) ) { - - $IP = dirname( dirname( dirname( dirname( __FILE__ ) ) ) ); - require "$IP/LocalSettings.php"; -} -else { - - $wgMediaWikiFarmCodeDir = dirname( dirname( dirname( __FILE__ ) ) ); - $wgMediaWikiFarmConfigDir = '/etc/mediawiki'; - $wgMediaWikiFarmCacheDir = '/tmp/mw-cache'; - require_once dirname( dirname( __FILE__ ) ) . '/config/MediaWikiFarmDirectories.php'; -} - - -# Include library -// @codingStandardsIgnoreStart MediaWiki.Usage.DirUsage.FunctionFound -require_once dirname( dirname( __FILE__ ) ) . '/src/MediaWikiFarm.php'; -// @codingStandardsIgnoreEnd - - /** * Get a command line parameter. * - * The parameter can be removed from the list, except the first parameter (script name). + * The parameter can be removed from the list. * * @param string|integer $name Parameter name or position (from 0). * @param bool $shift Remove this parameter from the list? @@ -70,11 +47,10 @@ # Search a positional parameter elseif( is_int( $name ) ) { - if( $name == 0 ) - $shift = false; if( $name >= $argc ) return null; $value = $argv[$name]; + $posArg = $name; $nbArgs = 1; } @@ -83,6 +59,8 @@ $argc -= $nbArgs; $argv = array_merge( array_slice( $argv, 0, $posArg ), array_slice( $argv, $posArg+$nbArgs ) ); + $_SERVER['argc'] = $argc; + $_SERVER['argv'] = $argv; } return $value; @@ -132,17 +110,48 @@ # Get wiki $mwfHost = mwfGetParam( 'wiki' ); +$_SERVER['HTTP_HOST'] = $mwfHost; +$_SERVER['SERVER_NAME'] = $mwfHost; if( is_null( $mwfHost ) ) mwfUsage(); # Get script -$mwfScript = mwfGetParam( 1 ); +$mwfScript = mwfGetParam( 1, false ); + +# Remove script name +mwfGetParam( 0 ); + if( is_null( $mwfScript ) ) mwfUsage(); -if( preg_match( '/^[a-zA-Z-]+$/', $mwfScript ) ) +if( preg_match( '/^[a-zA-Z-]+$/', $mwfScript ) ) { $mwfScript = 'maintenance/' . $mwfScript . '.php'; + $argv[0] = $mwfScript; +} + + +# Configuration of the MediaWiki Farm +# The config file is in different location depending if it is a mono- or multi-version installation +if( is_file( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . '/includes/DefaultSettings.php' ) ) { + + $IP = dirname( dirname( dirname( dirname( __FILE__ ) ) ) ); + require "$IP/LocalSettings.php"; +} +else { + + $wgMediaWikiFarmCodeDir = dirname( dirname( dirname( __FILE__ ) ) ); + $wgMediaWikiFarmConfigDir = '/etc/mediawiki'; + $wgMediaWikiFarmCacheDir = '/tmp/mw-cache'; + require_once dirname( dirname( __FILE__ ) ) . '/config/MediaWikiFarmDirectories.php'; +} + + +# Include library +// @codingStandardsIgnoreStart MediaWiki.Usage.DirUsage.FunctionFound +require_once dirname( dirname( __FILE__ ) ) . '/src/MediaWikiFarm.php'; +// @codingStandardsIgnoreEnd # Initialise the requested version -MediaWikiFarm::load( $mwfScript, $mwfHost ); +MediaWikiFarm::load( $mwfScript ); + # Display parameters $mwfVersion = MediaWikiFarm::getInstance()->params['version'] ? MediaWikiFarm::getInstance()->params['version'] : 'current'; @@ -155,8 +164,8 @@ PARAMS; + # Clean this script -$argv[0] = $mwfScript; unset( $mwfHost ); unset( $mwfScript ); unset( $mwfVersion ); @@ -170,6 +179,6 @@ } require $argv[0]; + # Update version after maintenance/update.php (the only case where another version is given before execution) MediaWikiFarm::getInstance()->updateVersionAfterMaintenance(); - diff --git a/src/MediaWikiFarm.php b/src/MediaWikiFarm.php index cba4ccf..810989f 100644 --- a/src/MediaWikiFarm.php +++ b/src/MediaWikiFarm.php @@ -56,15 +56,14 @@ * like `www/index.php`). * * @param string $entryPoint Name of the entry point, e.g. 'index.php', 'load.php'… - * @param string|null $host Requested host. * @return string $entryPoint Identical entry point as passed in input. */ - static function load( $entryPoint = '', $host = null ) { + static function load( $entryPoint = '' ) { global $wgMediaWikiFarm; # Initialise object - $wgMediaWikiFarm = self::getInstance( $host ); + $wgMediaWikiFarm = self::getInstance(); # Check existence if( !$wgMediaWikiFarm->checkExistence() ) { @@ -97,15 +96,25 @@ /** * Return (and if needed initialise) the unique object of type MediaWikiFarm. * - * @param string|null $host Requested host. + * @throws DomainException When there is no $_SERVER['HTTP_HOST'] nor $_SERVER['SERVER_NAME']. * @return MediaWikiFarm Singleton. */ - static function getInstance( $host = null ) { + static function getInstance() { global $wgMediaWikiFarmConfigDir, $wgMediaWikiFarmCodeDir, $wgMediaWikiFarmCacheDir; - if( self::$self == null ) - self::$self = new self( $host, $wgMediaWikiFarmConfigDir, $wgMediaWikiFarmCodeDir, $wgMediaWikiFarmCacheDir ); + # Object was already created + if( self::$self ) + return self::$self; + + # Detect the current host + # Warning: do not use $GLOBALS['_SERVER']['HTTP_HOST']: bug with PHP7: it is not initialised in early times of a script + if( array_key_exists( 'HTTP_HOST', $_SERVER ) || array_key_exists( 'SERVER_NAME', $_SERVER ) ) + $host = $_SERVER['HTTP_HOST'] ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']; + else throw new DomainException( 'Undefined host' ); + + # Create the object for this host + self::$self = new self( $host, $wgMediaWikiFarmConfigDir, $wgMediaWikiFarmCodeDir, $wgMediaWikiFarmCacheDir ); return self::$self; } @@ -266,7 +275,7 @@ */ function updateVersionAfterMaintenance() { - if( !array_key_exists( 'version', $this->params ) ) + if( !array_key_exists( 'version', $this->params ) || !$this->params['version'] ) return; $this->updateVersion( $this->params['version'] ); @@ -332,25 +341,18 @@ * 'unusable' becomes true. * It is a public method for testing needs, but it should never directly called in real code. * - * @param string|null $host Requested host. + * @param string $host Requested host. * @param string $configDir Configuration directory. * @param string|null $codeDir Code directory; if null, the current MediaWiki installation is used. * @param string|false|null $cacheDir Cache directory; if null, the cache is disabled. */ public function __construct( $host, $configDir, $codeDir = null, $cacheDir = null ) { - # Default value for $host - # Warning: do not use $GLOBALS['_SERVER']['HTTP_HOST']: bug with PHP7: it is not initialised in early times of a script - if( is_null( $host ) ) { - if( array_key_exists( 'HTTP_HOST', $_SERVER ) || array_key_exists( 'SERVER_NAME', $_SERVER ) ) - $host = $_SERVER['HTTP_HOST'] ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']; - } - # Default value for $cacheDir if( is_null( $cacheDir ) ) $cacheDir = '/tmp/mw-cache'; # Check parameters - if( !is_string( $host ) || $host == '' || + if( !is_string( $host ) || !(is_string( $configDir ) && is_dir( $configDir )) || !(is_null( $codeDir ) xor (is_string( $codeDir ) && is_dir( $codeDir ))) || !(is_string( $cacheDir ) xor $cacheDir === false) diff --git a/tests/phpunit/MediaWikiFarmTest.php b/tests/phpunit/MediaWikiFarmTest.php index 3c03a0c..b0993f3 100644 --- a/tests/phpunit/MediaWikiFarmTest.php +++ b/tests/phpunit/MediaWikiFarmTest.php @@ -6,15 +6,18 @@ */ class MediaWikiFarmTest extends MediaWikiTestCase { + /** @var MediaWikiFarm|null Test object. */ + protected $farm = null; + /** * Set up the default MediaWikiFarm object with a sample correct configuration file. */ - public function setUp() { + protected function setUp() { parent::setUp(); - $this->setMwGlobals( 'wgMediaWikiFarmConfigDir', dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'config' ); - MediaWikiFarm::getInstance( 'a.testfarm.example.org' ); + $wgMediaWikiFarmTestConfigDir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'config'; + $this->farm = new MediaWikiFarm( 'a.testfarm.example.org', $wgMediaWikiFarmTestConfigDir ); } /** @@ -23,8 +26,8 @@ public function testFailedConstruction() { $wgMediaWikiFarmBadConfigDir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'data'; - $wgMediaWikiFarm = new MediaWikiFarm( 'a.testfarm.example.org', $wgMediaWikiFarmBadConfigDir ); - $this->assertTrue( $wgMediaWikiFarm->unusable ); + $farm = new MediaWikiFarm( 'a.testfarm.example.org', $wgMediaWikiFarmBadConfigDir ); + $this->assertTrue( $farm->unusable ); } /** @@ -32,7 +35,7 @@ */ public function testSuccessfulConstruction() { - $this->assertFalse( MediaWikiFarm::getInstance()->unusable ); + $this->assertFalse( $this->farm->unusable ); } /** @@ -40,7 +43,7 @@ */ public function testFarmLocalSettingsFile() { - $this->assertEquals( MediaWikiFarm::getInstance()->getConfigFile(), dirname( dirname( dirname( __FILE__ ) ) ) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main.php' ); + $this->assertEquals( $this->farm->getConfigFile(), dirname( dirname( dirname( __FILE__ ) ) ) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main.php' ); } /** @@ -48,6 +51,6 @@ */ public function testVariables() { - $this->assertEquals( MediaWikiFarm::getInstance()->variables, array( 'wiki' => 'a' ) ); + $this->assertEquals( $this->farm->variables, array( 'wiki' => 'a' ) ); } } -- To view, visit https://gerrit.wikimedia.org/r/302200 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I22bef39cb8b05822e23b4e58773b0a64a1ba8a95 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/MediaWikiFarm Gerrit-Branch: master Gerrit-Owner: Seb35 <seb35wikipe...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits