X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2Fparser%2FMWTidy.php;h=807842b61c346b2cd37b4493e05faac32acf2414;hp=d446ccf66cf7ec0132b4f566a08d902d793a19f1;hb=ee734d0d3c7533bd9a690dbd71f5151da55c32ad;hpb=96a8ab6928b3e0be884c33eed8a1febf6fc4c31e diff --git a/includes/parser/MWTidy.php b/includes/parser/MWTidy.php index d446ccf66c..807842b61c 100644 --- a/includes/parser/MWTidy.php +++ b/includes/parser/MWTidy.php @@ -21,93 +21,6 @@ * @ingroup Parser */ -/** - * Class used to hide mw:editsection tokens from Tidy so that it doesn't break them - * or break on them. This is a bit of a hack for now, but hopefully in the future - * we may create a real postprocessor or something that will replace this. - * It's called wrapper because for now it basically takes over MWTidy::tidy's task - * of wrapping the text in a xhtml block - * - * This re-uses some of the parser's UNIQ tricks, though some of it is private so it's - * duplicated. Perhaps we should create an abstract marker hiding class. - * - * @ingroup Parser - */ -class MWTidyWrapper { - - /** - * @var ReplacementArray - */ - protected $mTokens; - - protected $mUniqPrefix; - - protected $mMarkerIndex; - - public function __construct() { - $this->mTokens = null; - $this->mUniqPrefix = null; - } - - /** - * @param string $text - * @return string - */ - 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 elements with placeholders - $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX, - array( &$this, 'replaceCallback' ), $text ); - // ...and markers - $wrappedtext = preg_replace_callback( '/\<\\/?mw:toc\>/', - array( &$this, 'replaceCallback' ), $wrappedtext ); - // ... and tags - $wrappedtext = preg_replace_callback( '/\/s', - array( &$this, 'replaceCallback' ), $wrappedtext ); - // Modify inline Microdata and elements so they say and 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}>)!', '' . - 'test' . $wrappedtext . ''; - - return $wrappedtext; - } - - /** - * @param array $m - * - * @return string - */ - public function replaceCallback( $m ) { - $marker = "{$this->mUniqPrefix}-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX; - $this->mMarkerIndex++; - $this->mTokens->setPair( $marker, $m[0] ); - return $marker; - } - - /** - * @param string $text - * @return string - */ - public function postprocess( $text ) { - // Revert back to <{link,meta}> - $text = preg_replace( '!]*?)(/{0,1}>)!', '<$1$2$3', $text ); - - // Restore the contents of placeholder tokens - $text = $this->mTokens->replace( $text ); - - return $text; - } - -} - /** * Class to interact with HTML tidy * @@ -118,32 +31,24 @@ class MWTidyWrapper { * @ingroup Parser */ class MWTidy { + private static $instance; + /** - * Interface with html tidy, used if $wgUseTidy = true. + * Interface with html tidy. * If tidy isn't able to correct the markup, the original will be * returned in all its glory with a warning comment appended. * - * @param string $text Hideous HTML input + * @param string $text HTML input fragment. This should not contain a + * or tag. * @return string Corrected HTML output */ public static function tidy( $text ) { - $wrapper = new MWTidyWrapper; - $wrappedtext = $wrapper->getWrapped( $text ); - - $retVal = null; - $correctedtext = self::clean( $wrappedtext, false, $retVal ); - - if ( $retVal < 0 ) { - wfDebug( "Possible tidy configuration error!\n" ); - return $text . "\n\n"; - } elseif ( is_null( $correctedtext ) ) { - wfDebug( "Tidy error detected!\n" ); - return $text . "\n\n"; + $driver = self::singleton(); + if ( !$driver ) { + throw new MWException( __METHOD__. + ': tidy is disabled, caller should have checked MWTidy::isEnabled()' ); } - - $correctedtext = $wrapper->postprocess( $correctedtext ); // restore any hidden tokens - - return $correctedtext; + return $driver->tidy( $text ); } /** @@ -154,170 +59,80 @@ 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; + $driver = self::singleton(); + if ( !$driver ) { + throw new MWException( __METHOD__. + ': tidy is disabled, caller should have checked MWTidy::isEnabled()' ); + } + if ( $driver->supportsValidate() ) { + return $driver->validate( $text, $errorStr ); + } else { + throw new MWException( __METHOD__ . ": error text return from HHVM tidy is not supported" ); + } } - /** - * 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; + public static function isEnabled() { + return self::singleton() !== false; + } - if ( $wgTidyInternal ) { - if ( wfIsHHVM() ) { - if ( $stderr ) { - throw new MWException( __METHOD__ . ": error text return from HHVM tidy is not supported" ); + protected static function singleton() { + global $wgUseTidy, $wgTidyInternal, $wgTidyConf, $wgDebugTidy, $wgTidyConfig, + $wgTidyBin, $wgTidyOpts; + + if ( self::$instance === null ) { + if ( $wgTidyConfig !== null ) { + $config = $wgTidyConfig; + } elseif ( $wgUseTidy ) { + // b/c configuration + $config = array( + 'tidyConfigFile' => $wgTidyConf, + 'debugComment' => $wgDebugTidy, + 'tidyBin' => $wgTidyBin, + 'tidyCommandLine' => $wgTidyOpts ); + if ( $wgTidyInternal ) { + if ( wfIsHHVM() ) { + $config['driver'] = 'RaggettInternalHHVM'; + } else { + $config['driver'] = 'RaggettInternalPHP'; + } + } else { + $config['driver'] = 'RaggettExternal'; } - return self::hhvmClean( $text, $retval ); } else { - return self::phpClean( $text, $stderr, $retval ); + return false; } - } else { - return self::externalClean( $text, $stderr, $retval ); - } - } - - /** - * Spawn an external HTML tidy process and get corrected markup back from it. - * Also called in OutputHandler.php for full page validation - * - * @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 string|null - */ - private static function externalClean( $text, $stderr = false, &$retval = null ) { - global $wgTidyConf, $wgTidyBin, $wgTidyOpts; - - $cleansource = ''; - $opts = ' -utf8'; - - if ( $stderr ) { - $descriptorspec = array( - 0 => array( 'pipe', 'r' ), - 1 => array( 'file', wfGetNull(), 'a' ), - 2 => array( 'pipe', 'w' ) - ); - } else { - $descriptorspec = array( - 0 => array( 'pipe', 'r' ), - 1 => array( 'pipe', 'w' ), - 2 => array( 'file', wfGetNull(), 'a' ) - ); - } - - $readpipe = $stderr ? 2 : 1; - $pipes = array(); - - $process = proc_open( - "$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes ); - - //NOTE: At least on linux, the process will be created even if tidy is not installed. - // This means that missing tidy will be treated as a validation failure. - - if ( is_resource( $process ) ) { - // Theoretically, this style of communication could cause a deadlock - // here. If the stdout buffer fills up, then writes to stdin could - // block. This doesn't appear to happen with tidy, because tidy only - // writes to stdout after it's finished reading from stdin. Search - // for tidyParseStdin and tidySaveStdout in console/tidy.c - fwrite( $pipes[0], $text ); - fclose( $pipes[0] ); - while ( !feof( $pipes[$readpipe] ) ) { - $cleansource .= fgets( $pipes[$readpipe], 1024 ); + switch ( $config['driver'] ) { + case 'RaggettInternalHHVM': + self::$instance = new MediaWiki\Tidy\RaggettInternalHHVM( $config ); + break; + case 'RaggettInternalPHP': + self::$instance = new MediaWiki\Tidy\RaggettInternalPHP( $config ); + break; + case 'RaggettExternal': + self::$instance = new MediaWiki\Tidy\RaggettExternal( $config ); + break; + case 'Html5Depurate': + self::$instance = new MediaWiki\Tidy\Html5Depurate( $config ); + break; + default: + throw new MWException( "Invalid tidy driver: \"{$config['driver']}\"" ); } - fclose( $pipes[$readpipe] ); - $retval = proc_close( $process ); - } else { - wfWarn( "Unable to start external tidy process" ); - $retval = -1; } - - if ( !$stderr && $cleansource == '' && $text != '' ) { - // Some kind of error happened, so we couldn't get the corrected text. - // Just give up; we'll use the source text and append a warning. - $cleansource = null; - } - - return $cleansource; + return self::$instance; } /** - * Use the HTML tidy extension to use the tidy library in-process, - * saving the overhead of spawning a new process. - * - * @param string $text HTML to check - * @param bool $stderr Whether to read result from error status instead of output - * @param int &$retval Exit code (-1 on internal error) - * @return string|null + * Set the driver to be used. This is for testing. + * @param TidyDriverBase|false|null $instance */ - private static function phpClean( $text, $stderr = false, &$retval = null ) { - global $wgTidyConf, $wgDebugTidy; - - if ( ( !wfIsHHVM() && !class_exists( 'tidy' ) ) || - ( wfIsHHVM() && !function_exists( 'tidy_repair_string' ) ) - ) { - wfWarn( "Unable to load internal tidy class." ); - $retval = -1; - - return null; - } - - $tidy = new tidy; - $tidy->parseString( $text, $wgTidyConf, 'utf8' ); - - if ( $stderr ) { - $retval = $tidy->getStatus(); - return $tidy->errorBuffer; - } - - $tidy->cleanRepair(); - $retval = $tidy->getStatus(); - if ( $retval == 2 ) { - // 2 is magic number for fatal error - // http://www.php.net/manual/en/function.tidy-get-status.php - $cleansource = null; - } else { - $cleansource = tidy_get_output( $tidy ); - if ( $wgDebugTidy && $retval > 0 ) { - $cleansource .= "', '-->', $tidy->errorBuffer ) . - "\n-->"; - } - } - - return $cleansource; + public static function setInstance( $instance ) { + self::$instance = $instance; } /** - * 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 + * Destroy the current singleton instance */ - 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; + public static function destroySingleton() { + self::$instance = null; } }