mark ApiEditPageTest as being slow tests
[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 $name String: 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 /**
62 * Attach an event handler to a given hook
63 *
64 * @since 1.18
65 *
66 * @param $name String: name of hook
67 * @param $callback Mixed: callback function to attach
68 */
69 public static function register( $name, $callback ) {
70 if( !isset( self::$handlers[$name] ) ) {
71 self::$handlers[$name] = array();
72 }
73
74 self::$handlers[$name][] = $callback;
75 }
76
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.
80 *
81 * @since 1.18
82 *
83 * @param $name String: name of hook
84 * @return Boolean: true if the hook has a function registered to it
85 */
86 public static function isRegistered( $name ) {
87 global $wgHooks;
88
89 return !empty( $wgHooks[$name] ) || !empty( self::$handlers[$name] );
90 }
91
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
96 *
97 * @throws MWException
98 * @throws FatalError
99 * @param $name String: name of the hook
100 *
101 * @return array
102 */
103 public static function getHandlers( $name ) {
104 global $wgHooks;
105
106 // Return quickly in the most common case
107 if ( empty( self::$handlers[$name] ) && empty( $wgHooks[$name] ) ) {
108 return array();
109 }
110
111 if ( !is_array( self::$handlers ) ) {
112 throw new MWException( "Local hooks array is not an array!\n" );
113 }
114
115 if ( !is_array( $wgHooks ) ) {
116 throw new MWException( "Global hooks array is not an array!\n" );
117 }
118
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] );
126 }
127
128 if ( !is_array( $hooks ) ) {
129 throw new MWException( "Hooks array for event '$name' is not an array!\n" );
130 }
131
132 return $hooks;
133 }
134
135 /**
136 * Call hook functions defined in Hooks::register
137 *
138 * @param $event String: event name
139 * @param $args Array: parameters passed to hook functions
140 *
141 * @return Boolean True if no handler aborted the hook
142 */
143 public static function run( $event, $args = array() ) {
144 global $wgHooks;
145
146 // Return quickly in the most common case
147 if ( empty( self::$handlers[$event] ) && empty( $wgHooks[$event] ) ) {
148 return true;
149 }
150
151 $hooks = self::getHandlers( $event );
152
153 foreach ( $hooks as $hook ) {
154 $object = null;
155 $method = null;
156 $func = null;
157 $data = null;
158 $have_data = false;
159 $closure = false;
160 $badhookmsg = false;
161
162 /**
163 * $hook can be: a function, an object, an array of $function and
164 * $data, an array of just a function, an array of object and
165 * method, or an array of object, method, and data.
166 */
167 if ( is_array( $hook ) ) {
168 if ( count( $hook ) < 1 ) {
169 throw new MWException( 'Empty array in hooks for ' . $event . "\n" );
170 } elseif ( is_object( $hook[0] ) ) {
171 $object = $hook[0];
172 if ( $object instanceof Closure ) {
173 $closure = true;
174 if ( count( $hook ) > 1 ) {
175 $data = $hook[1];
176 $have_data = true;
177 }
178 } else {
179 if ( count( $hook ) < 2 ) {
180 $method = 'on' . $event;
181 } else {
182 $method = $hook[1];
183 if ( count( $hook ) > 2 ) {
184 $data = $hook[2];
185 $have_data = true;
186 }
187 }
188 }
189 } elseif ( is_string( $hook[0] ) ) {
190 $func = $hook[0];
191 if ( count( $hook ) > 1) {
192 $data = $hook[1];
193 $have_data = true;
194 }
195 } else {
196 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
197 }
198 } elseif ( is_string( $hook ) ) { # functions look like strings, too
199 $func = $hook;
200 } elseif ( is_object( $hook ) ) {
201 $object = $hook;
202 if ( $object instanceof Closure ) {
203 $closure = true;
204 } else {
205 $method = "on" . $event;
206 }
207 } else {
208 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
209 }
210
211 /* We put the first data element on, if needed. */
212 if ( $have_data ) {
213 $hook_args = array_merge( array( $data ), $args );
214 } else {
215 $hook_args = $args;
216 }
217
218 if ( $closure ) {
219 $callback = $object;
220 $func = "hook-$event-closure";
221 } elseif ( isset( $object ) ) {
222 $func = get_class( $object ) . '::' . $method;
223 $callback = array( $object, $method );
224 } else {
225 $callback = $func;
226 }
227
228 // Run autoloader (workaround for call_user_func_array bug)
229 is_callable( $callback );
230
231 /**
232 * Call the hook. The documentation of call_user_func_array clearly
233 * states that FALSE is returned on failure. However this is not
234 * case always. In some version of PHP if the function signature
235 * does not match the call signature, PHP will issue an warning:
236 * Param y in x expected to be a reference, value given.
237 *
238 * In that case the call will also return null. The following code
239 * catches that warning and provides better error message. The
240 * function documentation also says that:
241 * In other words, it does not depend on the function signature
242 * whether the parameter is passed by a value or by a reference.
243 * There is also PHP bug http://bugs.php.net/bug.php?id=47554 which
244 * is unsurprisingly marked as bogus. In short handling of failures
245 * with call_user_func_array is a failure, the documentation for that
246 * function is wrong and misleading and PHP developers don't see any
247 * problem here.
248 */
249 $retval = null;
250 set_error_handler( 'Hooks::hookErrorHandler' );
251 wfProfileIn( $func );
252 try {
253 $retval = call_user_func_array( $callback, $hook_args );
254 } catch ( MWHookException $e ) {
255 $badhookmsg = $e->getMessage();
256 }
257 wfProfileOut( $func );
258 restore_error_handler();
259
260 /* String return is an error; false return means stop processing. */
261 if ( is_string( $retval ) ) {
262 throw new FatalError( $retval );
263 } elseif( $retval === null ) {
264 if ( $closure ) {
265 $prettyFunc = "$event closure";
266 } elseif( is_array( $callback ) ) {
267 if( is_object( $callback[0] ) ) {
268 $prettyClass = get_class( $callback[0] );
269 } else {
270 $prettyClass = strval( $callback[0] );
271 }
272 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
273 } else {
274 $prettyFunc = strval( $callback );
275 }
276 if ( $badhookmsg ) {
277 throw new MWException(
278 'Detected bug in an extension! ' .
279 "Hook $prettyFunc has invalid call signature; " . $badhookmsg
280 );
281 } else {
282 throw new MWException(
283 'Detected bug in an extension! ' .
284 "Hook $prettyFunc failed to return a value; " .
285 'should return true to continue hook processing or false to abort.'
286 );
287 }
288 } elseif ( !$retval ) {
289 return false;
290 }
291 }
292
293 return true;
294 }
295
296 /**
297 * This REALLY should be protected... but it's public for compatibility
298 *
299 * @since 1.18
300 *
301 * @param $errno int Unused
302 * @param $errstr String: error message
303 * @throws MWHookException
304 * @return Boolean: false
305 */
306 public static function hookErrorHandler( $errno, $errstr ) {
307 if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
308 throw new MWHookException( $errstr );
309 }
310 return false;
311 }
312 }