registration: Move attributes out of the top level
authorKunal Mehta <legoktm@member.fsf.org>
Fri, 2 Dec 2016 06:02:28 +0000 (22:02 -0800)
committerKunal Mehta <legoktm@member.fsf.org>
Tue, 16 May 2017 03:09:50 +0000 (20:09 -0700)
This moves attributes out of the top level, and namespaces them under
each extension. If the extension that it belongs to is not installed,
the attribute is not exported and dropped.

The full name of the attribute is the name of the extension plus the
name of the attribute key. This enforces the recommendation that the
attribute name start with the extension's name.

Add test coverage for attributes under manifest_version 1 and 2.

Bug: T133627
Depends-On: I5a148763f68989c8da313a4fb1d0213658ee4495
Depends-On: I5a148763f68989c8da313a4fb1d0213658ee4459
Change-Id: I8613a027c56e2c9d2c6a83ca14749eb1c8fc23be

docs/extension.schema.v2.json
includes/registration/ExtensionProcessor.php
includes/registration/ExtensionRegistry.php
tests/phpunit/includes/registration/ExtensionProcessorTest.php

index a2fdf65..0c476b0 100644 (file)
@@ -2,6 +2,7 @@
        "$schema": "http://json-schema.org/schema#",
        "description": "MediaWiki extension.json schema",
        "type": "object",
