3 * HTML validation and correction
9 * Class used to hide mw:editsection tokens from Tidy so that it doesn't break them
10 * or break on them. This is a bit of a hack for now, but hopefully in the future
11 * we may create a real postprocessor or something that will replace this.
12 * It's called wrapper because for now it basically takes over MWTidy::tidy's task
13 * of wrapping the text in a xhtml block
15 * This re-uses some of the parser's UNIQ tricks, though some of it is private so it's
16 * duplicated. Perhaps we should create an abstract marker hiding class.
21 * @var ReplacementArray
25 protected $mUniqPrefix;
27 protected $mMarkerIndex;
29 public function __construct() {
30 $this->mTokens
= null;
31 $this->mUniqPrefix
= null;
38 public function getWrapped( $text ) {
39 $this->mTokens
= new ReplacementArray
;
40 $this->mUniqPrefix
= "\x7fUNIQ" .
41 dechex( mt_rand( 0, 0x7fffffff ) ) . dechex( mt_rand( 0, 0x7fffffff ) );
42 $this->mMarkerIndex
= 0;
44 $wrappedtext = preg_replace_callback( ParserOutput
::EDITSECTION_REGEX
,
45 array( &$this, 'replaceEditSectionLinksCallback' ), $text );
47 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
48 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
49 '<head><title>test</title></head><body>'.$wrappedtext.'</body></html>';
59 function replaceEditSectionLinksCallback( $m ) {
60 $marker = "{$this->mUniqPrefix}-item-{$this->mMarkerIndex}" . Parser
::MARKER_SUFFIX
;
61 $this->mMarkerIndex++
;
62 $this->mTokens
->setPair( $marker, $m[0] );
70 public function postprocess( $text ) {
71 return $this->mTokens
->replace( $text );
77 * Class to interact with HTML tidy
79 * Either the external tidy program or the in-process tidy extension
80 * will be used depending on availability. Override the default
81 * $wgTidyInternal setting to disable the internal if it's not working.
87 * Interface with html tidy, used if $wgUseTidy = true.
88 * If tidy isn't able to correct the markup, the original will be
89 * returned in all its glory with a warning comment appended.
91 * @param $text String: hideous HTML input
92 * @return String: corrected HTML output
94 public static function tidy( $text ) {
95 global $wgTidyInternal;
97 $wrapper = new MWTidyWrapper
;
98 $wrappedtext = $wrapper->getWrapped( $text );
101 if ( $wgTidyInternal ) {
102 $correctedtext = self
::execInternalTidy( $wrappedtext, false, $retVal );
104 $correctedtext = self
::execExternalTidy( $wrappedtext, false, $retVal );
108 wfDebug( "Possible tidy configuration error!\n" );
109 return $text . "\n<!-- Tidy was unable to run -->\n";
110 } elseif ( is_null( $correctedtext ) ) {
111 wfDebug( "Tidy error detected!\n" );
112 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
115 $correctedtext = $wrapper->postprocess( $correctedtext ); // restore any hidden tokens
117 return $correctedtext;
120 function replaceEditSectionLinksCallback( $m ) {
125 * Check HTML for errors, used if $wgValidateAllHtml = true.
127 * @param $text String
128 * @param &$errorStr String: return the error string
129 * @return Boolean: whether the HTML is valid
131 public static function checkErrors( $text, &$errorStr = null ) {
132 global $wgTidyInternal;
135 if( $wgTidyInternal ) {
136 $errorStr = self
::execInternalTidy( $text, true, $retval );
138 $errorStr = self
::execExternalTidy( $text, true, $retval );
141 return ( $retval < 0 && $errorStr == '' ) ||
$retval == 0;
145 * Spawn an external HTML tidy process and get corrected markup back from it.
146 * Also called in OutputHandler.php for full page validation
148 * @param $text String: HTML to check
149 * @param $stderr Boolean: Whether to read result from STDERR rather than STDOUT
150 * @param &$retval Exit code (-1 on internal error)
151 * @return mixed String or null
153 private static function execExternalTidy( $text, $stderr = false, &$retval = null ) {
154 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
155 wfProfileIn( __METHOD__
);
161 $descriptorspec = array(
162 0 => array( 'pipe', 'r' ),
163 1 => array( 'file', wfGetNull(), 'a' ),
164 2 => array( 'pipe', 'w' )
167 $descriptorspec = array(
168 0 => array( 'pipe', 'r' ),
169 1 => array( 'pipe', 'w' ),
170 2 => array( 'file', wfGetNull(), 'a' )
174 $readpipe = $stderr ?
2 : 1;
177 $process = proc_open(
178 "$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes );
180 if ( is_resource( $process ) ) {
181 // Theoretically, this style of communication could cause a deadlock
182 // here. If the stdout buffer fills up, then writes to stdin could
183 // block. This doesn't appear to happen with tidy, because tidy only
184 // writes to stdout after it's finished reading from stdin. Search
185 // for tidyParseStdin and tidySaveStdout in console/tidy.c
186 fwrite( $pipes[0], $text );
188 while ( !feof( $pipes[$readpipe] ) ) {
189 $cleansource .= fgets( $pipes[$readpipe], 1024 );
191 fclose( $pipes[$readpipe] );
192 $retval = proc_close( $process );
194 wfWarn( "Unable to start external tidy process" );
198 if ( !$stderr && $cleansource == '' && $text != '' ) {
199 // Some kind of error happened, so we couldn't get the corrected text.
200 // Just give up; we'll use the source text and append a warning.
204 wfProfileOut( __METHOD__
);
209 * Use the HTML tidy extension to use the tidy library in-process,
210 * saving the overhead of spawning a new process.
212 * @param $text String: HTML to check
213 * @param $stderr Boolean: Whether to read result from error status instead of output
214 * @param &$retval Exit code (-1 on internal error)
215 * @return mixed String or null
217 private static function execInternalTidy( $text, $stderr = false, &$retval = null ) {
218 global $wgTidyConf, $wgDebugTidy;
219 wfProfileIn( __METHOD__
);
221 if ( !MWInit
::classExists( 'tidy' ) ) {
222 wfWarn( "Unable to load internal tidy class." );
228 $tidy->parseString( $text, $wgTidyConf, 'utf8' );
231 $retval = $tidy->getStatus();
233 wfProfileOut( __METHOD__
);
234 return $tidy->errorBuffer
;
236 $tidy->cleanRepair();
237 $retval = $tidy->getStatus();
238 if ( $retval == 2 ) {
239 // 2 is magic number for fatal error
240 // http://www.php.net/manual/en/function.tidy-get-status.php
243 $cleansource = tidy_get_output( $tidy );
244 if ( $wgDebugTidy && $retval > 0 ) {
245 $cleansource .= "<!--\nTidy reports:\n" .
246 str_replace( '-->', '-->', $tidy->errorBuffer
) .
251 wfProfileOut( __METHOD__
);