94f63c4dbc3baea3e89c1efa842ccb574c0b44af
2 if ( ! defined( 'MEDIAWIKI' ) )
5 * Hooks.php -- a tool for running hook functions
6 * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * @author Evan Prodromou <evan@wikitravel.org>
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.
33 function wfRunHooks($event, $args = null) {
36 $fname = 'wfRunHooks';
37 wfProfileIn( $fname );
39 if (!is_array($wgHooks)) {
40 wfDebugDieBacktrace("Global hooks array is not an array!\n");
41 wfProfileOut( $fname );
45 if (!array_key_exists($event, $wgHooks)) {
46 wfProfileOut( $fname );
50 if (!is_array($wgHooks[$event])) {
51 wfDebugDieBacktrace("Hooks array for event '$event' is not an array!\n");
52 wfProfileOut( $fname );
56 foreach ($wgHooks[$event] as $index => $hook) {
64 /* $hook can be: a function, an object, an array of $function and $data,
65 * an array of just a function, an array of object and method, or an
66 * array of object, method, and data.
69 if (is_array($hook)) {
70 if (count($hook) < 1) {
71 wfDebugDieBacktrace("Empty array in hooks for " . $event . "\n");
72 } else if (is_object($hook[0])) {
73 $object =& $wgHooks[$event][$index][0];
74 if (count($hook) < 2) {
75 $method = "on" . $event;
78 if (count($hook) > 2) {
83 } else if (is_string($hook[0])) {
85 if (count($hook) > 1) {
90 wfDebugDieBacktrace("Unknown datatype in hooks for " . $event . "\n");
92 } else if (is_string($hook)) { # functions look like strings, too
94 } else if (is_object($hook)) {
95 $object =& $wgHooks[$event][$index];
96 $method = "on" . $event;
98 wfDebugDieBacktrace("Unknown datatype in hooks for " . $event . "\n");
101 /* We put the first data element on, if needed. */
104 $hook_args = array_merge(array($data), $args);
110 if ( isset( $object ) ) {
111 $func = get_class( $object ) . '::' . $method;
115 wfProfileIn( $func );
116 if( isset( $object ) ) {
117 $retval = call_user_func_array(array(&$object, $method), $hook_args);
119 $retval = call_user_func_array($func, $hook_args);
121 wfProfileOut( $func );
123 /* String return is an error; false return means stop processing. */
125 if (is_string($retval)) {
127 $wgOut->fatalError($retval);
128 wfProfileOut( $fname );
130 } else if (!$retval) {
131 wfProfileOut( $fname );
136 wfProfileOut( $fname );