3 * A tool for running hook functions.
5 * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
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.
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.
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
21 * @author Evan Prodromou <evan@wikitravel.org>
28 * Call hook functions defined in $wgHooks
30 * Because programmers assign to $wgHooks, we need to be very
31 * careful about its contents. So, there's a lot more error-checking
32 * in here than would normally be necessary.
34 * @param $event String: event name
35 * @param $args Array: parameters passed to hook functions
38 function wfRunHooks($event, $args = array()) {
42 // Return quickly in the most common case
43 if ( !isset( $wgHooks[$event] ) ) {
47 if (!is_array($wgHooks)) {
48 throw new MWException("Global hooks array is not an array!\n");
52 if (!is_array($wgHooks[$event])) {
53 throw new MWException("Hooks array for event '$event' is not an array!\n");
57 foreach ($wgHooks[$event] as $index => $hook) {
67 /* $hook can be: a function, an object, an array of $function and $data,
68 * an array of just a function, an array of object and method, or an
69 * array of object, method, and data.
72 if ( is_array( $hook ) ) {
73 if ( count( $hook ) < 1 ) {
74 throw new MWException("Empty array in hooks for " . $event . "\n");
75 } else if ( is_object( $hook[0] ) ) {
76 $object = $wgHooks[$event][$index][0];
77 if ( $object instanceof Closure
) {
79 if ( count( $hook ) > 1 ) {
84 if ( count( $hook ) < 2 ) {
85 $method = "on" . $event;
88 if ( count( $hook ) > 2 ) {
94 } else if ( is_string( $hook[0] ) ) {
96 if ( count( $hook ) > 1) {
101 throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
103 } else if ( is_string( $hook ) ) { # functions look like strings, too
105 } else if ( is_object( $hook ) ) {
106 $object = $wgHooks[$event][$index];
107 if ( $object instanceof Closure
) {
110 $method = "on" . $event;
113 throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
116 /* We put the first data element on, if needed. */
119 $hook_args = array_merge(array($data), $args);
126 $func = "hook-$event-closure";
127 } elseif ( isset( $object ) ) {
128 $func = get_class( $object ) . '::' . $method;
129 $callback = array( $object, $method );
130 } elseif ( false !== ( $pos = strpos( $func, '::' ) ) ) {
131 $callback = array( substr( $func, 0, $pos ), substr( $func, $pos +
2 ) );
136 // Run autoloader (workaround for call_user_func_array bug)
137 is_callable( $callback );
139 /* Call the hook. The documentation of call_user_func_array clearly
140 * states that FALSE is returned on failure. However this is not
141 * case always. In some version of PHP if the function signature
142 * does not match the call signature, PHP will issue an warning:
143 * Param y in x expected to be a reference, value given.
145 * In that case the call will also return null. The following code
146 * catches that warning and provides better error message. The
147 * function documentation also says that:
148 * In other words, it does not depend on the function signature
149 * whether the parameter is passed by a value or by a reference.
150 * There is also PHP bug http://bugs.php.net/bug.php?id=47554 which
151 * is unsurprisingly marked as bogus. In short handling of failures
152 * with call_user_func_array is a failure, the documentation for that
153 * function is wrong and misleading and PHP developers don't see any
157 set_error_handler( 'hookErrorHandler' );
158 wfProfileIn( $func );
160 $retval = call_user_func_array( $callback, $hook_args );
161 } catch ( MWHookException
$e ) {
162 $badhookmsg = $e->getMessage();
164 wfProfileOut( $func );
165 restore_error_handler();
167 /* String return is an error; false return means stop processing. */
168 if ( is_string( $retval ) ) {
170 $wgOut->showFatalError( $retval );
172 } elseif( $retval === null ) {
174 $prettyFunc = "$event closure";
175 } elseif( is_array( $callback ) ) {
176 if( is_object( $callback[0] ) ) {
177 $prettyClass = get_class( $callback[0] );
179 $prettyClass = strval( $callback[0] );
181 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
183 $prettyFunc = strval( $callback );
186 throw new MWException( "Detected bug in an extension! " .
187 "Hook $prettyFunc has invalid call signature; " . $badhookmsg );
189 throw new MWException( "Detected bug in an extension! " .
190 "Hook $prettyFunc failed to return a value; " .
191 "should return true to continue hook processing or false to abort." );
193 } else if ( !$retval ) {
201 function hookErrorHandler( $errno, $errstr ) {
202 if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
203 throw new MWHookException( $errstr );
208 class MWHookException
extends MWException
{}