Revert changes to SpecialSearch.php in r70608 because Special:Search didn't load...
[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 // Return quickly in the most common case
36 if ( !isset( $wgHooks[$event] ) ) {
37 return true;
38 }
39
40 if (!is_array($wgHooks)) {
41 throw new MWException("Global hooks array is not an array!\n");
42 return false;
43 }
44
45 if (!is_array($wgHooks[$event])) {
46 throw new MWException("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 $closure = false;
58 $badhookmsg = false;
59
60 /* $hook can be: a function, an object, an array of $function and $data,
61 * an array of just a function, an array of object and method, or an
62 * array of object, method, and data.
63 */
64
65 if ( is_array( $hook ) ) {
66 if ( count( $hook ) < 1 ) {
67 throw new MWException("Empty array in hooks for " . $event . "\n");
68 } else if ( is_object( $hook[0] ) ) {
69 $object = $wgHooks[$event][$index][0];
70 if ( $object instanceof Closure ) {
71 $closure = true;
72 if ( count( $hook ) > 1 ) {
73 $data = $hook[1];
74 $have_data = true;
75 }
76 } else {
77 if ( count( $hook ) < 2 ) {
78 $method = "on" . $event;
79 } else {
80 $method = $hook[1];
81 if ( count( $hook ) > 2 ) {
82 $data = $hook[2];
83 $have_data = true;
84 }
85 }
86 }
87 } else if ( is_string( $hook[0] ) ) {
88 $func = $hook[0];
89 if ( count( $hook ) > 1) {
90 $data = $hook[1];
91 $have_data = true;
92 }
93 } else {
94 throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
95 }
96 } else if ( is_string( $hook ) ) { # functions look like strings, too
97 $func = $hook;
98 } else if ( is_object( $hook ) ) {
99 $object = $wgHooks[$event][$index];
100 if ( $object instanceof Closure ) {
101 $closure = true;
102 } else {
103 $method = "on" . $event;
104 }
105 } else {
106 throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
107 }
108
109 /* We put the first data element on, if needed. */
110
111 if ( $have_data ) {
112 $hook_args = array_merge(array($data), $args);
113 } else {
114 $hook_args = $args;
115 }
116
117 if ( $closure ) {
118 $callback = $object;
119 $func = "hook-$event-closure";
120 } elseif ( isset( $object ) ) {
121 $func = get_class( $object ) . '::' . $method;
122 $callback = array( $object, $method );
123 } elseif ( false !== ( $pos = strpos( $func, '::' ) ) ) {
124 $callback = array( substr( $func, 0, $pos ), substr( $func, $pos + 2 ) );
125 } else {
126 $callback = $func;
127 }
128
129 // Run autoloader (workaround for call_user_func_array bug)
130 is_callable( $callback );
131
132 /* Call the hook. The documentation of call_user_func_array clearly
133 * states that FALSE is returned on failure. However this is not
134 * case always. In some version of PHP if the function signature
135 * does not match the call signature, PHP will issue an warning:
136 * Param y in x expected to be a reference, value given.
137 *
138 * In that case the call will also return null. The following code
139 * catches that warning and provides better error message. The
140 * function documentation also says that:
141 * In other words, it does not depend on the function signature
142 * whether the parameter is passed by a value or by a reference.
143 * There is also PHP bug http://bugs.php.net/bug.php?id=47554 which
144 * is unsurprisingly marked as bogus. In short handling of failures
145 * with call_user_func_array is a failure, the documentation for that
146 * function is wrong and misleading and PHP developers don't see any
147 * problem here.
148 */
149 $retval = null;
150 set_error_handler( 'hookErrorHandler' );
151 wfProfileIn( $func );
152 try {
153 $retval = call_user_func_array( $callback, $hook_args );
154 } catch ( MWHookException $e ) {
155 $badhookmsg = $e->getMessage();
156 }
157 wfProfileOut( $func );
158 restore_error_handler();
159
160 /* String return is an error; false return means stop processing. */
161 if ( is_string( $retval ) ) {
162 global $wgOut;
163 $wgOut->showFatalError( $retval );
164 return false;
165 } elseif( $retval === null ) {
166 if ( $closure ) {
167 $prettyFunc = "$event closure";
168 } elseif( is_array( $callback ) ) {
169 if( is_object( $callback[0] ) ) {
170 $prettyClass = get_class( $callback[0] );
171 } else {
172 $prettyClass = strval( $callback[0] );
173 }
174 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
175 } else {
176 $prettyFunc = strval( $callback );
177 }
178 if ( $badhookmsg ) {
179 throw new MWException( "Detected bug in an extension! " .
180 "Hook $prettyFunc has invalid call signature; " . $badhookmsg );
181 } else {
182 throw new MWException( "Detected bug in an extension! " .
183 "Hook $prettyFunc failed to return a value; " .
184 "should return true to continue hook processing or false to abort." );
185 }
186 } else if ( !$retval ) {
187 return false;
188 }
189 }
190
191 return true;
192 }
193
194 function hookErrorHandler( $errno, $errstr ) {
195 if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
196 throw new MWHookException( $errstr );
197 }
198 return false;
199 }
200
201 class MWHookException extends MWException {}