MediaWiki REL1_33
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.
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition Setup.php:123
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.
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
$wgHooks['ArticleShow'][]
Definition hooks.txt:108
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:271
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:37
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))