Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / Hooks.php
blobed8b3edd845f8ab7c65c5168acf6e87bb62aadea
1 <?php
3 /**
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>
23 * @see hooks.txt
24 * @file
27 /**
28 * @since 1.18
30 class MWHookException extends MWException {}
32 /**
33 * Hooks class.
35 * Used to supersede $wgHooks, because globals are EVIL.
37 * @since 1.18
39 class Hooks {
41 /**
42 * Array of events mapped to an array of callbacks to be run
43 * when that event is triggered.
45 protected static $handlers = array();
47 /**
48 * Attach an event handler to a given hook.
50 * @param string $name Name of hook
51 * @param mixed $callback Callback function to attach
53 * @since 1.18
55 public static function register( $name, $callback ) {
56 if ( !isset( self::$handlers[$name] ) ) {
57 self::$handlers[$name] = array();
60 self::$handlers[$name][] = $callback;
63 /**
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.
69 * @since 1.21
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] );
80 /**
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.
84 * @since 1.18
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 ) {
90 global $wgHooks;
91 return !empty( $wgHooks[$name] ) || !empty( self::$handlers[$name] );
94 /**
95 * Returns an array of all the event functions attached to a hook
96 * This combines functions registered via Hooks::register and with $wgHooks.
98 * @since 1.18
100 * @param string $name Name of the hook
101 * @return array
103 public static function getHandlers( $name ) {
104 global $wgHooks;
106 if ( !self::isRegistered( $name ) ) {
107 return array();
108 } elseif ( !isset( self::$handlers[$name] ) ) {
109 return $wgHooks[$name];
110 } elseif ( !isset( $wgHooks[$name] ) ) {
111 return self::$handlers[$name];
112 } else {
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 * @throws MWException
130 * @throws FatalError
132 public static function run( $event, array $args = array() ) {
133 wfProfileIn( 'hook: ' . $event );
134 foreach ( self::getHandlers( $event ) as $hook ) {
135 // Turn non-array values into an array. (Can't use casting because of objects.)
136 if ( !is_array( $hook ) ) {
137 $hook = array( $hook );
140 if ( !array_filter( $hook ) ) {
141 // Either array is empty or it's an array filled with null/false/empty.
142 continue;
143 } elseif ( is_array( $hook[0] ) ) {
144 // First element is an array, meaning the developer intended
145 // the first element to be a callback. Merge it in so that
146 // processing can be uniform.
147 $hook = array_merge( $hook[0], array_slice( $hook, 1 ) );
151 * $hook can be: a function, an object, an array of $function and
152 * $data, an array of just a function, an array of object and
153 * method, or an array of object, method, and data.
155 if ( $hook[0] instanceof Closure ) {
156 $func = "hook-$event-closure";
157 $callback = array_shift( $hook );
158 } elseif ( is_object( $hook[0] ) ) {
159 $object = array_shift( $hook );
160 $method = array_shift( $hook );
162 // If no method was specified, default to on$event.
163 if ( $method === null ) {
164 $method = "on$event";
167 $func = get_class( $object ) . '::' . $method;
168 $callback = array( $object, $method );
169 } elseif ( is_string( $hook[0] ) ) {
170 $func = $callback = array_shift( $hook );
171 } else {
172 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
175 // Run autoloader (workaround for call_user_func_array bug)
176 // and throw error if not callable.
177 if ( !is_callable( $callback ) ) {
178 throw new MWException( 'Invalid callback in hooks for ' . $event . "\n" );
182 * Call the hook. The documentation of call_user_func_array says
183 * false is returned on failure. However, if the function signature
184 * does not match the call signature, PHP will issue an warning and
185 * return null instead. The following code catches that warning and
186 * provides better error message.
188 $retval = null;
189 $badhookmsg = null;
190 $hook_args = array_merge( $hook, $args );
192 // Profile first in case the Profiler causes errors.
193 wfProfileIn( $func );
194 set_error_handler( 'Hooks::hookErrorHandler' );
195 try {
196 $retval = call_user_func_array( $callback, $hook_args );
197 } catch ( MWHookException $e ) {
198 $badhookmsg = $e->getMessage();
200 restore_error_handler();
201 wfProfileOut( $func );
203 // Process the return value.
204 if ( is_string( $retval ) ) {
205 // String returned means error.
206 throw new FatalError( $retval );
207 } elseif ( $badhookmsg !== null ) {
208 // Exception was thrown from Hooks::hookErrorHandler.
209 throw new MWException(
210 'Detected bug in an extension! ' .
211 "Hook $func has invalid call signature; " . $badhookmsg
213 } elseif ( $retval === null ) {
214 // Null was returned. Error.
215 throw new MWException(
216 'Detected bug in an extension! ' .
217 "Hook $func failed to return a value; " .
218 'should return true to continue hook processing or false to abort.'
220 } elseif ( !$retval ) {
221 wfProfileOut( 'hook: ' . $event );
222 // False was returned. Stop processing, but no error.
223 return false;
227 wfProfileOut( 'hook: ' . $event );
228 return true;
232 * Handle PHP errors issued inside a hook. Catch errors that have to do with
233 * a function expecting a reference, and let all others pass through.
235 * This REALLY should be protected... but it's public for compatibility
237 * @since 1.18
239 * @param int $errno Error number (unused)
240 * @param string $errstr Error message
241 * @throws MWHookException If the error has to do with the function signature
242 * @return bool Always returns false
244 public static function hookErrorHandler( $errno, $errstr ) {
245 if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
246 throw new MWHookException( $errstr, $errno );
248 return false;