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 * Used to supersede $wgHooks, because globals are EVIL.
36 * Array of events mapped to an array of callbacks to be run
37 * when that event is triggered.
39 protected static $handlers = [];
42 * Attach an event handler to a given hook.
44 * @param string $name Name of hook
45 * @param callable $callback Callback function to attach
49 public static function register( $name, $callback ) {
50 self
::$handlers[$name][] = $callback;
54 * Clears hooks registered via Hooks::register(). Does not touch $wgHooks.
55 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
57 * @param string $name The name of the hook to clear.
60 * @throws MWException If not in testing mode.
63 public static function clear( $name ) {
64 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
65 throw new MWException( 'Cannot reset hooks in operation.' );
68 unset( self
::$handlers[$name] );
72 * Returns true if a hook has a function registered to it.
73 * The function may have been registered either via Hooks::register or in $wgHooks.
77 * @param string $name Name of hook
78 * @return bool True if the hook has a function registered to it
80 public static function isRegistered( $name ) {
82 return !empty( $wgHooks[$name] ) ||
!empty( self
::$handlers[$name] );
86 * Returns an array of all the event functions attached to a hook
87 * This combines functions registered via Hooks::register and with $wgHooks.
91 * @param string $name Name of the hook
94 public static function getHandlers( $name ) {
97 if ( !self
::isRegistered( $name ) ) {
99 } elseif ( !isset( self
::$handlers[$name] ) ) {
100 return $wgHooks[$name];
101 } elseif ( !isset( $wgHooks[$name] ) ) {
102 return self
::$handlers[$name];
104 return array_merge( self
::$handlers[$name], $wgHooks[$name] );
109 * @param string $event Event name
110 * @param array|callable $hook
111 * @param array $args Array of parameters passed to hook functions
112 * @param string|null $deprecatedVersion [optional]
113 * @param string &$fname [optional] Readable name of hook [returned]
114 * @return null|string|bool
116 private static function callHook( $event, $hook, array $args, $deprecatedVersion = null,
119 // Turn non-array values into an array. (Can't use casting because of objects.)
120 if ( !is_array( $hook ) ) {
124 if ( !array_filter( $hook ) ) {
125 // Either array is empty or it's an array filled with null/false/empty.
129 if ( is_array( $hook[0] ) ) {
130 // First element is an array, meaning the developer intended
131 // the first element to be a callback. Merge it in so that
132 // processing can be uniform.
133 $hook = array_merge( $hook[0], array_slice( $hook, 1 ) );
137 * $hook can be: a function, an object, an array of $function and
138 * $data, an array of just a function, an array of object and
139 * method, or an array of object, method, and data.
141 if ( $hook[0] instanceof Closure
) {
142 $fname = "hook-$event-closure";
143 $callback = array_shift( $hook );
144 } elseif ( is_object( $hook[0] ) ) {
145 $object = array_shift( $hook );
146 $method = array_shift( $hook );
148 // If no method was specified, default to on$event.
149 if ( $method === null ) {
150 $method = "on$event";
153 $fname = get_class( $object ) . '::' . $method;
154 $callback = [ $object, $method ];
155 } elseif ( is_string( $hook[0] ) ) {
156 $fname = $callback = array_shift( $hook );
158 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
161 // Run autoloader (workaround for call_user_func_array bug)
162 // and throw error if not callable.
163 if ( !is_callable( $callback ) ) {
164 throw new MWException( 'Invalid callback ' . $fname . ' in hooks for ' . $event . "\n" );
167 // mark hook as deprecated, if deprecation version is specified
168 if ( $deprecatedVersion !== null ) {
169 wfDeprecated( "$event hook (used in $fname)", $deprecatedVersion );
173 $hook_args = array_merge( $hook, $args );
174 return call_user_func_array( $callback, $hook_args );
178 * Call hook functions defined in Hooks::register and $wgHooks.
180 * For the given hook event, fetch the array of hook events and
181 * process them. Determine the proper callback for each hook and
182 * then call the actual hook using the appropriate arguments.
183 * Finally, process the return value and return/throw accordingly.
185 * For hook event that are not abortable through a handler's return value,
186 * use runWithoutAbort() instead.
188 * @param string $event Event name
189 * @param array $args Array of parameters passed to hook functions
190 * @param string|null $deprecatedVersion [optional] Mark hook as deprecated with version number
191 * @return bool True if no handler aborted the hook
195 * @throws MWException
196 * @since 1.22 A hook function is not required to return a value for
197 * processing to continue. Not returning a value (or explicitly
198 * returning null) is equivalent to returning true.
200 public static function run( $event, array $args = [], $deprecatedVersion = null ) {
201 foreach ( self
::getHandlers( $event ) as $hook ) {
202 $retval = self
::callHook( $event, $hook, $args, $deprecatedVersion );
203 if ( $retval === null ) {
207 // Process the return value.
208 if ( is_string( $retval ) ) {
209 // String returned means error.
210 throw new FatalError( $retval );
211 } elseif ( $retval === false ) {
212 // False was returned. Stop processing, but no error.
221 * Call hook functions defined in Hooks::register and $wgHooks.
223 * @param string $event Event name
224 * @param array $args Array of parameters passed to hook functions
225 * @param string|null $deprecatedVersion [optional] Mark hook as deprecated with version number
226 * @return bool Always true
227 * @throws MWException If a callback is invalid, unknown
228 * @throws UnexpectedValueException If a callback returns an abort value.
231 public static function runWithoutAbort( $event, array $args = [], $deprecatedVersion = null ) {
232 foreach ( self
::getHandlers( $event ) as $hook ) {
234 $retval = self
::callHook( $event, $hook, $args, $deprecatedVersion, $fname );
235 if ( $retval !== null && $retval !== true ) {
236 throw new UnexpectedValueException( "Invalid return from $fname for unabortable $event." );