MediaWiki  master
SearchEngine Class Reference

Contain a class for special pages. More...

Inheritance diagram for SearchEngine:

Public Member Functions

 augmentSearchResults (ISearchResultSet $resultSet)
 Augment search results with extra data. More...
 
 completionSearch ( $search)
 Perform a completion search. More...
 
 completionSearchWithVariants ( $search)
 Perform a completion search with variants. More...
 
 defaultPrefixSearch ( $search)
 Simple prefix search for subpages. More...
 
 delete ( $id, $title)
 Delete an indexed page Title should be pre-processed. More...
 
 extractTitles (SearchSuggestionSet $completionResults)
 Extract titles from completion results. More...
 
 getFeatureData ( $feature)
 Way to retrieve custom data set by setFeatureData or by the engine itself. More...
 
 getNearMatcher (Config $config)
 Get service class to finding near matches. More...
 
 getProfiles ( $profileType, User $user=null)
 Get a list of supported profiles. More...
 
 getSearchIndexFields ()
 Get fields for search index. More...
 
 getSort ()
 Get the sort direction of the search results. More...
 
 getTextFromContent (Title $t, Content $c=null)
 Get the raw text for updating the index from a content object Nicer search backends could possibly do something cooler than just returning raw text. More...
 
 getValidSorts ()
 Get the valid sort directions. More...
 
 legalSearchChars ( $type=self::CHARS_ALL)
 Get chars legal for search. More...
 
 makeSearchFieldMapping ( $name, $type)
 Create a search field definition. More...
 
 normalizeText ( $string)
 When overridden in derived class, performs database-specific conversions on text to be used for searching or updating search index. More...
 
 replacePrefixes ( $query)
 Parse some common prefixes: all (search everything) or namespace names and set the list of namespaces of this class accordingly. More...
 
 searchArchiveTitle ( $term)
 Perform a title search in the article archive. More...
 
 searchText ( $term)
 Perform a full text search query and return a result set. More...
 
 searchTitle ( $term)
 Perform a title-only search query and return a result set. More...
 
 setFeatureData ( $feature, $data)
 Way to pass custom data for engines. More...
 
 setLimitOffset ( $limit, $offset=0)
 Set the maximum number of results to return and how many to skip before returning the first. More...
 
 setNamespaces ( $namespaces)
 Set which namespaces the search should include. More...
 
 setShowSuggestion ( $showSuggestion)
 Set whether the searcher should try to build a suggestion. More...
 
 setSort ( $sort)
 Set the sort direction of the search results. More...
 
 supports ( $feature)
 
 textAlreadyUpdatedForIndex ()
 If an implementation of SearchEngine handles all of its own text processing in getTextFromContent() and doesn't require SearchUpdate::updateText()'s rather silly handling, it should return true here instead. More...
 
 update ( $id, $title, $text)
 Create or update the search index record for the given page. More...
 
 updateTitle ( $id, $title)
 Update a search index record's title only. More...
 

Static Public Member Functions

static parseNamespacePrefixes ( $query, $withAllKeyword=true, $withPrefixSearchExtractNamespaceHook=false)
 Parse some common prefixes: all (search everything) or namespace names. More...
 
static userHighlightPrefs ()
 Find snippet highlight settings for all users. More...
 

Public Attributes

int[] null $namespaces = [ NS_MAIN ]
 
string $prefix = ''
 
const CHARS_ALL = 1
 Integer flag for legalSearchChars: includes all chars allowed in a search query. More...
 
const CHARS_NO_SYNTAX = 2
 Integer flag for legalSearchChars: includes all chars allowed in a search term. More...
 
const COMPLETION_PROFILE_TYPE = 'completionSearchProfile'
 Profile type for completionSearch. More...
 
const DEFAULT_SORT = 'relevance'
 
const FT_QUERY_INDEP_PROFILE_TYPE = 'fulltextQueryIndepProfile'
 Profile type for query independent ranking features. More...
 

