]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/parser/Tidy.php
MediaWiki 1.15.0
[autoinstalls/mediawiki.git] / includes / parser / Tidy.php
1 <?php
2
3 /**
4  * Class to interact with HTML tidy
5  *
6  * Either the external tidy program or the in-process tidy extension
7  * will be used depending on availability. Override the default
8  * $wgTidyInternal setting to disable the internal if it's not working.
9  *
10  * @ingroup Parser
11  */
12 class MWTidy {
13
14         /**
15          * Interface with html tidy, used if $wgUseTidy = true.
16          * If tidy isn't able to correct the markup, the original will be
17          * returned in all its glory with a warning comment appended.
18          *
19          * @param string $text Hideous HTML input
20          * @return string Corrected HTML output
21          */
22         public static function tidy( $text ) {
23                 global $wgTidyInternal;
24
25                 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
26 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
27 '<head><title>test</title></head><body>'.$text.'</body></html>';
28
29                 # Tidy is known to clobber tabs; convert them to entities
30                 $wrappedtext = str_replace( "\t", '&#9;', $wrappedtext );
31
32                 if( $wgTidyInternal ) {
33                         $correctedtext = self::execInternalTidy( $wrappedtext );
34                 } else {
35                         $correctedtext = self::execExternalTidy( $wrappedtext );
36                 }
37                 if( is_null( $correctedtext ) ) {
38                         wfDebug( "Tidy error detected!\n" );
39                         return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
40                 }
41
42                 # Convert the tabs back from entities
43                 $correctedtext = str_replace( '&#9;', "\t", $correctedtext );
44
45                 return $correctedtext;
46         }
47
48         /**
49          * Check HTML for errors, used if $wgValidateAllHtml = true.
50          *
51          * @param $text String
52          * @param &$errorStr String: return the error string
53          * @return Boolean: whether the HTML is valid
54          */
55         public static function checkErrors( $text, &$errorStr = null ) {
56                 global $wgTidyInternal;
57                 
58                 $retval = 0;
59                 if( $wgTidyInternal ) {
60                         $errorStr = self::execInternalTidy( $text, true, $retval );
61                 } else {
62                         $errorStr = self::execExternalTidy( $text, true, $retval );
63                 }
64                 return ( $retval < 0 && $errorStr == '' ) || $retval == 0;
65         }
66
67         /**
68          * Spawn an external HTML tidy process and get corrected markup back from it.
69          * Also called in OutputHandler.php for full page validation
70          *
71          * @param $text String: HTML to check
72          * @param $stderr Boolean: Whether to read from STDERR rather than STDOUT
73          * @param &$retval Exit code (-1 on internal error)
74          * @retrun mixed String or null
75          */
76         private static function execExternalTidy( $text, $stderr = false, &$retval = null ) {
77                 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
78                 wfProfileIn( __METHOD__ );
79
80                 $cleansource = '';
81                 $opts = ' -utf8';
82
83                 if( $stderr ) {
84                         $descriptorspec = array(
85                                 0 => array( 'pipe', 'r' ),
86                                 1 => array( 'file', wfGetNull(), 'a' ),
87                                 2 => array( 'pipe', 'w' )
88                         );
89                 } else {
90                         $descriptorspec = array(
91                                 0 => array( 'pipe', 'r' ),
92                                 1 => array( 'pipe', 'w' ),
93                                 2 => array( 'file', wfGetNull(), 'a' )
94                         );
95                 }
96                 
97                 $readpipe = $stderr ? 2 : 1;
98                 $pipes = array();
99
100                 if( function_exists( 'proc_open' ) ) {
101                         $process = proc_open( "$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes );
102                         if ( is_resource( $process ) ) {
103                                 // Theoretically, this style of communication could cause a deadlock
104                                 // here. If the stdout buffer fills up, then writes to stdin could
105                                 // block. This doesn't appear to happen with tidy, because tidy only
106                                 // writes to stdout after it's finished reading from stdin. Search
107                                 // for tidyParseStdin and tidySaveStdout in console/tidy.c
108                                 fwrite( $pipes[0], $text );
109                                 fclose( $pipes[0] );
110                                 while ( !feof( $pipes[$readpipe] ) ) {
111                                         $cleansource .= fgets( $pipes[$readpipe], 1024 );
112                                 }
113                                 fclose( $pipes[$readpipe] );
114                                 $retval = proc_close( $process );
115                         } else {
116                                 $retval = -1;
117                         }
118                 } else {
119                         $retval = -1;   
120                 }
121
122                 wfProfileOut( __METHOD__ );
123
124                 if( !$stderr && $cleansource == '' && $text != '' ) {
125                         // Some kind of error happened, so we couldn't get the corrected text.
126                         // Just give up; we'll use the source text and append a warning.
127                         return null;
128                 } else {
129                         return $cleansource;
130                 }
131         }
132
133         /**
134          * Use the HTML tidy PECL extension to use the tidy library in-process,
135          * saving the overhead of spawning a new process.
136          *
137          * 'pear install tidy' should be able to compile the extension module.
138          */
139         private static function execInternalTidy( $text, $stderr = false, &$retval = null ) {
140                 global $wgTidyConf, $IP, $wgDebugTidy;
141                 wfProfileIn( __METHOD__ );
142
143                 $tidy = new tidy;
144                 $tidy->parseString( $text, $wgTidyConf, 'utf8' );
145
146                 if( $stderr ) {
147                         $retval = $tidy->getStatus();
148                         return $tidy->errorBuffer;
149                 } else {
150                         $tidy->cleanRepair();
151                         $retval = $tidy->getStatus();
152                         if( $retval == 2 ) {
153                                 // 2 is magic number for fatal error
154                                 // http://www.php.net/manual/en/function.tidy-get-status.php
155                                 $cleansource = null;
156                         } else {
157                                 $cleansource = tidy_get_output( $tidy );
158                         }
159                         if ( $wgDebugTidy && $retval > 0 ) {
160                                 $cleansource .= "<!--\nTidy reports:\n" .
161                                         str_replace( '-->', '--&gt;', $tidy->errorBuffer ) .
162                                         "\n-->";
163                         }
164         
165                         wfProfileOut( __METHOD__ );
166                         return $cleansource;
167                 }
168         }
169
170 }