MediaWiki REL1_39
MediaWiki\HookContainer\HookContainer Class Reference

HookContainer class. More...

Inheritance diagram for MediaWiki\HookContainer\HookContainer:
Collaboration diagram for MediaWiki\HookContainer\HookContainer:

Public Member Functions

 __construct (HookRegistry $hookRegistry, ObjectFactory $objectFactory)
 
 clear (string $hook)
 Clear hooks registered via Hooks::register().
 
 emitDeprecationWarnings ()
 Will log a deprecation warning if:
 
 getHandlers (string $hook, array $options=[])
 Return array of handler objects registered with given hook in the new system.
 
 getLegacyHandlers (string $hook)
 Get all handlers for legacy hooks system, plus any handlers added using register().
 
 isRegistered (string $hook)
 Return whether hook has any handlers registered to it.
 
 register (string $hook, $callback)
 Attach an event handler to a given hook.
 
 run (string $hook, array $args=[], array $options=[])
 Call registered hook functions through either the legacy $wgHooks or extension.json.
 
 salvage (SalvageableService $other)
 Salvage the state of HookContainer by retaining existing handler objects and hooks registered via HookContainer::register().
 
 scopedRegister (string $hook, $callback, bool $replace=false)
 Register hook and handler, allowing for easy removal.
 

Detailed Description

HookContainer class.

Main class for managing hooks

Since
1.35

Definition at line 45 of file HookContainer.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\HookContainer\HookContainer::__construct ( HookRegistry $hookRegistry,
ObjectFactory $objectFactory )
Parameters
HookRegistry$hookRegistry
ObjectFactory$objectFactory

Definition at line 75 of file HookContainer.php.

Member Function Documentation

◆ clear()

MediaWiki\HookContainer\HookContainer::clear ( string $hook)

Clear hooks registered via Hooks::register().

This is intended for use while testing and will fail if MW_PHPUNIT_TEST and MW_PARSER_TEST are not defined.

Parameters
string$hookName of hook to clear
Access: internal
For use by Hooks.php
Exceptions
MWExceptionIf not in testing mode.

Definition at line 187 of file HookContainer.php.

◆ emitDeprecationWarnings()

MediaWiki\HookContainer\HookContainer::emitDeprecationWarnings ( )

Will log a deprecation warning if:

  1. the hook is marked deprecated
  2. the "silent" flag is absent or false, and
  3. an extension registers a handler in the new way but does not acknowledge deprecation

Definition at line 487 of file HookContainer.php.

◆ getHandlers()

MediaWiki\HookContainer\HookContainer::getHandlers ( string $hook,
array $options = [] )

Return array of handler objects registered with given hook in the new system.

This does not include handlers registered dynamically using register(), nor does it include hooks registered via the old mechanism using $wgHooks.

Access: internal
For use by Hooks.php
Parameters
string$hookName of the hook
array$optionsHandler options, which may include:
  • noServices: Do not allow hook handlers with service dependencies
Returns
array non-deprecated handler objects

Definition at line 441 of file HookContainer.php.

◆ getLegacyHandlers()

MediaWiki\HookContainer\HookContainer::getLegacyHandlers ( string $hook)

Get all handlers for legacy hooks system, plus any handlers added using register().

Access: internal
For use by Hooks.php
Parameters
string$hookName of hook
Returns
callable[]

Definition at line 396 of file HookContainer.php.

References $keys.

◆ isRegistered()

MediaWiki\HookContainer\HookContainer::isRegistered ( string $hook)

Return whether hook has any handlers registered to it.

The function may have been registered via Hooks::register or in extension.json

Parameters
string$hookName of hook
Returns
bool Whether the hook has a handler registered to it

Definition at line 348 of file HookContainer.php.

◆ register()

MediaWiki\HookContainer\HookContainer::register ( string $hook,
$callback )

Attach an event handler to a given hook.

Parameters
string$hookName of hook
callable | string | array$callbackhandler object to attach

Definition at line 372 of file HookContainer.php.

References wfDeprecated().

◆ run()

MediaWiki\HookContainer\HookContainer::run ( string $hook,
array $args = [],
array $options = [] )

Call registered hook functions through either the legacy $wgHooks or extension.json.

For the given hook, fetch the array of handler objects and process them. Determine the proper callback for each hook and then call the actual hook using the appropriate arguments. Finally, process the return value and return/throw accordingly.

Parameters
string$hookName of the hook
array$argsArguments to pass to hook handler
array$optionsoptions map:
  • abortable: (bool) If false, handlers will not be allowed to abort the call sequence. An exception will be raised if a handler returns anything other than true or null.
  • deprecatedVersion: (string) Version of MediaWiki this hook was deprecated in. For supporting Hooks::run() legacy $deprecatedVersion parameter. New core code should add deprecated hooks to the DeprecatedHooks::$deprecatedHooks array literal. New extension code should use the DeprecatedHooks attribute.
  • silent: (bool) If true, do not raise a deprecation warning
  • noServices: (bool) If true, do not allow hook handlers with service dependencies
Returns
bool True if no handler aborted the hook
Exceptions
UnexpectedValueExceptionif handlers return an invalid value

Definition at line 125 of file HookContainer.php.

◆ salvage()

MediaWiki\HookContainer\HookContainer::salvage ( SalvageableService $other)

Salvage the state of HookContainer by retaining existing handler objects and hooks registered via HookContainer::register().

Necessary in the event that MediaWikiServices::resetGlobalInstance() is called after hooks have already been registered.

Parameters
HookContainer | SalvageableService$otherThe object to salvage state from. $other be of type HookContainer
Exceptions
MWException

Definition at line 93 of file HookContainer.php.

◆ scopedRegister()

MediaWiki\HookContainer\HookContainer::scopedRegister ( string $hook,
$callback,
bool $replace = false )

Register hook and handler, allowing for easy removal.

Intended for use in temporary registration e.g. testing

Parameters
string$hookName of hook
callable | string | array$callbackHandler object to attach
bool$replace(optional) By default adds callback to handler array. Set true to remove all existing callbacks for the hook.
Returns
ScopedCallback

Definition at line 212 of file HookContainer.php.


The documentation for this class was generated from the following file: