From 1b1b3cdb5438e4b649e425e0334c3a72bfca0598 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Mon, 25 Jul 2016 19:56:16 -0700 Subject: [PATCH] Move MediaHandler defaults out of global scope 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 | 19 ++++------------- includes/media/MediaHandlerFactory.php | 29 ++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 1e6030226c..27fdd90fd7 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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. diff --git a/includes/media/MediaHandlerFactory.php b/includes/media/MediaHandlerFactory.php index c6b4e8dbdc..1deecd7b09 100644 --- a/includes/media/MediaHandlerFactory.php +++ b/includes/media/MediaHandlerFactory.php @@ -29,14 +29,39 @@ 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; } -- 2.20.1