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