(bug 24007) Diff pages now mention the number of users having edited intermediate...
[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 # Tidy is known to clobber tabs; convert them to entities
35 $wrappedtext = str_replace( "\t", '&#9;', $wrappedtext );
36
37 if( $wgTidyInternal ) {
38 $correctedtext = self::execInternalTidy( $wrappedtext );
39 } else {
40 $correctedtext = self::execExternalTidy( $wrappedtext );
41 }
42 if( is_null( $correctedtext ) ) {
43 wfDebug( "Tidy error detected!\n" );
44 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
45 }
46
47 # Convert the tabs back from entities
48 $correctedtext = str_replace( '&#9;', "\t", $correctedtext );
49
50 return $correctedtext;
51 }
52
53 /**
54 * Check HTML for errors, used if $wgValidateAllHtml = true.
55 *
56 * @param $text String
57 * @param &$errorStr String: return the error string
58 * @return Boolean: whether the HTML is valid
59 */
60 public static function checkErrors( $text, &$errorStr = null ) {
61 global $wgTidyInternal;
62
63 $retval = 0;
64 if( $wgTidyInternal ) {
65 $errorStr = self::execInternalTidy( $text, true, $retval );
66 } else {
67 $errorStr = self::execExternalTidy( $text, true, $retval );
68 }
69 return ( $retval < 0 && $errorStr == '' ) || $retval == 0;
70 }
71
72 /**
73 * Spawn an external HTML tidy process and get corrected markup back from it.
74 * Also called in OutputHandler.php for full page validation
75 *
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
80 */
81 private static function execExternalTidy( $text, $stderr = false, &$retval = null ) {
82 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
83 wfProfileIn( __METHOD__ );
84
85 $cleansource = '';
86 $opts = ' -utf8';
87
88 if( $stderr ) {
89 $descriptorspec = array(
90 0 => array( 'pipe', 'r' ),
91 1 => array( 'file', wfGetNull(), 'a' ),
92 2 => array( 'pipe', 'w' )
93 );
94 } else {
95 $descriptorspec = array(
96 0 => array( 'pipe', 'r' ),
97 1 => array( 'pipe', 'w' ),
98 2 => array( 'file', wfGetNull(), 'a' )
99 );
100 }
101
102 $readpipe = $stderr ? 2 : 1;
103 $pipes = array();
104
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 );
114 fclose( $pipes[0] );
115 while ( !feof( $pipes[$readpipe] ) ) {
116 $cleansource .= fgets( $pipes[$readpipe], 1024 );
117 }
118 fclose( $pipes[$readpipe] );
119 $retval = proc_close( $process );
120 } else {
121 $retval = -1;
122 }
123 } else {
124 $retval = -1;
125 }
126
127 wfProfileOut( __METHOD__ );
128
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.
132 return null;
133 } else {
134 return $cleansource;
135 }
136 }
137
138 /**
139 * Use the HTML tidy PECL extension to use the tidy library in-process,
140 * saving the overhead of spawning a new process.
141 *
142 * 'pear install tidy' should be able to compile the extension module.
143 */
144 private static function execInternalTidy( $text, $stderr = false, &$retval = null ) {
145 global $wgTidyConf, $wgDebugTidy;
146 wfProfileIn( __METHOD__ );
147
148 $tidy = new tidy;
149 $tidy->parseString( $text, $wgTidyConf, 'utf8' );
150
151 if( $stderr ) {
152 $retval = $tidy->getStatus();
153 return $tidy->errorBuffer;
154 } else {
155 $tidy->cleanRepair();
156 $retval = $tidy->getStatus();
157 if( $retval == 2 ) {
158 // 2 is magic number for fatal error
159 // http://www.php.net/manual/en/function.tidy-get-status.php
160 $cleansource = null;
161 } else {
162 $cleansource = tidy_get_output( $tidy );
163 }
164 if ( $wgDebugTidy && $retval > 0 ) {
165 $cleansource .= "<!--\nTidy reports:\n" .
166 str_replace( '-->', '--&gt;', $tidy->errorBuffer ) .
167 "\n-->";
168 }
169
170 wfProfileOut( __METHOD__ );
171 return $cleansource;
172 }
173 }
174
175 }