4 * A tool for running hook functions.
6 * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 * @author Evan Prodromou <evan@wikitravel.org>
30 class MWHookException
extends MWException
{}
35 * Used to supersede $wgHooks, because globals are EVIL.
42 * Array of events mapped to an array of callbacks to be run
43 * when that event is triggered.
45 protected static $handlers = array();
48 * Attach an event handler to a given hook.
50 * @param string $name Name of hook
51 * @param mixed $callback Callback function to attach
55 public static function register( $name, $callback ) {
56 if ( !isset( self
::$handlers[$name] ) ) {
57 self
::$handlers[$name] = array();
60 self
::$handlers[$name][] = $callback;
64 * Clears hooks registered via Hooks::register(). Does not touch $wgHooks.
65 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
67 * @param string $name the name of the hook to clear.
70 * @throws MWException if not in testing mode.
72 public static function clear( $name ) {
73 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
74 throw new MWException( 'Cannot reset hooks in operation.' );
77 unset( self
::$handlers[$name] );
81 * Returns true if a hook has a function registered to it.
82 * The function may have been registered either via Hooks::register or in $wgHooks.
86 * @param string $name Name of hook
87 * @return bool True if the hook has a function registered to it
89 public static function isRegistered( $name ) {
91 return !empty( $wgHooks[$name] ) ||
!empty( self
::$handlers[$name] );
95 * Returns an array of all the event functions attached to a hook
96 * This combines functions registered via Hooks::register and with $wgHooks.
100 * @param string $name Name of the hook
103 public static function getHandlers( $name ) {
106 if ( !self
::isRegistered( $name ) ) {
108 } elseif ( !isset( self
::$handlers[$name] ) ) {
109 return $wgHooks[$name];
110 } elseif ( !isset( $wgHooks[$name] ) ) {
111 return self
::$handlers[$name];
113 return array_merge( self
::$handlers[$name], $wgHooks[$name] );
118 * Call hook functions defined in Hooks::register and $wgHooks.
120 * For a certain hook event, fetch the array of hook events and
121 * process them. Determine the proper callback for each hook and
122 * then call the actual hook using the appropriate arguments.
123 * Finally, process the return value and return/throw accordingly.
125 * @param string $event Event name
126 * @param array $args Array of parameters passed to hook functions
127 * @return bool True if no handler aborted the hook
129 * @since 1.22 A hook function is not required to return a value for
130 * processing to continue. Not returning a value (or explicitly
131 * returning null) is equivalent to returning true.
132 * @throws MWException
135 public static function run( $event, array $args = array() ) {
136 wfProfileIn( 'hook: ' . $event );
137 foreach ( self
::getHandlers( $event ) as $hook ) {
138 // Turn non-array values into an array. (Can't use casting because of objects.)
139 if ( !is_array( $hook ) ) {
140 $hook = array( $hook );
143 if ( !array_filter( $hook ) ) {
144 // Either array is empty or it's an array filled with null/false/empty.
146 } elseif ( is_array( $hook[0] ) ) {
147 // First element is an array, meaning the developer intended
148 // the first element to be a callback. Merge it in so that
149 // processing can be uniform.
150 $hook = array_merge( $hook[0], array_slice( $hook, 1 ) );
154 * $hook can be: a function, an object, an array of $function and
155 * $data, an array of just a function, an array of object and
156 * method, or an array of object, method, and data.
158 if ( $hook[0] instanceof Closure
) {
159 $func = "hook-$event-closure";
160 $callback = array_shift( $hook );
161 } elseif ( is_object( $hook[0] ) ) {
162 $object = array_shift( $hook );
163 $method = array_shift( $hook );
165 // If no method was specified, default to on$event.
166 if ( $method === null ) {
167 $method = "on$event";
170 $func = get_class( $object ) . '::' . $method;
171 $callback = array( $object, $method );
172 } elseif ( is_string( $hook[0] ) ) {
173 $func = $callback = array_shift( $hook );
175 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
178 // Run autoloader (workaround for call_user_func_array bug)
179 // and throw error if not callable.
180 if ( !is_callable( $callback ) ) {
181 throw new MWException( 'Invalid callback in hooks for ' . $event . "\n" );
185 * Call the hook. The documentation of call_user_func_array says
186 * false is returned on failure. However, if the function signature
187 * does not match the call signature, PHP will issue an warning and
188 * return null instead. The following code catches that warning and
189 * provides better error message.
193 $hook_args = array_merge( $hook, $args );
195 // Profile first in case the Profiler causes errors.
196 wfProfileIn( $func );
197 set_error_handler( 'Hooks::hookErrorHandler' );
199 $retval = call_user_func_array( $callback, $hook_args );
200 } catch ( MWHookException
$e ) {
201 $badhookmsg = $e->getMessage();
203 restore_error_handler();
204 wfProfileOut( $func );
206 // Process the return value.
207 if ( is_string( $retval ) ) {
208 // String returned means error.
209 throw new FatalError( $retval );
210 } elseif ( $badhookmsg !== null ) {
211 // Exception was thrown from Hooks::hookErrorHandler.
212 throw new MWException(
213 'Detected bug in an extension! ' .
214 "Hook $func has invalid call signature; " . $badhookmsg
216 } elseif ( $retval === false ) {
217 wfProfileOut( 'hook: ' . $event );
218 // False was returned. Stop processing, but no error.
223 wfProfileOut( 'hook: ' . $event );
228 * Handle PHP errors issued inside a hook. Catch errors that have to do with
229 * a function expecting a reference, and let all others pass through.
231 * This REALLY should be protected... but it's public for compatibility
235 * @param int $errno Error number (unused)
236 * @param string $errstr Error message
237 * @throws MWHookException If the error has to do with the function signature
238 * @return bool Always returns false
240 public static function hookErrorHandler( $errno, $errstr ) {
241 if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
242 throw new MWHookException( $errstr, $errno );