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