2020-01-18 20:25:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-02-05 13:18:36 +00:00
|
|
|
/**
|
|
|
|
|
* 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 Content
|
|
|
|
|
*
|
|
|
|
|
* @author Art Baltai
|
|
|
|
|
*/
|
|
|
|
|
|
2020-01-18 20:25:04 +00:00
|
|
|
namespace MediaWiki\Content;
|
|
|
|
|
|
|
|
|
|
use ContentHandler;
|
|
|
|
|
use FatalError;
|
2020-01-18 20:25:04 +00:00
|
|
|
use InvalidArgumentException;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
|
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
2020-01-18 20:25:04 +00:00
|
|
|
use MWException;
|
|
|
|
|
use MWUnknownContentModelException;
|
2020-06-01 01:53:14 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2020-01-18 20:25:04 +00:00
|
|
|
use UnexpectedValueException;
|
|
|
|
|
use Wikimedia\ObjectFactory;
|
2020-01-18 20:25:04 +00:00
|
|
|
|
2020-02-05 13:18:36 +00:00
|
|
|
/**
|
|
|
|
|
* Class ContentHandlerFactory
|
|
|
|
|
* @package MediaWiki\Content
|
|
|
|
|
* @ingroup Content
|
|
|
|
|
* @since 1.35
|
|
|
|
|
*/
|
2021-07-20 09:02:45 +00:00
|
|
|
final class ContentHandlerFactory implements IContentHandlerFactory {
|
2020-01-18 20:25:04 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string[]|callable[]
|
|
|
|
|
*/
|
2021-05-12 08:47:18 +00:00
|
|
|
private $handlerSpecs;
|
2020-01-18 20:25:04 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var ContentHandler[] Registry of ContentHandler instances by model id
|
|
|
|
|
*/
|
|
|
|
|
private $handlersByModel = [];
|
|
|
|
|
|
2020-06-01 01:53:14 +00:00
|
|
|
/** @var ObjectFactory */
|
2020-01-18 20:25:04 +00:00
|
|
|
private $objectFactory;
|
|
|
|
|
|
2020-06-01 01:53:14 +00:00
|
|
|
/** @var HookRunner */
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
private $hookRunner;
|
|
|
|
|
|
2020-06-01 01:53:14 +00:00
|
|
|
/** @var LoggerInterface */
|
|
|
|
|
private $logger;
|
|
|
|
|
|
2020-01-18 20:25:04 +00:00
|
|
|
/**
|
2020-02-05 13:18:36 +00:00
|
|
|
* @since 1.35
|
|
|
|
|
* @internal Use @see MediaWikiServices::getContentHandlerFactory
|
2020-01-18 20:25:04 +00:00
|
|
|
*
|
2020-02-05 13:18:36 +00:00
|
|
|
* @param string[]|callable[] $handlerSpecs An associative array mapping each known
|
|
|
|
|
* content model to the ObjectFactory spec used to construct its ContentHandler.
|
|
|
|
|
* This array typically comes from $wgContentHandlers.
|
2020-01-18 20:25:04 +00:00
|
|
|
* @param ObjectFactory $objectFactory
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
* @param HookContainer $hookContainer
|
2020-06-01 01:53:14 +00:00
|
|
|
* @param LoggerInterface $logger
|
2020-01-18 20:25:04 +00:00
|
|
|
*/
|
2020-06-01 01:53:14 +00:00
|
|
|
public function __construct(
|
|
|
|
|
array $handlerSpecs,
|
|
|
|
|
ObjectFactory $objectFactory,
|
|
|
|
|
HookContainer $hookContainer,
|
|
|
|
|
LoggerInterface $logger
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
) {
|
2020-01-18 20:25:04 +00:00
|
|
|
$this->handlerSpecs = $handlerSpecs;
|
2020-01-18 20:25:04 +00:00
|
|
|
$this->objectFactory = $objectFactory;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
2020-06-01 01:53:14 +00:00
|
|
|
$this->logger = $logger;
|
2020-01-18 20:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $modelID
|
|
|
|
|
*
|
|
|
|
|
* @return ContentHandler
|
|
|
|
|
* @throws MWException For internal errors and problems in the configuration.
|
|
|
|
|
* @throws MWUnknownContentModelException If no handler is known for the model ID.
|
|
|
|
|
*/
|
|
|
|
|
public function getContentHandler( string $modelID ): ContentHandler {
|
|
|
|
|
if ( empty( $this->handlersByModel[$modelID] ) ) {
|
|
|
|
|
$contentHandler = $this->createForModelID( $modelID );
|
|
|
|
|
|
2020-06-01 01:53:14 +00:00
|
|
|
$this->logger->info(
|
|
|
|
|
"Registered handler for {$modelID}: " . get_class( $contentHandler )
|
|
|
|
|
);
|
2020-01-18 20:25:04 +00:00
|
|
|
$this->handlersByModel[$modelID] = $contentHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->handlersByModel[$modelID];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Define HandlerSpec for ModelID.
|
|
|
|
|
* @param string $modelID
|
|
|
|
|
* @param callable|string $handlerSpec
|
|
|
|
|
*
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @internal
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function defineContentHandler( string $modelID, $handlerSpec ): void {
|
|
|
|
|
if ( !is_callable( $handlerSpec ) && !is_string( $handlerSpec ) ) {
|
|
|
|
|
throw new MWException(
|
|
|
|
|
"ContentHandler Spec for modelID '{$modelID}' must be callable or class name"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
unset( $this->handlersByModel[$modelID] );
|
|
|
|
|
$this->handlerSpecs[$modelID] = $handlerSpec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get defined ModelIDs
|
|
|
|
|
*
|
|
|
|
|
* @return string[]
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @throws FatalError
|
|
|
|
|
*/
|
|
|
|
|
public function getContentModels(): array {
|
2020-01-18 20:25:04 +00:00
|
|
|
$modelsFromHook = [];
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->hookRunner->onGetContentModels( $modelsFromHook );
|
2021-03-14 21:21:10 +00:00
|
|
|
$models = array_merge( // auto-registered from config and MediaWikiServices or manual
|
2020-01-18 20:25:04 +00:00
|
|
|
array_keys( $this->handlerSpecs ),
|
|
|
|
|
|
2020-03-07 04:29:53 +00:00
|
|
|
// incorrect registered and called: without HOOK_NAME_GET_CONTENT_MODELS
|
2020-01-18 20:25:04 +00:00
|
|
|
array_keys( $this->handlersByModel ),
|
|
|
|
|
|
|
|
|
|
// correct registered: as HOOK_NAME_GET_CONTENT_MODELS
|
|
|
|
|
$modelsFromHook );
|
2020-01-18 20:25:04 +00:00
|
|
|
|
2020-01-18 20:25:04 +00:00
|
|
|
return array_unique( $models );
|
2020-01-18 20:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string[]
|
|
|
|
|
* @throws MWException
|
|
|
|
|
*/
|
|
|
|
|
public function getAllContentFormats(): array {
|
|
|
|
|
$formats = [];
|
|
|
|
|
foreach ( $this->handlerSpecs as $model => $class ) {
|
2021-06-11 02:52:06 +00:00
|
|
|
$formats += array_fill_keys(
|
|
|
|
|
$this->getContentHandler( $model )->getSupportedFormats(),
|
|
|
|
|
true );
|
2020-01-18 20:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array_keys( $formats );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $modelID
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws MWException
|
|
|
|
|
*/
|
|
|
|
|
public function isDefinedModel( string $modelID ): bool {
|
|
|
|
|
return in_array( $modelID, $this->getContentModels(), true );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create ContentHandler for ModelID
|
|
|
|
|
*
|
|
|
|
|
* @param string $modelID The ID of the content model for which to get a handler.
|
|
|
|
|
* Use CONTENT_MODEL_XXX constants.
|
|
|
|
|
*
|
|
|
|
|
* @return ContentHandler The ContentHandler singleton for handling the model given by the ID.
|
|
|
|
|
*
|
|
|
|
|
* @throws MWUnknownContentModelException If no handler is known for the model ID.
|
|
|
|
|
* @throws MWException For internal errors and problems in the configuration.
|
|
|
|
|
*/
|
|
|
|
|
private function createForModelID( string $modelID ): ContentHandler {
|
|
|
|
|
$handlerSpec = $this->handlerSpecs[$modelID] ?? null;
|
|
|
|
|
if ( $handlerSpec !== null ) {
|
|
|
|
|
return $this->createContentHandlerFromHandlerSpec( $modelID, $handlerSpec );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->createContentHandlerFromHook( $modelID );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $modelID
|
|
|
|
|
* @param ContentHandler $contentHandler
|
|
|
|
|
*
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @throws MWUnknownContentModelException
|
|
|
|
|
*/
|
|
|
|
|
private function validateContentHandler( string $modelID, $contentHandler ): void {
|
|
|
|
|
if ( $contentHandler === null ) {
|
|
|
|
|
throw new MWUnknownContentModelException( $modelID );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_object( $contentHandler ) ) {
|
|
|
|
|
throw new MWException(
|
|
|
|
|
"ContentHandler for model {$modelID} wrong: non-object given."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$contentHandler instanceof ContentHandler ) {
|
|
|
|
|
throw new MWException(
|
|
|
|
|
"ContentHandler for model {$modelID} must supply a ContentHandler instance, "
|
2020-01-18 20:25:04 +00:00
|
|
|
. get_class( $contentHandler ) . 'given.'
|
2020-01-18 20:25:04 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $modelID
|
|
|
|
|
* @param callable|string $handlerSpec
|
|
|
|
|
*
|
|
|
|
|
* @return ContentHandler
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @throws MWUnknownContentModelException
|
|
|
|
|
*/
|
|
|
|
|
private function createContentHandlerFromHandlerSpec(
|
|
|
|
|
string $modelID, $handlerSpec
|
|
|
|
|
): ContentHandler {
|
2020-01-18 20:25:04 +00:00
|
|
|
try {
|
|
|
|
|
/**
|
|
|
|
|
* @var ContentHandler $contentHandler
|
|
|
|
|
*/
|
2020-11-29 21:59:07 +00:00
|
|
|
$contentHandler = $this->objectFactory->createObject(
|
|
|
|
|
$handlerSpec,
|
2020-01-18 20:25:04 +00:00
|
|
|
[
|
|
|
|
|
'assertClass' => ContentHandler::class,
|
|
|
|
|
'allowCallable' => true,
|
|
|
|
|
'allowClassName' => true,
|
|
|
|
|
'extraArgs' => [ $modelID ],
|
2020-11-29 21:59:07 +00:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
} catch ( InvalidArgumentException $e ) {
|
2020-05-10 00:09:19 +00:00
|
|
|
// legacy support
|
2020-11-29 21:59:07 +00:00
|
|
|
throw new MWException(
|
|
|
|
|
"Wrong Argument HandlerSpec for ModelID: {$modelID}. " .
|
|
|
|
|
"Error: {$e->getMessage()}"
|
|
|
|
|
);
|
|
|
|
|
} catch ( UnexpectedValueException $e ) {
|
2020-05-10 00:09:19 +00:00
|
|
|
// legacy support
|
2020-11-29 21:59:07 +00:00
|
|
|
throw new MWException(
|
|
|
|
|
"Wrong HandlerSpec class for ModelID: {$modelID}. " .
|
|
|
|
|
"Error: {$e->getMessage()}"
|
|
|
|
|
);
|
2020-01-18 20:25:04 +00:00
|
|
|
}
|
|
|
|
|
$this->validateContentHandler( $modelID, $contentHandler );
|
|
|
|
|
|
|
|
|
|
return $contentHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $modelID
|
|
|
|
|
*
|
|
|
|
|
* @return ContentHandler
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @throws MWUnknownContentModelException
|
|
|
|
|
*/
|
|
|
|
|
private function createContentHandlerFromHook( string $modelID ): ContentHandler {
|
|
|
|
|
$contentHandler = null;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->hookRunner->onContentHandlerForModelID( $modelID, $contentHandler );
|
2020-01-18 20:25:04 +00:00
|
|
|
$this->validateContentHandler( $modelID, $contentHandler );
|
|
|
|
|
|
|
|
|
|
'@phan-var ContentHandler $contentHandler';
|
|
|
|
|
|
|
|
|
|
return $contentHandler;
|
|
|
|
|
}
|
|
|
|
|
}
|