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 if( $wgTidyInternal ) {
35 $correctedtext = self
::execInternalTidy( $wrappedtext );
37 $correctedtext = self
::execExternalTidy( $wrappedtext );
39 if( is_null( $correctedtext ) ) {
40 wfDebug( "Tidy error detected!\n" );
41 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
44 return $correctedtext;
48 * Check HTML for errors, used if $wgValidateAllHtml = true.
51 * @param &$errorStr String: return the error string
52 * @return Boolean: whether the HTML is valid
54 public static function checkErrors( $text, &$errorStr = null ) {
55 global $wgTidyInternal;
58 if( $wgTidyInternal ) {
59 $errorStr = self
::execInternalTidy( $text, true, $retval );
61 $errorStr = self
::execExternalTidy( $text, true, $retval );
63 return ( $retval < 0 && $errorStr == '' ) ||
$retval == 0;
67 * Spawn an external HTML tidy process and get corrected markup back from it.
68 * Also called in OutputHandler.php for full page validation
70 * @param $text String: HTML to check
71 * @param $stderr Boolean: Whether to read from STDERR rather than STDOUT
72 * @param &$retval Exit code (-1 on internal error)
73 * @return mixed String or null
75 private static function execExternalTidy( $text, $stderr = false, &$retval = null ) {
76 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
77 wfProfileIn( __METHOD__
);
83 $descriptorspec = array(
84 0 => array( 'pipe', 'r' ),
85 1 => array( 'file', wfGetNull(), 'a' ),
86 2 => array( 'pipe', 'w' )
89 $descriptorspec = array(
90 0 => array( 'pipe', 'r' ),
91 1 => array( 'pipe', 'w' ),
92 2 => array( 'file', wfGetNull(), 'a' )
96 $readpipe = $stderr ?
2 : 1;
99 if( function_exists( 'proc_open' ) ) {
100 $process = proc_open( "$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes );
101 if ( is_resource( $process ) ) {
102 // Theoretically, this style of communication could cause a deadlock
103 // here. If the stdout buffer fills up, then writes to stdin could
104 // block. This doesn't appear to happen with tidy, because tidy only
105 // writes to stdout after it's finished reading from stdin. Search
106 // for tidyParseStdin and tidySaveStdout in console/tidy.c
107 fwrite( $pipes[0], $text );
109 while ( !feof( $pipes[$readpipe] ) ) {
110 $cleansource .= fgets( $pipes[$readpipe], 1024 );
112 fclose( $pipes[$readpipe] );
113 $retval = proc_close( $process );
121 wfProfileOut( __METHOD__
);
123 if( !$stderr && $cleansource == '' && $text != '' ) {
124 // Some kind of error happened, so we couldn't get the corrected text.
125 // Just give up; we'll use the source text and append a warning.
133 * Use the HTML tidy PECL extension to use the tidy library in-process,
134 * saving the overhead of spawning a new process.
136 * 'pear install tidy' should be able to compile the extension module.
138 private static function execInternalTidy( $text, $stderr = false, &$retval = null ) {
139 global $wgTidyConf, $wgDebugTidy;
140 wfProfileIn( __METHOD__
);
143 $tidy->parseString( $text, $wgTidyConf, 'utf8' );
146 $retval = $tidy->getStatus();
147 return $tidy->errorBuffer
;
149 $tidy->cleanRepair();
150 $retval = $tidy->getStatus();
152 // 2 is magic number for fatal error
153 // http://www.php.net/manual/en/function.tidy-get-status.php
156 $cleansource = tidy_get_output( $tidy );
158 if ( $wgDebugTidy && $retval > 0 ) {
159 $cleansource .= "<!--\nTidy reports:\n" .
160 str_replace( '-->', '-->', $tidy->errorBuffer
) .
164 wfProfileOut( __METHOD__
);