Protected Member Functions

 completionSearchBackend ( $search)
 Perform a completion search. More...
 
 completionSearchBackendOverfetch ( $search)
 Perform an overfetch of completion search results. More...
 
 doSearchArchiveTitle ( $term)
 Perform a title search in the article archive. More...
 
 doSearchText ( $term)
 Perform a full text search query and return a result set. More...
 
 doSearchTitle ( $term)
 Perform a title-only search query and return a result set. More...
 
 normalizeNamespaces ( $search)
 Makes search simple string if it was namespaced. More...
 
 processCompletionResults ( $search, SearchSuggestionSet $suggestions)
 Process completion search results. More...
 
 simplePrefixSearch ( $search)
 Call out to simple search backend. More...
 

Static Protected Member Functions

static defaultNearMatcher ()
 Get near matcher for default SearchEngine. More...
 

Protected Attributes

array $features = []
 Feature values. More...
 
int $limit = 10
 
int $offset = 0
 
string[] $searchTerms = []
 
bool $showSuggestion = true
 

Private Member Functions

 maybePaginate (Closure $fn)
 Performs an overfetch and shrink operation to determine if the next page is available for search engines that do not explicitly implement their own pagination. More...
 

Private Attributes

 $sort = self::DEFAULT_SORT
 

Detailed Description

Contain a class for special pages.

Definition at line 34 of file SearchEngine.php.

Member Function Documentation

◆ augmentSearchResults()

SearchEngine::augmentSearchResults ( ISearchResultSet  $resultSet)

Augment search results with extra data.

Parameters
ISearchResultSet$resultSet

Definition at line 795 of file SearchEngine.php.

References Hooks\run(), and ISearchResultSet\setAugmentedData().

◆ completionSearch()

SearchEngine::completionSearch (   $search)

Perform a completion search.

Parameters
string$search
Returns
SearchSuggestionSet

Definition at line 573 of file SearchEngine.php.

References completionSearchBackendOverfetch(), SearchSuggestionSet\emptySuggestionSet(), normalizeNamespaces(), and processCompletionResults().

Referenced by completionSearchWithVariants().

◆ completionSearchBackend()

SearchEngine::completionSearchBackend (   $search)
protected

Perform a completion search.

Does not resolve namespaces and does not check variants. Search engine implementations may want to override this function.

Parameters
string$search
Returns
SearchSuggestionSet

Definition at line 548 of file SearchEngine.php.

References SearchSuggestionSet\fromStrings(), SearchSuggestionSet\fromTitles(), NS_SPECIAL, Hooks\run(), and simplePrefixSearch().

Referenced by completionSearchBackendOverfetch().

◆ completionSearchBackendOverfetch()

SearchEngine::completionSearchBackendOverfetch (   $search)
protected

Perform an overfetch of completion search results.

This allows determining if another page of results is available.

Parameters
string$search
Returns
SearchSuggestionSet

Definition at line 532 of file SearchEngine.php.

References completionSearchBackend().

Referenced by completionSearch(), and completionSearchWithVariants().

◆ completionSearchWithVariants()

SearchEngine::completionSearchWithVariants (   $search)

Perform a completion search with variants.

Parameters
string$search
Returns
SearchSuggestionSet

Definition at line 587 of file SearchEngine.php.

References completionSearch(), completionSearchBackendOverfetch(), SearchSuggestionSet\emptySuggestionSet(), normalizeNamespaces(), processCompletionResults(), and setLimitOffset().

◆ defaultNearMatcher()

static SearchEngine::defaultNearMatcher ( )
staticprotected

Get near matcher for default SearchEngine.

Returns
SearchNearMatcher

Definition at line 255 of file SearchEngine.php.

◆ defaultPrefixSearch()

SearchEngine::defaultPrefixSearch (   $search)

Simple prefix search for subpages.

Parameters
string$search
Returns
Title[]

Definition at line 695 of file SearchEngine.php.

References normalizeNamespaces(), and simplePrefixSearch().

◆ delete()

