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