Introduce MediaHandlerFactory to create MediaHandler objects
authorKunal Mehta <legoktm@member.fsf.org>
Tue, 26 Jul 2016 02:19:25 +0000 (19:19 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Wed, 3 Aug 2016 23:47:41 +0000 (16:47 -0700)
This will allow further refactoring of override logic in parser tests.

Ideally the factory class would not use $wgMediaHandlers directly, but
that ends up breaking too many tests for now.

Change-Id: I34a63ee7089ff26f86f3dd6f3cd1a37928bc4005

autoload.php
includes/MediaWikiServices.php
includes/ServiceWiring.php
includes/media/MediaHandler.php
includes/media/MediaHandlerFactory.php [new file with mode: 0644]
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/MediaWikiServicesTest.php

index 8c16adf..f9b0f64 100644 (file)
@@ -790,6 +790,7 @@ $wgAutoloadLocalClasses = [
        'MarkpatrolledAction' => __DIR__ . '/includes/actions/MarkpatrolledAction.php',
        'McTest' => __DIR__ . '/maintenance/mctest.php',
        'MediaHandler' => __DIR__ . '/includes/media/MediaHandler.php',
+       'MediaHandlerFactory' => __DIR__ . '/includes/media/MediaHandlerFactory.php',
        'MediaStatisticsPage' => __DIR__ . '/includes/specials/SpecialMediaStatistics.php',
        'MediaTransformError' => __DIR__ . '/includes/media/MediaTransformOutput.php',
        'MediaTransformInvalidParametersException' => __DIR__ . '/includes/media/MediaTransformInvalidParametersException.php',
index ff292cf..ac5fbe0 100644 (file)
@@ -11,6 +11,7 @@ use LBFactory;
 use LinkCache;
 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
 use LoadBalancer;
+use MediaHandlerFactory;
 use MediaWiki\Linker\LinkRenderer;
 use MediaWiki\Linker\LinkRendererFactory;
 use MediaWiki\Services\SalvageableService;
@@ -511,6 +512,14 @@ class MediaWikiServices extends ServiceContainer {
                return $this->getService( 'WatchedItemQueryService' );
        }
 
+       /**
+        * @since 1.28
+        * @return MediaHandlerFactory
+        */
+       public function getMediaHandlerFactory() {
+               return $this->getService( 'MediaHandlerFactory' );
+       }
+
        /**
         * @since 1.28
         * @return GenderCache
index d4f16ee..438f667 100644 (file)
@@ -158,6 +158,10 @@ return [
                return new WatchedItemQueryService( $services->getDBLoadBalancer() );
        },
 
+       'MediaHandlerFactory' => function( MediaWikiServices $services ) {
+               return new MediaHandlerFactory();
+       },
+
        'LinkCache' => function( MediaWikiServices $services ) {
                return new LinkCache(
                        $services->getTitleFormatter()
index 0ebfab7..70a43f2 100644 (file)
@@ -20,6 +20,7 @@
  * @file
  * @ingroup Media
  */
+use MediaWiki\MediaWikiServices;
 
 /**
  * Base media handler class
@@ -36,9 +37,6 @@ abstract class MediaHandler {
         */
        const MAX_ERR_LOG_SIZE = 65535;
 
-       /** @var MediaHandler[] Instance cache with array of MediaHandler */
-       protected static $handlers = [];
-
        /**
         * Get a MediaHandler for a given MIME type from the instance cache
         *
@@ -46,29 +44,8 @@ abstract class MediaHandler {
         * @return MediaHandler|bool
         */
        static function getHandler( $type ) {
-               global $wgMediaHandlers;
-               if ( !isset( $wgMediaHandlers[$type] ) ) {
-                       wfDebug( __METHOD__ . ": no handler found for $type.\n" );
-
-                       return false;
-               }
-               $class = $wgMediaHandlers[$type];
-               if ( !isset( self::$handlers[$class] ) ) {
-                       self::$handlers[$class] = new $class;
-                       if ( !self::$handlers[$class]->isEnabled() ) {
-                               wfDebug( __METHOD__ . ": $class is not enabled\n" );
-                               self::$handlers[$class] = false;
-                       }
-               }
-
-               return self::$handlers[$class];
-       }
-
-       /**
-        * Resets all static caches
-        */
-       public static function resetCache() {
-               self::$handlers = [];
+               return MediaWikiServices::getInstance()
+                       ->getMediaHandlerFactory()->getHandler( $type );
        }
 
        /**
diff --git a/includes/media/MediaHandlerFactory.php b/includes/media/MediaHandlerFactory.php
new file mode 100644 (file)
index 0000000..7e41242
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+/**
+ * Media-handling base classes and generic functionality.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Media
+ */
+
+/**
+ * Class to construct MediaHandler objects
+ *
+ * @since 1.28
+ */
+class MediaHandlerFactory {
+
+       /**
+        * @var MediaHandler[]
+        */
+       private $handlers;
+
+       /**
+        * @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;
+               }
+               $class = $wgMediaHandlers[$type];
+               if ( !isset( $this->handlers[$class] ) ) {
+                       $this->handlers[$class] = new $class;
+                       if ( !$this->handlers[$class]->isEnabled() ) {
+                               wfDebug( __METHOD__ . ": $class is not enabled\n" );
+                               $this->handlers[$class] = false;
+                       }
+               }
+
+               return $this->handlers[$class];
+       }
+}
index 8dfe628..9f09ddd 100644 (file)
@@ -341,7 +341,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                // TODO: move global state into MediaWikiServices
                RequestContext::resetMain();
-               MediaHandler::resetCache();
                if ( session_id() !== '' ) {
                        session_write_close();
                        session_id( '' );
@@ -530,7 +529,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                // TODO: move global state into MediaWikiServices
                RequestContext::resetMain();
-               MediaHandler::resetCache();
                if ( session_id() !== '' ) {
                        session_write_close();
                        session_id( '' );
index d20344d..ac8c43b 100644 (file)
@@ -316,6 +316,7 @@ class MediaWikiServicesTest extends MediaWikiTestCase {
                        'DBLoadBalancer' => [ 'DBLoadBalancer', 'LoadBalancer' ],
                        'WatchedItemStore' => [ 'WatchedItemStore', WatchedItemStore::class ],
                        'WatchedItemQueryService' => [ 'WatchedItemQueryService', WatchedItemQueryService::class ],
+                       'MediaHandlerFactory' => [ 'MediaHandlerFactory', MediaHandlerFactory::class ],
                        'GenderCache' => [ 'GenderCache', GenderCache::class ],
                        'LinkCache' => [ 'LinkCache', LinkCache::class ],
                        'LinkRenderer' => [ 'LinkRenderer', LinkRenderer::class ],