Rewrite TidySupport and add option --use-tidy-config
authorTim Starling <tstarling@wikimedia.org>
Thu, 7 Apr 2016 02:24:29 +0000 (12:24 +1000)
committerKunal Mehta <legoktm@member.fsf.org>
Tue, 12 Jul 2016 21:25:03 +0000 (14:25 -0700)
* 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

includes/parser/MWTidy.php
tests/parser/parserTest.inc
tests/parserTests.php
tests/phpunit/includes/parser/NewParserTest.php
tests/testHelpers.inc

index f281c25..a47e002 100644 (file)
@@ -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']}\"" );
                        }
index d602194..9adc3e6 100644 (file)
@@ -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 ) {
index 5e15694..f961dd4 100644 (file)
@@ -63,6 +63,8 @@ Options:
                    irrelevant differences. The accepted normalization functions
                    are: removeTbody to remove <tbody> 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;
index 8512572..c56626f 100644 (file)
@@ -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 ) {
index d04e0fc..1369406 100644 (file)
@@ -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;
        }
 }