Merge "shell: Add debug logging to find binaries that aren't being restricted"
[lhc/web/wiklou.git] / includes / media / MediaHandlerFactory.php
index 7e41242..543dc80 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
+       ];
+
+       /**
+        * @var array
+        */
+       private $registry;
+
+       /**
+        * Instance cache of MediaHandler objects by mimetype
+        *
         * @var MediaHandler[]
         */
        private $handlers;
 
+       public function __construct( array $registry ) {
+               $this->registry = $registry + self::$coreHandlers;
+       }
+
+       protected function getHandlerClass( $type ) {
+               if ( isset( $this->registry[$type] ) ) {
+                       return $this->registry[$type];
+               } else {
+                       return false;
+               }
+       }
+
        /**
         * @param string $type mimetype
         * @return bool|MediaHandler
         */
        public function getHandler( $type ) {
-               global $wgMediaHandlers;
-               if ( !isset( $wgMediaHandlers[$type] ) ) {
-                       wfDebug( __METHOD__ . ": no handler found for $type.\n" );
-
-                       return false;
+               if ( isset( $this->handlers[$type] ) ) {
+                       return $this->handlers[$type];
                }
-               $class = $wgMediaHandlers[$type];
-               if ( !isset( $this->handlers[$class] ) ) {
-                       $this->handlers[$class] = new $class;
-                       if ( !$this->handlers[$class]->isEnabled() ) {
+
+               $class = $this->getHandlerClass( $type );
+               if ( $class !== false ) {
+                       /** @var MediaHandler $handler */
+                       $handler = new $class;
+                       if ( !$handler->isEnabled() ) {
                                wfDebug( __METHOD__ . ": $class is not enabled\n" );
-                               $this->handlers[$class] = false;
+                               $handler = false;
                        }
+               } else {
+                       wfDebug( __METHOD__ . ": no handler found for $type.\n" );
+                       $handler = false;
                }
 
-               return $this->handlers[$class];
+               $this->handlers[$type] = $handler;
+               return $handler;
        }
 }