Merge "selenium: invoke jobs to enforce eventual consistency"
[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 HTML input fragment. This should not contain a
42 * <body> or <html> tag.
43 * @return string Corrected HTML output
44 * @throws MWException
45 */
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()' );
51 }
52 return $driver->tidy( $text );
53 }
54
55 /**
56 * @return bool
57 */
58 public static function isEnabled() {
59 return self::singleton() !== false;
60 }
61
62 /**
63 * @return bool|\MediaWiki\Tidy\TidyDriverBase
64 */
65 public static function singleton() {
66 global $wgUseTidy, $wgTidyInternal, $wgTidyConf, $wgDebugTidy, $wgTidyConfig,
67 $wgTidyBin, $wgTidyOpts;
68
69 if ( self::$instance === null ) {
70 if ( $wgTidyConfig !== null ) {
71 $config = $wgTidyConfig;
72 } elseif ( $wgUseTidy ) {
73 // b/c configuration
74 wfDeprecated( '$wgUseTidy', '1.26' );
75 $config = [
76 'tidyConfigFile' => $wgTidyConf,
77 'debugComment' => $wgDebugTidy,
78 'tidyBin' => $wgTidyBin,
79 'tidyCommandLine' => $wgTidyOpts ];
80 if ( $wgTidyInternal ) {
81 if ( wfIsHHVM() ) {
82 $config['driver'] = 'RaggettInternalHHVM';
83 } else {
84 $config['driver'] = 'RaggettInternalPHP';
85 }
86 } else {
87 $config['driver'] = 'RaggettExternal';
88 }
89 } else {
90 wfDeprecated( '$wgTidyConfig = null and $wgUseTidy = false', '1.26' );
91 return false;
92 }
93 self::$instance = self::factory( $config );
94 }
95 return self::$instance;
96 }
97
98 /**
99 * Create a new Tidy driver object from configuration.
100 * @see $wgTidyConfig
101 * @param array $config
102 * @return bool|\MediaWiki\Tidy\TidyDriverBase
103 * @throws MWException
104 */
105 public static function factory( array $config ) {
106 switch ( $config['driver'] ) {
107 case 'RaggettInternalHHVM':
108 $instance = new MediaWiki\Tidy\RaggettInternalHHVM( $config );
109 break;
110 case 'RaggettInternalPHP':
111 $instance = new MediaWiki\Tidy\RaggettInternalPHP( $config );
112 break;
113 case 'RaggettExternal':
114 $instance = new MediaWiki\Tidy\RaggettExternal( $config );
115 break;
116 case 'RemexHtml':
117 $instance = new MediaWiki\Tidy\RemexDriver( $config );
118 break;
119 case 'disabled':
120 return false;
121 default:
122 throw new MWException( "Invalid tidy driver: \"{$config['driver']}\"" );
123 }
124 return $instance;
125 }
126
127 /**
128 * Set the driver to be used. This is for testing.
129 * @param MediaWiki\Tidy\TidyDriverBase|false|null $instance
130 */
131 public static function setInstance( $instance ) {
132 self::$instance = $instance;
133 }
134
135 /**
136 * Destroy the current singleton instance
137 */
138 public static function destroySingleton() {
139 self::$instance = null;
140 }
141 }