]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/parser/MWTidy.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.git] / includes / parser / MWTidy.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 to interact with HTML tidy
26  *
27  * Either the external tidy program or the in-process tidy extension
28  * will be used depending on availability. Override the default
29  * $wgTidyInternal setting to disable the internal if it's not working.
30  *
31  * @ingroup Parser
32  */
33 class MWTidy {
34         private static $instance;
35
36         /**
37          * Interface with html tidy.
38          * If tidy isn't able to correct the markup, the original will be
39          * returned in all its glory with a warning comment appended.
40          *
41          * @param string $text HTML input fragment. This should not contain a
42          *                     <body> or <html> tag.
43          * @return string Corrected HTML output
44          * @throws MWException
45          */
46         public static function tidy( $text ) {
47                 $driver = self::singleton();
48                 if ( !$driver ) {
49                         throw new MWException( __METHOD__ .
50                                 ': tidy is disabled, caller should have checked MWTidy::isEnabled()' );
51                 }
52                 return $driver->tidy( $text );
53         }
54
55         /**
56          * Check HTML for errors, used if $wgValidateAllHtml = true.
57          *
58          * @param string $text
59          * @param string &$errorStr Return the error string
60          * @return bool Whether the HTML is valid
61          * @throws MWException
62          */
63         public static function checkErrors( $text, &$errorStr = null ) {
64                 $driver = self::singleton();
65                 if ( !$driver ) {
66                         throw new MWException( __METHOD__ .
67                                 ': tidy is disabled, caller should have checked MWTidy::isEnabled()' );
68                 }
69                 if ( $driver->supportsValidate() ) {
70                         return $driver->validate( $text, $errorStr );
71                 } else {
72                         throw new MWException( __METHOD__ . ": error text return from HHVM tidy is not supported" );
73                 }
74         }
75
76         /**
77          * @return bool
78          */
79         public static function isEnabled() {
80                 return self::singleton() !== false;
81         }
82
83         /**
84          * @return bool|\MediaWiki\Tidy\TidyDriverBase
85          */
86         public static function singleton() {
87                 global $wgUseTidy, $wgTidyInternal, $wgTidyConf, $wgDebugTidy, $wgTidyConfig,
88                         $wgTidyBin, $wgTidyOpts;
89
90                 if ( self::$instance === null ) {
91                         if ( $wgTidyConfig !== null ) {
92                                 $config = $wgTidyConfig;
93                         } elseif ( $wgUseTidy ) {
94                                 // b/c configuration
95                                 $config = [
96                                         'tidyConfigFile' => $wgTidyConf,
97                                         'debugComment' => $wgDebugTidy,
98                                         'tidyBin' => $wgTidyBin,
99                                         'tidyCommandLine' => $wgTidyOpts ];
100                                 if ( $wgTidyInternal ) {
101                                         if ( wfIsHHVM() ) {
102                                                 $config['driver'] = 'RaggettInternalHHVM';
103                                         } else {
104                                                 $config['driver'] = 'RaggettInternalPHP';
105                                         }
106                                 } else {
107                                         $config['driver'] = 'RaggettExternal';
108                                 }
109                         } else {
110                                 return false;
111                         }
112                         self::$instance = self::factory( $config );
113                 }
114                 return self::$instance;
115         }
116
117         /**
118          * Create a new Tidy driver object from configuration.
119          * @see $wgTidyConfig
120          * @param array $config
121          * @return bool|\MediaWiki\Tidy\TidyDriverBase
122          * @throws MWException
123          */
124         public static function factory( array $config ) {
125                 switch ( $config['driver'] ) {
126                         case 'RaggettInternalHHVM':
127                                 $instance = new MediaWiki\Tidy\RaggettInternalHHVM( $config );
128                                 break;
129                         case 'RaggettInternalPHP':
130                                 $instance = new MediaWiki\Tidy\RaggettInternalPHP( $config );
131                                 break;
132                         case 'RaggettExternal':
133                                 $instance = new MediaWiki\Tidy\RaggettExternal( $config );
134                                 break;
135                         case 'Html5Depurate':
136                                 $instance = new MediaWiki\Tidy\Html5Depurate( $config );
137                                 break;
138                         case 'Html5Internal':
139                                 $instance = new MediaWiki\Tidy\Html5Internal( $config );
140                                 break;
141                         case 'RemexHtml':
142                                 $instance = new MediaWiki\Tidy\RemexDriver( $config );
143                                 break;
144                         case 'disabled':
145                                 return false;
146                         default:
147                                 throw new MWException( "Invalid tidy driver: \"{$config['driver']}\"" );
148                 }
149                 return $instance;
150         }
151
152         /**
153          * Set the driver to be used. This is for testing.
154          * @param MediaWiki\Tidy\TidyDriverBase|false|null $instance
155          */
156         public static function setInstance( $instance ) {
157                 self::$instance = $instance;
158         }
159
160         /**
161          * Destroy the current singleton instance
162          */
163         public static function destroySingleton() {
164                 self::$instance = null;
165         }
166 }