Allow callbacks to be passed to $wgContentHandlers
authorBene <benestar.wikimedia@gmail.com>
Fri, 5 Feb 2016 13:40:39 +0000 (14:40 +0100)
committerBene <benestar.wikimedia@gmail.com>
Sat, 6 Feb 2016 08:48:11 +0000 (09:48 +0100)
Change-Id: Icf980313a6e7fcc83f5183c450b0a824353596b8

RELEASE-NOTES-1.27
docs/contenthandler.txt
includes/DefaultSettings.php
includes/content/ContentHandler.php
tests/phpunit/includes/content/ContentHandlerTest.php

index 5b9b2b8..d1f8ca7 100644 (file)
@@ -158,6 +158,8 @@ production.
   aria-describedby, aria-flowto, aria-label, aria-labelledby, aria-owns.
 * Removed "presentation" restriction on the HTML role attribute in wikitext.
   All values are now allowed for the role attribute.
+* $wgContentHandlers now also supports callbacks to create an instance of the
+  appropriate ContentHandler subclass.
 
 === External library changes in 1.27 ===
 
index 5f9a0b0..f1f478e 100644 (file)
@@ -148,7 +148,8 @@ using a model or format different from the default will result in an error.
 
 There are some new globals that can be used to control the behavior of the ContentHandler facility:
 
-* $wgContentHandlers associates content model IDs with the names of the appropriate ContentHandler subclasses.
+* $wgContentHandlers associates content model IDs with the names of the appropriate ContentHandler subclasses
+  or callbacks that create an instance of the appropriate ContentHandler subclass.
 
 * $wgNamespaceContentModels maps namespace IDs to a content model that should be the default for that namespace.
 
index 9c7106f..3ac3c8f 100644 (file)
@@ -904,7 +904,8 @@ $wgMediaHandlers = array(
 
 /**
  * Plugins for page content model handling.
- * Each entry in the array maps a model id to a class name.
+ * Each entry in the array maps a model id to a class name or callback
+ * that creates an instance of the appropriate ContentHandler subclass.
  *
  * @since 1.21
  */
index acaa288..e898e72 100644 (file)
@@ -357,11 +357,16 @@ abstract class ContentHandler {
                                throw new MWException( "ContentHandlerForModelID must supply a ContentHandler instance" );
                        }
                } else {
-                       $class = $wgContentHandlers[$modelId];
-                       $handler = new $class( $modelId );
+                       $classOrCallback = $wgContentHandlers[$modelId];
+
+                       if ( is_callable( $classOrCallback ) ) {
+                               $handler = call_user_func( $classOrCallback, $modelId );
+                       } else {
+                               $handler = new $classOrCallback( $modelId );
+                       }
 
                        if ( !( $handler instanceof ContentHandler ) ) {
-                               throw new MWException( "$class from \$wgContentHandlers is not " .
+                               throw new MWException( "$classOrCallback from \$wgContentHandlers is not " .
                                        "compatible with ContentHandler" );
                        }
                }
index 8178c12..a72247b 100644 (file)
@@ -26,6 +26,9 @@ class ContentHandlerTest extends MediaWikiTestCase {
                                CONTENT_MODEL_CSS => 'CssContentHandler',
                                CONTENT_MODEL_TEXT => 'TextContentHandler',
                                'testing' => 'DummyContentHandlerForTesting',
+                               'testing-callbacks' => function( $modelId ) {
+                                       return new DummyContentHandlerForTesting( $modelId );
+                               }
                        ),
                ) );
 
@@ -378,4 +381,26 @@ class ContentHandlerTest extends MediaWikiTestCase {
 
                return true;
        }
+
+       public function provideGetModelForID() {
+               return array(
+                       array( CONTENT_MODEL_WIKITEXT, 'WikitextContentHandler' ),
+                       array( CONTENT_MODEL_JAVASCRIPT, 'JavaScriptContentHandler' ),
+                       array( CONTENT_MODEL_JSON, 'JsonContentHandler' ),
+                       array( CONTENT_MODEL_CSS, 'CssContentHandler' ),
+                       array( CONTENT_MODEL_TEXT, 'TextContentHandler' ),
+                       array( 'testing', 'DummyContentHandlerForTesting' ),
+                       array( 'testing-callbacks', 'DummyContentHandlerForTesting' ),
+               );
+       }
+
+       /**
+        * @dataProvider provideGetModelForID
+        */
+       public function testGetModelForID( $modelId, $handlerClass ) {
+               $handler = ContentHandler::getForModelID( $modelId );
+
+               $this->assertInstanceOf( $handlerClass, $handler );
+       }
+
 }