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
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.
34 private static $instance;
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
45 public static function tidy( $text ) {
46 $driver = self
::singleton();
48 throw new MWException( __METHOD__
.
49 ': tidy is disabled, caller should have checked MWTidy::isEnabled()' );
51 return $driver->tidy( $text );
55 * Get CSS modules needed if HTML from the current driver is to be displayed.
57 * This is just a migration tool to allow some changes expected as part of
58 * Tidy replacement (T89331) to be exposed on the client side via user
59 * scripts, without actually replacing tidy. See T49673.
63 public static function getModuleStyles() {
64 $driver = self
::singleton();
65 if ( $driver && $driver instanceof MediaWiki\Tidy\RaggettBase
) {
66 return [ 'mediawiki.raggett' ];
73 * Check HTML for errors, used if $wgValidateAllHtml = true.
76 * @param string &$errorStr Return the error string
77 * @return bool Whether the HTML is valid
79 public static function checkErrors( $text, &$errorStr = null ) {
80 $driver = self
::singleton();
82 throw new MWException( __METHOD__
.
83 ': tidy is disabled, caller should have checked MWTidy::isEnabled()' );
85 if ( $driver->supportsValidate() ) {
86 return $driver->validate( $text, $errorStr );
88 throw new MWException( __METHOD__
. ": error text return from HHVM tidy is not supported" );
92 public static function isEnabled() {
93 return self
::singleton() !== false;
96 protected static function singleton() {
97 global $wgUseTidy, $wgTidyInternal, $wgTidyConf, $wgDebugTidy, $wgTidyConfig,
98 $wgTidyBin, $wgTidyOpts;
100 if ( self
::$instance === null ) {
101 if ( $wgTidyConfig !== null ) {
102 $config = $wgTidyConfig;
103 } elseif ( $wgUseTidy ) {
106 'tidyConfigFile' => $wgTidyConf,
107 'debugComment' => $wgDebugTidy,
108 'tidyBin' => $wgTidyBin,
109 'tidyCommandLine' => $wgTidyOpts ];
110 if ( $wgTidyInternal ) {
112 $config['driver'] = 'RaggettInternalHHVM';
114 $config['driver'] = 'RaggettInternalPHP';
117 $config['driver'] = 'RaggettExternal';
122 switch ( $config['driver'] ) {
123 case 'RaggettInternalHHVM':
124 self
::$instance = new MediaWiki\Tidy\
RaggettInternalHHVM( $config );
126 case 'RaggettInternalPHP':
127 self
::$instance = new MediaWiki\Tidy\
RaggettInternalPHP( $config );
129 case 'RaggettExternal':
130 self
::$instance = new MediaWiki\Tidy\
RaggettExternal( $config );
132 case 'Html5Depurate':
133 self
::$instance = new MediaWiki\Tidy\
Html5Depurate( $config );
136 throw new MWException( "Invalid tidy driver: \"{$config['driver']}\"" );
139 return self
::$instance;
143 * Set the driver to be used. This is for testing.
144 * @param TidyDriverBase|false|null $instance
146 public static function setInstance( $instance ) {
147 self
::$instance = $instance;
151 * Destroy the current singleton instance
153 public static function destroySingleton() {
154 self
::$instance = null;