SearchEngine::delete (   $id,
  $title 
)

Delete an indexed page Title should be pre-processed.

STUB

Parameters
int$idPage id that was deleted
string$titleTitle of page that was deleted

Reimplemented in SearchMySQL.

Definition at line 479 of file SearchEngine.php.

◆ doSearchArchiveTitle()

SearchEngine::doSearchArchiveTitle (   $term)
protected

Perform a title search in the article archive.

Parameters
string$termRaw search term
Returns
Status
Since
1.32

Definition at line 126 of file SearchEngine.php.

References StatusValue\newGood().

Referenced by searchArchiveTitle().

◆ doSearchText()

SearchEngine::doSearchText (   $term)
protected

Perform a full text search query and return a result set.

Parameters
string$termRaw search term
Returns
ISearchResultSet|Status|null
Since
1.32

Reimplemented in SearchDatabase.

Definition at line 97 of file SearchEngine.php.

Referenced by searchText().

◆ doSearchTitle()

SearchEngine::doSearchTitle (   $term)
protected

Perform a title-only search query and return a result set.

Parameters
string$termRaw search term
Returns
ISearchResultSet|null
Since
1.32

Reimplemented in SearchDatabase.

Definition at line 154 of file SearchEngine.php.

Referenced by searchTitle().

◆ extractTitles()

SearchEngine::extractTitles ( SearchSuggestionSet  $completionResults)

Extract titles from completion results.

Parameters
SearchSuggestionSet$completionResults
Returns
Title[]

Definition at line 618 of file SearchEngine.php.

References SearchSuggestion\getSuggestedTitle(), and SearchSuggestionSet\map().

◆ getFeatureData()

SearchEngine::getFeatureData (   $feature)

Way to retrieve custom data set by setFeatureData or by the engine itself.

Since
1.29
Parameters
string$featurefeature name
Returns
mixed the feature value or null if unset

Definition at line 224 of file SearchEngine.php.

◆ getNearMatcher()

SearchEngine::getNearMatcher ( Config  $config)

Get service class to finding near matches.

Parameters
Config$configConfiguration to use for the matcher.
Returns
SearchNearMatcher

Definition at line 246 of file SearchEngine.php.

◆ getProfiles()

SearchEngine::getProfiles (   $profileType,
User  $user = null 
)

Get a list of supported profiles.

Some search engine implementations may expose specific profiles to fine-tune its behaviors. The profile can be passed as a feature data with setFeatureData( $profileType, $profileName ) The array returned by this function contains the following keys:

  • name: the profile name to use with setFeatureData
  • desc-message: the i18n description
  • default: set to true if this profile is the default
Since
1.28
Parameters
string$profileTypethe type of profiles
User | null$userthe user requesting the list of profiles
Returns
array|null the list of profiles or null if none available -return null|array{name:string,desc-message:string,default?:bool}

Definition at line 732 of file SearchEngine.php.

◆ getSearchIndexFields()

SearchEngine::getSearchIndexFields ( )

Get fields for search index.

Since
1.28
Returns
SearchIndexField[] Index field definitions for all content handlers

Definition at line 753 of file SearchEngine.php.

References ContentHandler\getContentModels(), ContentHandler\getForModelID(), and Hooks\run().

◆ getSort()

SearchEngine::getSort ( )

Get the sort direction of the search results.

Since
1.25
Returns
string

Definition at line 346 of file SearchEngine.php.

References $sort.

◆ getTextFromContent()

SearchEngine::getTextFromContent ( Title  $t,
Content  $c = null 
)

Get the raw text for updating the index from a content object Nicer search backends could possibly do something cooler than just returning raw text.

Todo:
This isn't ideal, we'd really like to have content-specific handling here
Parameters
Title$tTitle we're indexing
Content | null$cContent of the page to index
Returns
string
Deprecated:
since 1.34 use Content::getTextForSearchIndex directly

Definition at line 494 of file SearchEngine.php.

◆ getValidSorts()

SearchEngine::getValidSorts ( )

