Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / includes / parser / MWTidy.php
index 32d8373..01bf2d0 100644 (file)
@@ -41,6 +41,7 @@ class MWTidy {
         * @param string $text HTML input fragment. This should not contain a
         *                     <body> or <html> tag.
         * @return string Corrected HTML output
+        * @throws MWException
         */
        public static function tidy( $text ) {
                $driver = self::singleton();
@@ -57,6 +58,7 @@ class MWTidy {
         * @param string $text
         * @param string &$errorStr Return the error string
         * @return bool Whether the HTML is valid
+        * @throws MWException
         */
        public static function checkErrors( $text, &$errorStr = null ) {
                $driver = self::singleton();
@@ -71,10 +73,16 @@ class MWTidy {
                }
        }
 
+       /**
+        * @return bool
+        */
        public static function isEnabled() {
                return self::singleton() !== false;
        }
 
+       /**
+        * @return bool|\MediaWiki\Tidy\TidyDriverBase
+        */
        protected static function singleton() {
                global $wgUseTidy, $wgTidyInternal, $wgTidyConf, $wgDebugTidy, $wgTidyConfig,
                        $wgTidyBin, $wgTidyOpts;
@@ -101,34 +109,49 @@ class MWTidy {
                        } else {
                                return false;
                        }
-                       switch ( $config['driver'] ) {
-                               case 'RaggettInternalHHVM':
-                                       self::$instance = new MediaWiki\Tidy\RaggettInternalHHVM( $config );
-                                       break;
-                               case 'RaggettInternalPHP':
-                                       self::$instance = new MediaWiki\Tidy\RaggettInternalPHP( $config );
-                                       break;
-                               case 'RaggettExternal':
-                                       self::$instance = new MediaWiki\Tidy\RaggettExternal( $config );
-                                       break;
-                               case 'Html5Depurate':
-                                       self::$instance = new MediaWiki\Tidy\Html5Depurate( $config );
-                                       break;
-                               case 'Html5Internal':
-                                       self::$instance = new MediaWiki\Tidy\Html5Internal( $config );
-                                       break;
-                               case 'disabled':
-                                       return false;
-                               default:
-                                       throw new MWException( "Invalid tidy driver: \"{$config['driver']}\"" );
-                       }
+                       self::$instance = self::factory( $config );
                }
                return self::$instance;
        }
 
+       /**
+        * Create a new Tidy driver object from configuration.
+        * @see $wgTidyConfig
+        * @param array $config
+        * @return bool|\MediaWiki\Tidy\TidyDriverBase
+        * @throws MWException
+        */
+       public static function factory( array $config ) {
+               switch ( $config['driver'] ) {
+                       case 'RaggettInternalHHVM':
+                               $instance = new MediaWiki\Tidy\RaggettInternalHHVM( $config );
+                               break;
+                       case 'RaggettInternalPHP':
+                               $instance = new MediaWiki\Tidy\RaggettInternalPHP( $config );
+                               break;
+                       case 'RaggettExternal':
+                               $instance = new MediaWiki\Tidy\RaggettExternal( $config );
+                               break;
+                       case 'Html5Depurate':
+                               $instance = new MediaWiki\Tidy\Html5Depurate( $config );
+                               break;
+                       case 'Html5Internal':
+                               $instance = new MediaWiki\Tidy\Html5Internal( $config );
+                               break;
+                       case 'RemexHtml':
+                               $instance = new MediaWiki\Tidy\RemexDriver( $config );
+                               break;
+                       case 'disabled':
+                               return false;
+                       default:
+                               throw new MWException( "Invalid tidy driver: \"{$config['driver']}\"" );
+               }
+               return $instance;
+       }
+
        /**
         * Set the driver to be used. This is for testing.
-        * @param TidyDriverBase|false|null $instance
+        * @param MediaWiki\Tidy\TidyDriverBase|false|null $instance
         */
        public static function setInstance( $instance ) {
                self::$instance = $instance;