MediaWiki REL1_31
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 if ( !isset( self::$handlers[$name] ) ) {
51 self::$handlers[$name] = [];
52 }
53
54 self::$handlers[$name][] = $callback;
55 }
56
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
83 public static function isRegistered( $name ) {
84 global $wgHooks;
85 return !empty( $wgHooks[$name] ) || !empty( self::$handlers[$name] );
86 }
87
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
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
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
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
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}
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition Setup.php:112
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:66
static callHook( $event, $hook, array $args, $deprecatedVersion=null, &$fname=null)
Definition Hooks.php:119
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition Hooks.php:203
static getHandlers( $name)
Returns an array of all the event functions attached to a hook This combines functions registered via...
Definition Hooks.php:97
static isRegistered( $name)
Returns true if a hook has a function registered to it.
Definition Hooks.php:83
static runWithoutAbort( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition Hooks.php:234
MediaWiki exception.
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account incomplete not yet checked for validity & $retval
Definition hooks.txt:266
$wgHooks['ArticleShow'][]
Definition hooks.txt:108
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302