* Fixed unclosed <p> tag
[lhc/web/wiklou.git] / includes / Hooks.php
1 <?php
2 /**
3 * Hooks.php -- a tool for running hook functions
4 * Copyright 2004, 2005 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 Prodromou <evan@wikitravel.org>
21 * @package MediaWiki
22 * @see hooks.txt
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($event, $args) {
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 if (!array_key_exists($event, $wgHooks)) {
43 return true;
44 }
45
46 if (!is_array($wgHooks[$event])) {
47 wfDieDebugBacktrace("Hooks array for event '$event' is not an array!\n");
48 return false;
49 }
50
51 foreach ($wgHooks[$event] as $hook) {
52
53 $object = NULL;
54 $method = NULL;
55 $func = NULL;
56 $data = NULL;
57 $have_data = false;
58
59 /* $hook can be: a function, an object, an array of $function and $data,
60 * an array of just a function, an array of object and method, or an
61 * array of object, method, and data.
62 */
63
64 if (is_array($hook)) {
65 if (count($hook) < 1) {
66 wfDieDebugBacktrace("Empty array in hooks for " . $event . "\n");
67 } else if (is_object($hook[0])) {
68 $object = $hook[0];
69 if (count($hook) < 2) {
70 $method = "on" . $event;
71 } else {
72 $method = $hook[1];
73 if (count($hook) > 2) {
74 $data = $hook[2];
75 $have_data = true;
76 }
77 }
78 } else if (is_string($hook[0])) {
79 $func = $hook[0];
80 if (count($hook) > 1) {
81 $data = $hook[1];
82 $have_data = true;
83 }
84 } else {
85 wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
86 }
87 } else if (is_string($hook)) { # functions look like strings, too
88 $func = $hook;
89 } else if (is_object($hook)) {
90 $object = $hook;
91 $method = "on" . $event;
92 } else {
93 wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
94 }
95
96 /* We put the first data element on, if needed. */
97
98 if ($have_data) {
99 $hook_args = array_merge(array($data), $args);
100 } else {
101 $hook_args = $args;
102 }
103
104 /* Call the hook. */
105
106 if ($object) {
107 $retval = call_user_func_array(array($object, $method), $hook_args);
108 } else {
109 $retval = call_user_func_array($func, $hook_args);
110 }
111
112 /* String return is an error; false return means stop processing. */
113
114 if (is_string($retval)) {
115 global $wgOut;
116 $wgOut->fatalError($retval);
117 return false;
118 } else if (!$retval) {
119 return false;
120 }
121 }
122
123 return true;
124 }
125 } /* if defined(MEDIAWIKI) */
126 ?>