MediaWiki  master
Hooks.php
Go to the documentation of this file.
1 <?php
2 
34 class 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 ) {
85  return !empty( $wgHooks[$name] ) || !empty( self::$handlers[$name] );
86  }
87 
97  public static function getHandlers( $name ) {
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 }
the array() calling protocol came about after MediaWiki 1.4rc1.
static callHook($event, $hook, array $args, $deprecatedVersion=null, &$fname=null)
Definition: Hooks.php:119
static runWithoutAbort($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:234
static isRegistered($name)
Returns true if a hook has a function registered to it.
Definition: Hooks.php:83
static getHandlers($name)
Returns an array of all the event functions attached to a hook This combines functions registered via...
Definition: Hooks.php:97
$wgHooks['ArticleShow'][]
Definition: hooks.txt:108
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
if($line===false) $args
Definition: cdb.php:63
static clear($name)
Clears hooks registered via Hooks::register().
Definition: Hooks.php:66
wfDeprecated($function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:203
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Exception class which takes an HTML error message, and does not produce a backtrace.
Definition: FatalError.php:28
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined...
Definition: Setup.php:36
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
static $handlers
Array of events mapped to an array of callbacks to be run when that event is triggered.
Definition: Hooks.php:39
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:244