Remove dead argument in LinkSearchPage
[lhc/web/wiklou.git] / includes / parser / MWTidy.php
index a108d2b..e29ee88 100644 (file)
@@ -40,13 +40,10 @@ class MWTidyWrapper {
         */
        protected $mTokens;
 
-       protected $mUniqPrefix;
-
        protected $mMarkerIndex;
 
        public function __construct() {
                $this->mTokens = null;
-               $this->mUniqPrefix = null;
        }
 
        /**
@@ -55,8 +52,6 @@ class MWTidyWrapper {
         */
        public function getWrapped( $text ) {
                $this->mTokens = new ReplacementArray;
-               $this->mUniqPrefix = "\x7fUNIQ" .
-                       dechex( mt_rand( 0, 0x7fffffff ) ) . dechex( mt_rand( 0, 0x7fffffff ) );
                $this->mMarkerIndex = 0;
 
                // Replace <mw:editsection> elements with placeholders
@@ -65,7 +60,9 @@ class MWTidyWrapper {
                // ...and <mw:toc> markers
                $wrappedtext = preg_replace_callback( '/\<\\/?mw:toc\>/',
                        array( &$this, 'replaceCallback' ), $wrappedtext );
-
+               // ... and <math> tags
+               $wrappedtext = preg_replace_callback( '/\<math(.*?)\<\\/math\>/s',
+                       array( &$this, 'replaceCallback' ), $wrappedtext );
                // Modify inline Microdata <link> and <meta> elements so they say <html-link> and <html-meta> so
                // we can trick Tidy into not stripping them out by including them in tidy's new-empty-tags config
                $wrappedtext = preg_replace( '!<(link|meta)([^>]*?)(/{0,1}>)!', '<html-$1$2$3', $wrappedtext );
@@ -84,7 +81,7 @@ class MWTidyWrapper {
         * @return string
         */
        public function replaceCallback( $m ) {
-               $marker = "{$this->mUniqPrefix}-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX;
+               $marker = Parser::MARKER_PREFIX . "-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX;
                $this->mMarkerIndex++;
                $this->mTokens->setPair( $marker, $m[0] );
                return $marker;
@@ -125,17 +122,11 @@ class MWTidy {
         * @return string Corrected HTML output
         */
        public static function tidy( $text ) {
-               global $wgTidyInternal;
-
                $wrapper = new MWTidyWrapper;
                $wrappedtext = $wrapper->getWrapped( $text );
 
                $retVal = null;
-               if ( $wgTidyInternal ) {
-                       $correctedtext = self::execInternalTidy( $wrappedtext, false, $retVal );
-               } else {
-                       $correctedtext = self::execExternalTidy( $wrappedtext, false, $retVal );
-               }
+               $correctedtext = self::clean( $wrappedtext, false, $retVal );
 
                if ( $retVal < 0 ) {
                        wfDebug( "Possible tidy configuration error!\n" );
@@ -158,16 +149,34 @@ class MWTidy {
         * @return bool Whether the HTML is valid
         */
        public static function checkErrors( $text, &$errorStr = null ) {
+               $retval = 0;
+               $errorStr = self::clean( $text, true, $retval );
+               return ( $retval < 0 && $errorStr == '' ) || $retval == 0;
+       }
+
+       /**
+        * Perform a clean/repair operation
+        * @param string $text HTML to check
+        * @param bool $stderr Whether to read result from STDERR rather than STDOUT
+        * @param int &$retval Exit code (-1 on internal error)
+        * @return null|string
+        * @throws MWException
+        */
+       private static function clean( $text, $stderr = false, &$retval = null ) {
                global $wgTidyInternal;
 
-               $retval = 0;
                if ( $wgTidyInternal ) {
-                       $errorStr = self::execInternalTidy( $text, true, $retval );
+                       if ( wfIsHHVM() ) {
+                               if ( $stderr ) {
+                                       throw new MWException( __METHOD__ . ": error text return from HHVM tidy is not supported" );
+                               }
+                               return self::hhvmClean( $text, $retval );
+                       } else {
+                               return self::phpClean( $text, $stderr, $retval );
+                       }
                } else {
-                       $errorStr = self::execExternalTidy( $text, true, $retval );
+                       return self::externalClean( $text, $stderr, $retval );
                }
-
-               return ( $retval < 0 && $errorStr == '' ) || $retval == 0;
        }
 
        /**
@@ -179,9 +188,8 @@ class MWTidy {
         * @param int &$retval Exit code (-1 on internal error)
         * @return string|null
         */
-       private static function execExternalTidy( $text, $stderr = false, &$retval = null ) {
+       private static function externalClean( $text, $stderr = false, &$retval = null ) {
                global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
-               wfProfileIn( __METHOD__ );
 
                $cleansource = '';
                $opts = ' -utf8';
@@ -233,7 +241,6 @@ class MWTidy {
                        $cleansource = null;
                }
 
-               wfProfileOut( __METHOD__ );
                return $cleansource;
        }
 
@@ -246,15 +253,15 @@ class MWTidy {
         * @param int &$retval Exit code (-1 on internal error)
         * @return string|null
         */
-       private static function execInternalTidy( $text, $stderr = false, &$retval = null ) {
+       private static function phpClean( $text, $stderr = false, &$retval = null ) {
                global $wgTidyConf, $wgDebugTidy;
-               wfProfileIn( __METHOD__ );
 
-               if ( !class_exists( 'tidy' ) ) {
+               if ( ( !wfIsHHVM() && !class_exists( 'tidy' ) ) ||
+                       ( wfIsHHVM() && !function_exists( 'tidy_repair_string' ) )
+               ) {
                        wfWarn( "Unable to load internal tidy class." );
                        $retval = -1;
 
-                       wfProfileOut( __METHOD__ );
                        return null;
                }
 
@@ -263,8 +270,6 @@ class MWTidy {
 
                if ( $stderr ) {
                        $retval = $tidy->getStatus();
-
-                       wfProfileOut( __METHOD__ );
                        return $tidy->errorBuffer;
                }
 
@@ -283,7 +288,31 @@ class MWTidy {
                        }
                }
 
-               wfProfileOut( __METHOD__ );
+               return $cleansource;
+       }
+
+       /**
+        * Use the tidy extension for HHVM from
+        * https://github.com/wikimedia/mediawiki-php-tidy
+        *
+        * This currently does not support the object-oriented interface, but
+        * tidy_repair_string() can be used for the most common tasks.
+        *
+        * @param string $text HTML to check
+        * @param int &$retval Exit code (-1 on internal error)
+        * @return string|null
+        */
+       private static function hhvmClean( $text, &$retval ) {
+               global $wgTidyConf;
+
+               $cleansource = tidy_repair_string( $text, $wgTidyConf, 'utf8' );
+               if ( $cleansource === false ) {
+                       $cleansource = null;
+                       $retval = -1;
+               } else {
+                       $retval = 0;
+               }
+
                return $cleansource;
        }
 }