Get the valid sort directions.

All search engines support 'relevance' but others might support more. The default in all implementations must be 'relevance.'

Since
1.25
Returns
string[] the valid sort directions for setSort

Definition at line 320 of file SearchEngine.php.

References DEFAULT_SORT.

Referenced by setSort().

◆ legalSearchChars()

SearchEngine::legalSearchChars (   $type = self::CHARS_ALL)

Get chars legal for search.

Parameters
int$typetype of search chars (see self::CHARS_ALL and self::CHARS_NO_SYNTAX). Defaults to CHARS_ALL
Returns
string

Reimplemented in SearchSqlite, and SearchMySQL.

Definition at line 267 of file SearchEngine.php.

Referenced by SearchDatabase\filter().

◆ makeSearchFieldMapping()

SearchEngine::makeSearchFieldMapping (   $name,
  $type 
)

Create a search field definition.

Specific search engines should override this method to create search fields.

Parameters
string$name
string$typeOne of the types in SearchIndexField::INDEX_TYPE_*
Returns
SearchIndexField
Since
1.28

Definition at line 744 of file SearchEngine.php.

Referenced by ContentHandler\addSearchField(), FileContentHandler\getFieldsForSearchIndex(), WikitextContentHandler\getFieldsForSearchIndex(), TextContentHandler\getFieldsForSearchIndex(), and ContentHandler\getFieldsForSearchIndex().

◆ maybePaginate()

SearchEngine::maybePaginate ( Closure  $fn)
private

Performs an overfetch and shrink operation to determine if the next page is available for search engines that do not explicitly implement their own pagination.

Parameters
Closure$fnTakes no arguments
Returns
ISearchResultSet|Status<ISearchResultSet>|null Result of calling $fn

Definition at line 166 of file SearchEngine.php.

References StatusValue\getValue().

Referenced by searchText(), and searchTitle().

◆ normalizeNamespaces()

SearchEngine::normalizeNamespaces (   $search)
protected

Makes search simple string if it was namespaced.

Sets namespaces of the search to namespaces extracted from string.

Parameters
string$search
Returns
string Simplified search string

Definition at line 516 of file SearchEngine.php.

References parseNamespacePrefixes(), and setNamespaces().

Referenced by completionSearch(), completionSearchWithVariants(), and defaultPrefixSearch().

◆ normalizeText()

SearchEngine::normalizeText (   $string)

When overridden in derived class, performs database-specific conversions on text to be used for searching or updating search index.

Default implementation does nothing (simply returns $string).

Parameters
string$stringString to process
Returns
string

Reimplemented in SearchMySQL.

Definition at line 236 of file SearchEngine.php.

◆ parseNamespacePrefixes()

static SearchEngine::parseNamespacePrefixes (   $query,
  $withAllKeyword = true,
  $withPrefixSearchExtractNamespaceHook = false 
)
static

Parse some common prefixes: all (search everything) or namespace names.

Parameters
string$query
bool$withAllKeywordactivate support of the "all:" keyword and its translations to activate searching on all namespaces.
bool$withPrefixSearchExtractNamespaceHookcall the PrefixSearchExtractNamespace hook if classic namespace identification did not match.
Returns
false|array false if no namespace was extracted, an array with the parsed query at index 0 and an array of namespaces at index 1 (or null for all namespaces).
Exceptions
FatalError
MWException

Definition at line 378 of file SearchEngine.php.

References $prefix, NS_MAIN, Hooks\run(), and wfMessage().

Referenced by SearchDatabase\extractNamespacePrefix(), normalizeNamespaces(), and PrefixSearch\search().

◆ processCompletionResults()

SearchEngine::processCompletionResults (   $search,
SearchSuggestionSet  $suggestions 
)
protected

◆ replacePrefixes()

SearchEngine::replacePrefixes (   $query)

Parse some common prefixes: all (search everything) or namespace names and set the list of namespaces of this class accordingly.

Deprecated:
since 1.32; should be handled internally by the search engine
Parameters
string$query
Returns
string

