* Drop 5.1 compat code
[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 } else {
207 $callback = $func;
208 }
209
210 // Run autoloader (workaround for call_user_func_array bug)
211 is_callable( $callback );
212
213 /**
214 * Call the hook. The documentation of call_user_func_array clearly
215 * states that FALSE is returned on failure. However this is not
216 * case always. In some version of PHP if the function signature
217 * does not match the call signature, PHP will issue an warning:
218 * Param y in x expected to be a reference, value given.
219 *
220 * In that case the call will also return null. The following code
221 * catches that warning and provides better error message. The
222 * function documentation also says that:
223 * In other words, it does not depend on the function signature
224 * whether the parameter is passed by a value or by a reference.
225 * There is also PHP bug http://bugs.php.net/bug.php?id=47554 which
226 * is unsurprisingly marked as bogus. In short handling of failures
227 * with call_user_func_array is a failure, the documentation for that
228 * function is wrong and misleading and PHP developers don't see any
229 * problem here.
230 */
231 $retval = null;
232 set_error_handler( array( 'Hooks', 'hookErrorHandler' ) );
233 wfProfileIn( $func );
234 try {
235 $retval = call_user_func_array( $callback, $hook_args );
236 } catch ( MWHookException $e ) {
237 $badhookmsg = $e->getMessage();
238 }
239 wfProfileOut( $func );
240 restore_error_handler();
241
242 /* String return is an error; false return means stop processing. */
243 if ( is_string( $retval ) ) {
244 global $wgOut;
245 $wgOut->showFatalError( $retval );
246 return false;
247 } elseif( $retval === null ) {
248 if ( $closure ) {
249 $prettyFunc = "$event closure";
250 } elseif( is_array( $callback ) ) {
251 if( is_object( $callback[0] ) ) {
252 $prettyClass = get_class( $callback[0] );
253 } else {
254 $prettyClass = strval( $callback[0] );
255 }
256 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
257 } else {
258 $prettyFunc = strval( $callback );
259 }
260 if ( $badhookmsg ) {
261 throw new MWException(
262 'Detected bug in an extension! ' .
263 "Hook $prettyFunc has invalid call signature; " . $badhookmsg
264 );
265 } else {
266 throw new MWException(
267 'Detected bug in an extension! ' .
268 "Hook $prettyFunc failed to return a value; " .
269 'should return true to continue hook processing or false to abort.'
270 );
271 }
272 } else if ( !$retval ) {
273 return false;
274 }
275 }
276
277 return true;
278 }
279
280 /**
281 * This REALLY should be protected... but it's public for compatibility
282 *
283 * @param $errno Unused
284 * @param $errstr String: error message
285 * @return Boolean: false
286 */
287 public static function hookErrorHandler( $errno, $errstr ) {
288 if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
289 throw new MWHookException( $errstr );
290 }
291 return false;
292 }
293 }