3 * Hooks.php -- a tool for running hook functions
4 * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 * @author Evan Prodromou <evan@wikitravel.org>
27 * Because programmers assign to $wgHooks, we need to be very
28 * careful about its contents. So, there's a lot more error-checking
29 * in here than would normally be necessary.
31 function wfRunHooks($event, $args = array()) {
35 // Return quickly in the most common case
36 if ( !isset( $wgHooks[$event] ) ) {
40 if (!is_array($wgHooks)) {
41 throw new MWException("Global hooks array is not an array!\n");
45 if (!is_array($wgHooks[$event])) {
46 throw new MWException("Hooks array for event '$event' is not an array!\n");
50 foreach ($wgHooks[$event] as $index => $hook) {
59 /* $hook can be: a function, an object, an array of $function and $data,
60 * an array of just a function, an array of object and method, or an
61 * array of object, method, and data.
64 if ( is_array( $hook ) ) {
65 if ( count( $hook ) < 1 ) {
66 throw new MWException("Empty array in hooks for " . $event . "\n");
67 } else if ( is_object( $hook[0] ) ) {
68 $object = $wgHooks[$event][$index][0];
69 if ( $object instanceof Closure ) {
71 if ( count( $hook ) > 1 ) {
76 if ( count( $hook ) < 2 ) {
77 $method = "on" . $event;
80 if ( count( $hook ) > 2 ) {
86 } else if ( is_string( $hook[0] ) ) {
88 if ( count( $hook ) > 1) {
93 throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
95 } else if ( is_string( $hook ) ) { # functions look like strings, too
97 } else if ( is_object( $hook ) ) {
98 $object = $wgHooks[$event][$index];
99 if ( $object instanceof Closure ) {
102 $method = "on" . $event;
105 throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
108 /* We put the first data element on, if needed. */
111 $hook_args = array_merge(array($data), $args);
118 $func = "hook-$event-closure";
119 } elseif ( isset( $object ) ) {
120 $func = get_class( $object ) . '::' . $method;
121 $callback = array( $object, $method );
122 } elseif ( false !== ( $pos = strpos( $func, '::' ) ) ) {
123 $callback = array( substr( $func, 0, $pos ), substr( $func, $pos + 2 ) );
128 // Run autoloader (workaround for call_user_func_array bug)
129 is_callable( $callback );
132 wfProfileIn( $func );
133 $retval = call_user_func_array( $callback, $hook_args );
134 wfProfileOut( $func );
136 /* String return is an error; false return means stop processing. */
138 if ( is_string( $retval ) ) {
140 $wgOut->showFatalError( $retval );
142 } elseif( $retval === null ) {
144 $prettyFunc = "$event closure";
145 } elseif( is_array( $callback ) ) {
146 if( is_object( $callback[0] ) ) {
147 $prettyClass = get_class( $callback[0] );
149 $prettyClass = strval( $callback[0] );
151 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
153 $prettyFunc = strval( $callback );
155 throw new MWException( "Detected bug in an extension! " .
156 "Hook $prettyFunc failed to return a value; " .
157 "should return true to continue hook processing or false to abort." );
158 } else if ( !$retval ) {