Allow callbacks to be passed to $wgContentHandlers

Change-Id: Icf980313a6e7fcc83f5183c450b0a824353596b8
This commit is contained in:
Bene 2016-02-05 14:40:39 +01:00
parent 829b2e7d01
commit 9e3e3306b0
5 changed files with 39 additions and 5 deletions

View 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 ===

View 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.

View 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
*/

View 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" );
}
}

View 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 );
}
}