MediaWiki  1.30.0
SearchEngine Class Reference

Contain a class for special pages. More...

Inheritance diagram for SearchEngine:
Collaboration diagram for SearchEngine:

Public Member Functions

 augmentSearchResults (SearchResultSet $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...
 
 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...
 
 transformSearchTerm ( $term)
 Transform search term in cases when parts of the query came as different GET params (when supported), e.g. 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 create ( $type=null)
 Load up the appropriate search engine class for the currently active database backend, and return a configured instance. More...
 
static defaultNamespaces ()
 An array of namespaces indexes to be searched by default. More...
 
static getNearMatch ( $searchterm)
 If an exact title match can be found, or a very slightly close match, return the title. More...
 
static getNearMatchResultSet ( $searchterm)
 Do a near match (see SearchEngine::getNearMatch) and wrap it into a SearchResultSet. More...
 
static getOpenSearchTemplate ()
 Get OpenSearch suggestion template. More...
 
static getSearchTypes ()
 Return the search engines we support. More...
 
static legalSearchChars ( $type=self::CHARS_ALL)
 Get chars legal for search NOTE: usage as static is deprecated and preserved only as BC measure. More...
 
static namespacesAsText ( $namespaces)
 Get a list of namespace names useful for showing in tooltips and preferences. More...
 
static parseNamespacePrefixes ( $query)
 Parse some common prefixes: all (search everything) or namespace names. More...
 
static searchableNamespaces ()
 Make a list of searchable namespaces and their canonical names. More...
 
static userHighlightPrefs ()
 Find snippet highlight settings for all users. More...
 
static userNamespaces ( $user)
 Extract default namespaces to search from the given user's settings, returning a list of index numbers. More...
 

Public Attributes

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

Protected Member Functions

 completionSearchBackend ( $search)
 Perform a completion search. 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
 
array string $searchTerms = []
 
bool $showSuggestion = true
 

Private Attributes

 $sort = 'relevance'
 

Detailed Description

Contain a class for special pages.

Definition at line 34 of file SearchEngine.php.

Member Function Documentation

◆ augmentSearchResults()

SearchEngine::augmentSearchResults ( SearchResultSet  $resultSet)

Augment search results with extra data.

Parameters
SearchResultSet$resultSet

Definition at line 786 of file SearchEngine.php.

References $name, $rowAugmentors, as, Hooks\run(), and SearchResultSet\setAugmentedData().

◆ completionSearch()

SearchEngine::completionSearch (   $search)

◆ 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 490 of file SearchEngine.php.

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

Referenced by completionSearch(), and completionSearchWithVariants().

◆ completionSearchWithVariants()

SearchEngine::completionSearchWithVariants (   $search)

◆ create()

static SearchEngine::create (   $type = null)
static

Load up the appropriate search engine class for the currently active database backend, and return a configured instance.

Deprecated:
since 1.27; Use SearchEngineFactory::create
Parameters
string$typeType of search backend, if not the default
Returns
SearchEngine

Definition at line 694 of file SearchEngine.php.

References $type.

◆ defaultNamespaces()

static SearchEngine::defaultNamespaces ( )
static

An array of namespaces indexes to be searched by default.

Deprecated:
since 1.27; use SearchEngineConfig::defaultNamespaces()
Returns
array

Definition at line 672 of file SearchEngine.php.

◆ defaultNearMatcher()

static SearchEngine::defaultNearMatcher ( )
staticprotected

Get near matcher for default SearchEngine.

Returns
SearchNearMatcher

Definition at line 187 of file SearchEngine.php.

◆ defaultPrefixSearch()

SearchEngine::defaultPrefixSearch (   $search)

Simple prefix search for subpages.

Parameters
string$search
Returns
Title[]

Definition at line 626 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 405 of file SearchEngine.php.

◆ extractTitles()

SearchEngine::extractTitles ( SearchSuggestionSet  $completionResults)

Extract titles from completion results.

Parameters
SearchSuggestionSet$completionResults
Returns
Title[]

Definition at line 560 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 140 of file SearchEngine.php.

◆ getNearMatch()

static SearchEngine::getNearMatch (   $searchterm)
static

If an exact title match can be found, or a very slightly close match, return the title.

If no match, returns NULL.

Deprecated:
since 1.27; Use SearchEngine::getNearMatcher()
Parameters
string$searchterm
Returns
Title

Definition at line 199 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 178 of file SearchEngine.php.

References $wgContLang, and global.

◆ getNearMatchResultSet()

static SearchEngine::getNearMatchResultSet (   $searchterm)
static

Do a near match (see SearchEngine::getNearMatch) and wrap it into a SearchResultSet.

Deprecated:
since 1.27; Use SearchEngine::getNearMatcher()
Parameters
string$searchterm
Returns
SearchResultSet

Definition at line 210 of file SearchEngine.php.

◆ getOpenSearchTemplate()

static SearchEngine::getOpenSearchTemplate ( )
static

Get OpenSearch suggestion template.

Deprecated:
since 1.25
Returns
string

Definition at line 415 of file SearchEngine.php.

References ApiOpenSearch\getOpenSearchTemplate(), and wfDeprecated().

◆ 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

Definition at line 723 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 744 of file SearchEngine.php.

References $e, $handler, as, ContentHandler\getContentModels(), ContentHandler\getForModelID(), and Hooks\run().

◆ getSearchTypes()

static SearchEngine::getSearchTypes ( )
static

Return the search engines we support.

If only $wgSearchType is set, it'll be an array of just that one item.

Deprecated:
since 1.27; use SearchEngineConfig::getSearchTypes()
Returns
array

Definition at line 704 of file SearchEngine.php.

◆ getSort()

SearchEngine::getSort ( )

Get the sort direction of the search results.

Since
1.25
Returns
string

Definition at line 300 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$cContent of the page to index
Returns
string

Definition at line 430 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 should be 'relevance.'

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

Definition at line 274 of file SearchEngine.php.

Referenced by setSort().

◆ legalSearchChars()

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

Get chars legal for search NOTE: usage as static is deprecated and preserved only as BC measure.

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

Reimplemented in SearchOracle, SearchMySQL, and SearchSqlite.

Definition at line 221 of file SearchEngine.php.

Referenced by SearchDatabase\filter(), and SearchMssql\parseQuery().

◆ makeSearchFieldMapping()

SearchEngine::makeSearchFieldMapping (   $name,
  $type 
)

Create a search field definition.

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

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

Definition at line 735 of file SearchEngine.php.

◆ namespacesAsText()

static SearchEngine::namespacesAsText (   $namespaces)
static

Get a list of namespace names useful for showing in tooltips and preferences.

Deprecated:
since 1.27; use SearchEngineConfig::namespacesAsText()
Parameters
array$namespaces
Returns
array

Definition at line 683 of file SearchEngine.php.

References $namespaces.

◆ 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 451 of file SearchEngine.php.

References $namespaces, $title, Title\newFromText(), NS_FILE, NS_MAIN, NS_MEDIA, Hooks\run(), 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 155 of file SearchEngine.php.

References $wgContLang, and global.

◆ parseNamespacePrefixes()

static SearchEngine::parseNamespacePrefixes (   $query)
static

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

Parameters
string$query
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).

