(bug 5378) General logs link in Special:Contributions
[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
36 if (!is_array($wgHooks)) {
37 wfDebugDieBacktrace("Global hooks array is not an array!\n");
38 return false;
39 }
40
41 if (!array_key_exists($event, $wgHooks)) {
42 return true;
43 }
44
45 if (!is_array($wgHooks[$event])) {
46 wfDebugDieBacktrace("Hooks array for event '$event' is not an array!\n");
47 return false;
48 }
49
50 foreach ($wgHooks[$event] as $index => $hook) {
51
52 $object = NULL;
53 $method = NULL;
54 $func = NULL;
55 $data = NULL;
56 $have_data = false;
57
58 /* $hook can be: a function, an object, an array of $function and $data,
59 * an array of just a function, an array of object and method, or an
60 * array of object, method, and data.
61 */
62
63 if (is_array($hook)) {
64 if (count($hook) < 1) {
65 wfDebugDieBacktrace("Empty array in hooks for " . $event . "\n");
66 } else if (is_object($hook[0])) {
67 $object =& $wgHooks[$event][$index][0];
68 if (count($hook) < 2) {
69 $method = "on" . $event;
70 } else {
71 $method = $hook[1];
72 if (count($hook) > 2) {
73 $data = $hook[2];
74 $have_data = true;
75 }
76 }
77 } else if (is_string($hook[0])) {
78 $func = $hook[0];
79 if (count($hook) > 1) {
80 $data = $hook[1];
81 $have_data = true;
82 }
83 } else {
84 var_dump( $wgHooks );
85 wfDebugDieBacktrace("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 =& $wgHooks[$event][$index];
91 $method = "on" . $event;
92 } else {
93 wfDebugDieBacktrace("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
105 if ( isset( $object ) ) {
106 $func = get_class( $object ) . '::' . $method;
107 }
108
109 /* Call the hook. */
110 wfProfileIn( $func );
111 if( isset( $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 wfProfileOut( $func );
117
118 /* String return is an error; false return means stop processing. */
119
120 if (is_string($retval)) {
121 global $wgOut;
122 $wgOut->fatalError($retval);
123 return false;
124 } else if (!$retval) {
125 return false;
126 }
127 }
128
129 return true;
130 }
131 ?>