3 * Hooks.php -- a tool for running hook functions
4 * Copyright 2004, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * @author <evan@wikitravel.org>
25 if (defined('MEDIAWIKI')) {
28 * Because programmers assign to $wgHooks, we need to be very
29 * careful about its contents. So, there's a lot more error-checking
30 * in here than would normally be necessary.
32 function wfRunHooks() {
36 if (!is_array($wgHooks)) {
37 wfDieDebugBacktrace("Global hooks array is not an array!\n");
41 $args = func_get_args();
43 if (count($args) < 1) {
44 wfDieDebugBacktrace("No event name given for wfRunHooks().\n");
48 $event = array_shift($args);
50 if (!array_key_exists($event, $wgHooks)) {
54 if (!is_array($wgHooks[$event])) {
55 wfDieDebugBacktrace("Hooks array for event '$event' is not an array!\n");
59 foreach ($wgHooks[$event] as $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 wfDieDebugBacktrace("Empty array in hooks for " . $event . "\n");
75 } else if (is_object($hook[0])) {
77 if (count($hook) < 2) {
78 $method = "on" . $event;
81 if (count($hook) > 2) {
86 } else if (is_string($hook[0])) {
88 if (count($hook) > 1) {
93 wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
95 } else if (is_string($hook)) { # functions look like strings, too
97 } else if (is_object($hook)) {
99 $method = "on" . $event;
101 wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
105 $hook_args = array_merge(array($data), $args);
111 $retval = call_user_func_array(array($object, $method), $hook_args);
113 $retval = call_user_func_array($func, $hook_args);
116 if (is_string($retval)) {
118 $wgOut->fatalError($retval);
120 } else if (!$retval) {
127 } /* if defined(MEDIAWIKI) */