Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.21% covered (warning)
66.21%
145 / 219
29.27% covered (danger)
29.27%
12 / 41
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchEngine
66.51% covered (warning)
66.51%
145 / 218
29.27% covered (danger)
29.27%
12 / 41
386.43
0.00% covered (danger)
0.00%
0 / 1
 searchText
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 doSearchText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 searchArchiveTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doSearchArchiveTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 searchTitle
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 doSearchTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 maybePaginate
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
 supports
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
6.00
 setFeatureData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFeatureData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 normalizeText
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNearMatcher
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 defaultNearMatcher
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 legalSearchChars
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLimitOffset
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setNamespaces
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 setShowSuggestion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getValidSorts
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setSort
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getSort
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 parseNamespacePrefixes
97.06% covered (success)
97.06%
33 / 34
0.00% covered (danger)
0.00%
0 / 1
11
 userHighlightPrefs
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 update
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 updateTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 delete
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 normalizeNamespaces
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 completionSearchBackendOverfetch
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 completionSearchBackend
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 completionSearch
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 completionSearchWithVariants
90.91% covered (success)
90.91%
20 / 22
0.00% covered (danger)
0.00%
0 / 1
5.02
 extractTitles
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 processCompletionResults
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
7
 defaultPrefixSearch
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 simplePrefixSearch
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getProfiles
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 makeSearchFieldMapping
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSearchIndexFields
81.82% covered (warning)
81.82%
18 / 22
0.00% covered (danger)
0.00%
0 / 1
7.29
 augmentSearchResults
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
56
 setHookContainer
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getHookContainer
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getHookRunner
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
1<?php
2/**
3 * Basic search engine
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Search
8 */
9
10namespace MediaWiki\Search;
11
12use Closure;
13use InvalidArgumentException;
14use MediaWiki\Config\Config;
15use MediaWiki\Content\UnknownContentModelException;
16use MediaWiki\HookContainer\HookContainer;
17use MediaWiki\HookContainer\HookRunner;
18use MediaWiki\MediaWikiServices;
19use MediaWiki\Status\Status;
20use MediaWiki\Title\Title;
21use MediaWiki\User\User;
22use SplObjectStorage;
23
24/**
25 * @defgroup Search Search
26 */
27
28/**
29 * Contain a class for special pages
30 * @stable to extend
31 * @ingroup Search
32 */
33abstract class SearchEngine {
34    public const DEFAULT_SORT = 'relevance';
35
36    /** @var string */
37    public $prefix = '';
38
39    /** @var int[]|null */
40    public $namespaces = [ NS_MAIN ];
41
42    /** @var int */
43    protected $limit = 10;
44
45    /** @var int */
46    protected $offset = 0;
47
48    /**
49     * @var string[]
50     * @deprecated since 1.34
51     */
52    protected $searchTerms = [];
53
54    /** @var bool */
55    protected $showSuggestion = true;
56    /** @var string */
57    private $sort = self::DEFAULT_SORT;
58
59    /** @var array Feature values */
60    protected $features = [];
61
62    /** @var HookContainer */
63    private $hookContainer;
64
65    /** @var HookRunner */
66    private $hookRunner;
67
68    /** Profile type for completionSearch */
69    public const COMPLETION_PROFILE_TYPE = 'completionSearchProfile';
70
71    /** Profile type for query independent ranking features (ex: article popularity) */
72    public const FT_QUERY_INDEP_PROFILE_TYPE = 'fulltextQueryIndepProfile';
73
74    /** Profile type for query dependent ranking features (ex: field weights) */
75    public const FT_QUERY_DEP_PROFILE_TYPE = 'fulltextQueryDepProfile';
76
77    /** Integer flag for legalSearchChars: includes all chars allowed in a search query */
78    protected const CHARS_ALL = 1;
79
80    /** Integer flag for legalSearchChars: includes all chars allowed in a search term */
81    protected const CHARS_NO_SYNTAX = 2;
82
83    /**
84     * Name of the feature data entry holding an identifier generated by some search backends.
85     * This may be used by some endpoint to propagate it via the X-Search-ID header for analytics/debugging
86     * purposes.
87     */
88    public const SEARCH_ID = 'searchId';
89
90    /**
91     * Perform a full text search query and return a result set.
92     * If full text searches are not supported or disabled, return null.
93     *
94     * @note As of 1.32 overriding this function is deprecated. It will
95     * be converted to final in 1.34. Override self::doSearchText().
96     *
97     * @param string $term Raw search term
98     * @return ISearchResultSet|Status<ISearchResultSet>|null
99     */
100    public function searchText( $term ) {
101        return $this->maybePaginate( function () use ( $term ) {
102            return $this->doSearchText( $term );
103        } );
104    }
105
106    /**
107     * Perform a full text search query and return a result set.
108     *
109     * @stable to override
110     *
111     * @param string $term Raw search term
112     * @return ISearchResultSet|Status<ISearchResultSet>|null
113     * @since 1.32
114     */
115    protected function doSearchText( $term ) {
116        return null;
117    }
118
119    /**
120     * Perform a title search in the article archive.
121     * NOTE: these results still should be filtered by
122     * matching against PageArchive, permissions checks etc
123     * The results returned by this methods are only suggestions and
124     * may not end up being shown to the user.
125     *
126     * @note As of 1.32 overriding this function is deprecated. It will
127     * be converted to final in 1.34. Override self::doSearchArchiveTitle().
128     *
129     * @param string $term Raw search term
130     * @return Status
131     * @since 1.29
132     */
133    public function searchArchiveTitle( $term ) {
134        return $this->doSearchArchiveTitle( $term );
135    }
136
137    /**
138     * Perform a title search in the article archive.
139     *
140     * @stable to override
141     *
142     * @param string $term Raw search term
143     * @return Status
144     * @since 1.32
145     */
146    protected function doSearchArchiveTitle( $term ) {
147        return Status::newGood( [] );
148    }
149
150    /**
151     * Perform a title-only search query and return a result set.
152     * If title searches are not supported or disabled, return null.
153     * STUB
154     *
155     * @note As of 1.32 overriding this function is deprecated. It will
156     * be converted to final in 1.34. Override self::doSearchTitle().
157     *
158     * @param string $term Raw search term
159     * @return ISearchResultSet|null
160     */
161    public function searchTitle( $term ) {
162        return $this->maybePaginate( function () use ( $term ) {
163            return $this->doSearchTitle( $term );
164        } );
165    }
166
167    /**
168     * Perform a title-only search query and return a result set.
169     *
170     * @stable to override
171     *
172     * @param string $term Raw search term
173     * @return ISearchResultSet|null
174     * @since 1.32
175     */
176    protected function doSearchTitle( $term ) {
177        return null;
178    }
179
180    /**
181     * Performs an overfetch and shrink operation to determine if
182     * the next page is available for search engines that do not
183     * explicitly implement their own pagination.
184     *
185     * @param Closure $fn Takes no arguments
186     * @return ISearchResultSet|Status<ISearchResultSet>|null Result of calling $fn
187     */
188    private function maybePaginate( Closure $fn ) {
189        if ( $this instanceof PaginatingSearchEngine ) {
190            return $fn();
191        }
192        $this->limit++;
193        try {
194            $resultSetOrStatus = $fn();
195        } finally {
196            $this->limit--;
197        }
198
199        $resultSet = null;
200        if ( $resultSetOrStatus instanceof ISearchResultSet ) {
201            $resultSet = $resultSetOrStatus;
202        } elseif ( $resultSetOrStatus instanceof Status &&
203            $resultSetOrStatus->getValue() instanceof ISearchResultSet
204        ) {
205            $resultSet = $resultSetOrStatus->getValue();
206        }
207        if ( $resultSet ) {
208            $resultSet->shrink( $this->limit );
209        }
210
211        return $resultSetOrStatus;
212    }
213
214    /**
215     * @since 1.18
216     * @stable to override
217     *
218     * @param string $feature
219     * @return bool
220     */
221    public function supports( $feature ) {
222        switch ( $feature ) {
223            case 'search-update':
224                return true;
225            case 'title-suffix-filter':
226            default:
227                return false;
228        }
229    }
230
231    /**
232     * Way to pass custom data for engines
233     * @since 1.18
234     * @param string $feature
235     * @param mixed $data
236     */
237    public function setFeatureData( $feature, $data ) {
238        $this->features[$feature] = $data;
239    }
240
241    /**
242     * Way to retrieve custom data set by setFeatureData
243     * or by the engine itself.
244     * @since 1.29
245     * @param string $feature feature name
246     * @return mixed the feature value or null if unset
247     */
248    public function getFeatureData( $feature ) {
249        return $this->features[$feature] ?? null;
250    }
251
252    /**
253     * When overridden in derived class, performs database-specific conversions
254     * on text to be used for searching or updating search index.
255     * Default implementation does nothing (simply returns $string).
256     *
257     * @param string $string String to process
258     * @return string
259     */
260    public function normalizeText( $string ) {
261        // Some languages such as Chinese require word segmentation
262        return MediaWikiServices::getInstance()->getContentLanguage()->segmentByWord( $string );
263    }
264
265    /**
266     * Get service class to finding near matches.
267     *
268     * @return TitleMatcher
269     * @deprecated since 1.40, use MediaWikiServices::getInstance()->getTitleMatcher()
270     */
271    public function getNearMatcher( Config $config ) {
272        return MediaWikiServices::getInstance()->getTitleMatcher();
273    }
274
275    /**
276     * Get near matcher for default SearchEngine.
277     *
278     * @return TitleMatcher
279     * @deprecated since 1.40, MediaWikiServices::getInstance()->getTitleMatcher()
280     */
281    protected static function defaultNearMatcher() {
282        wfDeprecated( __METHOD__, '1.40' );
283        return MediaWikiServices::getInstance()->getTitleMatcher();
284    }
285
286    /**
287     * Get chars legal for search
288     * @param int $type type of search chars (see self::CHARS_ALL
289     * and self::CHARS_NO_SYNTAX). Defaults to CHARS_ALL
290     * @return string
291     */
292    public function legalSearchChars( $type = self::CHARS_ALL ) {
293        return "A-Za-z_'.0-9\\x80-\\xFF\\-";
294    }
295
296    /**
297     * Set the maximum number of results to return
298     * and how many to skip before returning the first.
299     *
300     * @param int $limit
301     * @param int $offset
302     */
303    public function setLimitOffset( $limit, $offset = 0 ) {
304        $this->limit = intval( $limit );
305        $this->offset = intval( $offset );
306    }
307
308    /**
309     * Set which namespaces the search should include.
310     * Give an array of namespace index numbers.
311     *
312     * @param int[]|null $namespaces
313     */
314    public function setNamespaces( $namespaces ) {
315        if ( $namespaces ) {
316            // Filter namespaces to only keep valid ones
317            $validNs = MediaWikiServices::getInstance()->getSearchEngineConfig()->searchableNamespaces();
318            $namespaces = array_filter( $namespaces,
319                static fn ( $id ) => $id < 0 || isset( $validNs[$id] )
320            );
321        } else {
322            $namespaces = [];
323        }
324        $this->namespaces = $namespaces;
325    }
326
327    /**
328     * Set whether the searcher should try to build a suggestion.  Note: some searchers
329     * don't support building a suggestion in the first place and others don't respect
330     * this flag.
331     *
332     * @param bool $showSuggestion Should the searcher try to build suggestions
333     */
334    public function setShowSuggestion( $showSuggestion ) {
335        $this->showSuggestion = $showSuggestion;
336    }
337
338    /**
339     * Get the valid sort directions.  All search engines support 'relevance' but others
340     * might support more. The default in all implementations must be 'relevance.'
341     *
342     * @since 1.25
343     * @stable to override
344     *
345     * @return string[] the valid sort directions for setSort
346     */
347    public function getValidSorts() {
348        return [ self::DEFAULT_SORT ];
349    }
350
351    /**
352     * Set the sort direction of the search results. Must be one returned by
353     * SearchEngine::getValidSorts()
354     *
355     * @since 1.25
356     * @param string $sort sort direction for query result
357     */
358    public function setSort( $sort ) {
359        if ( !in_array( $sort, $this->getValidSorts() ) ) {
360            throw new InvalidArgumentException( "Invalid sort: $sort" .
361                "Must be one of: " . implode( ', ', $this->getValidSorts() ) );
362        }
363        $this->sort = $sort;
364    }
365
366    /**
367     * Get the sort direction of the search results
368     *
369     * @since 1.25
370     * @return string
371     */
372    public function getSort() {
373        return $this->sort;
374    }
375
376    /**
377     * Parse some common prefixes: all (search everything)
378     * or namespace names
379     *
380     * @param string $query
381     * @param bool $withAllKeyword activate support of the "all:" keyword and its
382     * translations to activate searching on all namespaces.
383     * @param bool $withPrefixSearchExtractNamespaceHook call the PrefixSearchExtractNamespace hook
384     *  if classic namespace identification did not match.
385     * @return false|array false if no namespace was extracted, an array
386     * with the parsed query at index 0 and an array of namespaces at index
387     * 1 (or null for all namespaces).
388     */
389    public static function parseNamespacePrefixes(
390        $query,
391        $withAllKeyword = true,
392        $withPrefixSearchExtractNamespaceHook = false
393    ) {
394        $parsed = $query;
395        if ( !str_contains( $query, ':' ) ) { // nothing to do
396            return false;
397        }
398        $extractedNamespace = null;
399
400        $allQuery = false;
401        if ( $withAllKeyword ) {
402            $allkeywords = [];
403
404            $allkeywords[] = wfMessage( 'searchall' )->inContentLanguage()->text() . ":";
405            // force all: so that we have a common syntax for all the wikis
406            if ( !in_array( 'all:', $allkeywords ) ) {
407                $allkeywords[] = 'all:';
408            }
409
410            foreach ( $allkeywords as $kw ) {
411                if ( str_starts_with( $query, $kw ) ) {
412                    $parsed = substr( $query, strlen( $kw ) );
413                    $allQuery = true;
414                    break;
415                }
416            }
417        }
418
419        if ( !$allQuery && str_contains( $query, ':' ) ) {
420            $colonPos = strpos( $query, ':' );
421            // Trim whitespace around the prefix so that "User : Foo" is treated the same as
422            // "User:Foo"; Title parsing canonicalizes the colon the same way.
423            $prefix = str_replace( ' ', '_', trim( substr( $query, 0, $colonPos ) ) );
424            $services = MediaWikiServices::getInstance();
425            $index = $services->getContentLanguage()->getNsIndex( $prefix );
426            if ( $index !== false ) {
427                $extractedNamespace = [ $index ];
428                $parsed = ltrim( substr( $query, $colonPos + 1 ) );
429            } elseif ( $withPrefixSearchExtractNamespaceHook ) {
430                $hookNamespaces = [ NS_MAIN ];
431                $hookQuery = $query;
432                ( new HookRunner( $services->getHookContainer() ) )
433                    ->onPrefixSearchExtractNamespace( $hookNamespaces, $hookQuery );
434                if ( $hookQuery !== $query ) {
435                    $parsed = $hookQuery;
436                    $extractedNamespace = $hookNamespaces;
437                } else {
438                    return false;
439                }
440            } else {
441                return false;
442            }
443        }
444
445        return [ $parsed, $extractedNamespace ];
446    }
447
448    /**
449     * Find snippet highlight settings for all users
450     * @return array Contextlines, contextchars
451     * @deprecated since 1.34; use the SearchHighlighter constants directly
452     * @see SearchHighlighter::DEFAULT_CONTEXT_CHARS
453     * @see SearchHighlighter::DEFAULT_CONTEXT_LINES
454     */
455    public static function userHighlightPrefs() {
456        $contextlines = SearchHighlighter::DEFAULT_CONTEXT_LINES;
457        $contextchars = SearchHighlighter::DEFAULT_CONTEXT_CHARS;
458        return [ $contextlines, $contextchars ];
459    }
460
461    /**
462     * Create or update the search index record for the given page.
463     * Title and text should be pre-processed.
464     * STUB
465     *
466     * @param int $id
467     * @param string $title
468     * @param string $text
469     */
470    public function update( $id, $title, $text ) {
471        // no-op
472    }
473
474    /**
475     * Update a search index record's title only.
476     * Title should be pre-processed.
477     * STUB
478     *
479     * @param int $id
480     * @param string $title
481     */
482    public function updateTitle( $id, $title ) {
483        // no-op
484    }
485
486    /**
487     * Delete an indexed page
488     * Title should be pre-processed.
489     * STUB
490     *
491     * @param int $id Page id that was deleted
492     * @param string $title Title of page that was deleted
493     */
494    public function delete( $id, $title ) {
495        // no-op
496    }
497
498    /**
499     * Makes search simple string if it was namespaced.
500     * Sets namespaces of the search to namespaces extracted from string.
501     * @param string $search
502     * @return string Simplified search string
503     */
504    protected function normalizeNamespaces( $search ) {
505        $queryAndNs = self::parseNamespacePrefixes( $search, false, true );
506        if ( $queryAndNs !== false ) {
507            $this->setNamespaces( $queryAndNs[1] );
508            return $queryAndNs[0];
509        }
510        return $search;
511    }
512
513    /**
514     * Perform an overfetch of completion search results. This allows
515     * determining if another page of results is available.
516     *
517     * @param string $search
518     * @return SearchSuggestionSet
519     */
520    protected function completionSearchBackendOverfetch( $search ) {
521        $this->limit++;
522        try {
523            return $this->completionSearchBackend( $search );
524        } finally {
525            $this->limit--;
526        }
527    }
528
529    /**
530     * Perform a completion search.
531     * Does not resolve namespaces and does not check variants.
532     * Search engine implementations may want to override this function.
533     *
534     * @stable to override
535     *
536     * @param string $search
537     * @return SearchSuggestionSet
538     */
539    protected function completionSearchBackend( $search ) {
540        $search = trim( $search );
541        $results = $this->simplePrefixSearch( $search );
542        return SearchSuggestionSet::fromTitles( $results );
543    }
544
545    /**
546     * Perform a completion search.
547     * @param string $search
548     * @return SearchSuggestionSet
549     */
550    public function completionSearch( $search ) {
551        if ( trim( $search ) === '' ) {
552            return SearchSuggestionSet::emptySuggestionSet(); // Return empty result
553        }
554        $search = $this->normalizeNamespaces( $search );
555        $suggestions = $this->completionSearchBackendOverfetch( $search );
556        return $this->processCompletionResults( $search, $suggestions );
557    }
558
559    /**
560     * Perform a completion search with variants.
561     * @stable to override
562     *
563     * @param string $search
564     * @return SearchSuggestionSet
565     */
566    public function completionSearchWithVariants( $search ) {
567        if ( trim( $search ) === '' ) {
568            return SearchSuggestionSet::emptySuggestionSet(); // Return empty result
569        }
570        $search = $this->normalizeNamespaces( $search );
571
572        $results = $this->completionSearchBackendOverfetch( $search );
573        $fallbackLimit = 1 + $this->limit - $results->getSize();
574        if ( $fallbackLimit > 0 ) {
575            $services = MediaWikiServices::getInstance();
576            $fallbackSearches = $services->getLanguageConverterFactory()
577                ->getLanguageConverter( $services->getContentLanguage() )
578                ->autoConvertToAllVariants( $search );
579            $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
580
581            $origLimit = $this->limit;
582            $origOffset = $this->offset;
583            foreach ( $fallbackSearches as $fbs ) {
584                try {
585                    $this->setLimitOffset( $fallbackLimit );
586                    $fallbackSearchResult = $this->completionSearch( $fbs );
587                    $results->appendAll( $fallbackSearchResult );
588                    $fallbackLimit -= $fallbackSearchResult->getSize();
589                } finally {
590                    $this->setLimitOffset( $origLimit, $origOffset );
591                }
592                if ( $fallbackLimit <= 0 ) {
593                    break;
594                }
595            }
596        }
597        return $this->processCompletionResults( $search, $results );
598    }
599
600    /**
601     * Extract titles from completion results
602     * @param SearchSuggestionSet $completionResults
603     * @return Title[]
604     */
605    public function extractTitles( SearchSuggestionSet $completionResults ) {
606        return $completionResults->map( static function ( SearchSuggestion $sugg ) {
607            return $sugg->getSuggestedTitle();
608        } );
609    }
610
611    /**
612     * Process completion search results.
613     * Resolves the titles and rescores.
614     * @param string $search
615     * @param SearchSuggestionSet $suggestions
616     * @return SearchSuggestionSet
617     */
618    protected function processCompletionResults( $search, SearchSuggestionSet $suggestions ) {
619        // We over-fetched to determine pagination. Shrink back down if we have extra results
620        // and mark if pagination is possible
621        $suggestions->shrink( $this->limit );
622
623        $search = trim( $search );
624        // preload the titles with LinkBatch
625        $suggestedTitles = $suggestions->map( static function ( SearchSuggestion $sugg ) {
626            return $sugg->getSuggestedTitle();
627        } );
628        $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
629        $linkBatchFactory->newLinkBatch( $suggestedTitles )
630            ->setCaller( __METHOD__ )
631            ->execute();
632
633        $diff = $suggestions->filter( static function ( SearchSuggestion $sugg ) {
634            return $sugg->getSuggestedTitle()->isKnown();
635        } );
636        if ( $diff > 0 ) {
637            $statsFactory = MediaWikiServices::getInstance()->getStatsFactory();
638            $statsFactory->getCounter( 'search_completion_missing_total' )
639                ->incrementBy( (float)$diff );
640        }
641
642        // SearchExactMatchRescorer should probably be refactored to work directly on top of a SearchSuggestionSet
643        // instead of converting it to array and trying to infer if it has re-scored anything by inspected the head
644        // of the returned array.
645        $results = $suggestions->map( static function ( SearchSuggestion $sugg ) {
646            return $sugg->getSuggestedTitle()->getPrefixedText();
647        } );
648
649        $rescorer = new SearchExactMatchRescorer();
650        if ( $this->offset === 0 ) {
651            // Rescore results with an exact title match
652            // NOTE: in some cases like cross-namespace redirects
653            // (frequently used as shortcuts e.g. WP:WP on huwiki) some
654            // backends like Cirrus will return no results. We should still
655            // try an exact title match to workaround this limitation
656            $rescoredResults = $rescorer->rescore( $search, $this->namespaces, $results, $this->limit );
657        } else {
658            // No need to rescore if offset is not 0
659            // The exact match must have been returned at position 0
660            // if it existed.
661            $rescoredResults = $results;
662        }
663
664        if ( count( $rescoredResults ) > 0 ) {
665            $found = array_search( $rescoredResults[0], $results );
666            if ( $found === false ) {
667                // If the first result is not in the previous array it
668                // means that we found a new exact match
669                $exactMatch = SearchSuggestion::fromTitle( 0, Title::newFromText( $rescoredResults[0] ) );
670                $suggestions->prepend( $exactMatch );
671                if ( $rescorer->getReplacedRedirect() !== null ) {
672                    // the exact match rescorer replaced one of the suggestion found by the search engine
673                    // let's remove it from our suggestions set to avoid showing duplicates
674                    $suggestions->remove( SearchSuggestion::fromTitle( 0,
675                        Title::newFromText( $rescorer->getReplacedRedirect() ) ) );
676                }
677                $suggestions->shrink( $this->limit );
678            } else {
679                // if the first result is not the same we need to rescore
680                if ( $found > 0 ) {
681                    $suggestions->rescore( $found );
682                }
683            }
684        }
685
686        return $suggestions;
687    }
688
689    /**
690     * Simple prefix search for subpages.
691     * @param string $search
692     * @return Title[]
693     */
694    public function defaultPrefixSearch( $search ) {
695        if ( trim( $search ) === '' ) {
696            return [];
697        }
698
699        $search = $this->normalizeNamespaces( $search );
700        return $this->simplePrefixSearch( $search );
701    }
702
703    /**
704     * Call out to simple search backend.
705     * Defaults to TitlePrefixSearch.
706     * @param string $search
707     * @return Title[]
708     */
709    protected function simplePrefixSearch( $search ) {
710        // Use default database prefix search
711        $backend = new TitlePrefixSearch;
712        return $backend->defaultSearchBackend( $this->namespaces, $search, $this->limit, $this->offset );
713    }
714
715    /**
716     * Get a list of supported profiles.
717     * Some search engine implementations may expose specific profiles to fine-tune
718     * its behaviors.
719     * The profile can be passed as a feature data with setFeatureData( $profileType, $profileName )
720     * The array returned by this function contains the following keys:
721     * - name: the profile name to use with setFeatureData
722     * - desc-message: the i18n description
723     * - default: set to true if this profile is the default
724     *
725     * @since 1.28
726     * @stable to override
727     *
728     * @param string $profileType the type of profiles
729     * @param User|null $user the user requesting the list of profiles
730     * @return array|null the list of profiles or null if none available
731     * @phan-return null|array{name:string,desc-message:string,default?:bool}
732     */
733    public function getProfiles( $profileType, ?User $user = null ) {
734        return null;
735    }
736
737    /**
738     * Create a search field definition.
739     * Specific search engines should override this method to create search fields.
740     * @stable to override
741     *
742     * @param string $name
743     * @param string $type One of the types in SearchIndexField::INDEX_TYPE_*
744     * @return SearchIndexField
745     * @since 1.28
746     */
747    public function makeSearchFieldMapping( $name, $type ) {
748        return new NullIndexField();
749    }
750
751    /**
752     * Get fields for search index
753     * @since 1.28
754     * @return SearchIndexField[] Index field definitions for all content handlers
755     */
756    public function getSearchIndexFields() {
757        $models = MediaWikiServices::getInstance()->getContentHandlerFactory()->getContentModels();
758        $fields = [];
759        $seenHandlers = new SplObjectStorage();
760        foreach ( $models as $model ) {
761            try {
762                $handler = MediaWikiServices::getInstance()
763                    ->getContentHandlerFactory()
764                    ->getContentHandler( $model );
765            } catch ( UnknownContentModelException ) {
766                // If we can find no handler, ignore it
767                continue;
768            }
769            // Several models can have the same handler, so avoid processing it repeatedly
770            if ( $seenHandlers->offsetExists( $handler ) ) {
771                // We already did this one
772                continue;
773            }
774            $seenHandlers->offsetSet( $handler );
775            $handlerFields = $handler->getFieldsForSearchIndex( $this );
776            foreach ( $handlerFields as $fieldName => $fieldData ) {
777                if ( empty( $fields[$fieldName] ) ) {
778                    $fields[$fieldName] = $fieldData;
779                } else {
780                    // TODO: do we allow some clashes with the same type or reject all of them?
781                    $mergeDef = $fields[$fieldName]->merge( $fieldData );
782                    if ( !$mergeDef ) {
783                        throw new InvalidArgumentException( "Duplicate field $fieldName for model $model" );
784                    }
785                    $fields[$fieldName] = $mergeDef;
786                }
787            }
788        }
789        // Hook to allow extensions to produce search mapping fields
790        $this->getHookRunner()->onSearchIndexFields( $fields, $this );
791        return $fields;
792    }
793
794    /**
795     * Augment search results with extra data.
796     */
797    public function augmentSearchResults( ISearchResultSet $resultSet ) {
798        $setAugmentors = [];
799        $rowAugmentors = [];
800        $this->getHookRunner()->onSearchResultsAugment( $setAugmentors, $rowAugmentors );
801        if ( !$setAugmentors && !$rowAugmentors ) {
802            // We're done here
803            return;
804        }
805
806        // Convert row augmenters to set augmentor
807        foreach ( $rowAugmentors as $name => $row ) {
808            if ( isset( $setAugmentors[$name] ) ) {
809                throw new InvalidArgumentException( "Both row and set augmentors are defined for $name" );
810            }
811            $setAugmentors[$name] = new PerRowAugmentor( $row );
812        }
813
814        /**
815         * @var string $name
816         * @var ResultSetAugmentor $augmentor
817         */
818        foreach ( $setAugmentors as $name => $augmentor ) {
819            $data = $augmentor->augmentAll( $resultSet );
820            if ( $data ) {
821                $resultSet->setAugmentedData( $name, $data );
822            }
823        }
824    }
825
826    /**
827     * @since 1.35
828     * @internal
829     * @param HookContainer $hookContainer
830     */
831    public function setHookContainer( HookContainer $hookContainer ) {
832        $this->hookContainer = $hookContainer;
833        $this->hookRunner = new HookRunner( $hookContainer );
834    }
835
836    /**
837     * Get a HookContainer, for running extension hooks or for hook metadata.
838     *
839     * @since 1.35
840     * @return HookContainer
841     */
842    protected function getHookContainer(): HookContainer {
843        if ( !$this->hookContainer ) {
844            // This shouldn't be hit in core, but it is needed for CirrusSearch
845            // which commonly creates a CirrusSearch object without cirrus being
846            // configured in $wgSearchType/$wgSearchTypeAlternatives.
847            $this->hookContainer = MediaWikiServices::getInstance()->getHookContainer();
848        }
849        return $this->hookContainer;
850    }
851
852    /**
853     * Get a HookRunner for running core hooks.
854     *
855     * @internal This is for use by core only. Hook interfaces may be removed
856     *   without notice.
857     * @since 1.35
858     * @return HookRunner
859     */
860    protected function getHookRunner(): HookRunner {
861        if ( !$this->hookRunner ) {
862            $this->hookRunner = new HookRunner( $this->getHookContainer() );
863        }
864        return $this->hookRunner;
865    }
866
867}
868
869/** @deprecated class alias since 1.46 */
870class_alias( SearchEngine::class, 'SearchEngine' );