3 * HTML validation and correction
9 * Class to interact with HTML tidy
11 * Either the external tidy program or the in-process tidy extension
12 * will be used depending on availability. Override the default
13 * $wgTidyInternal setting to disable the internal if it's not working.
20 * Interface with html tidy, used if $wgUseTidy = true.
21 * If tidy isn't able to correct the markup, the original will be
22 * returned in all its glory with a warning comment appended.
24 * @param $text String: hideous HTML input
25 * @return String: corrected HTML output
27 public static function tidy( $text ) {
28 global $wgTidyInternal;
30 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
31 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
32 '<head><title>test</title></head><body>'.$text.'</body></html>';
34 # Tidy is known to clobber tabs; convert them to entities
35 $wrappedtext = str_replace( "\t", '	', $wrappedtext );
37 if( $wgTidyInternal ) {
38 $correctedtext = self
::execInternalTidy( $wrappedtext );
40 $correctedtext = self
::execExternalTidy( $wrappedtext );
42 if( is_null( $correctedtext ) ) {
43 wfDebug( "Tidy error detected!\n" );
44 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
47 # Convert the tabs back from entities
48 $correctedtext = str_replace( '	', "\t", $correctedtext );
50 return $correctedtext;
54 * Check HTML for errors, used if $wgValidateAllHtml = true.
57 * @param &$errorStr String: return the error string
58 * @return Boolean: whether the HTML is valid
60 public static function checkErrors( $text, &$errorStr = null ) {
61 global $wgTidyInternal;
64 if( $wgTidyInternal ) {
65 $errorStr = self
::execInternalTidy( $text, true, $retval );
67 $errorStr = self
::execExternalTidy( $text, true, $retval );
69 return ( $retval < 0 && $errorStr == '' ) ||
$retval == 0;
73 * Spawn an external HTML tidy process and get corrected markup back from it.
74 * Also called in OutputHandler.php for full page validation
76 * @param $text String: HTML to check
77 * @param $stderr Boolean: Whether to read from STDERR rather than STDOUT
78 * @param &$retval Exit code (-1 on internal error)
79 * @return mixed String or null
81 private static function execExternalTidy( $text, $stderr = false, &$retval = null ) {
82 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
83 wfProfileIn( __METHOD__
);
89 $descriptorspec = array(
90 0 => array( 'pipe', 'r' ),
91 1 => array( 'file', wfGetNull(), 'a' ),
92 2 => array( 'pipe', 'w' )
95 $descriptorspec = array(
96 0 => array( 'pipe', 'r' ),
97 1 => array( 'pipe', 'w' ),
98 2 => array( 'file', wfGetNull(), 'a' )
102 $readpipe = $stderr ?
2 : 1;
105 if( function_exists( 'proc_open' ) ) {
106 $process = proc_open( "$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes );
107 if ( is_resource( $process ) ) {
108 // Theoretically, this style of communication could cause a deadlock
109 // here. If the stdout buffer fills up, then writes to stdin could
110 // block. This doesn't appear to happen with tidy, because tidy only
111 // writes to stdout after it's finished reading from stdin. Search
112 // for tidyParseStdin and tidySaveStdout in console/tidy.c
113 fwrite( $pipes[0], $text );
115 while ( !feof( $pipes[$readpipe] ) ) {
116 $cleansource .= fgets( $pipes[$readpipe], 1024 );
118 fclose( $pipes[$readpipe] );
119 $retval = proc_close( $process );
127 wfProfileOut( __METHOD__
);
129 if( !$stderr && $cleansource == '' && $text != '' ) {
130 // Some kind of error happened, so we couldn't get the corrected text.
131 // Just give up; we'll use the source text and append a warning.
139 * Use the HTML tidy PECL extension to use the tidy library in-process,
140 * saving the overhead of spawning a new process.
142 * 'pear install tidy' should be able to compile the extension module.
144 private static function execInternalTidy( $text, $stderr = false, &$retval = null ) {
145 global $wgTidyConf, $wgDebugTidy;
146 wfProfileIn( __METHOD__
);
149 $tidy->parseString( $text, $wgTidyConf, 'utf8' );
152 $retval = $tidy->getStatus();
153 return $tidy->errorBuffer
;
155 $tidy->cleanRepair();
156 $retval = $tidy->getStatus();
158 // 2 is magic number for fatal error
159 // http://www.php.net/manual/en/function.tidy-get-status.php
162 $cleansource = tidy_get_output( $tidy );
164 if ( $wgDebugTidy && $retval > 0 ) {
165 $cleansource .= "<!--\nTidy reports:\n" .
166 str_replace( '-->', '-->', $tidy->errorBuffer
) .
170 wfProfileOut( __METHOD__
);