d0e50bc707783e393ab58761428ed33515d864f9
[lhc/web/wiklou.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 Hideous HTML input
42 * @return string Corrected HTML output
43 */
44 public static function tidy( $text ) {
45 $driver = self::singleton();
46 if ( !$driver ) {
47 throw new MWException( __METHOD__.
48 ': tidy is disabled, caller should have checked MWTidy::isEnabled()' );
49 }
50 return $driver->tidy( $text );
51 }
52
53 /**
54 * Check HTML for errors, used if $wgValidateAllHtml = true.
55 *
56 * @param string $text
57 * @param string &$errorStr Return the error string
58 * @return bool Whether the HTML is valid
59 */
60 public static function checkErrors( $text, &$errorStr = null ) {
61 $driver = self::singleton();
62 if ( !$driver ) {
63 throw new MWException( __METHOD__.
64 ': tidy is disabled, caller should have checked MWTidy::isEnabled()' );
65 }
66 if ( $driver->supportsValidate() ) {
67 return $driver->validate( $text, $errorStr );
68 } else {
69 throw new MWException( __METHOD__ . ": error text return from HHVM tidy is not supported" );
70 }
71 }
72
73 public static function isEnabled() {
74 return self::singleton() !== false;
75 }
76
77 protected static function singleton() {
78 global $wgUseTidy, $wgTidyInternal, $wgTidyConf, $wgDebugTidy, $wgTidyConfig,
79 $wgTidyBin, $wgTidyOpts;
80
81 if ( self::$instance === null ) {
82 if ( $wgTidyConfig !== null ) {
83 $config = $wgTidyConfig;
84 } elseif ( $wgUseTidy ) {
85 // b/c configuration
86 $config = array(
87 'tidyConfigFile' => $wgTidyConf,
88 'debugComment' => $wgDebugTidy,
89 'tidyBin' => $wgTidyBin,
90 'tidyCommandLine' => $wgTidyOpts );
91 if ( $wgTidyInternal ) {
92 if ( wfIsHHVM() ) {
93 $config['driver'] = 'RaggettInternalHHVM';
94 } else {
95 $config['driver'] = 'RaggettInternalPHP';
96 }
97 } else {
98 $config['driver'] = 'RaggettExternal';
99 }
100 } else {
101 return false;
102 }
103 switch ( $config['driver'] ) {
104 case 'RaggettInternalHHVM':
105 self::$instance = new MediaWiki\Tidy\RaggettInternalHHVM( $config );
106 break;
107 case 'RaggettInternalPHP':
108 self::$instance = new MediaWiki\Tidy\RaggettInternalPHP( $config );
109 break;
110 case 'RaggettExternal':
111 self::$instance = new MediaWiki\Tidy\RaggettExternal( $config );
112 break;
113 default:
114 throw new MWException( "Invalid tidy driver: \"{$config['driver']}\"" );
115 }
116 }
117 return self::$instance;
118 }
119
120 /**
121 * Set the driver to be used. This is for testing.
122 * @param TidyDriverBase|false|null $instance
123 */
124 public static function setInstance( $instance ) {
125 self::$instance = $instance;
126 }
127
128 /**
129 * Destroy the current singleton instance
130 */
131 public static function destroySingleton() {
132 self::$instance = null;
133 }
134 }