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
This commit is contained in:
Florian 2016-04-29 23:21:40 +02:00 committed by Kunal Mehta
parent e7f803b071
commit 7c4d73183c
4 changed files with 18 additions and 5 deletions

2
docs/extension.schema.json Normal file → Executable file
View file

@ -684,7 +684,7 @@
"type": "object"
},
"Hooks": {
"type": "object",
"type": [ "string", "object" ],
"description": "Hooks this extension uses (mapping of hook name to callback)"
},
"JobClasses": {

8
includes/registration/ExtensionProcessor.php Normal file → Executable file
View file

@ -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;
}
}
}

5
maintenance/convertExtensionToRegistration.php Normal file → Executable file
View file

@ -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;
}

View file

@ -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' ] ],