jenkins-bot has submitted this change and it was merged. Change subject: Allow callbacks to be passed to $wgContentHandlers ......................................................................
Allow callbacks to be passed to $wgContentHandlers Change-Id: Icf980313a6e7fcc83f5183c450b0a824353596b8 --- M RELEASE-NOTES-1.27 M docs/contenthandler.txt M includes/DefaultSettings.php M includes/content/ContentHandler.php M tests/phpunit/includes/content/ContentHandlerTest.php 5 files changed, 39 insertions(+), 5 deletions(-) Approvals: Daniel Kinzler: Looks good to me, approved jenkins-bot: Verified diff --git a/RELEASE-NOTES-1.27 b/RELEASE-NOTES-1.27 index 5b9b2b8..d1f8ca7 100644 --- a/RELEASE-NOTES-1.27 +++ b/RELEASE-NOTES-1.27 @@ -158,6 +158,8 @@ 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 === diff --git a/docs/contenthandler.txt b/docs/contenthandler.txt index 5f9a0b0..f1f478e 100644 --- a/docs/contenthandler.txt +++ b/docs/contenthandler.txt @@ -148,7 +148,8 @@ 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. diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 9c7106f..3ac3c8f 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -904,7 +904,8 @@ /** * 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 */ diff --git a/includes/content/ContentHandler.php b/includes/content/ContentHandler.php index acaa288..e898e72 100644 --- a/includes/content/ContentHandler.php +++ b/includes/content/ContentHandler.php @@ -357,11 +357,16 @@ 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" ); } } diff --git a/tests/phpunit/includes/content/ContentHandlerTest.php b/tests/phpunit/includes/content/ContentHandlerTest.php index 8178c12..a72247b 100644 --- a/tests/phpunit/includes/content/ContentHandlerTest.php +++ b/tests/phpunit/includes/content/ContentHandlerTest.php @@ -26,6 +26,9 @@ CONTENT_MODEL_CSS => 'CssContentHandler', CONTENT_MODEL_TEXT => 'TextContentHandler', 'testing' => 'DummyContentHandlerForTesting', + 'testing-callbacks' => function( $modelId ) { + return new DummyContentHandlerForTesting( $modelId ); + } ), ) ); @@ -378,4 +381,26 @@ 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 ); + } + } -- To view, visit https://gerrit.wikimedia.org/r/268666 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Icf980313a6e7fcc83f5183c450b0a824353596b8 Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Bene <benestar.wikime...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: Jackmcbarn <jackmcb...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits