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