3 * A tool for running hook functions.
5 * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 * @author Evan Prodromou <evan@wikitravel.org>
29 class MWHookException
extends MWException
{}
34 * Used to supersede $wgHooks, because globals are EVIL.
40 protected static $handlers = array();
43 * Clears hooks registered via Hooks::register(). Does not touch $wgHooks.
44 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
48 * @param string $name the name of the hook to clear.
50 * @throws MWException if not in testing mode.
52 public static function clear( $name ) {
53 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
54 throw new MWException( 'can not reset hooks in operation.' );
57 unset( self
::$handlers[$name] );
61 * Attach an event handler to a given hook
65 * @param string $name name of hook
66 * @param $callback Mixed: callback function to attach
68 public static function register( $name, $callback ) {
69 if( !isset( self
::$handlers[$name] ) ) {
70 self
::$handlers[$name] = array();
73 self
::$handlers[$name][] = $callback;
77 * Returns true if a hook has a function registered to it.
78 * The function may have been registered either via Hooks::register or in $wgHooks.
82 * @param string $name name of hook
83 * @return Boolean: true if the hook has a function registered to it
85 public static function isRegistered( $name ) {
88 return !empty( $wgHooks[$name] ) ||
!empty( self
::$handlers[$name] );
92 * Returns an array of all the event functions attached to a hook
93 * This combines functions registered via Hooks::register and with $wgHooks.
98 * @param string $name name of the hook
102 public static function getHandlers( $name ) {
105 // Return quickly in the most common case
106 if ( empty( self
::$handlers[$name] ) && empty( $wgHooks[$name] ) ) {
110 if ( !is_array( self
::$handlers ) ) {
111 throw new MWException( "Local hooks array is not an array!\n" );
114 if ( !is_array( $wgHooks ) ) {
115 throw new MWException( "Global hooks array is not an array!\n" );
118 if ( empty( Hooks
::$handlers[$name] ) ) {
119 $hooks = $wgHooks[$name];
120 } elseif ( empty( $wgHooks[$name] ) ) {
121 $hooks = Hooks
::$handlers[$name];
123 // so they are both not empty...
124 $hooks = array_merge( Hooks
::$handlers[$name], $wgHooks[$name] );
127 if ( !is_array( $hooks ) ) {
128 throw new MWException( "Hooks array for event '$name' is not an array!\n" );
135 * Call hook functions defined in Hooks::register
137 * @param string $event event name
138 * @param $args Array: parameters passed to hook functions
140 * @throws MWException
142 * @return Boolean True if no handler aborted the hook
144 public static function run( $event, $args = array() ) {
147 // Return quickly in the most common case
148 if ( empty( self
::$handlers[$event] ) && empty( $wgHooks[$event] ) ) {
152 wfProfileIn( 'hook: ' . $event );
153 $hooks = self
::getHandlers( $event );
155 foreach ( $hooks as $hook ) {
165 * $hook can be: a function, an object, an array of $function and
166 * $data, an array of just a function, an array of object and
167 * method, or an array of object, method, and data.
169 if ( is_array( $hook ) ) {
170 if ( count( $hook ) < 1 ) {
171 wfProfileOut( 'hook: ' . $event );
172 throw new MWException( 'Empty array in hooks for ' . $event . "\n" );
173 } elseif ( is_object( $hook[0] ) ) {
175 if ( $object instanceof Closure
) {
177 if ( count( $hook ) > 1 ) {
182 if ( count( $hook ) < 2 ) {
183 $method = 'on' . $event;
186 if ( count( $hook ) > 2 ) {
192 } elseif ( is_string( $hook[0] ) ) {
194 if ( count( $hook ) > 1 ) {
199 wfProfileOut( 'hook: ' . $event );
200 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
202 } elseif ( is_string( $hook ) ) { # functions look like strings, too
204 } elseif ( is_object( $hook ) ) {
206 if ( $object instanceof Closure
) {
209 $method = "on" . $event;
212 wfProfileOut( 'hook: ' . $event );
213 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
216 /* We put the first data element on, if needed. */
218 $hook_args = array_merge( array( $data ), $args );
225 $func = "hook-$event-closure";
226 } elseif ( isset( $object ) ) {
227 $func = get_class( $object ) . '::' . $method;
228 $callback = array( $object, $method );
233 // Run autoloader (workaround for call_user_func_array bug)
234 is_callable( $callback );
237 * Call the hook. The documentation of call_user_func_array clearly
238 * states that FALSE is returned on failure. However this is not
239 * case always. In some version of PHP if the function signature
240 * does not match the call signature, PHP will issue an warning:
241 * Param y in x expected to be a reference, value given.
243 * In that case the call will also return null. The following code
244 * catches that warning and provides better error message. The
245 * function documentation also says that:
246 * In other words, it does not depend on the function signature
247 * whether the parameter is passed by a value or by a reference.
248 * There is also PHP bug http://bugs.php.net/bug.php?id=47554 which
249 * is unsurprisingly marked as bogus. In short handling of failures
250 * with call_user_func_array is a failure, the documentation for that
251 * function is wrong and misleading and PHP developers don't see any
255 set_error_handler( 'Hooks::hookErrorHandler' );
256 wfProfileIn( $func );
258 $retval = call_user_func_array( $callback, $hook_args );
259 } catch ( MWHookException
$e ) {
260 $badhookmsg = $e->getMessage();
262 wfProfileOut( $func );
263 restore_error_handler();
265 /* String return is an error; false return means stop processing. */
266 if ( is_string( $retval ) ) {
267 throw new FatalError( $retval );
268 } elseif( $retval === null ) {
270 $prettyFunc = "$event closure";
271 } elseif( is_array( $callback ) ) {
272 if( is_object( $callback[0] ) ) {
273 $prettyClass = get_class( $callback[0] );
275 $prettyClass = strval( $callback[0] );
277 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
279 $prettyFunc = strval( $callback );
282 wfProfileOut( 'hook: ' . $event );
283 throw new MWException(
284 'Detected bug in an extension! ' .
285 "Hook $prettyFunc has invalid call signature; " . $badhookmsg
288 wfProfileOut( 'hook: ' . $event );
289 throw new MWException(
290 'Detected bug in an extension! ' .
291 "Hook $prettyFunc failed to return a value; " .
292 'should return true to continue hook processing or false to abort.'
295 } elseif ( !$retval ) {
296 wfProfileOut( 'hook: ' . $event );
301 wfProfileOut( 'hook: ' . $event );
306 * This REALLY should be protected... but it's public for compatibility
310 * @param int $errno Unused
311 * @param string $errstr error message
312 * @throws MWHookException
313 * @return Boolean: false
315 public static function hookErrorHandler( $errno, $errstr ) {
316 if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
317 throw new MWHookException( $errstr );