diff options
author | Evan Prodromou <evanprodromou@users.mediawiki.org> | 2005-03-13 15:29:43 +0000 |
---|---|---|
committer | Evan Prodromou <evanprodromou@users.mediawiki.org> | 2005-03-13 15:29:43 +0000 |
commit | d63d4248a6c98ee3e9c8772ac9b12e0958a262c8 (patch) | |
tree | d37d4fc186ed16b8afc3851507173eb23b4bb4dd /includes/Hooks.php | |
parent | 06e6cd5d995ea7d52d1c17e663ff8e13218192dd (diff) | |
download | mediawikicore-d63d4248a6c98ee3e9c8772ac9b12e0958a262c8.tar.gz mediawikicore-d63d4248a6c98ee3e9c8772ac9b12e0958a262c8.zip |
Changed the calling protocol for function wfRunHooks() in Hooks.php.
Previously, this function used variable arguments to allow
different hooks to pass different parameters. However, var args
silently convert reference-calling to value-calling. So a call
that used to work like this:
# old
wfRunHooks('SomeEvent', $param1, &$param2, $param3);
...now works like this:
# new
wfRunHooks('SomeEvent', array($param1, &$param2, $param3));
Hook functions can now change pass-by-reference parameters correctly
(e.g. $param2 in the above example).
All calls to wfRunHooks() were changed and tested, and the change
was documented in docs/hooks.doc. This change was originally checked
in on REL1_4 branch as a bugfix, but per vibber reverted and checked
in to HEAD instead.
Notes
Notes:
http://mediawiki.org/wiki/Special:Code/MediaWiki/7679
Diffstat (limited to 'includes/Hooks.php')
-rw-r--r-- | includes/Hooks.php | 174 |
1 files changed, 86 insertions, 88 deletions
diff --git a/includes/Hooks.php b/includes/Hooks.php index b156a6a6733a..9198d004333a 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -1,7 +1,7 @@ <?php /** * Hooks.php -- a tool for running hook functions - * Copyright 2004, Evan Prodromou <evan@wikitravel.org>. + * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,105 +24,103 @@ if (defined('MEDIAWIKI')) { -/** - * Because programmers assign to $wgHooks, we need to be very - * careful about its contents. So, there's a lot more error-checking - * in here than would normally be necessary. - */ -function wfRunHooks() { + /** + * Because programmers assign to $wgHooks, we need to be very + * careful about its contents. So, there's a lot more error-checking + * in here than would normally be necessary. + */ + + function wfRunHooks($event, $args) { - global $wgHooks; - - if (!is_array($wgHooks)) { - wfDieDebugBacktrace("Global hooks array is not an array!\n"); - return false; - } - - $args = func_get_args(); - - if (count($args) < 1) { - wfDieDebugBacktrace("No event name given for wfRunHooks().\n"); - return false; - } - - $event = array_shift($args); - - if (!array_key_exists($event, $wgHooks)) { - return true; - } + global $wgHooks; - if (!is_array($wgHooks[$event])) { - wfDieDebugBacktrace("Hooks array for event '$event' is not an array!\n"); - return false; - } + if (!is_array($wgHooks)) { + wfDieDebugBacktrace("Global hooks array is not an array!\n"); + return false; + } - foreach ($wgHooks[$event] as $hook) { + if (!array_key_exists($event, $wgHooks)) { + return true; + } - $object = NULL; - $method = NULL; - $func = NULL; - $data = NULL; - $have_data = false; - - /* $hook can be: a function, an object, an array of $function and $data, - * an array of just a function, an array of object and method, or an - * array of object, method, and data. - */ + if (!is_array($wgHooks[$event])) { + wfDieDebugBacktrace("Hooks array for event '$event' is not an array!\n"); + return false; + } - if (is_array($hook)) { - if (count($hook) < 1) { - wfDieDebugBacktrace("Empty array in hooks for " . $event . "\n"); - } else if (is_object($hook[0])) { - $object = $hook[0]; - if (count($hook) < 2) { - $method = "on" . $event; - } else { - $method = $hook[1]; - if (count($hook) > 2) { - $data = $hook[2]; + foreach ($wgHooks[$event] as $hook) { + + $object = NULL; + $method = NULL; + $func = NULL; + $data = NULL; + $have_data = false; + + /* $hook can be: a function, an object, an array of $function and $data, + * an array of just a function, an array of object and method, or an + * array of object, method, and data. + */ + + if (is_array($hook)) { + if (count($hook) < 1) { + wfDieDebugBacktrace("Empty array in hooks for " . $event . "\n"); + } else if (is_object($hook[0])) { + $object = $hook[0]; + if (count($hook) < 2) { + $method = "on" . $event; + } else { + $method = $hook[1]; + if (count($hook) > 2) { + $data = $hook[2]; + $have_data = true; + } + } + } else if (is_string($hook[0])) { + $func = $hook[0]; + if (count($hook) > 1) { + $data = $hook[1]; $have_data = true; } + } else { + wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n"); } - } else if (is_string($hook[0])) { - $func = $hook[0]; - if (count($hook) > 1) { - $data = $hook[1]; - $have_data = true; - } + } else if (is_string($hook)) { # functions look like strings, too + $func = $hook; + } else if (is_object($hook)) { + $object = $hook; + $method = "on" . $event; } else { wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n"); } - } else if (is_string($hook)) { # functions look like strings, too - $func = $hook; - } else if (is_object($hook)) { - $object = $hook; - $method = "on" . $event; - } else { - wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n"); - } - - if ($have_data) { - $hook_args = array_merge(array($data), $args); - } else { - $hook_args = $args; - } - - if ($object) { - $retval = call_user_func_array(array($object, $method), $hook_args); - } else { - $retval = call_user_func_array($func, $hook_args); + + /* We put the first data element on, if needed. */ + + if ($have_data) { + $hook_args = array_merge(array($data), $args); + } else { + $hook_args = $args; + } + + /* Call the hook. */ + + if ($object) { + $retval = call_user_func_array(array($object, $method), $hook_args); + } else { + $retval = call_user_func_array($func, $hook_args); + } + + /* String return is an error; false return means stop processing. */ + + if (is_string($retval)) { + global $wgOut; + $wgOut->fatalError($retval); + return false; + } else if (!$retval) { + return false; + } } - if (is_string($retval)) { - global $wgOut; - $wgOut->fatalError($retval); - return false; - } else if (!$retval) { - return false; - } + return true; } - - return true; -} } /* if defined(MEDIAWIKI) */ -?>
\ No newline at end of file +?>
\ No newline at end of file |