MediaWiki REL1_34
Hooks.php
Go to the documentation of this file.
1<?php
2
34class Hooks {
39 protected static $handlers = [];
40
49 public static function register( $name, $callback ) {
50 self::$handlers[$name][] = $callback;
51 }
52
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
80 public static function isRegistered( $name ) {
81 global $wgHooks;
82 return !empty( $wgHooks[$name] ) || !empty( self::$handlers[$name] );
83 }
84
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
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
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
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
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}
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
$wgHooks['AdminLinks'][]
if( $line===false) $args
Definition cdb.php:64
Exception class which takes an HTML error message, and does not produce a backtrace.
Hooks class.
Definition Hooks.php:34
static $handlers
Array of events mapped to an array of callbacks to be run when that event is triggered.
Definition Hooks.php:39
static clear( $name)
Clears hooks registered via Hooks::register().
Definition Hooks.php:63
static callHook( $event, $hook, array $args, $deprecatedVersion=null, &$fname=null)
Definition Hooks.php:116
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition Hooks.php:200
static getHandlers( $name)
Returns an array of all the event functions attached to a hook This combines functions registered via...
Definition Hooks.php:94
static isRegistered( $name)
Returns true if a hook has a function registered to it.
Definition Hooks.php:80
static runWithoutAbort( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition Hooks.php:231
MediaWiki exception.