+       "additionalProperties": false,
        "properties": {
                "manifest_version": {
                        "type": "integer",
                        "type": "array",
                        "description": "List of service wiring files to be loaded by the default instance of MediaWikiServices"
                },
+               "attributes": {
+                       "description":"Registration information for other extensions",
+                       "type": "object",
+                       "patternProperties": {
+                               ".*": {
+                                       "type": "object",
+                                       "patternProperties": {
+                                               ".*": {
+                                                       "type": ["array", "object"]
+                                               }
+                                       }
+                               }
+                       }
+               },
                "load_composer_autoloader": {
                        "type": "boolean",
                        "description": "Load the composer autoloader for this extension, if one is present"
index 1212f99..d14be3f 100644 (file)
@@ -56,6 +56,16 @@ class ExtensionProcessor implements Processor {
                'ValidSkinNames',
        ];
 
+       /**
+        * Top-level attributes that come from MW core
+        *
+        * @var string[]
+        */
+       protected static $coreAttributes = [
+               'SkinOOUIThemes',
+               'TrackingCategories',
+       ];
+
        /**
         * Mapping of global settings to their specific merge strategies.
         *
@@ -160,6 +170,14 @@ class ExtensionProcessor implements Processor {
         */
        protected $attributes = [];
 
+       /**
+        * Extension attributes, keyed by name =>
+        *  settings.
+        *
+        * @var array
+        */
+       protected $extAttributes = [];
+
        /**
         * @param string $path
         * @param array $info
@@ -186,14 +204,47 @@ class ExtensionProcessor implements Processor {
                        $this->callbacks[$name] = $info['callback'];
                }
 
+               if ( $version === 2 ) {
+                       $this->extractAttributes( $path, $info );
+               }
+
                foreach ( $info as $key => $val ) {
+                       // If it's a global setting,
                        if ( in_array( $key, self::$globalSettings ) ) {
                                $this->storeToArray( $path, "wg$key", $val, $this->globals );
+                               continue;
+                       }
                        // Ignore anything that starts with a @
-                       } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
-                               && !in_array( $key, self::$creditsAttributes )
-                       ) {
-                               $this->storeToArray( $path, $key, $val, $this->attributes );
+                       if ( $key[0] === '@' ) {
+                               continue;
+                       }
+
+                       if ( $version === 2 ) {
+                               // Only whitelisted attributes are set
+                               if ( in_array( $key, self::$coreAttributes ) ) {
+                                       $this->storeToArray( $path, $key, $val, $this->attributes );
+                               }
+                       } else {
+                               // version === 1
+                               if ( !in_array( $key, self::$notAttributes )
+                                       && !in_array( $key, self::$creditsAttributes )
+                               ) {
+                                       // If it's not blacklisted, it's an attribute
+                                       $this->storeToArray( $path, $key, $val, $this->attributes );
+                               }
+                       }
+
+               }
+       }
+
+       /**
+        * @param string $path
+        * @param array $info
+        */
+       protected function extractAttributes( $path, array $info ) {
+               if ( isset( $info['attributes'] ) ) {
+                       foreach ( $info['attributes'] as $extName => $value ) {
+                               $this->storeToArray( $path, $extName, $value, $this->extAttributes );
                        }
                }
        }
@@ -206,6 +257,22 @@ class ExtensionProcessor implements Processor {
                        }
                }
 
+               // Merge $this->extAttributes into $this->attributes depending on what is loaded
+               foreach ( $this->extAttributes as $extName => $value ) {
+                       // Only set the attribute if $extName is loaded (and hence present in credits)
+                       if ( isset( $this->credits[$extName] ) ) {
+                               foreach ( $value as $attrName => $attrValue ) {
+                                       $this->storeToArray(
+                                               '', // Don't provide a path since it's impossible to generate an error here
+                                               $extName . $attrName,
+                                               $attrValue,
+                                               $this->attributes
+                                       );
+                               }
+                               unset( $this->extAttributes[$extName] );
+                       }
+               }
+
                return [
                        'globals' => $this->globals,
                        'defines' => $this->defines,
index 344dd8f..0c832fe 100644 (file)
@@ -31,7 +31,7 @@ class ExtensionRegistry {
        /**
         * Bump whenever the registration cache needs resetting
         */
-       const CACHE_VERSION = 5;
+       const CACHE_VERSION = 6;
 
        /**
         * Special key that defines the merge strategy
index d15725d..ebe0bde 100644 (file)
@@ -447,6 +447,72 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                ];
        }
 
+       /**
+        * Attributes under manifest_version 2
+        *
+        * @covers ExtensionProcessor::extractAttributes
+        * @covers ExtensionProcessor::getExtractedInfo
+        */
+       public function testExtractAttributes() {
+               $processor = new ExtensionProcessor();
+               // Load FooBar extension
+               $processor->extractInfo( $this->dir, [ 'name' => 'FooBar' ], 2 );
+               $processor->extractInfo(
+                       $this->dir,
+                       [
+                               'name' => 'Baz',
+                               'attributes' => [
+                                       // Loaded
+                                       'FooBar' => [
+                                               'Plugins' => [
+                                                       'ext.baz.foobar',
+                                               ],
+                                       ],
+                                       // Not loaded
+                                       'FizzBuzz' => [
+                                               'MorePlugins' => [
+                                                       'ext.baz.fizzbuzz',
+                                               ],
+                                       ],
+                               ],
+                       ],
+                       2
+               );
+
+               $info = $processor->getExtractedInfo();
+               $this->assertArrayHasKey( 'FooBarPlugins', $info['attributes'] );
+               $this->assertSame( [ 'ext.baz.foobar' ], $info['attributes']['FooBarPlugins'] );
+               $this->assertArrayNotHasKey( 'FizzBuzzMorePlugins', $info['attributes'] );
+       }
+
+       /**
+        * Attributes under manifest_version 1
+        *
+        * @covers ExtensionProcessor::extractInfo
+        */
+       public function testAttributes1() {
+               $processor = new ExtensionProcessor();
+               $processor->extractInfo(
+                       $this->dir,
+                       [
+                               'name' => 'FooBar',
+                               'FooBarPlugins' => [
+                                       'ext.baz.foobar',
+                               ],
+                               'FizzBuzzMorePlugins' => [
+                                       'ext.baz.fizzbuzz',
+                               ],
+                       ],
+                       1
+               );
+
+               $info = $processor->getExtractedInfo();
+               $this->assertArrayHasKey( 'FooBarPlugins', $info['attributes'] );
+               $this->assertSame( [ 'ext.baz.foobar' ], $info['attributes']['FooBarPlugins'] );
+               $this->assertArrayHasKey( 'FizzBuzzMorePlugins', $info['attributes'] );
+               $this->assertSame( [ 'ext.baz.fizzbuzz' ], $info['attributes']['FizzBuzzMorePlugins'] );
+       }
+
        public function testGlobalSettingsDocumentedInSchema() {
                global $IP;
                $globalSettings = TestingAccessWrapper::newFromClass(