Definition at line 359 of file SearchEngine.php.

◆ searchArchiveTitle()

SearchEngine::searchArchiveTitle (   $term)

Perform a title search in the article archive.

NOTE: these results still should be filtered by matching against PageArchive, permissions checks etc The results returned by this methods are only sugegstions and may not end up being shown to the user.

As of 1.32 overriding this function is deprecated. It will be converted to final in 1.34. Override self::doSearchArchiveTitle().

Parameters
string$termRaw search term
Returns
Status
Since
1.29

Definition at line 115 of file SearchEngine.php.

References doSearchArchiveTitle().

◆ searchText()

SearchEngine::searchText (   $term)

Perform a full text search query and return a result set.

If full text searches are not supported or disabled, return null.

As of 1.32 overriding this function is deprecated. It will be converted to final in 1.34. Override self::doSearchText().

Parameters
string$termRaw search term
Returns
ISearchResultSet|Status|null

Definition at line 84 of file SearchEngine.php.

References doSearchText(), and maybePaginate().

◆ searchTitle()

SearchEngine::searchTitle (   $term)

Perform a title-only search query and return a result set.

If title searches are not supported or disabled, return null. STUB

As of 1.32 overriding this function is deprecated. It will be converted to final in 1.34. Override self::doSearchTitle().

Parameters
string$termRaw search term
Returns
ISearchResultSet|null

Definition at line 141 of file SearchEngine.php.

References doSearchTitle(), and maybePaginate().

◆ setFeatureData()

SearchEngine::setFeatureData (   $feature,
  $data 
)

Way to pass custom data for engines.

Since
1.18
Parameters
string$feature
mixed$data

Definition at line 213 of file SearchEngine.php.

◆ setLimitOffset()

SearchEngine::setLimitOffset (   $limit,
  $offset = 0 
)

Set the maximum number of results to return and how many to skip before returning the first.

Parameters
int$limit
int$offset

Definition at line 278 of file SearchEngine.php.

References $limit, and $offset.

Referenced by completionSearchWithVariants().

◆ setNamespaces()

SearchEngine::setNamespaces (   $namespaces)

Set which namespaces the search should include.

Give an array of namespace index numbers.

Parameters
int[] | null$namespaces

Definition at line 289 of file SearchEngine.php.

References $namespaces.

Referenced by normalizeNamespaces().

◆ setShowSuggestion()

SearchEngine::setShowSuggestion (   $showSuggestion)

Set whether the searcher should try to build a suggestion.

Note: some searchers don't support building a suggestion in the first place and others don't respect this flag.

Parameters
bool$showSuggestionShould the searcher try to build suggestions

Definition at line 309 of file SearchEngine.php.

References $showSuggestion.

◆ setSort()

SearchEngine::setSort (   $sort)

Set the sort direction of the search results.

Must be one returned by SearchEngine::getValidSorts()

Since
1.25
Exceptions
InvalidArgumentException
Parameters
string$sortsort direction for query result

Definition at line 332 of file SearchEngine.php.

References $sort, and getValidSorts().

◆ simplePrefixSearch()

SearchEngine::simplePrefixSearch (   $search)
protected

Call out to simple search backend.

Defaults to TitlePrefixSearch.

Parameters
string$search
Returns
Title[]

Definition at line 710 of file SearchEngine.php.

References PrefixSearch\defaultSearchBackend().

Referenced by completionSearchBackend(), and defaultPrefixSearch().

◆ supports()

SearchEngine::supports (   $feature)
Since
1.18
Parameters
string$feature
Returns
bool

Reimplemented in SearchMySQL.

Definition at line 197 of file SearchEngine.php.

◆ textAlreadyUpdatedForIndex()

SearchEngine::textAlreadyUpdatedForIndex ( )

If an implementation of SearchEngine handles all of its own text processing in getTextFromContent() and doesn't require SearchUpdate::updateText()'s rather silly handling, it should return true here instead.

