From: Tim Starling Date: Thu, 7 Apr 2016 02:24:29 +0000 (+1000) Subject: Rewrite TidySupport and add option --use-tidy-config X-Git-Tag: 1.31.0-rc.0~6385 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/?a=commitdiff_plain;h=8a57d86ea7ec28ac12c60cde1436d688678e9340;p=lhc%2Fweb%2Fwiklou.git Rewrite TidySupport and add option --use-tidy-config * Have TidySupport provide $wgTidyConfig instead of the legacy globals * Add --use-tidy-config option to parserTests.php. This tells TidySupport to use the tidy configuration from LocalSettings.php instead of the traditional safe defaults. * Add a way for TidySupport to disable tidy via $wgTidyConfig, using driver=>disabled Change-Id: Ie76e68e2d5238d0a1aef49a1a815c0d1cd8bfdae --- diff --git a/includes/parser/MWTidy.php b/includes/parser/MWTidy.php index f281c25b3c..a47e002b96 100644 --- a/includes/parser/MWTidy.php +++ b/includes/parser/MWTidy.php @@ -135,6 +135,8 @@ class MWTidy { case 'Html5Internal': self::$instance = new MediaWiki\Tidy\Html5Internal( $config ); break; + case 'disabled': + return false; default: throw new MWException( "Invalid tidy driver: \"{$config['driver']}\"" ); } diff --git a/tests/parser/parserTest.inc b/tests/parser/parserTest.inc index d602194b0e..9adc3e6d8f 100644 --- a/tests/parser/parserTest.inc +++ b/tests/parser/parserTest.inc @@ -164,7 +164,7 @@ class ParserTest { $this->runParsoid = isset( $options['run-parsoid'] ); $this->djVuSupport = new DjVuSupport(); - $this->tidySupport = new TidySupport(); + $this->tidySupport = new TidySupport( isset( $options['use-tidy-config'] ) ); if ( !$this->tidySupport->isEnabled() ) { echo "Warning: tidy is not installed, skipping some tests\n"; } @@ -854,8 +854,6 @@ class ParserTest { * @return RequestContext */ private function setupGlobals( $opts = '', $config = '' ) { - global $IP; - # Find out values for some special options. $lang = self::getOptionValue( 'language', $opts, 'en' ); @@ -939,12 +937,8 @@ class ParserTest { 'wgDisableLangConversion' => false, 'wgDisableTitleConversion' => false, // Tidy options. - 'wgUseTidy' => isset( $opts['tidy'] ), - 'wgTidyConfig' => null, - 'wgDebugTidy' => false, - 'wgTidyConf' => $IP . '/includes/tidy/tidy.conf', - 'wgTidyOpts' => '', - 'wgTidyInternal' => $this->tidySupport->isInternal(), + 'wgUseTidy' => false, + 'wgTidyConfig' => isset( $opts['tidy'] ) ? $this->tidySupport->getConfig() : null ]; if ( $config ) { diff --git a/tests/parserTests.php b/tests/parserTests.php index 5e15694c4c..f961dd44ca 100644 --- a/tests/parserTests.php +++ b/tests/parserTests.php @@ -63,6 +63,8 @@ Options: irrelevant differences. The accepted normalization functions are: removeTbody to remove tags; and trimWhitespace to trim whitespace from the start and end of text nodes. + --use-tidy-config Use the wiki's Tidy configuration instead of known-good + defaults. --help Show this help message ENDS; diff --git a/tests/phpunit/includes/parser/NewParserTest.php b/tests/phpunit/includes/parser/NewParserTest.php index 8512572a25..c56626f47b 100644 --- a/tests/phpunit/includes/parser/NewParserTest.php +++ b/tests/phpunit/includes/parser/NewParserTest.php @@ -163,12 +163,8 @@ class NewParserTest extends MediaWikiTestCase { $this->djVuSupport = new DjVuSupport(); // Tidy support $this->tidySupport = new TidySupport(); - $tmpGlobals['wgTidyConfig'] = null; + $tmpGlobals['wgTidyConfig'] = $this->tidySupport->getConfig(); $tmpGlobals['wgUseTidy'] = false; - $tmpGlobals['wgDebugTidy'] = false; - $tmpGlobals['wgTidyConf'] = $IP . '/includes/tidy/tidy.conf'; - $tmpGlobals['wgTidyOpts'] = ''; - $tmpGlobals['wgTidyInternal'] = $this->tidySupport->isInternal(); $this->setMwGlobals( $tmpGlobals ); @@ -452,7 +448,8 @@ class NewParserTest extends MediaWikiTestCase { 'wgMathDirectory' => $uploadDir . '/math', 'wgDefaultLanguageVariant' => $variant, 'wgLinkHolderBatchSize' => $linkHolderBatchSize, - 'wgUseTidy' => isset( $opts['tidy'] ), + 'wgUseTidy' => false, + 'wgTidyConfig' => isset( $opts['tidy'] ) ? $this->tidySupport->getConfig() : null ]; if ( $config ) { diff --git a/tests/testHelpers.inc b/tests/testHelpers.inc index d04e0fcb54..13694063c1 100644 --- a/tests/testHelpers.inc +++ b/tests/testHelpers.inc @@ -837,30 +837,60 @@ class DjVuSupport { * Initialize and detect the tidy support */ class TidySupport { - private $internalTidy; - private $externalTidy; + private $enabled; + private $config; /** * Determine if there is a usable tidy. */ - public function __construct() { - global $wgTidyBin; - - $this->internalTidy = extension_loaded( 'tidy' ) && - class_exists( 'tidy' ) && !wfIsHHVM(); - - $this->externalTidy = is_executable( $wgTidyBin ) || - Installer::locateExecutableInDefaultPaths( [ $wgTidyBin ] ) - !== false; - } - - /** - * Returns true if we should use internal tidy. - * - * @return bool - */ - public function isInternal() { - return $this->internalTidy; + public function __construct( $useConfiguration = false ) { + global $IP, $wgUseTidy, $wgTidyBin, $wgTidyInternal, $wgTidyConfig, + $wgTidyConf, $wgTidyOpts; + + $this->enabled = true; + if ( $useConfiguration ) { + if ( $wgTidyConfig !== null ) { + $this->config = $wgTidyConfig; + } elseif ( $wgUseTidy ) { + $this->config = [ + 'tidyConfigFile' => $wgTidyConf, + 'debugComment' => false, + 'tidyBin' => $wgTidyBin, + 'tidyCommandLine' => $wgTidyOpts + ]; + if ( $wgTidyInternal ) { + $this->config['driver'] = wfIsHHVM() ? 'RaggettInternalHHVM' : 'RaggettInternalPHP'; + } else { + $this->config['driver'] = 'RaggettExternal'; + } + } else { + $this->enabled = false; + } + } else { + $this->config = [ + 'tidyConfigFile' => "$IP/includes/tidy/tidy.conf", + 'tidyCommandLine' => '', + ]; + if ( extension_loaded( 'tidy' ) && class_exists( 'tidy' ) ) { + $this->config['driver'] = wfIsHHVM() ? 'RaggettInternalHHVM' : 'RaggettInternalPHP'; + } else { + if ( is_executable( $wgTidyBin ) ) { + $this->config['driver'] = 'RaggettExternal'; + $this->config['tidyBin'] = $wgTidyBin; + } else { + $path = Installer::locateExecutableInDefaultPaths( $wgTidyBin ); + if ( $path !== false ) { + $this->config['driver'] = 'RaggettExternal'; + $this->config['tidyBin'] = $wgTidyBin; + } else { + $this->enabled = false; + } + } + } + } + if ( !$this->enabled ) { + $this->config = [ 'driver' => 'disabled' ]; + } } /** @@ -869,6 +899,10 @@ class TidySupport { * @return bool */ public function isEnabled() { - return $this->internalTidy || $this->externalTidy; + return $this->enabled; + } + + public function getConfig() { + return $this->config; } }