a29e5fe50664f873e9fcf140dba3b0ffe2f78b4e
[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 * @throws MWException
142 * @throws FatalError
143 * @return Boolean True if no handler aborted the hook
144 */
145 public static function run( $event, $args = array() ) {
146 global $wgHooks;
147
148 // Return quickly in the most common case
149 if ( empty( self::$handlers[$event] ) && empty( $wgHooks[$event] ) ) {
150 return true;
151 }
152
153 wfProfileIn( 'hook: ' . $event );
154 $hooks = self::getHandlers( $event );
155
156 foreach ( $hooks as $hook ) {
157 $object = null;
158 $method = null;
159 $func = null;
160 $data = null;
161 $have_data = false;
162 $closure = false;
163 $badhookmsg = false;
164
165 /**
166 * $hook can be: a function, an object, an array of $function and
167 * $data, an array of just a function, an array of object and
168 * method, or an array of object, method, and data.
169 */
170 if ( is_array( $hook ) ) {
171 if ( count( $hook ) < 1 ) {
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 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
200 }
201 } elseif ( is_string( $hook ) ) { # functions look like strings, too
202 $func = $hook;
203 } elseif ( is_object( $hook ) ) {
204 $object = $hook;
205 if ( $object instanceof Closure ) {
206 $closure = true;
207 } else {
208 $method = "on" . $event;
209 }
210 } else {
211 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
212 }
213
214 /* We put the first data element on, if needed. */
215 if ( $have_data ) {
216 $hook_args = array_merge( array( $data ), $args );
217 } else {
218 $hook_args = $args;
219 }
220
221 if ( $closure ) {
222 $callback = $object;
223 $func = "hook-$event-closure";
224 } elseif ( isset( $object ) ) {
225 $func = get_class( $object ) . '::' . $method;
226 $callback = array( $object, $method );
227 } else {
228 $callback = $func;
229 }
230
231 // Run autoloader (workaround for call_user_func_array bug)
232 is_callable( $callback );
233
234 /**
235 * Call the hook. The documentation of call_user_func_array clearly
236 * states that FALSE is returned on failure. However this is not
237 * case always. In some version of PHP if the function signature
238 * does not match the call signature, PHP will issue an warning:
239 * Param y in x expected to be a reference, value given.
240 *
241 * In that case the call will also return null. The following code
242 * catches that warning and provides better error message. The
243 * function documentation also says that:
244 * In other words, it does not depend on the function signature
245 * whether the parameter is passed by a value or by a reference.
246 * There is also PHP bug http://bugs.php.net/bug.php?id=47554 which
247 * is unsurprisingly marked as bogus. In short handling of failures
248 * with call_user_func_array is a failure, the documentation for that
249 * function is wrong and misleading and PHP developers don't see any
250 * problem here.
251 */
252 $retval = null;
253 set_error_handler( 'Hooks::hookErrorHandler' );
254 wfProfileIn( $func );
255 try {
256 $retval = call_user_func_array( $callback, $hook_args );
257 } catch ( MWHookException $e ) {
258 $badhookmsg = $e->getMessage();
259 }
260 wfProfileOut( $func );
261 restore_error_handler();
262
263 /* String return is an error; false return means stop processing. */
264 if ( is_string( $retval ) ) {
265 throw new FatalError( $retval );
266 } elseif( $retval === null ) {
267 if ( $closure ) {
268 $prettyFunc = "$event closure";
269 } elseif( is_array( $callback ) ) {
270 if( is_object( $callback[0] ) ) {
271 $prettyClass = get_class( $callback[0] );
272 } else {
273 $prettyClass = strval( $callback[0] );
274 }
275 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
276 } else {
277 $prettyFunc = strval( $callback );
278 }
279 if ( $badhookmsg ) {
280 throw new MWException(
281 'Detected bug in an extension! ' .
282 "Hook $prettyFunc has invalid call signature; " . $badhookmsg
283 );
284 } else {
285 throw new MWException(
286 'Detected bug in an extension! ' .
287 "Hook $prettyFunc failed to return a value; " .
288 'should return true to continue hook processing or false to abort.'
289 );
290 }
291 } elseif ( !$retval ) {
292 wfProfileOut( 'hook: ' . $event );
293 return false;
294 }
295 }
296
297 wfProfileOut( 'hook: ' . $event );
298 return true;
299 }
300
301 /**
302 * This REALLY should be protected... but it's public for compatibility
303 *
304 * @since 1.18
305 *
306 * @param $errno int Unused
307 * @param $errstr String: error message
308 * @throws MWHookException
309 * @return Boolean: false
310 */
311 public static function hookErrorHandler( $errno, $errstr ) {
312 if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
313 throw new MWHookException( $errstr );
314 }
315 return false;
316 }
317 }