MediaHandlerFactory.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Media-handling base classes and generic functionality.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Media
  22. */
  23. /**
  24. * Class to construct MediaHandler objects
  25. *
  26. * @since 1.28
  27. */
  28. class MediaHandlerFactory {
  29. /**
  30. * Default, MediaWiki core media handlers
  31. *
  32. * @var array
  33. */
  34. private static $coreHandlers = [
  35. 'image/jpeg' => JpegHandler::class,
  36. 'image/png' => PNGHandler::class,
  37. 'image/gif' => GIFHandler::class,
  38. 'image/tiff' => TiffHandler::class,
  39. 'image/webp' => WebPHandler::class,
  40. 'image/x-ms-bmp' => BmpHandler::class,
  41. 'image/x-bmp' => BmpHandler::class,
  42. 'image/x-xcf' => XCFHandler::class,
  43. 'image/svg+xml' => SvgHandler::class, // official
  44. 'image/svg' => SvgHandler::class, // compat
  45. 'image/vnd.djvu' => DjVuHandler::class, // official
  46. 'image/x.djvu' => DjVuHandler::class, // compat
  47. 'image/x-djvu' => DjVuHandler::class, // compat
  48. ];
  49. /**
  50. * @var array
  51. */
  52. private $registry;
  53. /**
  54. * Instance cache of MediaHandler objects by mimetype
  55. *
  56. * @var MediaHandler[]
  57. */
  58. private $handlers;
  59. public function __construct( array $registry ) {
  60. $this->registry = $registry + self::$coreHandlers;
  61. }
  62. protected function getHandlerClass( $type ) {
  63. return $this->registry[$type] ?? false;
  64. }
  65. /**
  66. * @param string $type mimetype
  67. * @return bool|MediaHandler
  68. */
  69. public function getHandler( $type ) {
  70. if ( isset( $this->handlers[$type] ) ) {
  71. return $this->handlers[$type];
  72. }
  73. $class = $this->getHandlerClass( $type );
  74. if ( $class !== false ) {
  75. /** @var MediaHandler $handler */
  76. $handler = new $class;
  77. if ( !$handler->isEnabled() ) {
  78. wfDebug( __METHOD__ . ": $class is not enabled\n" );
  79. $handler = false;
  80. }
  81. } else {
  82. wfDebug( __METHOD__ . ": no handler found for $type.\n" );
  83. $handler = false;
  84. }
  85. $this->handlers[$type] = $handler;
  86. return $handler;
  87. }
  88. }