aboutsummaryrefslogtreecommitdiffstats
path: root/includes/search
diff options
context:
space:
mode:
authorTim Starling <tstarling@wikimedia.org>2020-03-19 13:42:09 +1100
committerReedy <reedy@wikimedia.org>2020-05-30 14:23:28 +0000
commit68c433bd23bd134883225f03626a94a16fc16bda (patch)
tree85bbc470ac4e40853bb012e04d7a4bda9f5be293 /includes/search
parent2530009b8c1146716f23539925d9c86c620db918 (diff)
downloadmediawikicore-68c433bd23bd134883225f03626a94a16fc16bda.tar.gz
mediawikicore-68c433bd23bd134883225f03626a94a16fc16bda.zip
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
Diffstat (limited to 'includes/search')
-rw-r--r--includes/search/PrefixSearch.php7
-rw-r--r--includes/search/RevisionSearchResultTrait.php2
-rw-r--r--includes/search/SearchEngine.php63
-rw-r--r--includes/search/SearchEngineConfig.php21
-rw-r--r--includes/search/SearchEngineFactory.php12
-rw-r--r--includes/search/SearchNearMatcher.php24
-rw-r--r--includes/search/searchwidgets/FullSearchResultWidget.php26
-rw-r--r--includes/search/searchwidgets/SearchFormWidget.php36
8 files changed, 154 insertions, 37 deletions
diff --git a/includes/search/PrefixSearch.php b/includes/search/PrefixSearch.php
index 828cc7112e79..0d7b5a616d31 100644
--- a/includes/search/PrefixSearch.php
+++ b/includes/search/PrefixSearch.php
@@ -122,10 +122,9 @@ abstract class PrefixSearch {
}
}
$srchres = [];
- if ( Hooks::run(
- 'PrefixSearchBackend',
- [ $namespaces, $search, $limit, &$srchres, $offset ]
- ) ) {
+ if ( Hooks::runner()->onPrefixSearchBackend(
+ $namespaces, $search, $limit, $srchres, $offset )
+ ) {
return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
}
return $this->strings(
diff --git a/includes/search/RevisionSearchResultTrait.php b/includes/search/RevisionSearchResultTrait.php
index 01f68c4fd06e..8901d99ddad5 100644
--- a/includes/search/RevisionSearchResultTrait.php
+++ b/includes/search/RevisionSearchResultTrait.php
@@ -44,7 +44,7 @@ trait RevisionSearchResultTrait {
if ( $title !== null ) {
$services = MediaWikiServices::getInstance();
$id = false;
- Hooks::run( 'SearchResultInitFromTitle', [ $title, &$id ] );
+ Hooks::runner()->onSearchResultInitFromTitle( $title, $id );
$this->mRevisionRecord = $services->getRevisionLookup()->getRevisionByTitle(
$title,
diff --git a/includes/search/SearchEngine.php b/includes/search/SearchEngine.php
index 7ba3fb041b03..0d33111b1c8f 100644
--- a/includes/search/SearchEngine.php
+++ b/includes/search/SearchEngine.php
@@ -25,6 +25,8 @@
* @defgroup Search Search
*/
+use MediaWiki\HookContainer\HookContainer;
+use MediaWiki\HookContainer\HookRunner;
use MediaWiki\MediaWikiServices;
/**
@@ -59,6 +61,12 @@ abstract class SearchEngine {
/** @var array Feature values */
protected $features = [];
+ /** @var HookContainer */
+ private $hookContainer;
+
+ /** @var HookRunner */
+ private $hookRunner;
+
/** Profile type for completionSearch */
public const COMPLETION_PROFILE_TYPE = 'completionSearchProfile';
@@ -245,7 +253,9 @@ abstract class SearchEngine {
*/
public function getNearMatcher( Config $config ) {
return new SearchNearMatcher( $config,
- MediaWikiServices::getInstance()->getContentLanguage() );
+ MediaWikiServices::getInstance()->getContentLanguage(),
+ $this->getHookContainer()
+ );
}
/**
@@ -415,7 +425,7 @@ abstract class SearchEngine {
} elseif ( $withPrefixSearchExtractNamespaceHook ) {
$hookNamespaces = [ NS_MAIN ];
$hookQuery = $query;
- Hooks::run( 'PrefixSearchExtractNamespace', [ &$hookNamespaces, &$hookQuery ] );
+ Hooks::runner()->onPrefixSearchExtractNamespace( $hookNamespaces, $hookQuery );
if ( $hookQuery !== $query ) {
$parsed = $hookQuery;
$extractedNamespace = $hookNamespaces;
@@ -551,9 +561,9 @@ abstract class SearchEngine {
$search = trim( $search );
if ( !in_array( NS_SPECIAL, $this->namespaces ) && // We do not run hook on Special: search
- !Hooks::run( 'PrefixSearchBackend',
- [ $this->namespaces, $search, $this->limit, &$results, $this->offset ]
- ) ) {
+ !$this->getHookRunner()->onPrefixSearchBackend(
+ $this->namespaces, $search, $this->limit, $results, $this->offset )
+ ) {
// False means hook worked.
// FIXME: Yes, the API is weird. That's why it is going to be deprecated.
@@ -785,7 +795,7 @@ abstract class SearchEngine {
}
}
// Hook to allow extensions to produce search mapping fields
- Hooks::run( 'SearchIndexFields', [ &$fields, $this ] );
+ $this->getHookRunner()->onSearchIndexFields( $fields, $this );
return $fields;
}
@@ -797,7 +807,7 @@ abstract class SearchEngine {
public function augmentSearchResults( ISearchResultSet $resultSet ) {
$setAugmentors = [];
$rowAugmentors = [];
- Hooks::run( "SearchResultsAugment", [ &$setAugmentors, &$rowAugmentors ] );
+ $this->getHookRunner()->onSearchResultsAugment( $setAugmentors, $rowAugmentors );
if ( !$setAugmentors && !$rowAugmentors ) {
// We're done here
return;
@@ -822,4 +832,43 @@ abstract class SearchEngine {
}
}
}
+
+ /**
+ * @since 1.35
+ * @internal
+ * @param HookContainer $hookContainer
+ */
+ public function setHookContainer( HookContainer $hookContainer ) {
+ $this->hookContainer = $hookContainer;
+ $this->hookRunner = new HookRunner( $hookContainer );
+ }
+
+ /**
+ * Get a HookContainer, for running extension hooks or for hook metadata.
+ *
+ * @since 1.35
+ * @return HookContainer
+ */
+ protected function getHookContainer() : HookContainer {
+ if ( !$this->hookContainer ) {
+ $this->hookContainer = MediaWikiServices::getInstance()->getHookContainer();
+ }
+ return $this->hookContainer;
+ }
+
+ /**
+ * Get a HookRunner for running core hooks.
+ *
+ * @internal This is for use by core only. Hook interfaces may be removed
+ * without notice.
+ * @since 1.35
+ * @return HookRunner
+ */
+ protected function getHookRunner() : HookRunner {
+ if ( !$this->hookRunner ) {
+ $this->hookRunner = new HookRunner( $this->getHookContainer() );
+ }
+ return $this->hookRunner;
+ }
+
}
diff --git a/includes/search/SearchEngineConfig.php b/includes/search/SearchEngineConfig.php
index 73ada316d0d0..b241ba7523cb 100644
--- a/includes/search/SearchEngineConfig.php
+++ b/includes/search/SearchEngineConfig.php
@@ -1,5 +1,8 @@
<?php
+use MediaWiki\HookContainer\HookContainer;
+use MediaWiki\HookContainer\HookRunner;
+
/**
* Configuration handling class for SearchEngine.
* Provides added service over plain configuration.
@@ -30,10 +33,24 @@ class SearchEngineConfig {
*/
private $engineMappings;
- public function __construct( Config $config, Language $lang, array $mappings ) {
+ /**
+ * @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 );
}
/**
@@ -57,7 +74,7 @@ class SearchEngineConfig {
}
}
- Hooks::run( 'SearchableNamespaces', [ &$arr ] );
+ $this->hookRunner->onSearchableNamespaces( $arr );
return $arr;
}
diff --git a/includes/search/SearchEngineFactory.php b/includes/search/SearchEngineFactory.php
index 2e3223809313..3ad7543fea37 100644
--- a/includes/search/SearchEngineFactory.php
+++ b/includes/search/SearchEngineFactory.php
@@ -1,5 +1,6 @@
<?php
+use MediaWiki\HookContainer\HookContainer;
use MediaWiki\MediaWikiServices;
use Wikimedia\ObjectFactory;
use Wikimedia\Rdbms\IDatabase;
@@ -16,8 +17,12 @@ class SearchEngineFactory {
*/
private $config;
- public function __construct( SearchEngineConfig $config ) {
+ /** @var HookContainer */
+ private $hookContainer;
+
+ public function __construct( SearchEngineConfig $config, HookContainer $hookContainer ) {
$this->config = $config;
+ $this->hookContainer = $hookContainer;
}
/**
@@ -54,7 +59,10 @@ class SearchEngineFactory {
$args['extraArgs'][] = $lb;
}
- return ObjectFactory::getObjectFromSpec( $spec, $args );
+ /** @var SearchEngine $engine */
+ $engine = ObjectFactory::getObjectFromSpec( $spec, $args );
+ $engine->setHookContainer( $this->hookContainer );
+ return $engine;
}
/**
diff --git a/includes/search/SearchNearMatcher.php b/includes/search/SearchNearMatcher.php
index c775c48b217c..638eb78b6f54 100644
--- a/includes/search/SearchNearMatcher.php
+++ b/includes/search/SearchNearMatcher.php
@@ -1,5 +1,7 @@
<?php
+use MediaWiki\HookContainer\HookContainer;
+use MediaWiki\HookContainer\HookRunner;
use MediaWiki\MediaWikiServices;
/**
@@ -24,11 +26,23 @@ class SearchNearMatcher {
*/
private $languageConverter;
- public function __construct( Config $config, Language $lang ) {
+ /**
+ * @var HookRunner
+ */
+ private $hookRunner;
+
+ /**
+ * SearchNearMatcher constructor.
+ * @param Config $config
+ * @param Language $lang
+ * @param HookContainer $hookContainer
+ */
+ public function __construct( Config $config, Language $lang, HookContainer $hookContainer ) {
$this->config = $config;
$this->language = $lang;
$this->languageConverter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
->getLanguageConverter( $lang );
+ $this->hookRunner = new HookRunner( $hookContainer );
}
/**
@@ -41,7 +55,7 @@ class SearchNearMatcher {
public function getNearMatch( $searchterm ) {
$title = $this->getNearMatchInternal( $searchterm );
- Hooks::run( 'SearchGetNearMatchComplete', [ $searchterm, &$title ] );
+ $this->hookRunner->onSearchGetNearMatchComplete( $searchterm, $title );
return $title;
}
@@ -72,7 +86,7 @@ class SearchNearMatcher {
}
$titleResult = null;
- if ( !Hooks::run( 'SearchGetNearMatchBefore', [ $allSearchTerms, &$titleResult ] ) ) {
+ if ( !$this->hookRunner->onSearchGetNearMatchBefore( $allSearchTerms, $titleResult ) ) {
return $titleResult;
}
@@ -105,7 +119,7 @@ class SearchNearMatcher {
return $title;
}
- if ( !Hooks::run( 'SearchAfterNoDirectMatch', [ $term, &$title ] ) ) {
+ if ( !$this->hookRunner->onSearchAfterNoDirectMatch( $term, $title ) ) {
return $title;
}
@@ -135,7 +149,7 @@ class SearchNearMatcher {
// Give hooks a chance at better match variants
$title = null;
- if ( !Hooks::run( 'SearchGetNearMatch', [ $term, &$title ] ) ) {
+ if ( !$this->hookRunner->onSearchGetNearMatch( $term, $title ) ) {
return $title;
}
}
diff --git a/includes/search/searchwidgets/FullSearchResultWidget.php b/includes/search/searchwidgets/FullSearchResultWidget.php
index 3785d293582f..706deffc0b44 100644
--- a/includes/search/searchwidgets/FullSearchResultWidget.php
+++ b/includes/search/searchwidgets/FullSearchResultWidget.php
@@ -3,8 +3,9 @@
namespace MediaWiki\Search\SearchWidgets;
use Category;
-use Hooks;
use HtmlArmor;
+use MediaWiki\HookContainer\HookContainer;
+use MediaWiki\HookContainer\HookRunner;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\MediaWikiServices;
use SearchResult;
@@ -23,10 +24,15 @@ class FullSearchResultWidget implements SearchResultWidget {
protected $specialPage;
/** @var LinkRenderer */
protected $linkRenderer;
+ /** @var HookRunner */
+ private $hookRunner;
- public function __construct( SpecialSearch $specialPage, LinkRenderer $linkRenderer ) {
+ public function __construct( SpecialSearch $specialPage, LinkRenderer $linkRenderer,
+ HookContainer $hookContainer
+ ) {
$this->specialPage = $specialPage;
$this->linkRenderer = $linkRenderer;
+ $this->hookRunner = new HookRunner( $hookContainer );
}
/**
@@ -82,11 +88,10 @@ class FullSearchResultWidget implements SearchResultWidget {
// TODO: remove this instanceof and always pass [], let implementors do the cast if
// they want to be SearchDatabase specific
$terms = $result instanceof \SqlSearchResult ? $result->getTermMatches() : [];
- if ( !Hooks::run( 'ShowSearchHit', [
- $this->specialPage, $result, $terms,
- &$link, &$redirect, &$section, &$extract,
- &$score, &$desc, &$date, &$related, &$html
- ] ) ) {
+ if ( !$this->hookRunner->onShowSearchHit( $this->specialPage, $result,
+ $terms, $link, $redirect, $section, $extract, $score,
+ $desc, $date, $related, $html )
+ ) {
return $html;
}
}
@@ -139,10 +144,9 @@ class FullSearchResultWidget implements SearchResultWidget {
$query = [];
$attributes = [ 'data-serp-pos' => $position ];
- Hooks::run( 'ShowSearchHitTitle',
- [ &$title, &$snippet, $result,
- $result instanceof \SqlSearchResult ? $result->getTermMatches() : [],
- $this->specialPage, &$query, &$attributes ] );
+ $this->hookRunner->onShowSearchHitTitle( $title, $snippet, $result,
+ $result instanceof \SqlSearchResult ? $result->getTermMatches() : [],
+ $this->specialPage, $query, $attributes );
$link = $this->linkRenderer->makeLink(
$title,
diff --git a/includes/search/searchwidgets/SearchFormWidget.php b/includes/search/searchwidgets/SearchFormWidget.php
index 13e58d9a33f0..8f913996b024 100644
--- a/includes/search/searchwidgets/SearchFormWidget.php
+++ b/includes/search/searchwidgets/SearchFormWidget.php
@@ -2,8 +2,9 @@
namespace MediaWiki\Search\SearchWidgets;
-use Hooks;
use Html;
+use MediaWiki\HookContainer\HookContainer;
+use MediaWiki\HookContainer\HookRunner;
use MediaWiki\MediaWikiServices;
use MediaWiki\Widget\SearchInputWidget;
use SearchEngineConfig;
@@ -17,19 +18,27 @@ class SearchFormWidget {
protected $searchConfig;
/** @var array */
protected $profiles;
+ /** @var HookContainer $hookContainer */
+ private $hookContainer;
+ /** @var HookRunner */
+ private $hookRunner;
/**
* @param SpecialSearch $specialSearch
* @param SearchEngineConfig $searchConfig
+ * @param HookContainer $hookContainer
* @param array $profiles
*/
public function __construct(
SpecialSearch $specialSearch,
SearchEngineConfig $searchConfig,
+ HookContainer $hookContainer,
array $profiles
) {
$this->specialSearch = $specialSearch;
$this->searchConfig = $searchConfig;
+ $this->hookContainer = $hookContainer;
+ $this->hookRunner = new HookRunner( $hookContainer );
$this->profiles = $profiles;
}
@@ -233,9 +242,8 @@ class SearchFormWidget {
$html .= $this->powerSearchBox( $term, [] );
} else {
$form = '';
- Hooks::run( 'SpecialSearchProfileForm', [
- $this->specialSearch, &$form, $profile, $term, []
- ] );
+ $this->getHookRunner()->onSpecialSearchProfileForm(
+ $this->specialSearch, $form, $profile, $term, [] );
$html .= $form;
}
@@ -289,7 +297,7 @@ class SearchFormWidget {
$showSections = [
'namespaceTables' => "<table>" . implode( '</table><table>', $namespaceTables ) . '</table>',
];
- Hooks::run( 'SpecialSearchPowerBox', [ &$showSections, $term, &$opts ] );
+ $this->getHookRunner()->onSpecialSearchPowerBox( $showSections, $term, $opts );
$hidden = '';
foreach ( $opts as $key => $value ) {
@@ -336,4 +344,22 @@ class SearchFormWidget {
$remember .
"</fieldset>";
}
+
+ /**
+ * @since 1.35
+ * @return HookContainer
+ */
+ protected function getHookContainer() {
+ return $this->hookContainer;
+ }
+
+ /**
+ * @internal This is for use by core only. Hook interfaces may be removed
+ * without notice.
+ * @since 1.35
+ * @return HookRunner
+ */
+ protected function getHookRunner() {
+ return $this->hookRunner;
+ }
}