Merge "Improve "selfmove" message's wording"
[lhc/web/wiklou.git] / includes / Hooks.php
1 <?php
2
3 /**
4 * A tool for running hook functions.
5 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 *
22 * @author Evan Prodromou <evan@wikitravel.org>
23 * @see hooks.txt
24 * @file
25 */
26
27 /**
28 * Hooks class.
29 *
30 * Used to supersede $wgHooks, because globals are EVIL.
31 *
32 * @since 1.18
33 */
34 class Hooks {
35 /**
36 * Array of events mapped to an array of callbacks to be run
37 * when that event is triggered.
38 */
39 protected static $handlers = [];
40
41 /**
42 * Attach an event handler to a given hook.
43 *
44 * @param string $name Name of hook
45 * @param callable $callback Callback function to attach
46 *
47 * @since 1.18
48 */
49 public static function register( $name, $callback ) {
50 if ( !isset( self::$handlers[$name] ) ) {
51 self::$handlers[$name] = [];
52 }
53
54 self::$handlers[$name][] = $callback;
55 }
56
57 /**
58 * Clears hooks registered via Hooks::register(). Does not touch $wgHooks.
59 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
60 *
61 * @param string $name The name of the hook to clear.
62 *
63 * @since 1.21
64 * @throws MWException If not in testing mode.
65 */
66 public static function clear( $name ) {
67 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
68 throw new MWException( 'Cannot reset hooks in operation.' );
69 }
70
71 unset( self::$handlers[$name] );
72 }
73
74 /**
75 * Returns true if a hook has a function registered to it.
76 * The function may have been registered either via Hooks::register or in $wgHooks.
77 *
78 * @since 1.18
79 *
80 * @param string $name Name of hook
81 * @return bool True if the hook has a function registered to it
82 */
83 public static function isRegistered( $name ) {
84 global $wgHooks;
85 return !empty( $wgHooks[$name] ) || !empty( self::$handlers[$name] );
86 }
87
88 /**
89 * Returns an array of all the event functions attached to a hook
90 * This combines functions registered via Hooks::register and with $wgHooks.
91 *
92 * @since 1.18
93 *
94 * @param string $name Name of the hook
95 * @return array
96 */
97 public static function getHandlers( $name ) {
98 global $wgHooks;
99
100 if ( !self::isRegistered( $name ) ) {
101 return [];
102 } elseif ( !isset( self::$handlers[$name] ) ) {
103 return $wgHooks[$name];
104 } elseif ( !isset( $wgHooks[$name] ) ) {
105 return self::$handlers[$name];
106 } else {
107 return array_merge( self::$handlers[$name], $wgHooks[$name] );
108 }
109 }
110
111 /**
112 * @param string $event Event name
113 * @param array|callable $hook
114 * @param array $args Array of parameters passed to hook functions
115 * @param string|null $deprecatedVersion [optional]
116 * @param string &$fname [optional] Readable name of hook [returned]
117 * @return null|string|bool
118 */
119 private static function callHook( $event, $hook, array $args, $deprecatedVersion = null,
120 &$fname = null
121 ) {
122 // Turn non-array values into an array. (Can't use casting because of objects.)
123 if ( !is_array( $hook ) ) {
124 $hook = [ $hook ];
125 }
126
127 if ( !array_filter( $hook ) ) {
128 // Either array is empty or it's an array filled with null/false/empty.
129 return null;
130 }
131
132 if ( is_array( $hook[0] ) ) {
133 // First element is an array, meaning the developer intended
134 // the first element to be a callback. Merge it in so that
135 // processing can be uniform.
136 $hook = array_merge( $hook[0], array_slice( $hook, 1 ) );
137 }
138
139 /**
140 * $hook can be: a function, an object, an array of $function and
141 * $data, an array of just a function, an array of object and
142 * method, or an array of object, method, and data.
143 */
144 if ( $hook[0] instanceof Closure ) {
145 $fname = "hook-$event-closure";
146 $callback = array_shift( $hook );
147 } elseif ( is_object( $hook[0] ) ) {
148 $object = array_shift( $hook );
149 $method = array_shift( $hook );
150
151 // If no method was specified, default to on$event.
152 if ( $method === null ) {
153 $method = "on$event";
154 }
155
156 $fname = get_class( $object ) . '::' . $method;
157 $callback = [ $object, $method ];
158 } elseif ( is_string( $hook[0] ) ) {
159 $fname = $callback = array_shift( $hook );
160 } else {
161 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
162 }
163
164 // Run autoloader (workaround for call_user_func_array bug)
165 // and throw error if not callable.
166 if ( !is_callable( $callback ) ) {
167 throw new MWException( 'Invalid callback ' . $fname . ' in hooks for ' . $event . "\n" );
168 }
169
170 // mark hook as deprecated, if deprecation version is specified
171 if ( $deprecatedVersion !== null ) {
172 wfDeprecated( "$event hook (used in $fname)", $deprecatedVersion );
173 }
174
175 // Call the hook.
176 $hook_args = array_merge( $hook, $args );
177 return call_user_func_array( $callback, $hook_args );
178 }
179
180 /**
181 * Call hook functions defined in Hooks::register and $wgHooks.
182 *
183 * For the given hook event, fetch the array of hook events and
184 * process them. Determine the proper callback for each hook and
185 * then call the actual hook using the appropriate arguments.
186 * Finally, process the return value and return/throw accordingly.
187 *
188 * For hook event that are not abortable through a handler's return value,
189 * use runWithoutAbort() instead.
190 *
191 * @param string $event Event name
192 * @param array $args Array of parameters passed to hook functions
193 * @param string|null $deprecatedVersion [optional] Mark hook as deprecated with version number
194 * @return bool True if no handler aborted the hook
195 *
196 * @throws Exception
197 * @throws FatalError
198 * @throws MWException
199 * @since 1.22 A hook function is not required to return a value for
200 * processing to continue. Not returning a value (or explicitly
201 * returning null) is equivalent to returning true.
202 */
203 public static function run( $event, array $args = [], $deprecatedVersion = null ) {
204 foreach ( self::getHandlers( $event ) as $hook ) {
205 $retval = self::callHook( $event, $hook, $args, $deprecatedVersion );
206 if ( $retval === null ) {
207 continue;
208 }
209
210 // Process the return value.
211 if ( is_string( $retval ) ) {
212 // String returned means error.
213 throw new FatalError( $retval );
214 } elseif ( $retval === false ) {
215 // False was returned. Stop processing, but no error.
216 return false;
217 }
218 }
219
220 return true;
221 }
222
223 /**
224 * Call hook functions defined in Hooks::register and $wgHooks.
225 *
226 * @param string $event Event name
227 * @param array $args Array of parameters passed to hook functions
228 * @param string|null $deprecatedVersion [optional] Mark hook as deprecated with version number
229 * @return bool Always true
230 * @throws MWException If a callback is invalid, unknown
231 * @throws UnexpectedValueException If a callback returns an abort value.
232 * @since 1.30
233 */
234 public static function runWithoutAbort( $event, array $args = [], $deprecatedVersion = null ) {
235 foreach ( self::getHandlers( $event ) as $hook ) {
236 $fname = null;
237 $retval = self::callHook( $event, $hook, $args, $deprecatedVersion, $fname );
238 if ( $retval !== null && $retval !== true ) {
239 throw new UnexpectedValueException( "Invalid return from $fname for unabortable $event." );
240 }
241 }
242 return true;
243 }
244 }