2020-02-10 14:47:46 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
2020-05-27 23:45:23 +00:00
|
|
|
* @ingroup Hooks
|
|
|
|
|
* @defgroup Hooks Hooks
|
|
|
|
|
* Hooks allow custom code to be executed when an event occurs; this module
|
|
|
|
|
* includes all hooks provided by MediaWiki Core; for more information, see
|
|
|
|
|
* https://www.mediawiki.org/wiki/Manual:Hooks.
|
2020-02-10 14:47:46 +00:00
|
|
|
*/
|
|
|
|
|
|
2020-04-17 06:03:29 +00:00
|
|
|
namespace MediaWiki\HookContainer;
|
2020-02-10 14:47:46 +00:00
|
|
|
|
|
|
|
|
use Closure;
|
2020-04-16 06:20:16 +00:00
|
|
|
use MWDebug;
|
2020-02-10 14:47:46 +00:00
|
|
|
use MWException;
|
|
|
|
|
use UnexpectedValueException;
|
|
|
|
|
use Wikimedia\Assert\Assert;
|
2020-09-07 09:30:43 +00:00
|
|
|
use Wikimedia\NonSerializable\NonSerializableTrait;
|
2020-02-10 14:47:46 +00:00
|
|
|
use Wikimedia\ObjectFactory;
|
|
|
|
|
use Wikimedia\ScopedCallback;
|
|
|
|
|
use Wikimedia\Services\SalvageableService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* HookContainer class.
|
|
|
|
|
*
|
|
|
|
|
* Main class for managing hooks
|
|
|
|
|
*
|
|
|
|
|
* @since 1.35
|
|
|
|
|
*/
|
|
|
|
|
class HookContainer implements SalvageableService {
|
2020-09-07 09:30:43 +00:00
|
|
|
use NonSerializableTrait;
|
2020-02-10 14:47:46 +00:00
|
|
|
|
|
|
|
|
/** @var array Hooks and their callbacks registered through $this->register() */
|
2020-08-25 17:04:55 +00:00
|
|
|
private $dynamicHandlers = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array Tombstone count by hook name
|
|
|
|
|
*/
|
|
|
|
|
private $tombstones = [];
|
|
|
|
|
|
|
|
|
|
/** @var string magic value for use in $dynamicHandlers */
|
|
|
|
|
private const TOMBSTONE = 'TOMBSTONE';
|
2020-02-10 14:47:46 +00:00
|
|
|
|
|
|
|
|
/** @var array handler name and their handler objects */
|
2020-04-16 03:55:37 +00:00
|
|
|
private $handlersByName = [];
|
2020-02-10 14:47:46 +00:00
|
|
|
|
2020-05-11 08:58:38 +00:00
|
|
|
/** @var HookRegistry */
|
|
|
|
|
private $registry;
|
2020-02-10 14:47:46 +00:00
|
|
|
|
|
|
|
|
/** @var ObjectFactory */
|
|
|
|
|
private $objectFactory;
|
|
|
|
|
|
2020-04-16 03:55:37 +00:00
|
|
|
/** @var int The next ID to be used by scopedRegister() */
|
|
|
|
|
private $nextScopedRegisterId = 0;
|
|
|
|
|
|
2020-02-10 14:47:46 +00:00
|
|
|
/**
|
2020-05-11 08:58:38 +00:00
|
|
|
* @param HookRegistry $hookRegistry
|
2020-02-10 14:47:46 +00:00
|
|
|
* @param ObjectFactory $objectFactory
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
2020-05-11 08:58:38 +00:00
|
|
|
HookRegistry $hookRegistry,
|
|
|
|
|
ObjectFactory $objectFactory
|
2020-02-10 14:47:46 +00:00
|
|
|
) {
|
2020-05-11 08:58:38 +00:00
|
|
|
$this->registry = $hookRegistry;
|
2020-02-10 14:47:46 +00:00
|
|
|
$this->objectFactory = $objectFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @param HookContainer|SalvageableService $other The object to salvage state from. $other be
|
|
|
|
|
* of type HookContainer
|
|
|
|
|
* @throws MWException
|
|
|
|
|
*/
|
|
|
|
|
public function salvage( SalvageableService $other ) {
|
|
|
|
|
Assert::parameterType( self::class, $other, '$other' );
|
2020-08-25 17:04:55 +00:00
|
|
|
if ( $this->dynamicHandlers || $this->handlersByName ) {
|
2020-02-10 14:47:46 +00:00
|
|
|
throw new MWException( 'salvage() must be called immediately after construction' );
|
|
|
|
|
}
|
2020-04-16 03:55:37 +00:00
|
|
|
$this->handlersByName = $other->handlersByName;
|
2020-08-25 17:04:55 +00:00
|
|
|
$this->dynamicHandlers = $other->dynamicHandlers;
|
|
|
|
|
$this->tombstones = $other->tombstones;
|
2020-02-10 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @param string $hook Name of the hook
|
|
|
|
|
* @param array $args Arguments to pass to hook handler
|
|
|
|
|
* @param array $options options map:
|
|
|
|
|
* - abortable: (bool) If false, handlers will not be allowed to abort the call sequenece.
|
|
|
|
|
* 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
|
2020-04-16 06:20:16 +00:00
|
|
|
* 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.
|
2020-06-03 04:58:25 +00:00
|
|
|
* - silent: (bool) If true, do not raise a deprecation warning
|
2020-08-10 00:24:11 +00:00
|
|
|
* - noServices: (bool) If true, do not allow hook handlers with service dependencies
|
2020-02-10 14:47:46 +00:00
|
|
|
* @return bool True if no handler aborted the hook
|
|
|
|
|
* @throws UnexpectedValueException if handlers return an invalid value
|
|
|
|
|
*/
|
|
|
|
|
public function run( string $hook, array $args = [], array $options = [] ) : bool {
|
|
|
|
|
$legacyHandlers = $this->getLegacyHandlers( $hook );
|
|
|
|
|
$options = array_merge(
|
2020-05-11 08:58:38 +00:00
|
|
|
$this->registry->getDeprecatedHooks()->getDeprecationInfo( $hook ) ?? [],
|
2020-04-16 03:55:37 +00:00
|
|
|
$options
|
2020-02-10 14:47:46 +00:00
|
|
|
);
|
|
|
|
|
// Equivalent of legacy Hooks::runWithoutAbort()
|
|
|
|
|
$notAbortable = ( isset( $options['abortable'] ) && $options['abortable'] === false );
|
|
|
|
|
foreach ( $legacyHandlers as $handler ) {
|
|
|
|
|
$normalizedHandler = $this->normalizeHandler( $handler, $hook );
|
|
|
|
|
if ( $normalizedHandler ) {
|
|
|
|
|
$functionName = $normalizedHandler['functionName'];
|
|
|
|
|
$return = $this->callLegacyHook( $hook, $normalizedHandler, $args, $options );
|
|
|
|
|
if ( $notAbortable && $return !== null && $return !== true ) {
|
|
|
|
|
throw new UnexpectedValueException( "Invalid return from $functionName" .
|
|
|
|
|
" for unabortable $hook." );
|
|
|
|
|
}
|
|
|
|
|
if ( $return === false ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ( is_string( $return ) ) {
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
wfDeprecatedMsg(
|
|
|
|
|
"Returning a string from a hook handler is deprecated since MediaWiki 1.35 ' .
|
|
|
|
|
'(done by $functionName for $hook)",
|
|
|
|
|
'1.35', false, false
|
2020-02-10 14:47:46 +00:00
|
|
|
);
|
|
|
|
|
throw new UnexpectedValueException( $return );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 00:24:11 +00:00
|
|
|
$handlers = $this->getHandlers( $hook, $options );
|
2021-01-06 13:59:16 +00:00
|
|
|
$funcName = 'on' . strtr( ucfirst( $hook ), ':-', '__' );
|
2020-02-10 14:47:46 +00:00
|
|
|
|
|
|
|
|
foreach ( $handlers as $handler ) {
|
2020-04-16 03:55:37 +00:00
|
|
|
$return = $handler->$funcName( ...$args );
|
2020-02-10 14:47:46 +00:00
|
|
|
if ( $notAbortable && $return !== null && $return !== true ) {
|
|
|
|
|
throw new UnexpectedValueException(
|
|
|
|
|
"Invalid return from " . $funcName . " for unabortable $hook."
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-04-30 06:08:54 +00:00
|
|
|
if ( $return === false ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-02-10 14:47:46 +00:00
|
|
|
if ( $return !== null && !is_bool( $return ) ) {
|
|
|
|
|
throw new UnexpectedValueException( "Invalid return from " . $funcName . " for $hook." );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @param string $hook Name of hook to clear
|
|
|
|
|
*
|
|
|
|
|
* @internal For use by Hooks.php
|
|
|
|
|
* @throws MWException If not in testing mode.
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
*/
|
|
|
|
|
public function clear( string $hook ) : void {
|
|
|
|
|
if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
|
|
|
|
|
throw new MWException( 'Cannot reset hooks in operation.' );
|
|
|
|
|
}
|
2020-08-25 17:04:55 +00:00
|
|
|
|
|
|
|
|
// The tombstone logic makes it so the clear() operation can be reversed reliably,
|
|
|
|
|
// and does not affect global state.
|
|
|
|
|
// $this->tombstones[$hook]>0 suppresses any handlers from the HookRegistry,
|
|
|
|
|
// see getHandlers().
|
|
|
|
|
// The TOMBSTONE value in $this->dynamicHandlers[$hook] means that all handlers
|
|
|
|
|
// that precede it in the array are ignored, see getLegacyHandlers().
|
|
|
|
|
$this->dynamicHandlers[$hook][] = self::TOMBSTONE;
|
|
|
|
|
$this->tombstones[$hook] = ( $this->tombstones[$hook] ?? 0 ) + 1;
|
2020-02-10 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register hook and handler, allowing for easy removal.
|
|
|
|
|
* Intended for use in temporary registration e.g. testing
|
|
|
|
|
*
|
|
|
|
|
* @param string $hook Name of hook
|
2020-04-15 19:30:38 +00:00
|
|
|
* @param callable|string|array $callback Handler object to attach
|
|
|
|
|
* @param bool $replace (optional) By default adds callback to handler array.
|
|
|
|
|
* Set true to remove all existing callbacks for the hook.
|
2020-02-10 14:47:46 +00:00
|
|
|
* @return ScopedCallback
|
|
|
|
|
*/
|
2020-04-15 19:30:38 +00:00
|
|
|
public function scopedRegister( string $hook, $callback, bool $replace = false ) : ScopedCallback {
|
2020-08-25 17:04:55 +00:00
|
|
|
// Use a known key to register the handler, so we can later remove it
|
|
|
|
|
// from $this->dynamicHandlers using that key.
|
2020-07-20 21:42:10 +00:00
|
|
|
$id = 'TemporaryHook_' . $this->nextScopedRegisterId++;
|
2020-02-10 14:47:46 +00:00
|
|
|
|
2020-08-25 17:04:55 +00:00
|
|
|
if ( $replace ) {
|
|
|
|
|
// Use a known key for the tombstone, so we can later remove it
|
|
|
|
|
// from $this->dynamicHandlers using that key.
|
|
|
|
|
$ts = "{$id}_TOMBSTONE";
|
|
|
|
|
|
|
|
|
|
// See comment in clear() for the tombstone logic.
|
|
|
|
|
$this->dynamicHandlers[$hook][$ts] = self::TOMBSTONE;
|
|
|
|
|
$this->dynamicHandlers[$hook][$id] = $callback;
|
|
|
|
|
$this->tombstones[$hook] = ( $this->tombstones[$hook] ?? 0 ) + 1;
|
|
|
|
|
|
|
|
|
|
return new ScopedCallback(
|
|
|
|
|
function () use ( $hook, $id, $ts ) {
|
|
|
|
|
unset( $this->dynamicHandlers[$hook][$ts] );
|
|
|
|
|
unset( $this->dynamicHandlers[$hook][$id] );
|
|
|
|
|
$this->tombstones[$hook]--;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$this->dynamicHandlers[$hook][$id] = $callback;
|
|
|
|
|
return new ScopedCallback( function () use ( $hook, $id ) {
|
|
|
|
|
unset( $this->dynamicHandlers[$hook][$id] );
|
|
|
|
|
} );
|
2020-04-15 19:30:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 14:47:46 +00:00
|
|
|
/**
|
|
|
|
|
* Normalize/clean up format of argument passed as hook handler
|
|
|
|
|
*
|
|
|
|
|
* @param array|callable $handler Executable handler function
|
|
|
|
|
* @param string $hook Hook name
|
|
|
|
|
* @return array|false
|
|
|
|
|
* - handler: (callable) Executable handler function
|
|
|
|
|
* - functionName: (string) Handler name for passing to wfDeprecated() or Exceptions thrown
|
|
|
|
|
* - args: (array) handler function arguments
|
|
|
|
|
*/
|
|
|
|
|
private function normalizeHandler( $handler, string $hook ) {
|
|
|
|
|
$normalizedHandler = $handler;
|
|
|
|
|
if ( !is_array( $handler ) ) {
|
|
|
|
|
$normalizedHandler = [ $normalizedHandler ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Empty array or array filled with null/false/empty.
|
|
|
|
|
if ( !array_filter( $normalizedHandler ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_array( $normalizedHandler[0] ) ) {
|
|
|
|
|
// First element is an array, meaning the developer intended
|
|
|
|
|
// the first element to be a callback. Merge it in so that
|
|
|
|
|
// processing can be uniform.
|
|
|
|
|
$normalizedHandler = array_merge( $normalizedHandler[0], array_slice( $normalizedHandler, 1 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$firstArg = $normalizedHandler[0];
|
|
|
|
|
|
2021-05-25 05:35:37 +00:00
|
|
|
// Extract function name, handler callback, and any arguments for the callback
|
2020-02-10 14:47:46 +00:00
|
|
|
if ( $firstArg instanceof Closure ) {
|
|
|
|
|
$functionName = "hook-$hook-closure";
|
|
|
|
|
$callback = array_shift( $normalizedHandler );
|
|
|
|
|
} elseif ( is_object( $firstArg ) ) {
|
|
|
|
|
$object = array_shift( $normalizedHandler );
|
|
|
|
|
$functionName = array_shift( $normalizedHandler );
|
|
|
|
|
|
|
|
|
|
// If no method was specified, default to on$event
|
|
|
|
|
if ( $functionName === null ) {
|
|
|
|
|
$functionName = "on$hook";
|
2020-04-21 01:10:01 +00:00
|
|
|
} else {
|
|
|
|
|
$colonPos = strpos( $functionName, '::' );
|
|
|
|
|
if ( $colonPos !== false ) {
|
|
|
|
|
// Some extensions use [ $object, 'Class::func' ] which
|
|
|
|
|
// worked with call_user_func_array() but doesn't work now
|
2021-05-25 05:35:37 +00:00
|
|
|
// that we use a plain variadic call
|
2020-04-21 01:10:01 +00:00
|
|
|
$functionName = substr( $functionName, $colonPos + 2 );
|
|
|
|
|
}
|
2020-02-10 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$callback = [ $object, $functionName ];
|
|
|
|
|
} elseif ( is_string( $firstArg ) ) {
|
2021-05-25 05:35:37 +00:00
|
|
|
if ( is_callable( $normalizedHandler, true, $functionName )
|
|
|
|
|
&& class_exists( $firstArg ) // $firstArg can be a function in global scope
|
|
|
|
|
) {
|
|
|
|
|
$callback = $normalizedHandler;
|
|
|
|
|
$normalizedHandler = []; // Can't pass arguments here
|
|
|
|
|
} else {
|
|
|
|
|
$functionName = $callback = array_shift( $normalizedHandler );
|
|
|
|
|
}
|
2020-02-10 14:47:46 +00:00
|
|
|
} else {
|
|
|
|
|
throw new UnexpectedValueException( 'Unknown datatype in hooks for ' . $hook );
|
|
|
|
|
}
|
2021-05-25 05:35:37 +00:00
|
|
|
|
2020-02-10 14:47:46 +00:00
|
|
|
return [
|
|
|
|
|
'callback' => $callback,
|
|
|
|
|
'args' => $normalizedHandler,
|
|
|
|
|
'functionName' => $functionName,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run legacy hooks
|
|
|
|
|
* Hook can be: a function, an object, an array of $function and
|
|
|
|
|
* $data, an array of just a function, an array of object and
|
|
|
|
|
* method, or an array of object, method, and data
|
|
|
|
|
* (See hooks.txt for more details)
|
|
|
|
|
*
|
|
|
|
|
* @param string $hook
|
|
|
|
|
* @param array|callable $handler The name of the hooks handler function
|
|
|
|
|
* @param array $args Arguments for hook handler function
|
|
|
|
|
* @param array $options
|
|
|
|
|
* @return null|string|bool
|
|
|
|
|
*/
|
|
|
|
|
private function callLegacyHook( string $hook, $handler, array $args, array $options ) {
|
|
|
|
|
$callback = $handler['callback'];
|
|
|
|
|
$hookArgs = array_merge( $handler['args'], $args );
|
2020-06-03 04:58:25 +00:00
|
|
|
if ( isset( $options['deprecatedVersion'] ) && empty( $options['silent'] ) ) {
|
2020-02-10 14:47:46 +00:00
|
|
|
wfDeprecated(
|
|
|
|
|
"$hook hook (used in " . $handler['functionName'] . ")",
|
|
|
|
|
$options['deprecatedVersion'] ?? false,
|
|
|
|
|
$options['component'] ?? false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// Call the hooks
|
|
|
|
|
return $callback( ...$hookArgs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return whether hook has any handlers registered to it.
|
|
|
|
|
* The function may have been registered via Hooks::register or in extension.json
|
|
|
|
|
*
|
|
|
|
|
* @param string $hook Name of hook
|
|
|
|
|
* @return bool Whether the hook has a handler registered to it
|
|
|
|
|
*/
|
|
|
|
|
public function isRegistered( string $hook ) : bool {
|
2020-08-25 17:04:55 +00:00
|
|
|
if ( $this->tombstones[$hook] ?? false ) {
|
|
|
|
|
// If a tombstone is set, we only care about dynamically registered hooks,
|
|
|
|
|
// and leave it to getLegacyHandlers() to handle the cut-off.
|
|
|
|
|
return !empty( $this->getLegacyHandlers( $hook ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If no tombstone is set, we just check if any of the three arrays contains handlers.
|
|
|
|
|
if ( !empty( $this->registry->getGlobalHooks()[$hook] ) ||
|
|
|
|
|
!empty( $this->dynamicHandlers[$hook] ) ||
|
|
|
|
|
!empty( $this->registry->getExtensionHooks()[$hook] )
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2020-02-10 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attach an event handler to a given hook.
|
|
|
|
|
*
|
|
|
|
|
* @param string $hook Name of hook
|
|
|
|
|
* @param callable|string|array $callback handler object to attach
|
|
|
|
|
*/
|
|
|
|
|
public function register( string $hook, $callback ) {
|
2020-05-11 08:58:38 +00:00
|
|
|
$deprecatedHooks = $this->registry->getDeprecatedHooks();
|
|
|
|
|
$deprecated = $deprecatedHooks->isHookDeprecated( $hook );
|
2020-02-10 14:47:46 +00:00
|
|
|
if ( $deprecated ) {
|
2020-05-11 08:58:38 +00:00
|
|
|
$info = $deprecatedHooks->getDeprecationInfo( $hook );
|
2020-06-03 04:58:25 +00:00
|
|
|
if ( empty( $info['silent'] ) ) {
|
|
|
|
|
$deprecatedVersion = $info['deprecatedVersion'] ?? false;
|
|
|
|
|
$component = $info['component'] ?? false;
|
|
|
|
|
wfDeprecated(
|
|
|
|
|
"$hook hook", $deprecatedVersion, $component
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-02-10 14:47:46 +00:00
|
|
|
}
|
2020-08-25 17:04:55 +00:00
|
|
|
$this->dynamicHandlers[$hook][] = $callback;
|
2020-02-10 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-08-25 17:04:55 +00:00
|
|
|
* Get all handlers for legacy hooks system, plus any handlers added
|
|
|
|
|
* using register().
|
2020-02-10 14:47:46 +00:00
|
|
|
*
|
|
|
|
|
* @internal For use by Hooks.php
|
|
|
|
|
* @param string $hook Name of hook
|
2020-08-25 17:04:55 +00:00
|
|
|
* @return callable[]
|
2020-02-10 14:47:46 +00:00
|
|
|
*/
|
|
|
|
|
public function getLegacyHandlers( string $hook ) : array {
|
2020-08-25 17:04:55 +00:00
|
|
|
if ( $this->tombstones[$hook] ?? false ) {
|
|
|
|
|
// If there is at least one tombstone set for the hook,
|
|
|
|
|
// ignore all handlers from the registry, and
|
|
|
|
|
// only consider handlers registered after the tombstone
|
|
|
|
|
// was set.
|
|
|
|
|
$handlers = $this->dynamicHandlers[$hook] ?? [];
|
|
|
|
|
$keys = array_keys( $handlers );
|
|
|
|
|
|
|
|
|
|
// Loop over the handlers backwards, to find the last tombstone.
|
|
|
|
|
for ( $i = count( $keys ) - 1; $i >= 0; $i-- ) {
|
|
|
|
|
$k = $keys[$i];
|
|
|
|
|
$v = $handlers[$k];
|
|
|
|
|
|
|
|
|
|
if ( $v === self::TOMBSTONE ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return the part of $this->dynamicHandlers[$hook] after the TOMBSTONE
|
|
|
|
|
// marker, preserving keys.
|
|
|
|
|
$keys = array_slice( $keys, $i + 1 );
|
|
|
|
|
$handlers = array_intersect_key( $handlers, array_flip( $keys ) );
|
|
|
|
|
} else {
|
|
|
|
|
// If no tombstone is set, just merge the two arrays.
|
|
|
|
|
$handlers = array_merge(
|
|
|
|
|
$this->registry->getGlobalHooks()[$hook] ?? [],
|
|
|
|
|
$this->dynamicHandlers[$hook] ?? []
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 14:47:46 +00:00
|
|
|
return $handlers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-08-25 17:04:55 +00:00
|
|
|
* 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.
|
|
|
|
|
*
|
2020-02-10 14:47:46 +00:00
|
|
|
* @internal For use by Hooks.php
|
|
|
|
|
* @param string $hook Name of the hook
|
2020-08-10 00:24:11 +00:00
|
|
|
* @param array $options Handler options, which may include:
|
|
|
|
|
* - noServices: Do not allow hook handlers with service dependencies
|
2020-02-10 14:47:46 +00:00
|
|
|
* @return array non-deprecated handler objects
|
|
|
|
|
*/
|
2020-08-10 00:24:11 +00:00
|
|
|
public function getHandlers( string $hook, array $options = [] ) : array {
|
2020-08-25 17:04:55 +00:00
|
|
|
if ( $this->tombstones[$hook] ?? false ) {
|
|
|
|
|
// There is at least one tombstone for the hook, so suppress all new-style hooks.
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2020-02-10 14:47:46 +00:00
|
|
|
$handlers = [];
|
2020-05-11 08:58:38 +00:00
|
|
|
$deprecatedHooks = $this->registry->getDeprecatedHooks();
|
|
|
|
|
$registeredHooks = $this->registry->getExtensionHooks();
|
2020-02-10 14:47:46 +00:00
|
|
|
if ( isset( $registeredHooks[$hook] ) ) {
|
|
|
|
|
foreach ( $registeredHooks[$hook] as $hookReference ) {
|
|
|
|
|
// Non-legacy hooks have handler attributes
|
2020-08-10 00:24:11 +00:00
|
|
|
$handlerSpec = $hookReference['handler'];
|
2020-02-10 14:47:46 +00:00
|
|
|
// Skip hooks that both acknowledge deprecation and are deprecated in core
|
|
|
|
|
$flaggedDeprecated = !empty( $hookReference['deprecated'] );
|
2020-05-11 08:58:38 +00:00
|
|
|
$deprecated = $deprecatedHooks->isHookDeprecated( $hook );
|
2020-02-10 14:47:46 +00:00
|
|
|
if ( $deprecated && $flaggedDeprecated ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-08-10 00:24:11 +00:00
|
|
|
$handlerName = $handlerSpec['name'];
|
2020-11-17 06:15:02 +00:00
|
|
|
if (
|
|
|
|
|
!empty( $options['noServices'] ) && (
|
|
|
|
|
isset( $handlerSpec['services'] ) ||
|
|
|
|
|
isset( $handlerSpec['optional_services'] )
|
|
|
|
|
)
|
|
|
|
|
) {
|
2020-08-10 00:24:11 +00:00
|
|
|
throw new UnexpectedValueException(
|
|
|
|
|
"The handler for the hook $hook registered in " .
|
|
|
|
|
"{$hookReference['extensionPath']} has a service dependency, " .
|
|
|
|
|
"but this hook does not allow it." );
|
|
|
|
|
}
|
2020-02-10 14:47:46 +00:00
|
|
|
if ( !isset( $this->handlersByName[$handlerName] ) ) {
|
2020-04-30 06:08:54 +00:00
|
|
|
$this->handlersByName[$handlerName] =
|
2020-08-10 00:24:11 +00:00
|
|
|
$this->objectFactory->createObject( $handlerSpec );
|
2020-02-10 14:47:46 +00:00
|
|
|
}
|
|
|
|
|
$handlers[] = $this->handlersByName[$handlerName];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $handlers;
|
|
|
|
|
}
|
2020-04-16 06:20:16 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Will log a deprecation warning if:
|
|
|
|
|
* 1. the hook is marked deprecated
|
2020-06-03 04:58:25 +00:00
|
|
|
* 2. the "silent" flag is absent or false, and
|
|
|
|
|
* 3. an extension registers a handler in the new way but does not acknowledge deprecation
|
2020-04-16 06:20:16 +00:00
|
|
|
*/
|
|
|
|
|
public function emitDeprecationWarnings() {
|
2020-05-11 08:58:38 +00:00
|
|
|
$deprecatedHooks = $this->registry->getDeprecatedHooks();
|
|
|
|
|
$registeredHooks = $this->registry->getExtensionHooks();
|
2020-04-16 06:20:16 +00:00
|
|
|
foreach ( $registeredHooks as $name => $handlers ) {
|
2020-05-11 08:58:38 +00:00
|
|
|
if ( $deprecatedHooks->isHookDeprecated( $name ) ) {
|
|
|
|
|
$deprecationInfo = $deprecatedHooks->getDeprecationInfo( $name );
|
2020-06-03 04:58:25 +00:00
|
|
|
if ( !empty( $deprecationInfo['silent'] ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-04-16 06:20:16 +00:00
|
|
|
$version = $deprecationInfo['deprecatedVersion'] ?? '';
|
|
|
|
|
$component = $deprecationInfo['component'] ?? 'MediaWiki';
|
|
|
|
|
foreach ( $handlers as $handler ) {
|
|
|
|
|
if ( !isset( $handler['deprecated'] ) || !$handler['deprecated'] ) {
|
|
|
|
|
MWDebug::sendRawDeprecated(
|
|
|
|
|
"Hook $name was deprecated in $component $version " .
|
|
|
|
|
"but is registered in " . $handler['extensionPath']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-10 14:47:46 +00:00
|
|
|
}
|