* Added wfDie() wrapper, and some manual die(-1), to force the return code
[lhc/web/wiklou.git] / includes / Hooks.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( -1 );
4 /**
5 * Hooks.php -- a tool for running hook functions
6 * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * @author Evan Prodromou <evan@wikitravel.org>
23 * @package MediaWiki
24 * @see hooks.txt
25 */
26
27
28 /**
29 * Because programmers assign to $wgHooks, we need to be very
30 * careful about its contents. So, there's a lot more error-checking
31 * in here than would normally be necessary.
32 */
33 function wfRunHooks($event, $args = null) {
34
35 global $wgHooks;
36 $fname = 'wfRunHooks';
37 wfProfileIn( $fname );
38
39 if (!is_array($wgHooks)) {
40 wfDebugDieBacktrace("Global hooks array is not an array!\n");
41 wfProfileOut( $fname );
42 return false;
43 }
44
45 if (!array_key_exists($event, $wgHooks)) {
46 wfProfileOut( $fname );
47 return true;
48 }
49
50 if (!is_array($wgHooks[$event])) {
51 wfDebugDieBacktrace("Hooks array for event '$event' is not an array!\n");
52 wfProfileOut( $fname );
53 return false;
54 }
55
56 foreach ($wgHooks[$event] as $index => $hook) {
57
58 $object = NULL;
59 $method = NULL;
60 $func = NULL;
61 $data = NULL;
62 $have_data = false;
63
64 /* $hook can be: a function, an object, an array of $function and $data,
65 * an array of just a function, an array of object and method, or an
66 * array of object, method, and data.
67 */
68
69 if (is_array($hook)) {
70 if (count($hook) < 1) {
71 wfDebugDieBacktrace("Empty array in hooks for " . $event . "\n");
72 } else if (is_object($hook[0])) {
73 $object =& $wgHooks[$event][$index][0];
74 if (count($hook) < 2) {
75 $method = "on" . $event;
76 } else {
77 $method = $hook[1];
78 if (count($hook) > 2) {
79 $data = $hook[2];
80 $have_data = true;
81 }
82 }
83 } else if (is_string($hook[0])) {
84 $func = $hook[0];
85 if (count($hook) > 1) {
86 $data = $hook[1];
87 $have_data = true;
88 }
89 } else {
90 wfDebugDieBacktrace("Unknown datatype in hooks for " . $event . "\n");
91 }
92 } else if (is_string($hook)) { # functions look like strings, too
93 $func = $hook;
94 } else if (is_object($hook)) {
95 $object =& $wgHooks[$event][$index];
96 $method = "on" . $event;
97 } else {
98 wfDebugDieBacktrace("Unknown datatype in hooks for " . $event . "\n");
99 }
100
101 /* We put the first data element on, if needed. */
102
103 if ($have_data) {
104 $hook_args = array_merge(array($data), $args);
105 } else {
106 $hook_args = $args;
107 }
108
109
110 if ( isset( $object ) ) {
111 $func = get_class( $object ) . '::' . $method;
112 }
113
114 /* Call the hook. */
115 wfProfileIn( $func );
116 if( isset( $object ) ) {
117 $retval = call_user_func_array(array(&$object, $method), $hook_args);
118 } else {
119 $retval = call_user_func_array($func, $hook_args);
120 }
121 wfProfileOut( $func );
122
123 /* String return is an error; false return means stop processing. */
124
125 if (is_string($retval)) {
126 global $wgOut;
127 $wgOut->fatalError($retval);
128 wfProfileOut( $fname );
129 return false;
130 } else if (!$retval) {
131 wfProfileOut( $fname );
132 return false;
133 }
134 }
135
136 wfProfileOut( $fname );
137 return true;
138 }
139 ?>