Added array_replace_recursive merge strategy
authorYuri Astrakhan <yurik@wikimedia.org>
Tue, 18 Oct 2016 05:23:56 +0000 (01:23 -0400)
committerReedy <reedy@wikimedia.org>
Mon, 24 Oct 2016 19:20:04 +0000 (19:20 +0000)
For extension registry, add array_replace_recursive merge strategy,
as some extensions/configuration may prefer that to array_merge_recursive.

In some cases, configuration is merged from multiple extensions,
such as JsonConfig's $wgJsonConfigs configuration: ZeroBanner defines

"JsonZeroConfig": {
"namespace": 480,
"nsName": "Zero",
"isLocal": false,
"cacheKey": "1"
}

and mobile.php overrides it with
$wgJsonConfigs['JsonZeroConfig']['isLocal'] = false;
$wgJsonConfigs['JsonZeroConfig']['remote'] = [
'url' => 'https://zero.wikimedia.org/w/api.php',
'username' => $wmgZeroPortalApiUserName,
'password' => $wmgZeroPortalApiPassword,
];

Having identical value 'isLocal' would be converted into an array
if array_merge_recursive is used, but the replace strategy fixes it.

Change-Id: Ica6ddd0ae76f23e60de9b6235c6e2a3f2754a95d

docs/extension.schema.json
docs/extension.schema.v1.json
includes/registration/ExtensionRegistry.php
tests/phpunit/includes/registration/ExtensionRegistryTest.php

index e408d03..2cf5fe1 100644 (file)
                                                        "type": "string",
                                                        "enum": [
                                                                "array_merge_recursive",
+                                                               "array_replace_recursive",
                                                                "array_plus_2d",
                                                                "array_plus",
                                                                "array_merge"
index 6d48a06..8659dff 100644 (file)
                                                        "type": "string",
                                                        "enum": [
                                                                "array_merge_recursive",
+                                                               "array_replace_recursive",
                                                                "array_plus_2d",
                                                                "array_plus",
                                                                "array_merge"
index 78ec148..0236ea2 100644 (file)
@@ -258,6 +258,9 @@ class ExtensionRegistry {
                                case 'array_merge_recursive':
                                        $GLOBALS[$key] = array_merge_recursive( $GLOBALS[$key], $val );
                                        break;
+                               case 'array_replace_recursive':
+                                       $GLOBALS[$key] = array_replace_recursive( $GLOBALS[$key], $val );
+                                       break;
                                case 'array_plus_2d':
                                        $GLOBALS[$key] = wfArrayPlus2d( $GLOBALS[$key], $val );
                                        break;
index 167f52a..1de4265 100644 (file)
@@ -252,6 +252,41 @@ class ExtensionRegistryTest extends MediaWikiTestCase {
                                        'mwtestT100767' => false,
                                ],
                        ],
+                       [
+                               'test array_replace_recursive',
+                               [
+                                       'mwtestJsonConfigs' => [
+                                               'JsonZeroConfig' => [
+                                                       'namespace' => 480,
+                                                       'nsName' => 'Zero',
+                                                       'isLocal' => false,
+                                               ],
+                                       ],
+                               ],
+                               [
+                                       'mwtestJsonConfigs' => [
+                                               'JsonZeroConfig' => [
+                                                       'isLocal' => false,
+                                                       'remote' => [
+                                                               'username' => 'foo',
+                                                       ],
+                                               ],
+                                               ExtensionRegistry::MERGE_STRATEGY => 'array_replace_recursive',
+                                       ],
+                               ],
+                               [
+                                       'mwtestJsonConfigs' => [
+                                               'JsonZeroConfig' => [
+                                                       'namespace' => 480,
+                                                       'nsName' => 'Zero',
+                                                       'isLocal' => false,
+                                                       'remote' => [
+                                                               'username' => 'foo',
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                       ],
                ];
        }
 }