MediaWiki master
MediaHandlerFactory.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Media;
11
14use Psr\Log\LoggerInterface;
15
22
26 private const CORE_HANDLERS = [
27 'image/jpeg' => JpegHandler::class,
28 'image/png' => PNGHandler::class,
29 'image/gif' => GIFHandler::class,
30 'image/tiff' => TiffHandler::class,
31 'image/webp' => WebPHandler::class,
32 'image/x-ms-bmp' => BmpHandler::class,
33 'image/x-bmp' => BmpHandler::class,
34 'image/x-xcf' => XCFHandler::class,
35 'image/svg+xml' => SvgHandler::class, // official
36 'image/svg' => SvgHandler::class, // compat
37 'image/vnd.djvu' => DjVuHandler::class, // official
38 'image/x.djvu' => DjVuHandler::class, // compat
39 'image/x-djvu' => DjVuHandler::class, // compat
40 'image/jp2' => Jpeg2000Handler::class,
41 'image/jpx' => Jpeg2000Handler::class,
42 ];
43
45 private $registry;
46
47 public function __construct(
48 private LanguageFactory $langFactory,
49 private LoggerInterface $logger,
50 array $registry
51 ) {
52 $this->registry = $registry + self::CORE_HANDLERS;
53 }
54
59 protected function getHandlerClass( $type ) {
60 return $this->registry[$type] ?? false;
61 }
62
68 public function getHandler(
69 $type,
70 Language|string|null $lang = null
71 ) {
72 $class = $this->getHandlerClass( $type );
73 if ( $class !== false ) {
75 $handler = new $class;
76 if ( !$handler->isEnabled() ) {
77 $this->logger->debug(
78 '{class} is not enabled.',
79 [ 'class' => $class ]
80 );
81 $handler = false;
82 } elseif ( $lang !== null ) {
83 if ( !$lang instanceof Language ) {
84 $lang = $this->langFactory->getLanguage( $lang );
85 }
86 $handler->setLanguage( $lang );
87 }
88 } else {
89 $this->logger->debug(
90 'no handler found for {type}.',
91 [ 'type' => $type ]
92 );
93 $handler = false;
94 }
95
96 return $handler;
97 }
98}
99
101class_alias( MediaHandlerFactory::class, 'MediaHandlerFactory' );
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Base class for language-specific code.
Definition Language.php:65
Class to construct MediaHandler objects.
__construct(private LanguageFactory $langFactory, private LoggerInterface $logger, array $registry)
getHandler( $type, Language|string|null $lang=null)