Kill "* @return void"
[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 class MWHookException extends MWException {}
27
28 /**
29 * Hooks class.
30 *
31 * Used to supersede $wgHooks, because globals are EVIL.
32 */
33 class Hooks {
34
35 protected static $handlers = array();
36
37 /**
38 * Attach an event handler to a given hook
39 *
40 * @param $name Mixed: name of hook
41 * @param $callback Mixed: callback function to attach
42 */
43 public static function register( $name, $callback ) {
44 if( !isset( self::$handlers[$name] ) ) {
45 self::$handlers[$name] = array();
46 }
47
48 self::$handlers[$name][] = $callback;
49 }
50
51 /**
52 * Returns true if a hook has a function registered to it.
53 *
54 * @param $name Mixed: name of hook
55 * @return Boolean: true if a hook has a function registered to it
56 */
57 public static function isRegistered( $name ) {
58 if( !isset( self::$handlers[$name] ) ) {
59 self::$handlers[$name] = array();
60 }
61
62 return ( count( self::$handlers[$name] ) != 0 );
63 }
64
65 /**
66 * Returns an array of all the event functions attached to a hook
67 *
68 * @param $name Mixed: name of the hook
69 * @return array
70 */
71 public static function getHandlers( $name ) {
72 if( !isset( self::$handlers[$name] ) ) {
73 return array();
74 }
75
76 return self::$handlers[$name];
77 }
78
79 /**
80 * Call hook functions defined in Hooks::register
81 *
82 * Because programmers assign to $wgHooks, we need to be very
83 * careful about its contents. So, there's a lot more error-checking
84 * in here than would normally be necessary.
85 *
86 * @param $event String: event name
87 * @param $args Array: parameters passed to hook functions
88 * @return Boolean True if no handler aborted the hook
89 */
90 public static function run( $event, $args = array() ) {
91 global $wgHooks;
92
93 // Return quickly in the most common case
94 if ( !isset( self::$handlers[$event] ) && !isset( $wgHooks[$event] ) ) {
95 return true;
96 }
97
98 if ( !is_array( self::$handlers ) ) {
99 throw new MWException( "Local hooks array is not an array!\n" );
100 }
101
102 if ( !is_array( $wgHooks ) ) {
103 throw new MWException( "Global hooks array is not an array!\n" );
104 }
105
106 $new_handlers = (array) self::$handlers;
107 $old_handlers = (array) $wgHooks;
108
109 $hook_array = array_merge( $new_handlers, $old_handlers );
110
111 if ( !is_array( $hook_array[$event] ) ) {
112 throw new MWException( "Hooks array for event '$event' is not an array!\n" );
113 }
114
115 foreach ( $hook_array[$event] as $index => $hook ) {
116 $object = null;
117 $method = null;
118 $func = null;
119 $data = null;
120 $have_data = false;
121 $closure = false;
122 $badhookmsg = false;
123
124 /**
125 * $hook can be: a function, an object, an array of $function and
126 * $data, an array of just a function, an array of object and
127 * method, or an array of object, method, and data.
128 */
129 if ( is_array( $hook ) ) {
130 if ( count( $hook ) < 1 ) {
131 throw new MWException( 'Empty array in hooks for ' . $event . "\n" );
132 } elseif ( is_object( $hook[0] ) ) {
133 $object = $hook_array[$event][$index][0];
134 if ( $object instanceof Closure ) {
135 $closure = true;
136 if ( count( $hook ) > 1 ) {
137 $data = $hook[1];
138 $have_data = true;
139 }
140 } else {
141 if ( count( $hook ) < 2 ) {
142 $method = 'on' . $event;
143 } else {
144 $method = $hook[1];
145 if ( count( $hook ) > 2 ) {
146 $data = $hook[2];
147 $have_data = true;
148 }
149 }
150 }
151 } elseif ( is_string( $hook[0] ) ) {
152 $func = $hook[0];
153 if ( count( $hook ) > 1) {
154 $data = $hook[1];
155 $have_data = true;
156 }
157 } else {
158 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
159 }
160 } elseif ( is_string( $hook ) ) { # functions look like strings, too
161 $func = $hook;
162 } elseif ( is_object( $hook ) ) {
163 $object = $hook_array[$event][$index];
164 if ( $object instanceof Closure ) {
165 $closure = true;
166 } else {
167 $method = "on" . $event;
168 }
169 } else {
170 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
171 }
172
173 /* We put the first data element on, if needed. */
174 if ( $have_data ) {
175 $hook_args = array_merge( array( $data ), $args );
176 } else {
177 $hook_args = $args;
178 }
179
180 if ( $closure ) {
181 $callback = $object;
182 $func = "hook-$event-closure";
183 } elseif ( isset( $object ) ) {
184 $func = get_class( $object ) . '::' . $method;
185 $callback = array( $object, $method );
186 } else {
187 $callback = $func;
188 }
189
190 // Run autoloader (workaround for call_user_func_array bug)
191 is_callable( $callback );
192
193 /**
194 * Call the hook. The documentation of call_user_func_array clearly
195 * states that FALSE is returned on failure. However this is not
196 * case always. In some version of PHP if the function signature
197 * does not match the call signature, PHP will issue an warning:
198 * Param y in x expected to be a reference, value given.
199 *
200 * In that case the call will also return null. The following code
201 * catches that warning and provides better error message. The
202 * function documentation also says that:
203 * In other words, it does not depend on the function signature
204 * whether the parameter is passed by a value or by a reference.
205 * There is also PHP bug http://bugs.php.net/bug.php?id=47554 which
206 * is unsurprisingly marked as bogus. In short handling of failures
207 * with call_user_func_array is a failure, the documentation for that
208 * function is wrong and misleading and PHP developers don't see any
209 * problem here.
210 */
211 $retval = null;
212 set_error_handler( 'Hooks::hookErrorHandler' );
213 wfProfileIn( $func );
214 try {
215 $retval = call_user_func_array( $callback, $hook_args );
216 } catch ( MWHookException $e ) {
217 $badhookmsg = $e->getMessage();
218 }
219 wfProfileOut( $func );
220 restore_error_handler();
221
222 /* String return is an error; false return means stop processing. */
223 if ( is_string( $retval ) ) {
224 throw new FatalError( $retval );
225 } elseif( $retval === null ) {
226 if ( $closure ) {
227 $prettyFunc = "$event closure";
228 } elseif( is_array( $callback ) ) {
229 if( is_object( $callback[0] ) ) {
230 $prettyClass = get_class( $callback[0] );
231 } else {
232 $prettyClass = strval( $callback[0] );
233 }
234 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
235 } else {
236 $prettyFunc = strval( $callback );
237 }
238 if ( $badhookmsg ) {
239 throw new MWException(
240 'Detected bug in an extension! ' .
241 "Hook $prettyFunc has invalid call signature; " . $badhookmsg
242 );
243 } else {
244 throw new MWException(
245 'Detected bug in an extension! ' .
246 "Hook $prettyFunc failed to return a value; " .
247 'should return true to continue hook processing or false to abort.'
248 );
249 }
250 } elseif ( !$retval ) {
251 return false;
252 }
253 }
254
255 return true;
256 }
257
258 /**
259 * This REALLY should be protected... but it's public for compatibility
260 *
261 * @param $errno Unused
262 * @param $errstr String: error message
263 * @return Boolean: false
264 */
265 public static function hookErrorHandler( $errno, $errstr ) {
266 if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
267 throw new MWHookException( $errstr );
268 }
269 return false;
270 }
271 }