fix phpdoc comment
[lhc/web/wiklou.git] / includes / Hooks.php
1 <?php
2 /**
3 * Hooks.php -- a tool for running hook functions
4 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
5 *
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.
10 *
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.
15 *
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
19 *
20 * @author <evan@wikitravel.org>
21 * @package MediaWiki
22 * @see hooks.doc
23 */
24
25 if (defined('MEDIAWIKI')) {
26
27 /**
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.
31 */
32 function wfRunHooks() {
33
34 global $wgHooks;
35
36 if (!is_array($wgHooks)) {
37 wfDieDebugBacktrace("Global hooks array is not an array!\n");
38 return false;
39 }
40
41 $args = func_get_args();
42
43 if (count($args) < 1) {
44 wfDieDebugBacktrace("No event name given for wfRunHooks().\n");
45 return false;
46 }
47
48 $event = array_shift($args);
49
50 if (!array_key_exists($event, $wgHooks)) {
51 return true;
52 }
53
54 if (!is_array($wgHooks[$event])) {
55 wfDieDebugBacktrace("Hooks array for event '$event' is not an array!\n");
56 return false;
57 }
58
59 foreach ($wgHooks[$event] as $hook) {
60
61 $object = NULL;
62 $method = NULL;
63 $func = NULL;
64 $data = NULL;
65 $have_data = false;
66
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.
70 */
71
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])) {
76 $object = $hook[0];
77 if (count($hook) < 2) {
78 $method = "on" . $event;
79 } else {
80 $method = $hook[1];
81 if (count($hook) > 2) {
82 $data = $hook[2];
83 $have_data = true;
84 }
85 }
86 } else if (is_string($hook[0])) {
87 $func = $hook[0];
88 if (count($hook) > 1) {
89 $data = $hook[1];
90 $have_data = true;
91 }
92 } else {
93 wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
94 }
95 } else if (is_string($hook)) { # functions look like strings, too
96 $func = $hook;
97 } else if (is_object($hook)) {
98 $object = $hook;
99 $method = "on" . $event;
100 } else {
101 wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
102 }
103
104 if ($have_data) {
105 $hook_args = array_merge(array($data), $args);
106 } else {
107 $hook_args = $args;
108 }
109
110 if ($object) {
111 $retval = call_user_func_array(array($object, $method), $hook_args);
112 } else {
113 $retval = call_user_func_array($func, $hook_args);
114 }
115
116 if (is_string($retval)) {
117 global $wgOut;
118 $wgOut->fatalError($retval);
119 return false;
120 } else if (!$retval) {
121 return false;
122 }
123 }
124
125 return true;
126 }
127 } /* if defined(MEDIAWIKI) */
128 ?>