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 Hideous HTML input
42 * @return string Corrected HTML output
44 public static function tidy( $text ) {
45 $driver = self
::singleton();
47 throw new MWException( __METHOD__
.
48 ': tidy is disabled, caller should have checked MWTidy::isEnabled()' );
50 return $driver->tidy( $text );
54 * Check HTML for errors, used if $wgValidateAllHtml = true.
57 * @param string &$errorStr Return the error string
58 * @return bool Whether the HTML is valid
60 public static function checkErrors( $text, &$errorStr = null ) {
61 $driver = self
::singleton();
63 throw new MWException( __METHOD__
.
64 ': tidy is disabled, caller should have checked MWTidy::isEnabled()' );
66 if ( $driver->supportsValidate() ) {
67 return $driver->validate( $text, $errorStr );
69 throw new MWException( __METHOD__
. ": error text return from HHVM tidy is not supported" );
73 public static function isEnabled() {
74 return self
::singleton() !== false;
77 protected static function singleton() {
78 global $wgUseTidy, $wgTidyInternal, $wgTidyConf, $wgDebugTidy, $wgTidyConfig,
79 $wgTidyBin, $wgTidyOpts;
81 if ( self
::$instance === null ) {
82 if ( $wgTidyConfig !== null ) {
83 $config = $wgTidyConfig;
84 } elseif ( $wgUseTidy ) {
87 'tidyConfigFile' => $wgTidyConf,
88 'debugComment' => $wgDebugTidy,
89 'tidyBin' => $wgTidyBin,
90 'tidyCommandLine' => $wgTidyOpts );
91 if ( $wgTidyInternal ) {
93 $config['driver'] = 'RaggettInternalHHVM';
95 $config['driver'] = 'RaggettInternalPHP';
98 $config['driver'] = 'RaggettExternal';
103 switch ( $config['driver'] ) {
104 case 'RaggettInternalHHVM':
105 self
::$instance = new MediaWiki\Tidy\
RaggettInternalHHVM( $config );
107 case 'RaggettInternalPHP':
108 self
::$instance = new MediaWiki\Tidy\
RaggettInternalPHP( $config );
110 case 'RaggettExternal':
111 self
::$instance = new MediaWiki\Tidy\
RaggettExternal( $config );
114 throw new MWException( "Invalid tidy driver: \"{$config['driver']}\"" );
117 return self
::$instance;
121 * Set the driver to be used. This is for testing.
122 * @param TidyDriverBase|false|null $instance
124 public static function setInstance( $instance ) {
125 self
::$instance = $instance;
129 * Destroy the current singleton instance
131 public static function destroySingleton() {
132 self
::$instance = null;