Split parser related files to have one class in one file
[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 and configure Remex tidy
26 *
27 * @ingroup Parser
28 */
29 class MWTidy {
30 private static $instance;
31
32 /**
33 * Interface with Remex tidy.
34 * If tidy isn't able to correct the markup, the original will be
35 * returned in all its glory with a warning comment appended.
36 *
37 * @param string $text HTML input fragment. This should not contain a
38 * <body> or <html> tag.
39 * @return string Corrected HTML output
40 * @throws MWException
41 */
42 public static function tidy( $text ) {
43 $driver = self::singleton();
44 if ( !$driver ) {
45 throw new MWException( __METHOD__ .
46 ': tidy is disabled, caller should have checked MWTidy::isEnabled()' );
47 }
48 return $driver->tidy( $text );
49 }
50
51 /**
52 * @return bool
53 */
54 public static function isEnabled() {
55 return self::singleton() !== false;
56 }
57
58 /**
59 * @return bool|\MediaWiki\Tidy\TidyDriverBase
60 */
61 public static function singleton() {
62 global $wgTidyConfig;
63 if ( self::$instance === null ) {
64 self::$instance = self::factory( $wgTidyConfig );
65 }
66 return self::$instance;
67 }
68
69 /**
70 * Create a new Tidy driver object from configuration.
71 * @see $wgTidyConfig
72 * @param array|null $config Optional since 1.33
73 * @return bool|\MediaWiki\Tidy\TidyDriverBase
74 * @throws MWException
75 */
76 public static function factory( array $config = null ) {
77 return new MediaWiki\Tidy\RemexDriver( $config ?? [] );
78 }
79
80 /**
81 * Set the driver to be used. This is for testing.
82 * @param MediaWiki\Tidy\TidyDriverBase|false|null $instance
83 * @deprecated Since 1.33
84 */
85 public static function setInstance( $instance ) {
86 wfDeprecated( __METHOD__, '1.33' );
87 self::$instance = $instance;
88 }
89
90 /**
91 * Destroy the current singleton instance
92 */
93 public static function destroySingleton() {
94 self::$instance = null;
95 }
96 }