From 7c4d73183cd9a2b383dee213fa8de60aeec399db Mon Sep 17 00:00:00 2001 From: Florian Date: Fri, 29 Apr 2016 23:21:40 +0200 Subject: [PATCH] registration: Allow string value for Hooks Instead of forcing an object even for single-hook-listeners, allow string values, too (one hook listener for one hook, only). Also: use it as default for the conversion script, if only one listener is added to a hook (which is usually the case). This leads into a much cleaner output of the Hooks section of extension.json. Bug: T133628 Change-Id: Ie9e54f0931c41706eb8d82d00256698992ec41cc --- docs/extension.schema.json | 2 +- includes/registration/ExtensionProcessor.php | 8 ++++++-- maintenance/convertExtensionToRegistration.php | 5 ++++- .../includes/registration/ExtensionProcessorTest.php | 8 +++++++- 4 files changed, 18 insertions(+), 5 deletions(-) mode change 100644 => 100755 docs/extension.schema.json mode change 100644 => 100755 includes/registration/ExtensionProcessor.php mode change 100644 => 100755 maintenance/convertExtensionToRegistration.php diff --git a/docs/extension.schema.json b/docs/extension.schema.json old mode 100644 new mode 100755 index 3c2c057b7f..ed3eaa9ad1 --- a/docs/extension.schema.json +++ b/docs/extension.schema.json @@ -684,7 +684,7 @@ "type": "object" }, "Hooks": { - "type": "object", + "type": [ "string", "object" ], "description": "Hooks this extension uses (mapping of hook name to callback)" }, "JobClasses": { diff --git a/includes/registration/ExtensionProcessor.php b/includes/registration/ExtensionProcessor.php old mode 100644 new mode 100755 index f977124cd4..415e6647e1 --- a/includes/registration/ExtensionProcessor.php +++ b/includes/registration/ExtensionProcessor.php @@ -209,8 +209,12 @@ class ExtensionProcessor implements Processor { protected function extractHooks( array $info ) { if ( isset( $info['Hooks'] ) ) { foreach ( $info['Hooks'] as $name => $value ) { - foreach ( (array)$value as $callback ) { - $this->globals['wgHooks'][$name][] = $callback; + if ( is_array( $value ) ) { + foreach ( $value as $callback ) { + $this->globals['wgHooks'][$name][] = $callback; + } + } else { + $this->globals['wgHooks'][$name][] = $value; } } } diff --git a/maintenance/convertExtensionToRegistration.php b/maintenance/convertExtensionToRegistration.php old mode 100644 new mode 100755 index 8993146f8b..f9dd58c299 --- a/maintenance/convertExtensionToRegistration.php +++ b/maintenance/convertExtensionToRegistration.php @@ -216,7 +216,7 @@ class ConvertExtensionToRegistration extends Maintenance { } public function handleHooks( $realName, $value ) { - foreach ( $value as $hookName => $handlers ) { + foreach ( $value as $hookName => &$handlers ) { foreach ( $handlers as $func ) { if ( $func instanceof Closure ) { $this->error( "Error: Closures cannot be converted to JSON. " . @@ -230,6 +230,9 @@ class ConvertExtensionToRegistration extends Maintenance { ); } } + if ( count( $handlers ) === 1 ) { + $handlers = $handlers[0]; + } } $this->json[$realName] = $value; } diff --git a/tests/phpunit/includes/registration/ExtensionProcessorTest.php b/tests/phpunit/includes/registration/ExtensionProcessorTest.php index 27c0c60686..0120d79ec5 100644 --- a/tests/phpunit/includes/registration/ExtensionProcessorTest.php +++ b/tests/phpunit/includes/registration/ExtensionProcessorTest.php @@ -50,7 +50,7 @@ class ExtensionProcessorTest extends MediaWikiTestCase { self::$default, $merge, ], - // No current hooks, adding one for "FooBaz" + // No current hooks, adding one for "FooBaz" in string format [ [], [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default, @@ -62,6 +62,12 @@ class ExtensionProcessorTest extends MediaWikiTestCase { [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default, [ 'FooBaz' => [ 'PriorCallback', 'FooBazCallback' ] ] + $merge, ], + // No current hooks, adding one for "FooBaz" in verbose array format + [ + [], + [ 'Hooks' => [ 'FooBaz' => [ 'FooBazCallback' ] ] ] + self::$default, + [ 'FooBaz' => [ 'FooBazCallback' ] ] + $merge, + ], // Hook for "BarBaz", adding one for "FooBaz" [ [ 'BarBaz' => [ 'BarBazCallback' ] ], -- 2.20.1