DatabaseMysqlBase visibility cleanups
[mediawiki.git] / includes / parser / MWTidy.php
blob5e5461587b9d0648802d9a088a981314760abaa3
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 to interact with HTML tidy
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.
31 * @ingroup Parser
33 class MWTidy {
34 private static $instance;
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.
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
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()' );
52 return $driver->tidy( $text );
55 /**
56 * Check HTML for errors, used if $wgValidateAllHtml = true.
58 * @param string $text
59 * @param string &$errorStr Return the error string
60 * @return bool Whether the HTML is valid
61 * @throws MWException
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()' );
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" );
76 /**
77 * @return bool
79 public static function isEnabled() {
80 return self::singleton() !== false;
83 /**
84 * @return bool|\MediaWiki\Tidy\TidyDriverBase
86 protected static function singleton() {
87 global $wgUseTidy, $wgTidyInternal, $wgTidyConf, $wgDebugTidy, $wgTidyConfig,
88 $wgTidyBin, $wgTidyOpts;
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';
106 } else {
107 $config['driver'] = 'RaggettExternal';
109 } else {
110 return false;
112 self::$instance = self::factory( $config );
114 return self::$instance;
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
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 'disabled':
142 return false;
143 default:
144 throw new MWException( "Invalid tidy driver: \"{$config['driver']}\"" );
146 return $instance;
150 * Set the driver to be used. This is for testing.
151 * @param MediaWiki\Tidy\TidyDriverBase|false|null $instance
153 public static function setInstance( $instance ) {
154 self::$instance = $instance;
158 * Destroy the current singleton instance
160 public static function destroySingleton() {
161 self::$instance = null;