Allow extensions to register additional namespaces in extension.json

Extensions can register additional namespaces by setting the
"TemplateStylesNamespaces" attribte in their extension.json. This change is
analogous to Ia5d34cb78fa6af.

There isn't really a simpler way to do this here, as the config setting uses
namespaces as keys, while the attribute is using them as values. Furthermore,
keys with falsey values are ignored in the config setting, and attributes can't
use the same setting architecture, as array_merge_recursive doesn't maintain
numeric keys.

Bug: T200914
Depends-On: I9e62a02ed2044c847e9ab2dcdfab094001f88986
Change-Id: I2fa9b822ee39bcc5f95a293c8c4aad4d53ede30a
This commit is contained in:
MGChecker 2018-10-18 15:32:49 +02:00 committed by Kunal Mehta
parent c3b4da5cd1
commit f121c39613
3 changed files with 33 additions and 3 deletions

View file

@ -39,6 +39,9 @@
"Models": {
"sanitized-css": "css"
}
},
"TemplateStyles": {
"Namespaces": []
}
},
"callback": "TemplateStylesHooks::onRegistration",

View file

@ -227,7 +227,15 @@ class TemplateStylesHooks {
* @return bool
*/
public static function onContentHandlerDefaultModelFor( $title, &$model ) {
$enabledNamespaces = self::getConfig()->get( 'TemplateStylesNamespaces' );
// Allow overwriting attributes with config settings.
// Attributes can not use namespaces as keys, as processing them does not preserve
// integer keys.
$enabledNamespaces = self::getConfig()->get( 'TemplateStylesNamespaces' ) +
array_fill_keys(
ExtensionRegistry::getInstance()->getAttribute( 'TemplateStylesNamespaces' ),
true
);
if ( !empty( $enabledNamespaces[$title->getNamespace()] ) &&
$title->isSubpage() && substr( $title->getText(), -4 ) === '.css'
) {

View file

@ -117,10 +117,27 @@ class TemplateStylesHooksTest extends MediaWikiLangTestCase {
*/
public function testOnContentHandlerDefaultModelFor( $ns, $title, $expect ) {
$this->setMwGlobals( [
'wgTemplateStylesNamespaces' => [ 10 => true, 2 => false, 3000 => true, 3002 => true ],
'wgNamespacesWithSubpages' => [ 10 => true, 2 => true, 3000 => true, 3002 => false ],
'wgTemplateStylesNamespaces' => [
10 => true,
2 => false,
3000 => true,
3002 => true,
3006 => false,
],
'wgNamespacesWithSubpages' => [
10 => true,
2 => true,
3000 => true,
3002 => false,
3004 => true,
3006 => true
],
] );
$reset = ExtensionRegistry::getInstance()->setAttributeForTest(
'TemplateStylesNamespaces', [ 3004, 3006 ]
);
$model = 'unchanged';
$ret = TemplateStylesHooks::onContentHandlerDefaultModelFor(
Title::makeTitle( $ns, $title ), $model
@ -138,6 +155,8 @@ class TemplateStylesHooksTest extends MediaWikiLangTestCase {
[ 3000, 'Test/test.css', true ],
[ 3002, 'Test/test.css', false ],
[ 2, 'Test/test.css', false ],
[ 3004, 'Test/test.css', true ],
[ 3006, 'Test/test.css', false ],
];
}