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