From ab224f821196f8723acbf9ff81c89d311c93a8e2 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 18 Oct 2016 01:23:56 -0400 Subject: [PATCH] Added array_replace_recursive merge strategy 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 | 1 + docs/extension.schema.v1.json | 1 + includes/registration/ExtensionRegistry.php | 3 ++ .../registration/ExtensionRegistryTest.php | 35 +++++++++++++++++++ 4 files changed, 40 insertions(+) diff --git a/docs/extension.schema.json b/docs/extension.schema.json index e408d03b07..2cf5fe11b1 100644 --- a/docs/extension.schema.json +++ b/docs/extension.schema.json @@ -672,6 +672,7 @@ "type": "string", "enum": [ "array_merge_recursive", + "array_replace_recursive", "array_plus_2d", "array_plus", "array_merge" diff --git a/docs/extension.schema.v1.json b/docs/extension.schema.v1.json index 6d48a06877..8659dffa38 100644 --- a/docs/extension.schema.v1.json +++ b/docs/extension.schema.v1.json @@ -660,6 +660,7 @@ "type": "string", "enum": [ "array_merge_recursive", + "array_replace_recursive", "array_plus_2d", "array_plus", "array_merge" diff --git a/includes/registration/ExtensionRegistry.php b/includes/registration/ExtensionRegistry.php index 78ec148138..0236ea2425 100644 --- a/includes/registration/ExtensionRegistry.php +++ b/includes/registration/ExtensionRegistry.php @@ -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; diff --git a/tests/phpunit/includes/registration/ExtensionRegistryTest.php b/tests/phpunit/includes/registration/ExtensionRegistryTest.php index 167f52a16f..1de42650fd 100644 --- a/tests/phpunit/includes/registration/ExtensionRegistryTest.php +++ b/tests/phpunit/includes/registration/ExtensionRegistryTest.php @@ -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', + ], + ], + ], + ], + ], ]; } } -- 2.20.1