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
46 public static function tidy( $text ) {
47 $driver = self
::singleton();
49 throw new MWException( __METHOD__
.
50 ': tidy is disabled, caller should have checked MWTidy::isEnabled()' );
52 return $driver->tidy( $text );
56 * Check HTML for errors, used if $wgValidateAllHtml = true.
59 * @param string &$errorStr Return the error string
60 * @return bool Whether the HTML is valid
63 public static function checkErrors( $text, &$errorStr = null ) {
64 $driver = self
::singleton();
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 );
72 throw new MWException( __METHOD__
. ": error text return from HHVM tidy is not supported" );
79 public static function isEnabled() {
80 return self
::singleton() !== false;
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 ) {
96 'tidyConfigFile' => $wgTidyConf,
97 'debugComment' => $wgDebugTidy,
98 'tidyBin' => $wgTidyBin,
99 'tidyCommandLine' => $wgTidyOpts ];
100 if ( $wgTidyInternal ) {
102 $config['driver'] = 'RaggettInternalHHVM';
104 $config['driver'] = 'RaggettInternalPHP';
107 $config['driver'] = 'RaggettExternal';
112 self
::$instance = self
::factory( $config );
114 return self
::$instance;
118 * Create a new Tidy driver object from configuration.
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 );
129 case 'RaggettInternalPHP':
130 $instance = new MediaWiki\Tidy\
RaggettInternalPHP( $config );
132 case 'RaggettExternal':
133 $instance = new MediaWiki\Tidy\
RaggettExternal( $config );
135 case 'Html5Depurate':
136 $instance = new MediaWiki\Tidy\
Html5Depurate( $config );
138 case 'Html5Internal':
139 $instance = new MediaWiki\Tidy\
Html5Internal( $config );
144 throw new MWException( "Invalid tidy driver: \"{$config['driver']}\"" );
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;