MediaWiki  1.28.0
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 
131  public static function run( $event, array $args = [], $deprecatedVersion = null ) {
132  foreach ( self::getHandlers( $event ) as $hook ) {
133  // Turn non-array values into an array. (Can't use casting because of objects.)
134  if ( !is_array( $hook ) ) {
135  $hook = [ $hook ];
136  }
137 
138  if ( !array_filter( $hook ) ) {
139  // Either array is empty or it's an array filled with null/false/empty.
140  continue;
141  } elseif ( is_array( $hook[0] ) ) {
142  // First element is an array, meaning the developer intended
143  // the first element to be a callback. Merge it in so that
144  // processing can be uniform.
145  $hook = array_merge( $hook[0], array_slice( $hook, 1 ) );
146  }
147 
153  if ( $hook[0] instanceof Closure ) {
154  $func = "hook-$event-closure";
155  $callback = array_shift( $hook );
156  } elseif ( is_object( $hook[0] ) ) {
157  $object = array_shift( $hook );
158  $method = array_shift( $hook );
159 
160  // If no method was specified, default to on$event.
161  if ( $method === null ) {
162  $method = "on$event";
163  }
164 
165  $func = get_class( $object ) . '::' . $method;
166  $callback = [ $object, $method ];
167  } elseif ( is_string( $hook[0] ) ) {
168  $func = $callback = array_shift( $hook );
169  } else {
170  throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
171  }
172 
173  // Run autoloader (workaround for call_user_func_array bug)
174  // and throw error if not callable.
175  if ( !is_callable( $callback ) ) {
176  throw new MWException( 'Invalid callback ' . $func . ' in hooks for ' . $event . "\n" );
177  }
178 
179  /*
180  * Call the hook. The documentation of call_user_func_array says
181  * false is returned on failure. However, if the function signature
182  * does not match the call signature, PHP will issue an warning and
183  * return null instead. The following code catches that warning and
184  * provides better error message.
185  */
186  $retval = null;
187  $badhookmsg = null;
188  $hook_args = array_merge( $hook, $args );
189 
190  // mark hook as deprecated, if deprecation version is specified
191  if ( $deprecatedVersion !== null ) {
192  wfDeprecated( "$event hook (used in $func)", $deprecatedVersion );
193  }
194 
195  $retval = call_user_func_array( $callback, $hook_args );
196 
197  // Process the return value.
198  if ( is_string( $retval ) ) {
199  // String returned means error.
200  throw new FatalError( $retval );
201  } elseif ( $retval === false ) {
202  // False was returned. Stop processing, but no error.
203  return false;
204  }
205  }
206 
207  return true;
208  }
209 }
the array() calling protocol came about after MediaWiki 1.4rc1.
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:110
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:64
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:131
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
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:242
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300