6 use InvalidArgumentException;
85 $this->completionCacheExpiry =
$config->get(
'SearchSuggestCacheExpiry' );
91 if ( !in_array( $this->mode, self::SUPPORTED_MODES ) ) {
92 throw new InvalidArgumentException(
93 "Unsupported search mode `{$this->mode}` configured. Supported modes: " .
94 implode(
', ', self::SUPPORTED_MODES )
105 $searchEngine = $this->searchEngineFactory->create();
106 $searchEngine->setNamespaces( $this->searchEngineConfig->defaultNamespaces() );
107 $searchEngine->setLimitOffset( $limit, self::OFFSET );
108 return $searchEngine;
123 if ( $results instanceof
Status ) {
125 if ( !$status->isOK() ) {
126 list( $error ) = $status->splitByErrorType();
127 if ( $error->getErrors() ) {
128 $errorMessages = $error->getMessage();
130 new MessageValue(
"rest-search-error", [ $errorMessages->getKey() ] )
134 $statusValue = $status->getValue();
136 return $statusValue->extractResults();
139 return $results->extractResults();
155 if ( $this->mode == self::COMPLETION_MODE ) {
156 $completionSearch = $searchEngine->completionSearchWithVariants( $query );
159 $titleSearch = $searchEngine->searchTitle( $query );
160 $textSearch = $searchEngine->searchText( $query );
165 $mergedResults = array_merge( $titleSearchResults, $textSearchResults );
182 foreach ( $suggestions as $sugg ) {
183 $title = $sugg->getSuggestedTitle();
185 $pageID =
$title->getArticleID();
186 if ( !isset( $pageInfos[$pageID] ) &&
189 $pageInfos[ $pageID ] = [
$title, $sugg, null ];
208 foreach ( $searchResults as $result ) {
209 if ( !$result->isBrokenTitle() && !$result->isMissingRevision() ) {
210 $title = $result->getTitle();
211 $pageID =
$title->getArticleID();
212 if ( !isset( $pageInfos[$pageID] ) &&
215 $pageInfos[$pageID] = [
$title,
null, $result ];
230 return array_map(
function ( $pageInfo ) {
231 list(
$title, $sugg, $result ) = $pageInfo;
233 'id' =>
$title->getArticleID(),
234 'key' =>
$title->getPrefixedDBkey(),
235 'title' =>
$title->getPrefixedText(),
236 'excerpt' => ( $sugg ? $sugg->getText() : $result->getTextSnippet() ) ?:
null,
250 if ( $thumbnail ==
null ) {
256 'size' => $thumbnail->
getSize(),
260 'url' => $thumbnail->
getUrl(),
275 $descriptions = array_fill_keys( array_keys( $pageIdentities ),
null );
277 $this->
getHookRunner()->onSearchResultProvideDescription( $pageIdentities, $descriptions );
279 return array_map(
function ( $description ) {
280 return [
'description' => $description ];
296 $thumbnails = array_fill_keys( array_keys( $pageIdentities ),
null );
298 $this->
getHookRunner()->onSearchResultProvideThumbnail( $pageIdentities, $thumbnails );
300 return array_map(
function ( $thumbnail ) {
311 $pageInfos = $this->
doSearch( $searchEngine );
312 $pageIdentities = array_map(
function ( $pageInfo ) {
313 list(
$title ) = $pageInfo;
321 $result = array_map(
"array_merge",
329 if ( $this->mode === self::COMPLETION_MODE && $this->completionCacheExpiry ) {
333 $response->setHeader(
'Cache-Control',
'public, max-age=' . $this->completionCacheExpiry );
342 self::PARAM_SOURCE =>
'query',
343 ParamValidator::PARAM_TYPE =>
'string',
344 ParamValidator::PARAM_REQUIRED =>
true,
347 self::PARAM_SOURCE =>
'query',
348 ParamValidator::PARAM_TYPE =>
'integer',
349 ParamValidator::PARAM_REQUIRED =>
false,
351 IntegerDef::PARAM_MIN => 1,