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 if (!is_array($wgHooks)) {
36 throw new MWException("Global hooks array is not an array!\n");
40 if (!array_key_exists($event, $wgHooks)) {
44 if (!is_array($wgHooks[$event])) {
45 throw new MWException("Hooks array for event '$event' is not an array!\n");
49 foreach ($wgHooks[$event] as $index => $hook) {
58 /* $hook can be: a function, an object, an array of $function and $data,
59 * an array of just a function, an array of object and method, or an
60 * array of object, method, and data.
63 if ( is_array( $hook ) ) {
64 if ( count( $hook ) < 1 ) {
65 throw new MWException("Empty array in hooks for " . $event . "\n");
66 } else if ( is_object( $hook[0] ) ) {
67 $object = $wgHooks[$event][$index][0];
68 if ( $object instanceof Closure
) {
70 if ( count( $hook ) > 1 ) {
75 if ( count( $hook ) < 2 ) {
76 $method = "on" . $event;
79 if ( count( $hook ) > 2 ) {
85 } else if ( is_string( $hook[0] ) ) {
87 if ( count( $hook ) > 1) {
92 throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
94 } else if ( is_string( $hook ) ) { # functions look like strings, too
96 } else if ( is_object( $hook ) ) {
97 $object = $wgHooks[$event][$index];
98 if ( $object instanceof Closure
) {
101 $method = "on" . $event;
104 throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
107 /* We put the first data element on, if needed. */
110 $hook_args = array_merge(array($data), $args);
117 $func = "hook-$event-closure";
118 } elseif ( isset( $object ) ) {
119 $func = get_class( $object ) . '::' . $method;
120 $callback = array( $object, $method );
121 } elseif ( false !== ( $pos = strpos( $func, '::' ) ) ) {
122 $callback = array( substr( $func, 0, $pos ), substr( $func, $pos +
2 ) );
127 // Run autoloader (workaround for call_user_func_array bug)
128 is_callable( $callback );
131 wfProfileIn( $func );
132 $retval = call_user_func_array( $callback, $hook_args );
133 wfProfileOut( $func );
135 /* String return is an error; false return means stop processing. */
137 if ( is_string( $retval ) ) {
139 $wgOut->showFatalError( $retval );
141 } elseif( $retval === null ) {
143 $prettyFunc = "$event closure";
144 } elseif( is_array( $callback ) ) {
145 if( is_object( $callback[0] ) ) {
146 $prettyClass = get_class( $callback[0] );
148 $prettyClass = strval( $callback[0] );
150 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
152 $prettyFunc = strval( $callback );
154 throw new MWException( "Detected bug in an extension! " .
155 "Hook $prettyFunc failed to return a value; " .
156 "should return true to continue hook processing or false to abort." );
157 } else if ( !$retval ) {