2019-11-07 01:20:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Rest\Handler;
|
|
|
|
|
|
2020-03-25 21:33:27 +00:00
|
|
|
use Config;
|
2020-03-25 17:21:12 +00:00
|
|
|
use InvalidArgumentException;
|
2020-01-10 00:00:51 +00:00
|
|
|
use ISearchResultSet;
|
2022-01-13 19:35:27 +00:00
|
|
|
use MediaWiki\Page\PageIdentity;
|
|
|
|
|
use MediaWiki\Page\PageStore;
|
|
|
|
|
use MediaWiki\Page\RedirectLookup;
|
2021-10-08 20:05:15 +00:00
|
|
|
use MediaWiki\Permissions\PermissionManager;
|
2020-01-10 00:00:51 +00:00
|
|
|
use MediaWiki\Rest\Handler;
|
2019-11-07 01:20:38 +00:00
|
|
|
use MediaWiki\Rest\LocalizedHttpException;
|
|
|
|
|
use MediaWiki\Rest\Response;
|
2020-04-15 12:33:15 +00:00
|
|
|
use MediaWiki\Search\Entity\SearchResultThumbnail;
|
2019-11-07 01:20:38 +00:00
|
|
|
use SearchEngine;
|
2020-01-10 00:00:51 +00:00
|
|
|
use SearchEngineConfig;
|
|
|
|
|
use SearchEngineFactory;
|
|
|
|
|
use SearchResult;
|
2020-03-25 17:21:12 +00:00
|
|
|
use SearchSuggestion;
|
2019-11-07 01:20:38 +00:00
|
|
|
use Status;
|
2022-01-13 19:35:27 +00:00
|
|
|
use TitleFormatter;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Wikimedia\Message\MessageValue;
|
|
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2020-02-28 11:59:25 +00:00
|
|
|
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
|
2019-11-07 01:20:38 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handler class for Core REST API endpoint that handles basic search
|
|
|
|
|
*/
|
|
|
|
|
class SearchHandler extends Handler {
|
|
|
|
|
|
|
|
|
|
/** @var SearchEngineFactory */
|
|
|
|
|
private $searchEngineFactory;
|
|
|
|
|
|
|
|
|
|
/** @var SearchEngineConfig */
|
|
|
|
|
private $searchEngineConfig;
|
|
|
|
|
|
2021-10-08 20:05:15 +00:00
|
|
|
/** @var PermissionManager */
|
|
|
|
|
private $permissionManager;
|
|
|
|
|
|
2022-01-13 19:35:27 +00:00
|
|
|
/** @var RedirectLookup */
|
|
|
|
|
private $redirectLookup;
|
|
|
|
|
|
|
|
|
|
/** @var PageStore */
|
|
|
|
|
private $pageStore;
|
|
|
|
|
|
|
|
|
|
/** @var TitleFormatter */
|
|
|
|
|
private $titleFormatter;
|
|
|
|
|
|
2020-03-25 17:21:12 +00:00
|
|
|
/**
|
|
|
|
|
* Search page body and titles.
|
|
|
|
|
*/
|
|
|
|
|
public const FULLTEXT_MODE = 'fulltext';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Search title completion matches.
|
|
|
|
|
*/
|
|
|
|
|
public const COMPLETION_MODE = 'completion';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Supported modes
|
|
|
|
|
*/
|
|
|
|
|
private const SUPPORTED_MODES = [ self::FULLTEXT_MODE, self::COMPLETION_MODE ];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $mode = null;
|
|
|
|
|
|
2020-02-28 11:59:25 +00:00
|
|
|
/** Limit results to 50 pages per default */
|
2019-11-07 01:20:38 +00:00
|
|
|
private const LIMIT = 50;
|
|
|
|
|
|
2020-02-28 11:59:25 +00:00
|
|
|
/** Hard limit results to 100 pages */
|
|
|
|
|
private const MAX_LIMIT = 100;
|
|
|
|
|
|
2019-11-07 01:20:38 +00:00
|
|
|
/** Default to first page */
|
|
|
|
|
private const OFFSET = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-03-25 21:33:27 +00:00
|
|
|
* Expiry time for use as max-age value in the cache-control header
|
|
|
|
|
* of completion search responses.
|
|
|
|
|
* @see $wgSearchSuggestCacheExpiry
|
|
|
|
|
* @var int|null
|
|
|
|
|
*/
|
|
|
|
|
private $completionCacheExpiry;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Config $config
|
2019-11-07 01:20:38 +00:00
|
|
|
* @param SearchEngineFactory $searchEngineFactory
|
|
|
|
|
* @param SearchEngineConfig $searchEngineConfig
|
2021-10-08 20:05:15 +00:00
|
|
|
* @param PermissionManager $permissionManager
|
2022-01-13 19:35:27 +00:00
|
|
|
* @param RedirectLookup $redirectLookup
|
|
|
|
|
* @param PageStore $pageStore
|
|
|
|
|
* @param TitleFormatter $titleFormatter
|
2019-11-07 01:20:38 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct(
|
2020-03-25 21:33:27 +00:00
|
|
|
Config $config,
|
2019-11-07 01:20:38 +00:00
|
|
|
SearchEngineFactory $searchEngineFactory,
|
2021-10-08 20:05:15 +00:00
|
|
|
SearchEngineConfig $searchEngineConfig,
|
2022-01-13 19:35:27 +00:00
|
|
|
PermissionManager $permissionManager,
|
|
|
|
|
RedirectLookup $redirectLookup,
|
|
|
|
|
PageStore $pageStore,
|
|
|
|
|
TitleFormatter $titleFormatter
|
2019-11-07 01:20:38 +00:00
|
|
|
) {
|
|
|
|
|
$this->searchEngineFactory = $searchEngineFactory;
|
|
|
|
|
$this->searchEngineConfig = $searchEngineConfig;
|
2021-10-08 20:05:15 +00:00
|
|
|
$this->permissionManager = $permissionManager;
|
2022-01-13 19:35:27 +00:00
|
|
|
$this->redirectLookup = $redirectLookup;
|
|
|
|
|
$this->pageStore = $pageStore;
|
|
|
|
|
$this->titleFormatter = $titleFormatter;
|
2019-11-07 01:20:38 +00:00
|
|
|
|
2020-03-25 21:33:27 +00:00
|
|
|
// @todo Avoid injecting the entire config, see T246377
|
|
|
|
|
$this->completionCacheExpiry = $config->get( 'SearchSuggestCacheExpiry' );
|
2019-11-07 01:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-12 01:41:54 +00:00
|
|
|
protected function postInitSetup() {
|
|
|
|
|
$this->mode = $this->getConfig()['mode'] ?? self::FULLTEXT_MODE;
|
2020-03-25 17:21:12 +00:00
|
|
|
|
|
|
|
|
if ( !in_array( $this->mode, self::SUPPORTED_MODES ) ) {
|
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
|
"Unsupported search mode `{$this->mode}` configured. Supported modes: " .
|
|
|
|
|
implode( ', ', self::SUPPORTED_MODES )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 01:20:38 +00:00
|
|
|
/**
|
|
|
|
|
* @return SearchEngine
|
|
|
|
|
*/
|
|
|
|
|
private function createSearchEngine() {
|
2020-02-28 11:59:25 +00:00
|
|
|
$limit = $this->getValidatedParams()['limit'];
|
|
|
|
|
|
2019-11-07 01:20:38 +00:00
|
|
|
$searchEngine = $this->searchEngineFactory->create();
|
|
|
|
|
$searchEngine->setNamespaces( $this->searchEngineConfig->defaultNamespaces() );
|
2020-02-28 11:59:25 +00:00
|
|
|
$searchEngine->setLimitOffset( $limit, self::OFFSET );
|
2019-11-07 01:20:38 +00:00
|
|
|
return $searchEngine;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-22 17:20:09 +00:00
|
|
|
public function needsWriteAccess() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 01:20:38 +00:00
|
|
|
/**
|
|
|
|
|
* Get SearchResults when results are either SearchResultSet or Status objects
|
|
|
|
|
* @param ISearchResultSet|Status|null $results
|
|
|
|
|
* @return SearchResult[]
|
|
|
|
|
* @throws LocalizedHttpException
|
|
|
|
|
*/
|
2020-03-25 17:21:12 +00:00
|
|
|
private function getSearchResultsOrThrow( $results ) {
|
2019-11-07 01:20:38 +00:00
|
|
|
if ( $results ) {
|
|
|
|
|
if ( $results instanceof Status ) {
|
|
|
|
|
$status = $results;
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
list( $error ) = $status->splitByErrorType();
|
|
|
|
|
if ( $error->getErrors() ) { // Only throw for errors, suppress warnings (for now)
|
|
|
|
|
$errorMessages = $error->getMessage();
|
|
|
|
|
throw new LocalizedHttpException(
|
|
|
|
|
new MessageValue( "rest-search-error", [ $errorMessages->getKey() ] )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$statusValue = $status->getValue();
|
|
|
|
|
if ( $statusValue instanceof ISearchResultSet ) {
|
|
|
|
|
return $statusValue->extractResults();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return $results->extractResults();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-04-15 12:33:15 +00:00
|
|
|
* Execute search and return info about pages for further processing.
|
2020-03-25 17:21:12 +00:00
|
|
|
*
|
2019-11-07 01:20:38 +00:00
|
|
|
* @param SearchEngine $searchEngine
|
2020-04-15 12:33:15 +00:00
|
|
|
* @return array[]
|
2019-11-07 01:20:38 +00:00
|
|
|
* @throws LocalizedHttpException
|
|
|
|
|
*/
|
|
|
|
|
private function doSearch( $searchEngine ) {
|
|
|
|
|
$query = $this->getValidatedParams()['q'];
|
|
|
|
|
|
2020-03-25 17:21:12 +00:00
|
|
|
if ( $this->mode == self::COMPLETION_MODE ) {
|
|
|
|
|
$completionSearch = $searchEngine->completionSearchWithVariants( $query );
|
2022-01-13 19:35:27 +00:00
|
|
|
return $this->buildPageObjects( $completionSearch->getSuggestions() );
|
2020-03-25 17:21:12 +00:00
|
|
|
} else {
|
|
|
|
|
$titleSearch = $searchEngine->searchTitle( $query );
|
|
|
|
|
$textSearch = $searchEngine->searchText( $query );
|
2019-11-07 01:20:38 +00:00
|
|
|
|
2020-03-25 17:21:12 +00:00
|
|
|
$titleSearchResults = $this->getSearchResultsOrThrow( $titleSearch );
|
|
|
|
|
$textSearchResults = $this->getSearchResultsOrThrow( $textSearch );
|
|
|
|
|
|
|
|
|
|
$mergedResults = array_merge( $titleSearchResults, $textSearchResults );
|
2022-01-13 19:35:27 +00:00
|
|
|
return $this->buildPageObjects( $mergedResults );
|
2020-03-25 17:21:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-07 01:20:38 +00:00
|
|
|
|
2020-03-25 17:21:12 +00:00
|
|
|
/**
|
2022-01-13 19:35:27 +00:00
|
|
|
* Build an array of pageInfo objects.
|
|
|
|
|
* @param SearchSuggestion[]|SearchResult[] $searchResponse
|
2020-03-25 17:21:12 +00:00
|
|
|
*
|
2022-01-13 19:35:27 +00:00
|
|
|
* @phpcs:ignore Generic.Files.LineLength
|
|
|
|
|
* @phan-return array{int:array{pageIdentity:PageIdentity,suggestion:?SearchSuggestion,result:?SearchResult,redirect:?PageIdentity}} $pageInfos
|
|
|
|
|
* @return array Associative array mapping pageID to pageInfo objects:
|
|
|
|
|
* - pageIdentity: PageIdentity of page to return as the match
|
|
|
|
|
* - suggestion: SearchSuggestion or null if $searchResponse is SearchResults[]
|
|
|
|
|
* - result: SearchResult or null if $searchResponse is SearchSuggestions[]
|
|
|
|
|
* - redirect: PageIdentity or null if the SearchResult|SearchSuggestion was not a redirect
|
2020-03-25 17:21:12 +00:00
|
|
|
*/
|
2022-01-13 19:35:27 +00:00
|
|
|
private function buildPageObjects( array $searchResponse ): array {
|
2020-04-15 12:33:15 +00:00
|
|
|
$pageInfos = [];
|
2022-01-13 19:35:27 +00:00
|
|
|
foreach ( $searchResponse as $response ) {
|
|
|
|
|
$isSearchResult = $response instanceof SearchResult;
|
|
|
|
|
if ( $isSearchResult ) {
|
|
|
|
|
if ( $response->isBrokenTitle() || $response->isMissingRevision() ) {
|
|
|
|
|
continue;
|
2020-03-25 17:21:12 +00:00
|
|
|
}
|
2022-01-13 19:35:27 +00:00
|
|
|
$title = $response->getTitle();
|
|
|
|
|
} else {
|
|
|
|
|
$title = $response->getSuggestedTitle();
|
|
|
|
|
}
|
2022-02-09 14:29:34 +00:00
|
|
|
if ( !$title->canExist() ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-01-13 19:35:27 +00:00
|
|
|
$pageObj = $this->buildSinglePage( $title, $response );
|
|
|
|
|
if ( $pageObj ) {
|
|
|
|
|
// A redirect page's suggestion and redirect field should always come from the redirect target
|
|
|
|
|
$title = $pageObj['pageIdentity'];
|
|
|
|
|
if ( isset( $pageInfos[$title->getId()] ) ) { // if we already have the redirect set,
|
|
|
|
|
if ( $pageInfos[$title->getId()]['redirect'] !== null ) {
|
|
|
|
|
$pageInfos[$title->getId()]['result'] = $isSearchResult ? $response : null;
|
|
|
|
|
$pageInfos[$title->getId()]['suggestion'] = $isSearchResult ? null : $response;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$pageInfos[$title->getId()] = $pageObj;
|
2020-03-25 17:21:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-04-15 12:33:15 +00:00
|
|
|
return $pageInfos;
|
2019-11-07 01:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-13 19:35:27 +00:00
|
|
|
* Build one pageInfo object from either a SearchResult or SearchSuggestion.
|
|
|
|
|
* @param PageIdentity $title
|
|
|
|
|
* @param SearchResult|SearchSuggestion $result
|
2020-03-25 17:21:12 +00:00
|
|
|
*
|
2022-01-13 19:35:27 +00:00
|
|
|
* @phpcs:ignore Generic.Files.LineLength
|
|
|
|
|
* @phan-return (false|array{pageIdentity:PageIdentity,suggestion:?SearchSuggestion,result:?SearchResult,redirect:?PageIdentity}) $pageInfos
|
|
|
|
|
* @return bool|array Objects representing a given page:
|
|
|
|
|
* - pageIdentity: PageIdentity of page to return as the match
|
|
|
|
|
* - suggestion: SearchSuggestion or null if $searchResponse is SearchResults
|
|
|
|
|
* - result: SearchResult or null if $searchResponse is SearchSuggestions
|
|
|
|
|
* - redirect: PageIdentity|null depending on if the SearchResult|SearchSuggestion was a redirect
|
2019-11-07 01:20:38 +00:00
|
|
|
*/
|
2022-01-13 19:35:27 +00:00
|
|
|
private function buildSinglePage( $title, $result ) {
|
|
|
|
|
$redirectTarget = $this->redirectLookup->getRedirectTarget( $title );
|
|
|
|
|
if ( $redirectTarget ) { // Our page is a redirect
|
|
|
|
|
$redirectSource = $title;
|
|
|
|
|
$title = $this->pageStore->getPageForLink( $redirectTarget );
|
|
|
|
|
} else {
|
|
|
|
|
$redirectSource = null;
|
2019-11-07 01:20:38 +00:00
|
|
|
}
|
2022-01-13 19:35:27 +00:00
|
|
|
if ( !$title || !$title->exists() || !$this->getAuthority()->probablyCan( 'read', $title ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'pageIdentity' => $title,
|
|
|
|
|
'suggestion' => $result instanceof SearchSuggestion ? $result : null,
|
|
|
|
|
'result' => $result instanceof SearchResult ? $result : null,
|
|
|
|
|
'redirect' => $redirectSource
|
|
|
|
|
];
|
2020-04-15 12:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Turn array of page info into serializable array with common information about the page
|
2022-01-13 19:35:27 +00:00
|
|
|
* @param array $pageInfos Page Info objects
|
|
|
|
|
* @phpcs:ignore Generic.Files.LineLength
|
|
|
|
|
* @phan-param array{int:array{pageIdentity:PageIdentity,suggestion:SearchSuggestion,result:SearchResult,redirect:?PageIdentity}} $pageInfos
|
2020-04-15 12:33:15 +00:00
|
|
|
*
|
2022-01-13 19:35:27 +00:00
|
|
|
* @phan-return array{int:array{id:int,key:string,title:string,excerpt:?string,matched_title:?string}} $pageInfos
|
|
|
|
|
* @return array[] of [ id, key, title, excerpt, matched_title ]
|
2020-04-15 12:33:15 +00:00
|
|
|
*/
|
|
|
|
|
private function buildResultFromPageInfos( array $pageInfos ): array {
|
2022-01-13 19:35:27 +00:00
|
|
|
return array_map( function ( $pageInfo ) {
|
|
|
|
|
[
|
|
|
|
|
'pageIdentity' => $page,
|
|
|
|
|
'suggestion' => $sugg,
|
|
|
|
|
'result' => $result,
|
|
|
|
|
'redirect' => $redirect
|
|
|
|
|
] = $pageInfo;
|
|
|
|
|
$excerpt = $sugg ? $sugg->getText() : $result->getTextSnippet();
|
2020-04-15 12:33:15 +00:00
|
|
|
return [
|
2022-01-13 19:35:27 +00:00
|
|
|
'id' => $page->getId(),
|
|
|
|
|
'key' => $this->titleFormatter->getPrefixedDBkey( $page ),
|
|
|
|
|
'title' => $this->titleFormatter->getPrefixedText( $page ),
|
|
|
|
|
'excerpt' => $excerpt ?: null,
|
|
|
|
|
'matched_title' => $redirect ? $this->titleFormatter->getPrefixedText( $redirect ) : null
|
2020-04-15 12:33:15 +00:00
|
|
|
];
|
2022-01-13 19:35:27 +00:00
|
|
|
}, $pageInfos );
|
2020-04-15 12:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts SearchResultThumbnail object into serializable array
|
|
|
|
|
*
|
|
|
|
|
* @param SearchResultThumbnail|null $thumbnail
|
|
|
|
|
*
|
|
|
|
|
* @return array|null
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
private function serializeThumbnail( ?SearchResultThumbnail $thumbnail ): ?array {
|
2020-04-15 12:33:15 +00:00
|
|
|
if ( $thumbnail == null ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'mimetype' => $thumbnail->getMimeType(),
|
|
|
|
|
'size' => $thumbnail->getSize(),
|
|
|
|
|
'width' => $thumbnail->getWidth(),
|
|
|
|
|
'height' => $thumbnail->getHeight(),
|
|
|
|
|
'duration' => $thumbnail->getDuration(),
|
|
|
|
|
'url' => $thumbnail->getUrl(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-05-21 18:40:39 +00:00
|
|
|
* Turn page info into serializable array with description field for the page.
|
2020-04-15 12:33:15 +00:00
|
|
|
*
|
2020-05-21 18:40:39 +00:00
|
|
|
* The information about description should be provided by extension by implementing
|
|
|
|
|
* 'SearchResultProvideDescription' hook. Description is set to null if no extensions
|
|
|
|
|
* implement the hook.
|
2022-01-13 19:35:27 +00:00
|
|
|
* @param PageIdentity[] $pageIdentities
|
2020-04-15 12:33:15 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function buildDescriptionsFromPageIdentities( array $pageIdentities ) {
|
|
|
|
|
$descriptions = array_fill_keys( array_keys( $pageIdentities ), 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->getHookRunner()->onSearchResultProvideDescription( $pageIdentities, $descriptions );
|
2020-04-15 12:33:15 +00:00
|
|
|
|
2021-02-10 22:31:02 +00:00
|
|
|
return array_map( static function ( $description ) {
|
2020-04-15 12:33:15 +00:00
|
|
|
return [ 'description' => $description ];
|
|
|
|
|
}, $descriptions );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Turn page info into serializable array with thumbnail information for the page.
|
|
|
|
|
*
|
|
|
|
|
* The information about thumbnail should be provided by extension by implementing
|
|
|
|
|
* 'SearchResultProvideThumbnail' hook. Thumbnail is set to null if no extensions implement
|
|
|
|
|
* the hook.
|
|
|
|
|
*
|
2022-01-13 19:35:27 +00:00
|
|
|
* @param PageIdentity[] $pageIdentities
|
2020-04-15 12:33:15 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function buildThumbnailsFromPageIdentities( array $pageIdentities ) {
|
|
|
|
|
$thumbnails = array_fill_keys( array_keys( $pageIdentities ), 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->getHookRunner()->onSearchResultProvideThumbnail( $pageIdentities, $thumbnails );
|
2020-04-15 12:33:15 +00:00
|
|
|
|
|
|
|
|
return array_map( function ( $thumbnail ) {
|
|
|
|
|
return [ 'thumbnail' => $this->serializeThumbnail( $thumbnail ) ];
|
|
|
|
|
}, $thumbnails );
|
2019-11-07 01:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Response
|
|
|
|
|
* @throws LocalizedHttpException
|
|
|
|
|
*/
|
|
|
|
|
public function execute() {
|
|
|
|
|
$searchEngine = $this->createSearchEngine();
|
2020-04-15 12:33:15 +00:00
|
|
|
$pageInfos = $this->doSearch( $searchEngine );
|
2022-01-13 19:35:27 +00:00
|
|
|
$pageIdentities = array_combine(
|
|
|
|
|
array_keys( $pageInfos ),
|
|
|
|
|
array_column( $pageInfos, 'pageIdentity' )
|
|
|
|
|
);
|
2020-04-15 12:33:15 +00:00
|
|
|
|
2021-05-06 08:33:45 +00:00
|
|
|
// Remove empty entries resulting from non-proper pages like e.g. special pages
|
|
|
|
|
// in the search result.
|
|
|
|
|
$pageIdentities = array_filter( $pageIdentities );
|
2022-01-13 19:35:27 +00:00
|
|
|
$res = $this->buildResultFromPageInfos( $pageInfos );
|
2020-04-15 12:33:15 +00:00
|
|
|
$result = array_map( "array_merge",
|
|
|
|
|
$this->buildResultFromPageInfos( $pageInfos ),
|
|
|
|
|
$this->buildDescriptionsFromPageIdentities( $pageIdentities ),
|
|
|
|
|
$this->buildThumbnailsFromPageIdentities( $pageIdentities )
|
|
|
|
|
);
|
|
|
|
|
$response = $this->getResponseFactory()->createJson( [ 'pages' => $result ] );
|
2020-03-25 21:33:27 +00:00
|
|
|
|
|
|
|
|
if ( $this->mode === self::COMPLETION_MODE && $this->completionCacheExpiry ) {
|
|
|
|
|
// Type-ahead completion matches should be cached by the client and
|
|
|
|
|
// in the CDN, especially for short prefixes.
|
|
|
|
|
// See also $wgSearchSuggestCacheExpiry and ApiOpenSearch
|
2021-10-08 20:05:15 +00:00
|
|
|
if ( $this->permissionManager->isEveryoneAllowed( 'read' ) ) {
|
|
|
|
|
$response->setHeader( 'Cache-Control', 'public, max-age=' . $this->completionCacheExpiry );
|
|
|
|
|
} else {
|
|
|
|
|
$response->setHeader( 'Cache-Control', 'no-store, max-age=0' );
|
|
|
|
|
}
|
2020-03-25 21:33:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $response;
|
2019-11-07 01:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getParamSettings() {
|
|
|
|
|
return [
|
|
|
|
|
'q' => [
|
|
|
|
|
self::PARAM_SOURCE => 'query',
|
|
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
|
|
|
|
],
|
2020-02-28 11:59:25 +00:00
|
|
|
'limit' => [
|
|
|
|
|
self::PARAM_SOURCE => 'query',
|
|
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
|
|
|
|
ParamValidator::PARAM_REQUIRED => false,
|
|
|
|
|
ParamValidator::PARAM_DEFAULT => self::LIMIT,
|
|
|
|
|
IntegerDef::PARAM_MIN => 1,
|
|
|
|
|
IntegerDef::PARAM_MAX => self::MAX_LIMIT,
|
|
|
|
|
],
|
2019-11-07 01:20:38 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|