profiling
[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 $fname = 'wfRunHooks';
37 wfProfileIn( $fname );
38
39 if (!is_array($wgHooks)) {
40 wfDieDebugBacktrace("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 wfDieDebugBacktrace("Hooks array for event '$event' is not an array!\n");
52 wfProfileOut( $fname );
53 return false;
54 }
55
56 foreach ($wgHooks[$event] as $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 wfDieDebugBacktrace("Empty array in hooks for " . $event . "\n");
72 } else if (is_object($hook[0])) {
73 $object = $hook[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 wfDieDebugBacktrace("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 = $hook;
96 $method = "on" . $event;
97 } else {
98 wfDieDebugBacktrace("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 ( $object ) {
111 $func = get_class( $object ) . '::' . $method;
112 }
113
114 /* Call the hook. */
115 wfProfileIn( $func );
116 if ($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 } /* if defined(MEDIAWIKI) */
140 ?>