* Removed function_exists check in MWTidy, proc_open is on any PHP at out minimum...
[lhc/web/wiklou.git] / includes / parser / Tidy.php
1 <?php
2 /**
3 * HTML validation and correction
4 *
5 * @file
6 */
7
8 /**
9 * Class used to hide mw:editsection tokens from Tidy so that it doesn't break them
10 * or break on them. This is a bit of a hack for now, but hopefully in the future
11 * we may create a real postprocessor or something that will replace this.
12 * It's called wrapper because for now it basically takes over MWTidy::tidy's task
13 * of wrapping the text in a xhtml block
14 *
15 * This re-uses some of the parser's UNIQ tricks, though some of it is private so it's
16 * duplicated. Perhaps we should create an abstract marker hiding class.
17 */
18 class MWTidyWrapper {
19
20 /**
21 * @var ReplacementArray
22 */
23 protected $mTokens;
24
25 protected $mUniqPrefix;
26
27 protected $mMarkerIndex;
28
29 public function __construct() {
30 $this->mTokens = null;
31 $this->mUniqPrefix = null;
32 }
33
34 /**
35 * @param $text string
36 * @return string
37 */
38 public function getWrapped( $text ) {
39 $this->mTokens = new ReplacementArray;
40 $this->mUniqPrefix = "\x7fUNIQ" .
41 dechex( mt_rand( 0, 0x7fffffff ) ) . dechex( mt_rand( 0, 0x7fffffff ) );
42 $this->mMarkerIndex = 0;
43
44 $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
45 array( &$this, 'replaceEditSectionLinksCallback' ), $text );
46
47 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
48 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
49 '<head><title>test</title></head><body>'.$wrappedtext.'</body></html>';
50
51 return $wrappedtext;
52 }
53
54 /**
55 * @param $m array
56 *
57 * @return string
58 */
59 function replaceEditSectionLinksCallback( $m ) {
60 $marker = "{$this->mUniqPrefix}-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX;
61 $this->mMarkerIndex++;
62 $this->mTokens->setPair( $marker, $m[0] );
63 return $marker;
64 }
65
66 /**
67 * @param $text string
68 * @return string
69 */
70 public function postprocess( $text ) {
71 return $this->mTokens->replace( $text );
72 }
73
74 }
75
76 /**
77 * Class to interact with HTML tidy
78 *
79 * Either the external tidy program or the in-process tidy extension
80 * will be used depending on availability. Override the default
81 * $wgTidyInternal setting to disable the internal if it's not working.
82 *
83 * @ingroup Parser
84 */
85 class MWTidy {
86 /**
87 * Interface with html tidy, used if $wgUseTidy = true.
88 * If tidy isn't able to correct the markup, the original will be
89 * returned in all its glory with a warning comment appended.
90 *
91 * @param $text String: hideous HTML input
92 * @return String: corrected HTML output
93 */
94 public static function tidy( $text ) {
95 global $wgTidyInternal;
96
97 $wrapper = new MWTidyWrapper;
98 $wrappedtext = $wrapper->getWrapped( $text );
99
100 $retVal = null;
101 if ( $wgTidyInternal ) {
102 $correctedtext = self::execInternalTidy( $wrappedtext, false, $retVal );
103 } else {
104 $correctedtext = self::execExternalTidy( $wrappedtext, false, $retVal );
105 }
106
107 if ( $retVal < 0 ) {
108 wfDebug( "Possible tidy configuration error!\n" );
109 return $text . "\n<!-- Tidy was unable to run -->\n";
110 } elseif ( is_null( $correctedtext ) ) {
111 wfDebug( "Tidy error detected!\n" );
112 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
113 }
114
115 $correctedtext = $wrapper->postprocess( $correctedtext ); // restore any hidden tokens
116
117 return $correctedtext;
118 }
119
120 function replaceEditSectionLinksCallback( $m ) {
121
122 }
123
124 /**
125 * Check HTML for errors, used if $wgValidateAllHtml = true.
126 *
127 * @param $text String
128 * @param &$errorStr String: return the error string
129 * @return Boolean: whether the HTML is valid
130 */
131 public static function checkErrors( $text, &$errorStr = null ) {
132 global $wgTidyInternal;
133
134 $retval = 0;
135 if( $wgTidyInternal ) {
136 $errorStr = self::execInternalTidy( $text, true, $retval );
137 } else {
138 $errorStr = self::execExternalTidy( $text, true, $retval );
139 }
140
141 return ( $retval < 0 && $errorStr == '' ) || $retval == 0;
142 }
143
144 /**
145 * Spawn an external HTML tidy process and get corrected markup back from it.
146 * Also called in OutputHandler.php for full page validation
147 *
148 * @param $text String: HTML to check
149 * @param $stderr Boolean: Whether to read result from STDERR rather than STDOUT
150 * @param &$retval Exit code (-1 on internal error)
151 * @return mixed String or null
152 */
153 private static function execExternalTidy( $text, $stderr = false, &$retval = null ) {
154 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
155 wfProfileIn( __METHOD__ );
156
157 $cleansource = '';
158 $opts = ' -utf8';
159
160 if ( $stderr ) {
161 $descriptorspec = array(
162 0 => array( 'pipe', 'r' ),
163 1 => array( 'file', wfGetNull(), 'a' ),
164 2 => array( 'pipe', 'w' )
165 );
166 } else {
167 $descriptorspec = array(
168 0 => array( 'pipe', 'r' ),
169 1 => array( 'pipe', 'w' ),
170 2 => array( 'file', wfGetNull(), 'a' )
171 );
172 }
173
174 $readpipe = $stderr ? 2 : 1;
175 $pipes = array();
176
177 $process = proc_open(
178 "$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes );
179
180 if ( is_resource( $process ) ) {
181 // Theoretically, this style of communication could cause a deadlock
182 // here. If the stdout buffer fills up, then writes to stdin could
183 // block. This doesn't appear to happen with tidy, because tidy only
184 // writes to stdout after it's finished reading from stdin. Search
185 // for tidyParseStdin and tidySaveStdout in console/tidy.c
186 fwrite( $pipes[0], $text );
187 fclose( $pipes[0] );
188 while ( !feof( $pipes[$readpipe] ) ) {
189 $cleansource .= fgets( $pipes[$readpipe], 1024 );
190 }
191 fclose( $pipes[$readpipe] );
192 $retval = proc_close( $process );
193 } else {
194 wfWarn( "Unable to start external tidy process" );
195 $retval = -1;
196 }
197
198 if ( !$stderr && $cleansource == '' && $text != '' ) {
199 // Some kind of error happened, so we couldn't get the corrected text.
200 // Just give up; we'll use the source text and append a warning.
201 $cleansource = null;
202 }
203
204 wfProfileOut( __METHOD__ );
205 return $cleansource;
206 }
207
208 /**
209 * Use the HTML tidy extension to use the tidy library in-process,
210 * saving the overhead of spawning a new process.
211 *
212 * @param $text String: HTML to check
213 * @param $stderr Boolean: Whether to read result from error status instead of output
214 * @param &$retval Exit code (-1 on internal error)
215 * @return mixed String or null
216 */
217 private static function execInternalTidy( $text, $stderr = false, &$retval = null ) {
218 global $wgTidyConf, $wgDebugTidy;
219 wfProfileIn( __METHOD__ );
220
221 if ( !MWInit::classExists( 'tidy' ) ) {
222 wfWarn( "Unable to load internal tidy class." );
223 $retval = -1;
224 return null;
225 }
226
227 $tidy = new tidy;
228 $tidy->parseString( $text, $wgTidyConf, 'utf8' );
229
230 if ( $stderr ) {
231 $retval = $tidy->getStatus();
232
233 wfProfileOut( __METHOD__ );
234 return $tidy->errorBuffer;
235 } else {
236 $tidy->cleanRepair();
237 $retval = $tidy->getStatus();
238 if ( $retval == 2 ) {
239 // 2 is magic number for fatal error
240 // http://www.php.net/manual/en/function.tidy-get-status.php
241 $cleansource = null;
242 } else {
243 $cleansource = tidy_get_output( $tidy );
244 if ( $wgDebugTidy && $retval > 0 ) {
245 $cleansource .= "<!--\nTidy reports:\n" .
246 str_replace( '-->', '--&gt;', $tidy->errorBuffer ) .
247 "\n-->";
248 }
249 }
250
251 wfProfileOut( __METHOD__ );
252 return $cleansource;
253 }
254 }
255 }