Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / Hooks.php
blob9e201a229a45398ff19bd8133392f4917fbbf242
1 <?php
2 /**
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>
22 * @see hooks.txt
23 * @file
26 /**
27 * @since 1.18
29 class MWHookException extends MWException {}
31 /**
32 * Hooks class.
34 * Used to supersede $wgHooks, because globals are EVIL.
36 * @since 1.18
38 class Hooks {
40 protected static $handlers = array();
42 /**
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.
46 * @since 1.21
48 * @param $name String: 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 /**
62 * Attach an event handler to a given hook
64 * @since 1.18
66 * @param $name String: name of hook
67 * @param $callback Mixed: callback function to attach
69 public static function register( $name, $callback ) {
70 if( !isset( self::$handlers[$name] ) ) {
71 self::$handlers[$name] = array();
74 self::$handlers[$name][] = $callback;
77 /**
78 * Returns true if a hook has a function registered to it.
79 * The function may have been registered either via Hooks::register or in $wgHooks.
81 * @since 1.18
83 * @param $name String: name of hook
84 * @return Boolean: true if the hook has a function registered to it
86 public static function isRegistered( $name ) {
87 global $wgHooks;
89 return !empty( $wgHooks[$name] ) || !empty( self::$handlers[$name] );
92 /**
93 * Returns an array of all the event functions attached to a hook
94 * This combines functions registered via Hooks::register and with $wgHooks.
95 * @since 1.18
97 * @throws MWException
98 * @throws FatalError
99 * @param $name String: name of the hook
101 * @return array
103 public static function getHandlers( $name ) {
104 global $wgHooks;
106 // Return quickly in the most common case
107 if ( empty( self::$handlers[$name] ) && empty( $wgHooks[$name] ) ) {
108 return array();
111 if ( !is_array( self::$handlers ) ) {
112 throw new MWException( "Local hooks array is not an array!\n" );
115 if ( !is_array( $wgHooks ) ) {
116 throw new MWException( "Global hooks array is not an array!\n" );
119 if ( empty( Hooks::$handlers[$name] ) ) {
120 $hooks = $wgHooks[$name];
121 } elseif ( empty( $wgHooks[$name] ) ) {
122 $hooks = Hooks::$handlers[$name];
123 } else {
124 // so they are both not empty...
125 $hooks = array_merge( Hooks::$handlers[$name], $wgHooks[$name] );
128 if ( !is_array( $hooks ) ) {
129 throw new MWException( "Hooks array for event '$name' is not an array!\n" );
132 return $hooks;
136 * Call hook functions defined in Hooks::register
138 * @param $event String: event name
139 * @param $args Array: parameters passed to hook functions
141 * @throws MWException
142 * @throws FatalError
143 * @return Boolean True if no handler aborted the hook
145 public static function run( $event, $args = array() ) {
146 global $wgHooks;
148 // Return quickly in the most common case
149 if ( empty( self::$handlers[$event] ) && empty( $wgHooks[$event] ) ) {
150 return true;
153 $hooks = self::getHandlers( $event );
155 foreach ( $hooks as $hook ) {
156 $object = null;
157 $method = null;
158 $func = null;
159 $data = null;
160 $have_data = false;
161 $closure = false;
162 $badhookmsg = false;
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 throw new MWException( 'Empty array in hooks for ' . $event . "\n" );
172 } elseif ( is_object( $hook[0] ) ) {
173 $object = $hook[0];
174 if ( $object instanceof Closure ) {
175 $closure = true;
176 if ( count( $hook ) > 1 ) {
177 $data = $hook[1];
178 $have_data = true;
180 } else {
181 if ( count( $hook ) < 2 ) {
182 $method = 'on' . $event;
183 } else {
184 $method = $hook[1];
185 if ( count( $hook ) > 2 ) {
186 $data = $hook[2];
187 $have_data = true;
191 } elseif ( is_string( $hook[0] ) ) {
192 $func = $hook[0];
193 if ( count( $hook ) > 1) {
194 $data = $hook[1];
195 $have_data = true;
197 } else {
198 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
200 } elseif ( is_string( $hook ) ) { # functions look like strings, too
201 $func = $hook;
202 } elseif ( is_object( $hook ) ) {
203 $object = $hook;
204 if ( $object instanceof Closure ) {
205 $closure = true;
206 } else {
207 $method = "on" . $event;
209 } else {
210 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
213 /* We put the first data element on, if needed. */
214 if ( $have_data ) {
215 $hook_args = array_merge( array( $data ), $args );
216 } else {
217 $hook_args = $args;
220 if ( $closure ) {
221 $callback = $object;
222 $func = "hook-$event-closure";
223 } elseif ( isset( $object ) ) {
224 $func = get_class( $object ) . '::' . $method;
225 $callback = array( $object, $method );
226 } else {
227 $callback = $func;
230 // Run autoloader (workaround for call_user_func_array bug)
231 is_callable( $callback );
234 * Call the hook. The documentation of call_user_func_array clearly
235 * states that FALSE is returned on failure. However this is not
236 * case always. In some version of PHP if the function signature
237 * does not match the call signature, PHP will issue an warning:
238 * Param y in x expected to be a reference, value given.
240 * In that case the call will also return null. The following code
241 * catches that warning and provides better error message. The
242 * function documentation also says that:
243 * In other words, it does not depend on the function signature
244 * whether the parameter is passed by a value or by a reference.
245 * There is also PHP bug http://bugs.php.net/bug.php?id=47554 which
246 * is unsurprisingly marked as bogus. In short handling of failures
247 * with call_user_func_array is a failure, the documentation for that
248 * function is wrong and misleading and PHP developers don't see any
249 * problem here.
251 $retval = null;
252 set_error_handler( 'Hooks::hookErrorHandler' );
253 wfProfileIn( $func );
254 try {
255 $retval = call_user_func_array( $callback, $hook_args );
256 } catch ( MWHookException $e ) {
257 $badhookmsg = $e->getMessage();
259 wfProfileOut( $func );
260 restore_error_handler();
262 /* String return is an error; false return means stop processing. */
263 if ( is_string( $retval ) ) {
264 throw new FatalError( $retval );
265 } elseif( $retval === null ) {
266 if ( $closure ) {
267 $prettyFunc = "$event closure";
268 } elseif( is_array( $callback ) ) {
269 if( is_object( $callback[0] ) ) {
270 $prettyClass = get_class( $callback[0] );
271 } else {
272 $prettyClass = strval( $callback[0] );
274 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
275 } else {
276 $prettyFunc = strval( $callback );
278 if ( $badhookmsg ) {
279 throw new MWException(
280 'Detected bug in an extension! ' .
281 "Hook $prettyFunc has invalid call signature; " . $badhookmsg
283 } else {
284 throw new MWException(
285 'Detected bug in an extension! ' .
286 "Hook $prettyFunc failed to return a value; " .
287 'should return true to continue hook processing or false to abort.'
290 } elseif ( !$retval ) {
291 return false;
295 return true;
299 * This REALLY should be protected... but it's public for compatibility
301 * @since 1.18
303 * @param $errno int Unused
304 * @param $errstr String: error message
305 * @throws MWHookException
306 * @return Boolean: false
308 public static function hookErrorHandler( $errno, $errstr ) {
309 if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
310 throw new MWHookException( $errstr );
312 return false;