Move MediaHandler defaults out of global scope
authorKunal Mehta <legoktm@member.fsf.org>
Tue, 26 Jul 2016 02:56:16 +0000 (19:56 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Wed, 3 Aug 2016 23:47:46 +0000 (16:47 -0700)
The defaults that were in $wgMediaHandlers are now listed in
MediaHandlerFactory.

The main advantage of doing this is we get O(1) replacement when
extensions set a media handler in their extension.json.

Bug: T141305
Change-Id: I05771a673837ab8d6331eedc24eb707be7f3a250

includes/DefaultSettings.php
includes/media/MediaHandlerFactory.php

index 1e60302..27fdd90 100644 (file)
@@ -941,22 +941,11 @@ $wgTrustedMediaFormats = [
 /**
  * Plugins for media file type handling.
  * Each entry in the array maps a MIME type to a class name
+ *
+ * Core media handlers are listed in MediaHandlerFactory,
+ * and extensions should use extension.json.
  */
-$wgMediaHandlers = [
-       'image/jpeg' => 'JpegHandler',
-       'image/png' => 'PNGHandler',
-       'image/gif' => 'GIFHandler',
-       'image/tiff' => 'TiffHandler',
-       'image/webp' => 'WebPHandler',
-       'image/x-ms-bmp' => 'BmpHandler',
-       'image/x-bmp' => 'BmpHandler',
-       'image/x-xcf' => 'XCFHandler',
-       'image/svg+xml' => 'SvgHandler', // official
-       'image/svg' => 'SvgHandler', // compat
-       'image/vnd.djvu' => 'DjVuHandler', // official
-       'image/x.djvu' => 'DjVuHandler', // compat
-       'image/x-djvu' => 'DjVuHandler', // compat
-];
+$wgMediaHandlers = [];
 
 /**
  * Plugins for page content model handling.
index c6b4e8d..1deecd7 100644 (file)
 class MediaHandlerFactory {
 
        /**
+        * Default, MediaWiki core media handlers
+        *
+        * @var array
+        */
+       private static $coreHandlers = [
+               'image/jpeg' => JpegHandler::class,
+               'image/png' => PNGHandler::class,
+               'image/gif' => GIFHandler::class,
+               'image/tiff' => TiffHandler::class,
+               'image/webp' => WebPHandler::class,
+               'image/x-ms-bmp' => BmpHandler::class,
+               'image/x-bmp' => BmpHandler::class,
+               'image/x-xcf' => XCFHandler::class,
+               'image/svg+xml' => SvgHandler::class, // official
+               'image/svg' => SvgHandler::class, // compat
+               'image/vnd.djvu' => DjVuHandler::class, // official
+               'image/x.djvu' => DjVuHandler::class, // compat
+               'image/x-djvu' => DjVuHandler::class, // compat
+       ];
+
+       /**
+        * Instance cache of MediaHandler objects by mimetype
+        *
         * @var MediaHandler[]
         */
        private $handlers;
 
        protected function getHandlerClass( $type ) {
                global $wgMediaHandlers;
-               if ( isset( $wgMediaHandlers[$type] ) ) {
-                       return $wgMediaHandlers[$type];
+
+               $registry = $wgMediaHandlers + self::$coreHandlers;
+               if ( isset( $registry[$type] ) ) {
+                       return $registry[$type];
                } else {
                        return false;
                }