Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / parser / Tidy.php
blobed2d436d3b5b544019d7a678214e54df8bd25c5e
1 <?php
2 /**
3 * HTML validation and correction
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.
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.
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
20 * @file
21 * @ingroup Parser
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
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.
34 * @ingroup Parser
36 class MWTidyWrapper {
38 /**
39 * @var ReplacementArray
41 protected $mTokens;
43 protected $mUniqPrefix;
45 protected $mMarkerIndex;
47 public function __construct() {
48 $this->mTokens = null;
49 $this->mUniqPrefix = null;
52 /**
53 * @param $text string
54 * @return string
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;
62 $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
63 array( &$this, 'replaceEditSectionLinksCallback' ), $text );
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>';
69 return $wrappedtext;
72 /**
73 * @param $m array
75 * @return string
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;
84 /**
85 * @param $text string
86 * @return string
88 public function postprocess( $text ) {
89 return $this->mTokens->replace( $text );
94 /**
95 * Class to interact with HTML tidy
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.
101 * @ingroup Parser
103 class MWTidy {
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.
109 * @param $text String: hideous HTML input
110 * @return String: corrected HTML output
112 public static function tidy( $text ) {
113 global $wgTidyInternal;
115 $wrapper = new MWTidyWrapper;
116 $wrappedtext = $wrapper->getWrapped( $text );
118 $retVal = null;
119 if ( $wgTidyInternal ) {
120 $correctedtext = self::execInternalTidy( $wrappedtext, false, $retVal );
121 } else {
122 $correctedtext = self::execExternalTidy( $wrappedtext, false, $retVal );
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";
133 $correctedtext = $wrapper->postprocess( $correctedtext ); // restore any hidden tokens
135 return $correctedtext;
139 * Check HTML for errors, used if $wgValidateAllHtml = true.
141 * @param $text String
142 * @param &$errorStr String: return the error string
143 * @return Boolean: whether the HTML is valid
145 public static function checkErrors( $text, &$errorStr = null ) {
146 global $wgTidyInternal;
148 $retval = 0;
149 if( $wgTidyInternal ) {
150 $errorStr = self::execInternalTidy( $text, true, $retval );
151 } else {
152 $errorStr = self::execExternalTidy( $text, true, $retval );
155 return ( $retval < 0 && $errorStr == '' ) || $retval == 0;
159 * Spawn an external HTML tidy process and get corrected markup back from it.
160 * Also called in OutputHandler.php for full page validation
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
167 private static function execExternalTidy( $text, $stderr = false, &$retval = null ) {
168 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
169 wfProfileIn( __METHOD__ );
171 $cleansource = '';
172 $opts = ' -utf8';
174 if ( $stderr ) {
175 $descriptorspec = array(
176 0 => array( 'pipe', 'r' ),
177 1 => array( 'file', wfGetNull(), 'a' ),
178 2 => array( 'pipe', 'w' )
180 } else {
181 $descriptorspec = array(
182 0 => array( 'pipe', 'r' ),
183 1 => array( 'pipe', 'w' ),
184 2 => array( 'file', wfGetNull(), 'a' )
188 $readpipe = $stderr ? 2 : 1;
189 $pipes = array();
191 $process = proc_open(
192 "$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes );
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 );
205 fclose( $pipes[$readpipe] );
206 $retval = proc_close( $process );
207 } else {
208 wfWarn( "Unable to start external tidy process" );
209 $retval = -1;
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;
218 wfProfileOut( __METHOD__ );
219 return $cleansource;
223 * Use the HTML tidy extension to use the tidy library in-process,
224 * saving the overhead of spawning a new process.
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
231 private static function execInternalTidy( $text, $stderr = false, &$retval = null ) {
232 global $wgTidyConf, $wgDebugTidy;
233 wfProfileIn( __METHOD__ );
235 if ( !MWInit::classExists( 'tidy' ) ) {
236 wfWarn( "Unable to load internal tidy class." );
237 $retval = -1;
239 wfProfileOut( __METHOD__ );
240 return null;
243 $tidy = new tidy;
244 $tidy->parseString( $text, $wgTidyConf, 'utf8' );
246 if ( $stderr ) {
247 $retval = $tidy->getStatus();
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-->";
267 wfProfileOut( __METHOD__ );
268 return $cleansource;