Revert r42257 "Convert literal tabs to 	 when passing them through Tidy, to preven...
[lhc/web/wiklou.git] / includes / parser / Tidy.php
1 <?php
2 /**
3 * HTML validation and correction
4 *
5 * @file
6 */
7
8 /**
9 * Class to interact with HTML tidy
10 *
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.
14 *
15 * @ingroup Parser
16 */
17 class MWTidy {
18
19 /**
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.
23 *
24 * @param $text String: hideous HTML input
25 * @return String: corrected HTML output
26 */
27 public static function tidy( $text ) {
28 global $wgTidyInternal;
29
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>';
33
34 if( $wgTidyInternal ) {
35 $correctedtext = self::execInternalTidy( $wrappedtext );
36 } else {
37 $correctedtext = self::execExternalTidy( $wrappedtext );
38 }
39 if( is_null( $correctedtext ) ) {
40 wfDebug( "Tidy error detected!\n" );
41 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
42 }
43
44 return $correctedtext;
45 }
46
47 /**
48 * Check HTML for errors, used if $wgValidateAllHtml = true.
49 *
50 * @param $text String
51 * @param &$errorStr String: return the error string
52 * @return Boolean: whether the HTML is valid
53 */
54 public static function checkErrors( $text, &$errorStr = null ) {
55 global $wgTidyInternal;
56
57 $retval = 0;
58 if( $wgTidyInternal ) {
59 $errorStr = self::execInternalTidy( $text, true, $retval );
60 } else {
61 $errorStr = self::execExternalTidy( $text, true, $retval );
62 }
63 return ( $retval < 0 && $errorStr == '' ) || $retval == 0;
64 }
65
66 /**
67 * Spawn an external HTML tidy process and get corrected markup back from it.
68 * Also called in OutputHandler.php for full page validation
69 *
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
74 */
75 private static function execExternalTidy( $text, $stderr = false, &$retval = null ) {
76 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
77 wfProfileIn( __METHOD__ );
78
79 $cleansource = '';
80 $opts = ' -utf8';
81
82 if( $stderr ) {
83 $descriptorspec = array(
84 0 => array( 'pipe', 'r' ),
85 1 => array( 'file', wfGetNull(), 'a' ),
86 2 => array( 'pipe', 'w' )
87 );
88 } else {
89 $descriptorspec = array(
90 0 => array( 'pipe', 'r' ),
91 1 => array( 'pipe', 'w' ),
92 2 => array( 'file', wfGetNull(), 'a' )
93 );
94 }
95
96 $readpipe = $stderr ? 2 : 1;
97 $pipes = array();
98
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 );
108 fclose( $pipes[0] );
109 while ( !feof( $pipes[$readpipe] ) ) {
110 $cleansource .= fgets( $pipes[$readpipe], 1024 );
111 }
112 fclose( $pipes[$readpipe] );
113 $retval = proc_close( $process );
114 } else {
115 $retval = -1;
116 }
117 } else {
118 $retval = -1;
119 }
120
121 wfProfileOut( __METHOD__ );
122
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.
126 return null;
127 } else {
128 return $cleansource;
129 }
130 }
131
132 /**
133 * Use the HTML tidy PECL extension to use the tidy library in-process,
134 * saving the overhead of spawning a new process.
135 *
136 * 'pear install tidy' should be able to compile the extension module.
137 */
138 private static function execInternalTidy( $text, $stderr = false, &$retval = null ) {
139 global $wgTidyConf, $wgDebugTidy;
140 wfProfileIn( __METHOD__ );
141
142 $tidy = new tidy;
143 $tidy->parseString( $text, $wgTidyConf, 'utf8' );
144
145 if( $stderr ) {
146 $retval = $tidy->getStatus();
147 return $tidy->errorBuffer;
148 } else {
149 $tidy->cleanRepair();
150 $retval = $tidy->getStatus();
151 if( $retval == 2 ) {
152 // 2 is magic number for fatal error
153 // http://www.php.net/manual/en/function.tidy-get-status.php
154 $cleansource = null;
155 } else {
156 $cleansource = tidy_get_output( $tidy );
157 }
158 if ( $wgDebugTidy && $retval > 0 ) {
159 $cleansource .= "<!--\nTidy reports:\n" .
160 str_replace( '-->', '--&gt;', $tidy->errorBuffer ) .
161 "\n-->";
162 }
163
164 wfProfileOut( __METHOD__ );
165 return $cleansource;
166 }
167 }
168
169 }