Definition at line 330 of file SearchEngine.php.

References $prefix, $query, $wgContLang, global, and wfMessage().

Referenced by replacePrefixes().

◆ 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.

Parameters
string$query
Returns
string

Definition at line 312 of file SearchEngine.php.

References $query, namespaces, and parseNamespacePrefixes().

◆ searchableNamespaces()

static SearchEngine::searchableNamespaces ( )
static

Make a list of searchable namespaces and their canonical names.

Deprecated:
since 1.27; use SearchEngineConfig::searchableNamespaces()
Returns
array

Definition at line 652 of file SearchEngine.php.

Referenced by setNamespaces().

◆ 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.

Parameters
string$termRaw search term
Returns
Status<Title[]>
Since
1.29

Definition at line 92 of file SearchEngine.php.

References StatusValue\newGood().

◆ 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. STUB

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

Reimplemented in SearchMySQL, SearchSqlite, SearchOracle, SearchPostgres, and SearchMssql.

Definition at line 77 of file SearchEngine.php.

◆ 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

Parameters
string$termRaw search term
Returns
SearchResultSet|null

Reimplemented in SearchMySQL, SearchSqlite, SearchOracle, SearchMssql, and SearchPostgres.

Definition at line 104 of file SearchEngine.php.

◆ setFeatureData()

