Be less anal about entry points, class definitions do no harm to anyone, defined...
[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
26 /**
27 * Because programmers assign to $wgHooks, we need to be very
28 * careful about its contents. So, there's a lot more error-checking
29 * in here than would normally be necessary.
30 */
31 function wfRunHooks($event, $args = null) {
32
33 global $wgHooks;
34 $fname = 'wfRunHooks';
35 wfProfileIn( $fname );
36
37 if (!is_array($wgHooks)) {
38 wfDebugDieBacktrace("Global hooks array is not an array!\n");
39 wfProfileOut( $fname );
40 return false;
41 }
42
43 if (!array_key_exists($event, $wgHooks)) {
44 wfProfileOut( $fname );
45 return true;
46 }
47
48 if (!is_array($wgHooks[$event])) {
49 wfDebugDieBacktrace("Hooks array for event '$event' is not an array!\n");
50 wfProfileOut( $fname );
51 return false;
52 }
53
54 foreach ($wgHooks[$event] as $index => $hook) {
55
56 $object = NULL;
57 $method = NULL;
58 $func = NULL;
59 $data = NULL;
60 $have_data = false;
61
62 /* $hook can be: a function, an object, an array of $function and $data,
63 * an array of just a function, an array of object and method, or an
64 * array of object, method, and data.
65 */
66
67 if (is_array($hook)) {
68 if (count($hook) < 1) {
69 wfDebugDieBacktrace("Empty array in hooks for " . $event . "\n");
70 } else if (is_object($hook[0])) {
71 $object =& $wgHooks[$event][$index][0];
72 if (count($hook) < 2) {
73 $method = "on" . $event;
74 } else {
75 $method = $hook[1];
76 if (count($hook) > 2) {
77 $data = $hook[2];
78 $have_data = true;
79 }
80 }
81 } else if (is_string($hook[0])) {
82 $func = $hook[0];
83 if (count($hook) > 1) {
84 $data = $hook[1];
85 $have_data = true;
86 }
87 } else {
88 wfDebugDieBacktrace("Unknown datatype in hooks for " . $event . "\n");
89 }
90 } else if (is_string($hook)) { # functions look like strings, too
91 $func = $hook;
92 } else if (is_object($hook)) {
93 $object =& $wgHooks[$event][$index];
94 $method = "on" . $event;
95 } else {
96 wfDebugDieBacktrace("Unknown datatype in hooks for " . $event . "\n");
97 }
98
99 /* We put the first data element on, if needed. */
100
101 if ($have_data) {
102 $hook_args = array_merge(array($data), $args);
103 } else {
104 $hook_args = $args;
105 }
106
107
108 if ( isset( $object ) ) {
109 $func = get_class( $object ) . '::' . $method;
110 }
111
112 /* Call the hook. */
113 wfProfileIn( $func );
114 if( isset( $object ) ) {
115 $retval = call_user_func_array(array(&$object, $method), $hook_args);
116 } else {
117 $retval = call_user_func_array($func, $hook_args);
118 }
119 wfProfileOut( $func );
120
121 /* String return is an error; false return means stop processing. */
122
123 if (is_string($retval)) {
124 global $wgOut;
125 $wgOut->fatalError($retval);
126 wfProfileOut( $fname );
127 return false;
128 } else if (!$retval) {
129 wfProfileOut( $fname );
130 return false;
131 }
132 }
133
134 wfProfileOut( $fname );
135 return true;
136 }
137 ?>