Add a system of hooks to allow third-party code to run before, after, or
[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 * @seealso 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
33 function wfRunHooks() {
34
35 global $wgHooks;
36
37 if (!is_array($wgHooks)) {
38 wfDieDebugBacktrace("Global hooks array is not an array!\n");
39 return false;
40 }
41
42 $args = func_get_args();
43
44 if (count($args) < 1) {
45 wfDieDebugBacktrace("No event name given for wfRunHooks().\n");
46 return false;
47 }
48
49 $event = array_shift($args);
50
51 if (!array_key_exists($wgHooks, $event)) {
52 return true;
53 }
54
55 if (!is_array($wgHooks[$event])) {
56 wfDieDebugBacktrace("Hooks array for event '$event' is not an array!\n");
57 return false;
58 }
59
60 foreach ($wgHooks[$event] as $hook) {
61
62 $object = NULL;
63 $method = NULL;
64 $func = NULL;
65 $data = NULL;
66 $have_data = false;
67
68 /* $hook can be: a function, an object, an array of $function and $data,
69 * an array of just a function, an array of object and method, or an
70 * array of object, method, and data.
71 */
72
73 if (is_array($hook)) {
74 if (count($hook) < 1) {
75 wfDieDebugBacktrace("Empty array in hooks for " . $event . "\n");
76 } else if (is_object($hook[0])) {
77 $object = $hook[0];
78 if (count($hook) < 2) {
79 $method = "on" . $event;
80 } else {
81 $method = $hook[1];
82 if (count($hook) > 2) {
83 $data = $hook[2];
84 $have_data = true;
85 }
86 }
87 } else if (is_string($hook[0])) {
88 $func = $hook[0];
89 if (count($hook) > 1) {
90 $data = $hook[1];
91 $have_data = true;
92 }
93 } else {
94 wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
95 }
96 } else if (is_string($hook)) { # functions look like strings, too
97 $func = $hook;
98 } else if (is_object($hook)) {
99 $object = $hook;
100 $method = "on" . $event;
101 } else {
102 wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
103 }
104
105 if ($have_data) {
106 $hook_args = array_merge(array($data), $args);
107 } else {
108 $hook_args = $args;
109 }
110
111 if ($object) {
112 $retval = call_user_func_array(array($object, $method), $hook_args);
113 } else {
114 $retval = call_user_func_array($func, $hook_args);
115 }
116
117 if (is_string($retval)) {
118 global $wgOut;
119 $wgOut->fatalError($retval);
120 return false;
121 } else if (!$retval) {
122 return false;
123 }
124 }
125
126 return true;
127 }
128 }
129
130 ?>