4 * Class to interact with HTML tidy
6 * Either the external tidy program or the in-process tidy extension
7 * will be used depending on availability. Override the default
8 * $wgTidyInternal setting to disable the internal if it's not working.
15 * Interface with html tidy, used if $wgUseTidy = true.
16 * If tidy isn't able to correct the markup, the original will be
17 * returned in all its glory with a warning comment appended.
19 * @param string $text Hideous HTML input
20 * @return string Corrected HTML output
22 public static function tidy( $text ) {
23 global $wgTidyInternal;
25 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
26 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
27 '<head><title>test</title></head><body>'.$text.'</body></html>';
29 # Tidy is known to clobber tabs; convert them to entities
30 $wrappedtext = str_replace( "\t", '	', $wrappedtext );
32 if( $wgTidyInternal ) {
33 $correctedtext = self
::execInternalTidy( $wrappedtext );
35 $correctedtext = self
::execExternalTidy( $wrappedtext );
37 if( is_null( $correctedtext ) ) {
38 wfDebug( "Tidy error detected!\n" );
39 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
42 # Convert the tabs back from entities
43 $correctedtext = str_replace( '	', "\t", $correctedtext );
45 return $correctedtext;
49 * Check HTML for errors, used if $wgValidateAllHtml = true.
52 * @param &$errorStr String: return the error string
53 * @return Boolean: whether the HTML is valid
55 public static function checkErrors( $text, &$errorStr = null ) {
56 global $wgTidyInternal;
59 if( $wgTidyInternal ) {
60 $errorStr = self
::execInternalTidy( $text, true, $retval );
62 $errorStr = self
::execExternalTidy( $text, true, $retval );
64 return ( $retval < 0 && $errorStr == '' ) ||
$retval == 0;
68 * Spawn an external HTML tidy process and get corrected markup back from it.
69 * Also called in OutputHandler.php for full page validation
71 * @param $text String: HTML to check
72 * @param $stderr Boolean: Whether to read from STDERR rather than STDOUT
73 * @param &$retval Exit code (-1 on internal error)
74 * @retrun mixed String or null
76 private static function execExternalTidy( $text, $stderr = false, &$retval = null ) {
77 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
78 wfProfileIn( __METHOD__
);
84 $descriptorspec = array(
85 0 => array( 'pipe', 'r' ),
86 1 => array( 'file', wfGetNull(), 'a' ),
87 2 => array( 'pipe', 'w' )
90 $descriptorspec = array(
91 0 => array( 'pipe', 'r' ),
92 1 => array( 'pipe', 'w' ),
93 2 => array( 'file', wfGetNull(), 'a' )
97 $readpipe = $stderr ?
2 : 1;
100 if( function_exists( 'proc_open' ) ) {
101 $process = proc_open( "$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes );
102 if ( is_resource( $process ) ) {
103 // Theoretically, this style of communication could cause a deadlock
104 // here. If the stdout buffer fills up, then writes to stdin could
105 // block. This doesn't appear to happen with tidy, because tidy only
106 // writes to stdout after it's finished reading from stdin. Search
107 // for tidyParseStdin and tidySaveStdout in console/tidy.c
108 fwrite( $pipes[0], $text );
110 while ( !feof( $pipes[$readpipe] ) ) {
111 $cleansource .= fgets( $pipes[$readpipe], 1024 );
113 fclose( $pipes[$readpipe] );
114 $retval = proc_close( $process );
122 wfProfileOut( __METHOD__
);
124 if( !$stderr && $cleansource == '' && $text != '' ) {
125 // Some kind of error happened, so we couldn't get the corrected text.
126 // Just give up; we'll use the source text and append a warning.
134 * Use the HTML tidy PECL extension to use the tidy library in-process,
135 * saving the overhead of spawning a new process.
137 * 'pear install tidy' should be able to compile the extension module.
139 private static function execInternalTidy( $text, $stderr = false, &$retval = null ) {
140 global $wgTidyConf, $IP, $wgDebugTidy;
141 wfProfileIn( __METHOD__
);
144 $tidy->parseString( $text, $wgTidyConf, 'utf8' );
147 $retval = $tidy->getStatus();
148 return $tidy->errorBuffer
;
150 $tidy->cleanRepair();
151 $retval = $tidy->getStatus();
153 // 2 is magic number for fatal error
154 // http://www.php.net/manual/en/function.tidy-get-status.php
157 $cleansource = tidy_get_output( $tidy );
159 if ( $wgDebugTidy && $retval > 0 ) {
160 $cleansource .= "<!--\nTidy reports:\n" .
161 str_replace( '-->', '-->', $tidy->errorBuffer
) .
165 wfProfileOut( __METHOD__
);