(bug 17602) fix Monobook action tabs not quite touching the page body
[lhc/web/wiklou.git] / includes / Hooks.php
1 <?php
2 /**
3 * A tool for running hook functions.
4 *
5 * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
6 *
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.
11 *
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.
16 *
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
20 *
21 * @author Evan Prodromou <evan@wikitravel.org>
22 * @see hooks.txt
23 * @file
24 */
25
26 /**
27 * @since 1.18
28 */
29 class MWHookException extends MWException {}
30
31 /**
32 * Hooks class.
33 *
34 * Used to supersede $wgHooks, because globals are EVIL.
35 *
36 * @since 1.18
37 */
38 class Hooks {
39
40 protected static $handlers = array();
41
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.
45 *
46 * @since 1.21
47 *
48 * @param string $name the name of the hook to clear.
49 *
50 * @throws MWException if not in testing mode.
51 */
52 public static function clear( $name ) {
53 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
54 throw new MWException( 'can not reset hooks in operation.' );
55 }
56
57 unset( self::$handlers[$name] );
58 }
59
60 /**
61 * Attach an event handler to a given hook
62 *
63 * @since 1.18
64 *
65 * @param string $name name of hook
66 * @param $callback Mixed: callback function to attach
67 */
68 public static function register( $name, $callback ) {
69 if( !isset( self::$handlers[$name] ) ) {
70 self::$handlers[$name] = array();
71 }
72
73 self::$handlers[$name][] = $callback;
74 }
75
76 /**
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.
79 *
80 * @since 1.18
81 *
82 * @param string $name name of hook
83 * @return Boolean: true if the hook has a function registered to it
84 */
85 public static function isRegistered( $name ) {
86 global $wgHooks;
87
88 return !empty( $wgHooks[$name] ) || !empty( self::$handlers[$name] );
89 }
90
91 /**
92 * Returns an array of all the event functions attached to a hook
93 * This combines functions registered via Hooks::register and with $wgHooks.
94 * @since 1.18
95 *
96 * @throws MWException
97 * @throws FatalError
98 * @param string $name name of the hook
99 *
100 * @return array
101 */
102 public static function getHandlers( $name ) {
103 global $wgHooks;
104
105 // Return quickly in the most common case
106 if ( empty( self::$handlers[$name] ) && empty( $wgHooks[$name] ) ) {
107 return array();
108 }
109
110 if ( !is_array( self::$handlers ) ) {
111 throw new MWException( "Local hooks array is not an array!\n" );
112 }
113
114 if ( !is_array( $wgHooks ) ) {
115 throw new MWException( "Global hooks array is not an array!\n" );
116 }
117
118 if ( empty( Hooks::$handlers[$name] ) ) {
119 $hooks = $wgHooks[$name];
120 } elseif ( empty( $wgHooks[$name] ) ) {
121 $hooks = Hooks::$handlers[$name];
122 } else {
123 // so they are both not empty...
124 $hooks = array_merge( Hooks::$handlers[$name], $wgHooks[$name] );
125 }
126
127 if ( !is_array( $hooks ) ) {
128 throw new MWException( "Hooks array for event '$name' is not an array!\n" );
129 }
130
131 return $hooks;
132 }
133
134 /**
135 * Call hook functions defined in Hooks::register
136 *
137 * @param string $event event name
138 * @param $args Array: parameters passed to hook functions
139 *
140 * @throws MWException
141 * @throws FatalError
142 * @return Boolean True if no handler aborted the hook
143 */
144 public static function run( $event, $args = array() ) {
145 global $wgHooks;
146
147 // Return quickly in the most common case
148 if ( empty( self::$handlers[$event] ) && empty( $wgHooks[$event] ) ) {
149 return true;
150 }
151
152 wfProfileIn( 'hook: ' . $event );
153 $hooks = self::getHandlers( $event );
154
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;
163
164 /**
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.
168 */
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] ) ) {
174 $object = $hook[0];
175 if ( $object instanceof Closure ) {
176 $closure = true;
177 if ( count( $hook ) > 1 ) {
178 $data = $hook[1];
179 $have_data = true;
180 }
181 } else {
182 if ( count( $hook ) < 2 ) {
183 $method = 'on' . $event;
184 } else {
185 $method = $hook[1];
186 if ( count( $hook ) > 2 ) {
187 $data = $hook[2];
188 $have_data = true;
189 }
190 }
191 }
192 } elseif ( is_string( $hook[0] ) ) {
193 $func = $hook[0];
194 if ( count( $hook ) > 1 ) {
195 $data = $hook[1];
196 $have_data = true;
197 }
198 } else {
199 wfProfileOut( 'hook: ' . $event );
200 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
201 }
202 } elseif ( is_string( $hook ) ) { # functions look like strings, too
203 $func = $hook;
204 } elseif ( is_object( $hook ) ) {
205 $object = $hook;
206 if ( $object instanceof Closure ) {
207 $closure = true;
208 } else {
209 $method = "on" . $event;
210 }
211 } else {
212 wfProfileOut( 'hook: ' . $event );
213 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
214 }
215
216 /* We put the first data element on, if needed. */
217 if ( $have_data ) {
218 $hook_args = array_merge( array( $data ), $args );
219 } else {
220 $hook_args = $args;
221 }
222
223 if ( $closure ) {
224 $callback = $object;
225 $func = "hook-$event-closure";
226 } elseif ( isset( $object ) ) {
227 $func = get_class( $object ) . '::' . $method;
228 $callback = array( $object, $method );
229 } else {
230 $callback = $func;
231 }
232
233 // Run autoloader (workaround for call_user_func_array bug)
234 is_callable( $callback );
235
236 /**
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.
242 *
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
252 * problem here.
253 */
254 $retval = null;
255 set_error_handler( 'Hooks::hookErrorHandler' );
256 wfProfileIn( $func );
257 try {
258 $retval = call_user_func_array( $callback, $hook_args );
259 } catch ( MWHookException $e ) {
260 $badhookmsg = $e->getMessage();
261 }
262 wfProfileOut( $func );
263 restore_error_handler();
264
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 ) {
269 if ( $closure ) {
270 $prettyFunc = "$event closure";
271 } elseif( is_array( $callback ) ) {
272 if( is_object( $callback[0] ) ) {
273 $prettyClass = get_class( $callback[0] );
274 } else {
275 $prettyClass = strval( $callback[0] );
276 }
277 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
278 } else {
279 $prettyFunc = strval( $callback );
280 }
281 if ( $badhookmsg ) {
282 wfProfileOut( 'hook: ' . $event );
283 throw new MWException(
284 'Detected bug in an extension! ' .
285 "Hook $prettyFunc has invalid call signature; " . $badhookmsg
286 );
287 } else {
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.'
293 );
294 }
295 } elseif ( !$retval ) {
296 wfProfileOut( 'hook: ' . $event );
297 return false;
298 }
299 }
300
301 wfProfileOut( 'hook: ' . $event );
302 return true;
303 }
304
305 /**
306 * This REALLY should be protected... but it's public for compatibility
307 *
308 * @since 1.18
309 *
310 * @param int $errno Unused
311 * @param string $errstr error message
312 * @throws MWHookException
313 * @return Boolean: false
314 */
315 public static function hookErrorHandler( $errno, $errstr ) {
316 if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
317 throw new MWHookException( $errstr );
318 }
319 return false;
320 }
321 }