Returns
bool
Deprecated:
since 1.34 no longer needed since getTextFromContent is being deprecated

Definition at line 506 of file SearchEngine.php.

◆ update()

SearchEngine::update (   $id,
  $title,
  $text 
)

Create or update the search index record for the given page.

Title and text should be pre-processed. STUB

Parameters
int$id
string$title
string$text

Reimplemented in SearchPostgres, SearchMySQL, and SearchSqlite.

Definition at line 455 of file SearchEngine.php.

◆ updateTitle()

SearchEngine::updateTitle (   $id,
  $title 
)

Update a search index record's title only.

Title should be pre-processed. STUB

Parameters
int$id
string$title

Reimplemented in SearchMySQL, SearchSqlite, and SearchPostgres.

Definition at line 467 of file SearchEngine.php.

◆ userHighlightPrefs()

static SearchEngine::userHighlightPrefs ( )
static

Find snippet highlight settings for all users.

Returns
array Contextlines, contextchars
Deprecated:
in 1.34 use the SearchHighlighter constants directly
See also
SearchHighlighter::DEFAULT_CONTEXT_CHARS
SearchHighlighter::DEFAULT_CONTEXT_LINES

Definition at line 440 of file SearchEngine.php.

References SearchHighlighter\DEFAULT_CONTEXT_CHARS, and SearchHighlighter\DEFAULT_CONTEXT_LINES.

Member Data Documentation

◆ $features

array SearchEngine::$features = []
protected

Feature values.

Definition at line 60 of file SearchEngine.php.

◆ $limit

int SearchEngine::$limit = 10
protected

Definition at line 44 of file SearchEngine.php.

Referenced by SearchMySQL\limitResult(), and setLimitOffset().

◆ $namespaces

int [] null SearchEngine::$namespaces = [ NS_MAIN ]

◆ $offset

int SearchEngine::$offset = 0
protected

Definition at line 47 of file SearchEngine.php.

Referenced by SearchMySQL\limitResult(), and setLimitOffset().

◆ $prefix

string SearchEngine::$prefix = ''

Definition at line 38 of file SearchEngine.php.

Referenced by parseNamespacePrefixes().

◆ $searchTerms

string [] SearchEngine::$searchTerms = []
protected
Deprecated:
since 1.34

Definition at line 53 of file SearchEngine.php.

◆ $showSuggestion

bool SearchEngine::$showSuggestion = true
protected

Definition at line 56 of file SearchEngine.php.

Referenced by setShowSuggestion().

◆ $sort

SearchEngine::$sort = self::DEFAULT_SORT
private

Definition at line 57 of file SearchEngine.php.

Referenced by getSort(), and setSort().

◆ CHARS_ALL

const SearchEngine::CHARS_ALL = 1

Integer flag for legalSearchChars: includes all chars allowed in a search query.

Definition at line 69 of file SearchEngine.php.

◆ CHARS_NO_SYNTAX

const SearchEngine::CHARS_NO_SYNTAX = 2

Integer flag for legalSearchChars: includes all chars allowed in a search term.

Definition at line 72 of file SearchEngine.php.

◆ COMPLETION_PROFILE_TYPE

const SearchEngine::COMPLETION_PROFILE_TYPE = 'completionSearchProfile'

Profile type for completionSearch.

Definition at line 63 of file SearchEngine.php.

Referenced by ApiQueryPrefixSearch\getSearchProfileParams(), and ApiOpenSearch\getSearchProfileParams().

◆ DEFAULT_SORT

const SearchEngine::DEFAULT_SORT = 'relevance'

Definition at line 35 of file SearchEngine.php.

Referenced by getValidSorts(), and SpecialSearch\load().

◆ FT_QUERY_INDEP_PROFILE_TYPE

const SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE = 'fulltextQueryIndepProfile'

Profile type for query independent ranking features.

Definition at line 66 of file SearchEngine.php.

Referenced by ApiQuerySearch\getSearchProfileParams().


The documentation for this class was generated from the following file: