|
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
|
|
|
*/ |
|
45
|
|
|
public static function tidy( $text ) { |
|
46
|
|
|
$driver = self::singleton(); |
|
47
|
|
|
if ( !$driver ) { |
|
48
|
|
|
throw new MWException( __METHOD__ . |
|
49
|
|
|
': tidy is disabled, caller should have checked MWTidy::isEnabled()' ); |
|
50
|
|
|
} |
|
51
|
|
|
return $driver->tidy( $text ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get CSS modules needed if HTML from the current driver is to be displayed. |
|
56
|
|
|
* |
|
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. |
|
60
|
|
|
* |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function getModuleStyles() { |
|
64
|
|
|
$driver = self::singleton(); |
|
65
|
|
|
if ( $driver && $driver instanceof MediaWiki\Tidy\RaggettBase ) { |
|
66
|
|
|
return [ 'mediawiki.raggett' ]; |
|
67
|
|
|
} else { |
|
68
|
|
|
return []; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Check HTML for errors, used if $wgValidateAllHtml = true. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $text |
|
76
|
|
|
* @param string &$errorStr Return the error string |
|
77
|
|
|
* @return bool Whether the HTML is valid |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function checkErrors( $text, &$errorStr = null ) { |
|
80
|
|
|
$driver = self::singleton(); |
|
81
|
|
|
if ( !$driver ) { |
|
82
|
|
|
throw new MWException( __METHOD__ . |
|
83
|
|
|
': tidy is disabled, caller should have checked MWTidy::isEnabled()' ); |
|
84
|
|
|
} |
|
85
|
|
|
if ( $driver->supportsValidate() ) { |
|
86
|
|
|
return $driver->validate( $text, $errorStr ); |
|
87
|
|
|
} else { |
|
88
|
|
|
throw new MWException( __METHOD__ . ": error text return from HHVM tidy is not supported" ); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public static function isEnabled() { |
|
93
|
|
|
return self::singleton() !== false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected static function singleton() { |
|
97
|
|
|
global $wgUseTidy, $wgTidyInternal, $wgTidyConf, $wgDebugTidy, $wgTidyConfig, |
|
98
|
|
|
$wgTidyBin, $wgTidyOpts; |
|
99
|
|
|
|
|
100
|
|
|
if ( self::$instance === null ) { |
|
101
|
|
|
if ( $wgTidyConfig !== null ) { |
|
102
|
|
|
$config = $wgTidyConfig; |
|
103
|
|
|
} elseif ( $wgUseTidy ) { |
|
104
|
|
|
// b/c configuration |
|
105
|
|
|
$config = [ |
|
106
|
|
|
'tidyConfigFile' => $wgTidyConf, |
|
107
|
|
|
'debugComment' => $wgDebugTidy, |
|
108
|
|
|
'tidyBin' => $wgTidyBin, |
|
109
|
|
|
'tidyCommandLine' => $wgTidyOpts ]; |
|
110
|
|
|
if ( $wgTidyInternal ) { |
|
111
|
|
|
if ( wfIsHHVM() ) { |
|
112
|
|
|
$config['driver'] = 'RaggettInternalHHVM'; |
|
113
|
|
|
} else { |
|
114
|
|
|
$config['driver'] = 'RaggettInternalPHP'; |
|
115
|
|
|
} |
|
116
|
|
|
} else { |
|
117
|
|
|
$config['driver'] = 'RaggettExternal'; |
|
118
|
|
|
} |
|
119
|
|
|
} else { |
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
switch ( $config['driver'] ) { |
|
123
|
|
|
case 'RaggettInternalHHVM': |
|
124
|
|
|
self::$instance = new MediaWiki\Tidy\RaggettInternalHHVM( $config ); |
|
125
|
|
|
break; |
|
126
|
|
|
case 'RaggettInternalPHP': |
|
127
|
|
|
self::$instance = new MediaWiki\Tidy\RaggettInternalPHP( $config ); |
|
128
|
|
|
break; |
|
129
|
|
|
case 'RaggettExternal': |
|
130
|
|
|
self::$instance = new MediaWiki\Tidy\RaggettExternal( $config ); |
|
131
|
|
|
break; |
|
132
|
|
|
case 'Html5Depurate': |
|
133
|
|
|
self::$instance = new MediaWiki\Tidy\Html5Depurate( $config ); |
|
134
|
|
|
break; |
|
135
|
|
|
case 'Html5Internal': |
|
136
|
|
|
self::$instance = new MediaWiki\Tidy\Html5Internal( $config ); |
|
137
|
|
|
break; |
|
138
|
|
|
default: |
|
139
|
|
|
throw new MWException( "Invalid tidy driver: \"{$config['driver']}\"" ); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
return self::$instance; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Set the driver to be used. This is for testing. |
|
147
|
|
|
* @param TidyDriverBase|false|null $instance |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function setInstance( $instance ) { |
|
150
|
|
|
self::$instance = $instance; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Destroy the current singleton instance |
|
155
|
|
|
*/ |
|
156
|
|
|
public static function destroySingleton() { |
|
157
|
|
|
self::$instance = null; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|