SearchEngine::setFeatureData (   $feature,
  $data 
)

Way to pass custom data for engines.

Since
1.18
Parameters
string$feature
mixed$data

Definition at line 129 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 232 of file SearchEngine.php.

References $limit, and $offset.

Referenced by completionSearchWithVariants(), and SearchEnginePrefixTest\testSearchBackend().

◆ 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 243 of file SearchEngine.php.

References $namespaces, namespaces, searchableNamespaces(), and use.

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 263 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 286 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 641 of file SearchEngine.php.

References PrefixSearch\defaultSearchBackend(), and namespaces.

Referenced by completionSearchBackend(), and defaultPrefixSearch().

◆ supports()

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

Reimplemented in SearchMySQL.

Definition at line 113 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

Definition at line 441 of file SearchEngine.php.

◆ transformSearchTerm()

SearchEngine::transformSearchTerm (   $term)

Transform search term in cases when parts of the query came as different GET params (when supported), e.g.

for prefix queries: search=test&prefix=Main_Page/Archive -> test prefix:Main Page/Archive

Parameters
string$term
Returns
string

Definition at line 169 of file SearchEngine.php.

References $term.

◆ 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, SearchSqlite, SearchOracle, SearchMssql, and MockSearch.

Definition at line 381 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, SearchOracle, SearchMssql, and SearchPostgres.

Definition at line 393 of file SearchEngine.php.

◆ userHighlightPrefs()

static SearchEngine::userHighlightPrefs ( )
static

Find snippet highlight settings for all users.

Returns
array Contextlines, contextchars

Definition at line 366 of file SearchEngine.php.

◆ userNamespaces()

static SearchEngine::userNamespaces (   $user)
static

Extract default namespaces to search from the given user's settings, returning a list of index numbers.

Deprecated:
since 1.27; use SearchEngineConfig::userNamespaces()
Parameters
user$user
Returns
array

Definition at line 663 of file SearchEngine.php.

References $user.

Member Data Documentation

◆ $features

array SearchEngine::$features = []
protected

Feature values.

Definition at line 55 of file SearchEngine.php.

◆ $limit

int SearchEngine::$limit = 10
protected

Definition at line 42 of file SearchEngine.php.

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

◆ $namespaces

◆ $offset

int SearchEngine::$offset = 0
protected

Definition at line 45 of file SearchEngine.php.

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

◆ $prefix

string SearchEngine::$prefix = ''

Definition at line 36 of file SearchEngine.php.

Referenced by parseNamespacePrefixes().

◆ $searchTerms

array string SearchEngine::$searchTerms = []
protected

Definition at line 48 of file SearchEngine.php.

◆ $showSuggestion

bool SearchEngine::$showSuggestion = true
protected

Definition at line 51 of file SearchEngine.php.

Referenced by setShowSuggestion().

◆ $sort

SearchEngine::$sort = 'relevance'
private

Definition at line 52 of file SearchEngine.php.

Referenced by getSort(), and setSort().

◆ CHARS_ALL

const SearchEngine::CHARS_ALL = 1

@const int flag for legalSearchChars: includes all chars allowed in a search query

Definition at line 64 of file SearchEngine.php.

◆ CHARS_NO_SYNTAX

const SearchEngine::CHARS_NO_SYNTAX = 2

@const int flag for legalSearchChars: includes all chars allowed in a search term

Definition at line 67 of file SearchEngine.php.

◆ COMPLETION_PROFILE_TYPE

const SearchEngine::COMPLETION_PROFILE_TYPE = 'completionSearchProfile'

@const string profile type for completionSearch

Definition at line 58 of file SearchEngine.php.

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

◆ FT_QUERY_INDEP_PROFILE_TYPE

const SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE = 'fulltextQueryIndepProfile'

@const string profile type for query independent ranking features

Definition at line 61 of file SearchEngine.php.

Referenced by ApiQuerySearch\getSearchProfileParams().


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