Adding messages for new global group 'sysadmin'. Brion, Tim, Kate and JeLuF are all...
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * @author Evan Prodromou <evan@wikitravel.org>
21 * @see hooks.txt
22 * @file
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 = array()) {
32
33 global $wgHooks;
34
35 if (!is_array($wgHooks)) {
36 throw new MWException("Global hooks array is not an array!\n");
37 return false;
38 }
39
40 if (!array_key_exists($event, $wgHooks)) {
41 return true;
42 }
43
44 if (!is_array($wgHooks[$event])) {
45 throw new MWException("Hooks array for event '$event' is not an array!\n");
46 return false;
47 }
48
49 foreach ($wgHooks[$event] as $index => $hook) {
50
51 $object = NULL;
52 $method = NULL;
53 $func = NULL;
54 $data = NULL;
55 $have_data = false;
56
57 /* $hook can be: a function, an object, an array of $function and $data,
58 * an array of just a function, an array of object and method, or an
59 * array of object, method, and data.
60 */
61
62 if (is_array($hook)) {
63 if (count($hook) < 1) {
64 throw new MWException("Empty array in hooks for " . $event . "\n");
65 } else if (is_object($hook[0])) {
66 $object = $wgHooks[$event][$index][0];
67 if (count($hook) < 2) {
68 $method = "on" . $event;
69 } else {
70 $method = $hook[1];
71 if (count($hook) > 2) {
72 $data = $hook[2];
73 $have_data = true;
74 }
75 }
76 } else if (is_string($hook[0])) {
77 $func = $hook[0];
78 if (count($hook) > 1) {
79 $data = $hook[1];
80 $have_data = true;
81 }
82 } else {
83 var_dump( $wgHooks );
84 throw new MWException("Unknown datatype in hooks for " . $event . "\n");
85 }
86 } else if (is_string($hook)) { # functions look like strings, too
87 $func = $hook;
88 } else if (is_object($hook)) {
89 $object = $wgHooks[$event][$index];
90 $method = "on" . $event;
91 } else {
92 throw new MWException("Unknown datatype in hooks for " . $event . "\n");
93 }
94
95 /* We put the first data element on, if needed. */
96
97 if ($have_data) {
98 $hook_args = array_merge(array($data), $args);
99 } else {
100 $hook_args = $args;
101 }
102
103 if ( isset( $object ) ) {
104 $func = get_class( $object ) . '::' . $method;
105 $callback = array( $object, $method );
106 } elseif ( false !== ( $pos = strpos( $func, '::' ) ) ) {
107 $callback = array( substr( $func, 0, $pos ), substr( $func, $pos + 2 ) );
108 } else {
109 $callback = $func;
110 }
111
112 // Run autoloader (workaround for call_user_func_array bug)
113 is_callable( $callback );
114
115 /* Call the hook. */
116 wfProfileIn( $func );
117 $retval = call_user_func_array( $callback, $hook_args );
118 wfProfileOut( $func );
119
120 /* String return is an error; false return means stop processing. */
121
122 if (is_string($retval)) {
123 global $wgOut;
124 $wgOut->showFatalError($retval);
125 return false;
126 } elseif( $retval === null ) {
127 if( is_array( $callback ) ) {
128 if( is_object( $callback[0] ) ) {
129 $prettyClass = get_class( $callback[0] );
130 } else {
131 $prettyClass = strval( $callback[0] );
132 }
133 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
134 } else {
135 $prettyFunc = strval( $callback );
136 }
137 throw new MWException( "Detected bug in an extension! " .
138 "Hook $prettyFunc failed to return a value; " .
139 "should return true to continue hook processing or false to abort." );
140 } else if (!$retval) {
141 return false;
142 }
143 }
144
145 return true;
146 }