wiki.techinc.nl/includes/HookContainer/HookContainer.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

733 lines
23 KiB
PHP
Raw Normal View History

Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
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
* @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.
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
*/
namespace MediaWiki\HookContainer;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
use Closure;
use Error;
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
use InvalidArgumentException;
use LogicException;
use MediaWiki\Debug\MWDebug;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
use UnexpectedValueException;
use Wikimedia\Assert\Assert;
use Wikimedia\NonSerializable\NonSerializableTrait;
use Wikimedia\ObjectFactory\ObjectFactory;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
use Wikimedia\ScopedCallback;
use Wikimedia\Services\SalvageableService;
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
use function array_filter;
use function array_keys;
use function array_merge;
use function array_shift;
use function array_unique;
use function is_array;
use function is_object;
use function is_string;
use function strpos;
use function strtr;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
/**
* HookContainer class.
*
* Main class for managing hooks
*
* @since 1.35
*/
class HookContainer implements SalvageableService {
use NonSerializableTrait;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
public const NOOP = '*no-op*';
/**
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* Normalized hook handlers, as a 3D array:
* - the first level maps hook names to lists of handlers
* - the second is a list of handlers
* - each handler is an associative array with some well known keys, as returned by normalizeHandler()
* @var array<array>
*/
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
private $handlers = [];
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
/** @var array<object> handler name and their handler objects */
private $handlerObjects = [];
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
/** @var HookRegistry */
private $registry;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
/**
* Handlers registered by calling register().
* @var array
*/
private $extraHandlers = [];
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
/** @var ObjectFactory */
private $objectFactory;
/** @var int The next ID to be used by scopedRegister() */
private $nextScopedRegisterId = 0;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
/**
* @param HookRegistry $hookRegistry
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
* @param ObjectFactory $objectFactory
*/
public function __construct(
HookRegistry $hookRegistry,
ObjectFactory $objectFactory
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
) {
$this->registry = $hookRegistry;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
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
*/
public function salvage( SalvageableService $other ) {
Assert::parameterType( self::class, $other, '$other' );
if ( $this->handlers || $this->handlerObjects || $this->extraHandlers ) {
throw new LogicException( 'salvage() must be called immediately after construction' );
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$this->handlerObjects = $other->handlerObjects;
$this->handlers = $other->handlers;
$this->extraHandlers = $other->extraHandlers;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
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 sequence.
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
* 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
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
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 {
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$checkDeprecation = isset( $options['deprecatedVersion'] );
$abortable = $options['abortable'] ?? true;
foreach ( $this->getHandlers( $hook, $options ) as $handler ) {
if ( $checkDeprecation ) {
$this->checkDeprecation( $hook, $handler, $options );
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
}
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
// Compose callback arguments.
if ( !empty( $handler['args'] ) ) {
$callbackArgs = array_merge( $handler['args'], $args );
} else {
$callbackArgs = $args;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
// Call the handler.
$callback = $handler['callback'];
$return = $callback( ...$callbackArgs );
// Handler returned false, signal abort to caller
if ( $return === false ) {
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
if ( !$abortable ) {
throw new UnexpectedValueException( "Handler {$handler['functionName']}" .
" return false for unabortable $hook." );
}
return false;
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
} elseif ( $return !== null && $return !== true ) {
throw new UnexpectedValueException(
"Hook handlers can only return null or a boolean. Got an unexpected value from " .
"handler {$handler['functionName']} for $hook" );
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
}
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
return true;
}
/**
* Clear handlers of the given hook.
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
* 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 testing only
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
* @codeCoverageIgnore
*/
public function clear( string $hook ): void {
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
throw new LogicException( 'Cannot reset hooks in operation.' );
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$this->handlers[$hook] = [];
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
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
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* @param callable|string|array $handler Handler to attach
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
* @return ScopedCallback
*/
public function scopedRegister( string $hook, $handler ): ScopedCallback {
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$handler = $this->normalizeHandler( $hook, $handler );
if ( !$handler ) {
throw new InvalidArgumentException( 'Bad hook handler!' );
}
$this->checkDeprecation( $hook, $handler );
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$id = 'TemporaryHook_' . $this->nextScopedRegisterId++;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$this->getHandlers( $hook );
$this->handlers[$hook][$id] = $handler;
return new ScopedCallback( function () use ( $hook, $id ) {
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
unset( $this->handlers[$hook][$id] );
} );
}
/**
* Returns a callable array based on the handler specification provided.
* This will find the appropriate handler object to call a method on,
* This will find the appropriate handler object to call a method on,
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* instantiating it if it doesn't exist yet.
*
* @param string $hook The name of the hook the handler was registered for
* @param array $handler A hook handler specification as given in an extension.json file.
* @param array $options Options to apply. If the 'noServices' option is set and the
* handler requires service injection, this method will throw an
* UnexpectedValueException.
*
* @return array
*/
private function makeExtensionHandlerCallback( string $hook, array $handler, array $options = [] ): array {
$spec = $handler['handler'];
$name = $spec['name'];
if (
!empty( $options['noServices'] ) && (
!empty( $spec['services'] ) ||
!empty( $spec['optional_services'] )
)
) {
throw new UnexpectedValueException(
"The handler for the hook $hook registered in " .
"{$handler['extensionPath']} has a service dependency, " .
"but this hook does not allow it." );
}
if ( !isset( $this->handlerObjects[$name] ) ) {
// @phan-suppress-next-line PhanTypeInvalidCallableArraySize
$this->handlerObjects[$name] = $this->objectFactory->createObject( $spec );
}
$obj = $this->handlerObjects[$name];
$method = $this->getHookMethodName( $hook );
return [ $obj, $method ];
}
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
/**
* Normalize/clean up format of argument passed as hook handler
*
* @param string $hook Hook name
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* @param string|array|callable $handler Executable handler function. See register() for supported structures.
* @param array $options see makeExtensionHandlerCallback()
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
*
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
* @return array|false
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* - callback: (callable) Executable handler function
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
* - functionName: (string) Handler name for passing to wfDeprecated() or Exceptions thrown
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* - args: (array) Extra handler function arguments (omitted when not needed)
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
*/
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
private function normalizeHandler( string $hook, $handler, array $options = [] ) {
if ( is_object( $handler ) && !$handler instanceof Closure ) {
$handler = [ $handler, $this->getHookMethodName( $hook ) ];
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
// Backwards compatibility with old-style callable that uses a qualified method name.
if ( is_array( $handler ) && is_object( $handler[0] ?? false ) && is_string( $handler[1] ?? false ) ) {
$ofs = strpos( $handler[1], '::' );
if ( $ofs !== false ) {
$msg = self::callableToString( $handler );
wfDeprecatedMsg( "Deprecated handler style for hook '$hook': " .
"callable using qualified method name ($msg)" );
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$handler[1] = substr( $handler[1], $ofs + 2 );
}
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
// Backwards compatibility: support objects wrapped in an array but no method name.
if ( is_array( $handler ) && is_object( $handler[0] ?? false ) && !isset( $handler[1] ) ) {
if ( !$handler[0] instanceof Closure ) {
$handler[1] = $this->getHookMethodName( $hook );
$msg = self::callableToString( $handler );
wfDeprecatedMsg( "Deprecated handler style for hook '$hook': object wrapped in array ($msg)" );
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
}
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
// The empty callback is used to represent a no-op handler in some test cases.
if ( $handler === [] || $handler === null || $handler === false || $handler === self::NOOP ) {
if ( $handler !== self::NOOP ) {
wfDeprecatedMsg(
"Deprecated handler style for hook '$hook': falsy value, use HookContainer::NOOP instead."
);
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
return [
'callback' => static function () {
// no-op
},
'functionName' => self::NOOP,
];
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
// Plain callback
if ( self::mayBeCallable( $handler ) ) {
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
return [
'callback' => $handler,
'functionName' => self::callableToString( $handler ),
];
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
// Not callable and not an array. Something is wrong.
if ( !is_array( $handler ) ) {
return false;
}
// Empty array or array filled with null/false/empty.
if ( !array_filter( $handler ) ) {
return false;
}
// ExtensionRegistry style handler
if ( isset( $handler['handler'] ) ) {
// Skip hooks that both acknowledge deprecation and are deprecated in core
if ( $handler['deprecated'] ?? false ) {
$deprecatedHooks = $this->registry->getDeprecatedHooks();
$deprecated = $deprecatedHooks->isHookDeprecated( $hook );
if ( $deprecated ) {
return false;
}
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$callback = $this->makeExtensionHandlerCallback( $hook, $handler, $options );
return [
'callback' => $callback,
'functionName' => self::callableToString( $callback ),
];
}
// Not an indexed array, something is wrong.
if ( !isset( $handler[0] ) ) {
return false;
}
// Backwards compatibility: support for arrays of the form [ $object, $method, $data... ]
if ( is_object( $handler[0] ) && is_string( $handler[1] ?? false ) && array_key_exists( 2, $handler ) ) {
$obj = $handler[0];
if ( !$obj instanceof Closure ) {
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset
$method = $handler[1];
$handler = array_merge(
[ [ $obj, $method ] ],
array_slice( $handler, 2 )
);
$msg = self::callableToString( $handler[1] );
wfDeprecatedMsg( "Deprecated handler style for hook '$hook': callable array with extra data ($msg)" );
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
}
// The only option left is an array of the form [ $callable, $data... ]
$callback = array_shift( $handler );
// Backwards-compatibility for callbacks in the form [ [ $function ], $data ]
if ( is_array( $callback ) && count( $callback ) === 1 && is_string( $callback[0] ?? null ) ) {
$callback = $callback[0];
$msg = self::callableToString( $callback );
wfDeprecatedMsg( "Deprecated handler style for hook '$hook': function wrapped in array ($msg)" );
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
}
if ( !self::mayBeCallable( $callback ) ) {
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
return false;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
}
$msg = self::callableToString( $callback );
wfDeprecatedMsg( "Deprecated handler style for hook '$hook': callable wrapped in array ($msg)" );
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
return [
'callback' => $callback,
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
'functionName' => self::callableToString( $callback ),
'args' => $handler,
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
];
}
/**
* 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 {
return (bool)$this->getHandlers( $hook );
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
}
/**
* Attach an event handler to a given hook.
*
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* The handler should be given in one of the following forms:
*
* 1) A callable (string, array, or closure)
* 2) An extension hook handler spec in the form returned by
* HookRegistry::getExtensionHooks
*
* Several other forms are supported for backwards compatibility, but
* should not be used when calling this method directly.
*
* @note This method accepts "broken callables", that is, callable
* structures that reference classes that could not be found or could
* not be loaded, e.g. because they implement an interface that cannot
* be loaded. This situation may legitimately arise when implementing
* hooks defined by extensions that are not present.
* In that case, the hook will never fire and registering the "broken"
* handlers is harmless. If a broken hook handler is registered for a
* hook that is indeed called, it will cause an error. This is
* intentional: we don't want to silently ignore mistakes like mistyped
* class names in a hook handler registration.
*
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
* @param string $hook Name of hook
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* @param string|array|callable $handler handler
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
*/
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
public function register( string $hook, $handler ) {
$this->checkDeprecation( $hook, $handler );
if ( !isset( $this->handlers[$hook] ) ) {
// Just remember the handler for later.
// NOTE: It would be nice to normalize immediately. But since some extensions make extensive
// use of this method for registering hooks on every call, that could be a performance
// issue. This is particularly true if the hook is declared in a way that would require
// service objects to be instantiated.
$this->extraHandlers[$hook][] = $handler;
return;
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$normalized = $this->normalizeHandler( $hook, $handler );
if ( !$normalized ) {
throw new InvalidArgumentException( 'Bad hook handler!' );
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$this->getHandlers( $hook );
$this->handlers[$hook][] = $normalized;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
}
/**
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* Get handler callbacks.
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
*
* @deprecated since 1.41.
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* @internal For use by FauxHookHandlerArray. Delete when no longer needed.
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
* @param string $hook Name of hook
* @return callable[]
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
*/
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
public function getHandlerCallbacks( string $hook ): array {
wfDeprecated( __METHOD__, '1.41' );
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$handlers = $this->getHandlers( $hook );
$callbacks = [];
foreach ( $handlers as $h ) {
$callback = $h['callback'];
if ( isset( $h['args'] ) ) {
// Needs curry in order to pass extra arguments.
// NOTE: This does not support reference parameters!
$extraArgs = $h['args'];
$callbacks[] = static function ( ...$hookArgs ) use ( $callback, $extraArgs ) {
return $callback( ...$extraArgs, ...$hookArgs );
};
} else {
$callbacks[] = $callback;
}
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
return $callbacks;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
}
/**
* Returns the names of all hooks that have at least one handler registered.
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* @return string[]
*/
public function getHookNames(): array {
$names = array_merge(
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
array_keys( array_filter( $this->handlers ) ),
array_keys( array_filter( $this->extraHandlers ) ),
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
array_keys( array_filter( $this->registry->getGlobalHooks() ) ),
array_keys( array_filter( $this->registry->getExtensionHooks() ) )
);
return array_unique( $names );
}
/**
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* Return the array of handlers for the given hook.
*
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* @param string $hook Name of the hook
* @param array $options Handler options, which may include:
* - noServices: Do not allow hook handlers with service dependencies
* @return array[] A list of handler entries
*/
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
private function getHandlers( string $hook, array $options = [] ): array {
if ( !isset( $this->handlers[$hook] ) ) {
$handlers = [];
$registeredHooks = $this->registry->getExtensionHooks();
$configuredHooks = $this->registry->getGlobalHooks();
$rawHandlers = array_merge(
$configuredHooks[ $hook ] ?? [],
$registeredHooks[ $hook ] ?? [],
$this->extraHandlers[ $hook ] ?? [],
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
);
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
foreach ( $rawHandlers as $raw ) {
$handler = $this->normalizeHandler( $hook, $raw, $options );
if ( !$handler ) {
// XXX: log this?!
// NOTE: also happens for deprecated hooks, which is fine!
continue;
}
$handlers[] = $handler;
}
$this->handlers[ $hook ] = $handlers;
}
return $this->handlers[ $hook ];
}
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
/**
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* Return the array of strings that describe the handler registered with the given hook.
*
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* @internal Only public for use by ApiQuerySiteInfo.php and SpecialVersion.php
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
* @param string $hook Name of the hook
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* @return string[] A list of handler descriptions
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
*/
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
public function getHandlerDescriptions( string $hook ): array {
$descriptions = [];
if ( isset( $this->handlers[ $hook ] ) ) {
$rawHandlers = $this->handlers[ $hook ];
} else {
$registeredHooks = $this->registry->getExtensionHooks();
$configuredHooks = $this->registry->getGlobalHooks();
$rawHandlers = array_merge(
$configuredHooks[ $hook ] ?? [],
$registeredHooks[ $hook ] ?? [],
$this->extraHandlers[ $hook ] ?? [],
);
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
foreach ( $rawHandlers as $raw ) {
$descr = $this->describeHandler( $hook, $raw );
if ( $descr ) {
$descriptions[] = $descr;
}
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
return $descriptions;
}
/**
* Returns a human-readable description of the given handler.
*
* @param string $hook
* @param string|array|callable $handler
*
* @return ?string
*/
private function describeHandler( string $hook, $handler ): ?string {
if ( is_array( $handler ) ) {
// already normalized
if ( isset( $handler['functionName'] ) ) {
return $handler['functionName'];
}
if ( isset( $handler['callback'] ) ) {
return self::callableToString( $handler['callback'] );
}
if ( isset( $handler['handler']['class'] ) ) {
// New style hook. Avoid instantiating the handler object
$method = $this->getHookMethodName( $hook );
return $handler['handler']['class'] . '::' . $method;
}
}
$handler = $this->normalizeHandler( $hook, $handler );
return $handler ? $handler['functionName'] : null;
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
}
/**
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* For each hook handler of each hook, this will log a deprecation if:
* 1. the hook is marked deprecated and
* 2. the "silent" flag is absent or false, and
* 3. an extension registers a handler in the new way but does not acknowledge deprecation
*/
public function emitDeprecationWarnings() {
$deprecatedHooks = $this->registry->getDeprecatedHooks();
$extensionHooks = $this->registry->getExtensionHooks();
foreach ( $extensionHooks as $name => $handlers ) {
if ( $deprecatedHooks->isHookDeprecated( $name ) ) {
$deprecationInfo = $deprecatedHooks->getDeprecationInfo( $name );
if ( !empty( $deprecationInfo['silent'] ) ) {
continue;
}
$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']
);
}
}
}
}
}
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
/**
* Will trigger a deprecation warning if the given hook is deprecated and the deprecation
* is not marked as silent.
*
* @param string $hook The name of the hook.
* @param array|callable|string $handler A handler spec
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
* @param array|null $deprecationInfo Deprecation info if the caller already knows it.
* If not given, it will be looked up from the hook registry.
*
* @return void
*/
private function checkDeprecation( string $hook, $handler, ?array $deprecationInfo = null ): void {
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
if ( !$deprecationInfo ) {
$deprecatedHooks = $this->registry->getDeprecatedHooks();
$deprecationInfo = $deprecatedHooks->getDeprecationInfo( $hook );
}
if ( $deprecationInfo && empty( $deprecationInfo['silent'] ) ) {
$description = $this->describeHandler( $hook, $handler );
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
wfDeprecated(
"$hook hook (used in $description)",
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
$deprecationInfo['deprecatedVersion'] ?? false,
$deprecationInfo['component'] ?? false
);
}
}
/**
* Returns a human-readable representation of the given callable.
*
* @param callable $callable
*
* @return string
*/
private static function callableToString( $callable ): string {
if ( is_string( $callable ) ) {
return $callable;
}
if ( $callable instanceof Closure ) {
$hash = spl_object_hash( $callable );
return "*closure#$hash*";
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
}
if ( is_array( $callable ) ) {
[ $on, $func ] = $callable;
if ( is_object( $on ) ) {
$on = get_class( $on );
}
return "$on::$func";
}
throw new InvalidArgumentException( 'Unexpected kind of callable' );
Simplify HookContainer (v2) This reverts change I50c3d1c5df (commit b0317287bc), thus reinstating change I7d690a1172 (commit d139eb07fe). The only change from the original is in getHookMethodName(), additionally replacing '-' with '_' (not just ':' and '\'). The original commit message follows: This converts all hook handlers to the same internal representation. This is done lazily, when the hook is run for the first time. The logic for temporarily disabling handlers by calling scopedRegister() with the $replace parameter set has been greatly simplified. There are some minor changes to the class's interface and behavior, none of which should be breaking changes: * run() will emit deprecation warnings if and only if it was called with the deprecationVersion option set, for all kinds of handlers. The idea is that deprecated hooks should emit a warning either from run(), or from emitDeprecationWarnings(). The latter happens if the hook is listed in DeprecatedHooks. * register() now also accepts hook handlers declared in the way that extensions register hooks. * Attempts to call register() with an invalid hook definition now result in an invalidArgumentException. * Attempts to call register() for a deprecated hook will consistently result in a deprecation warning. * The internal getRegisteredHooks() method has been removed in favor of the identical getHookNames() method. * The internal getLegacyHandlers method has been removed in favor of getHandlerDescriptions() and getHandlerCallbacks(). * The call order changed so that dynamically registered handlers are called last, instead of getting called before handler objects from extensions. Bug: T338213 Change-Id: I6efb09e314ad2b124a33a757fdda2a07ae0d8f7c
2023-06-06 09:25:48 +00:00
}
/**
* Returns the default handler method name for the given hook.
*
* @param string $hook
*
* @return string
*/
private function getHookMethodName( string $hook ): string {
$hook = strtr( $hook, ':\\-', '___' );
return "on$hook";
}
/**
* Replacement for is_callable that will also return true when the callable uses a class
* that cannot be loaded.
*
* This may legitimately happen when a hook handler uses a hook interfaces that is defined
* in another extension. In that case, the hook itself is also defined in the other extension,
* so the hook will never be called and no problem arises.
*
* However, it is entirely possible to register broken handlers for hooks that will indeed
* be called, causing an error. This is intentional: we don't want to silently ignore
* mistakes like mistyped class names in a hook handler registration.
*
* @param mixed $v
*
* @return bool
*/
private static function mayBeCallable( $v ): bool {
try {
return is_callable( $v );
} catch ( Error $error ) {
// If the callable uses a class that can't be loaded because it extends an unknown base class.
// Continue as if is_callable had returned true, to allow the handler to be registered.
if ( preg_match( '/Class.*not found/', $error->getMessage() ) ) {
return true;
}
throw $error;
}
}
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
}