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
170 lines
3.9 KiB
PHP
170 lines
3.9 KiB
PHP
<?php
|
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
|
use MediaWiki\HookContainer\HookRunner;
|
|
|
|
/**
|
|
* Configuration handling class for SearchEngine.
|
|
* Provides added service over plain configuration.
|
|
*
|
|
* @since 1.27
|
|
*/
|
|
class SearchEngineConfig {
|
|
|
|
/**
|
|
* Config object from which the settings will be derived.
|
|
* @var Config
|
|
*/
|
|
private $config;
|
|
|
|
/**
|
|
* Current language
|
|
* @var Language
|
|
*/
|
|
private $language;
|
|
|
|
/**
|
|
* Search Engine Mappings
|
|
*
|
|
* Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives).
|
|
* Value is a specification for ObjectFactory.
|
|
*
|
|
* @var array
|
|
*/
|
|
private $engineMappings;
|
|
|
|
/**
|
|
* @var HookRunner
|
|
*/
|
|
private $hookRunner;
|
|
|
|
/**
|
|
* @param Config $config
|
|
* @param Language $lang
|
|
* @param HookContainer $hookContainer
|
|
* @param array $mappings
|
|
*/
|
|
public function __construct( Config $config, Language $lang,
|
|
HookContainer $hookContainer, array $mappings
|
|
) {
|
|
$this->config = $config;
|
|
$this->language = $lang;
|
|
$this->engineMappings = $mappings;
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
|
}
|
|
|
|
/**
|
|
* Retrieve original config.
|
|
* @return Config
|
|
*/
|
|
public function getConfig() {
|
|
return $this->config;
|
|
}
|
|
|
|
/**
|
|
* Make a list of searchable namespaces and their canonical names.
|
|
* @return string[] Namespace ID => name
|
|
* @phan-return array<int,string>
|
|
*/
|
|
public function searchableNamespaces() {
|
|
$arr = [];
|
|
foreach ( $this->language->getNamespaces() as $ns => $name ) {
|
|
if ( $ns >= NS_MAIN ) {
|
|
$arr[$ns] = $name;
|
|
}
|
|
}
|
|
|
|
$this->hookRunner->onSearchableNamespaces( $arr );
|
|
return $arr;
|
|
}
|
|
|
|
/**
|
|
* Extract default namespaces to search from the given user's
|
|
* settings, returning a list of index numbers.
|
|
*
|
|
* @param user $user
|
|
* @return int[]
|
|
*/
|
|
public function userNamespaces( $user ) {
|
|
$arr = [];
|
|
foreach ( $this->searchableNamespaces() as $ns => $name ) {
|
|
if ( $user->getOption( 'searchNs' . $ns ) ) {
|
|
$arr[] = $ns;
|
|
}
|
|
}
|
|
|
|
return $arr;
|
|
}
|
|
|
|
/**
|
|
* An array of namespaces indexes to be searched by default
|
|
*
|
|
* @return int[] Namespace IDs
|
|
*/
|
|
public function defaultNamespaces() {
|
|
return array_keys( $this->config->get( 'NamespacesToBeSearchedDefault' ), true );
|
|
}
|
|
|
|
/**
|
|
* Return the search engines we support. If only $wgSearchType
|
|
* is set, it'll be an array of just that one item.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getSearchTypes() {
|
|
$alternatives = $this->config->get( 'SearchTypeAlternatives' ) ?: [];
|
|
array_unshift( $alternatives, $this->config->get( 'SearchType' ) );
|
|
|
|
return $alternatives;
|
|
}
|
|
|
|
/**
|
|
* Return the search engine configured in $wgSearchType, etc.
|
|
*
|
|
* @return string|null
|
|
*/
|
|
public function getSearchType() {
|
|
return $this->config->get( 'SearchType' );
|
|
}
|
|
|
|
/**
|
|
* Returns the mappings between canonical search name and underlying PHP class
|
|
*
|
|
* Key is the canonical name (used in $wgSearchType and $wgSearchTypeAlternatives).
|
|
* Value is a specification for ObjectFactory.
|
|
*
|
|
* For example to be able to use 'foobarsearch' in $wgSearchType and
|
|
* $wgSearchTypeAlternatives but the PHP class for 'foobarsearch'
|
|
* is 'MediaWiki\Extensions\FoobarSearch\FoobarSearch' set:
|
|
*
|
|
* @par extension.json Example:
|
|
* @code
|
|
* "SearchMappings": {
|
|
* "foobarsearch": { "class": "MediaWiki\\Extensions\\FoobarSearch\\FoobarSearch" }
|
|
* }
|
|
* @endcode
|
|
*
|
|
* @since 1.35
|
|
* @return array
|
|
*/
|
|
public function getSearchMappings() {
|
|
return $this->engineMappings;
|
|
}
|
|
|
|
/**
|
|
* Get a list of namespace names useful for showing in tooltips
|
|
* and preferences.
|
|
*
|
|
* @param int[] $namespaces
|
|
* @return string[] List of names
|
|
*/
|
|
public function namespacesAsText( $namespaces ) {
|
|
$formatted = array_map( [ $this->language, 'getFormattedNsText' ], $namespaces );
|
|
foreach ( $formatted as $key => $ns ) {
|
|
if ( empty( $ns ) ) {
|
|
$formatted[$key] = wfMessage( 'blanknamespace' )->text();
|
|
}
|
|
}
|
|
return $formatted;
|
|
}
|
|
}
|