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