MediaWiki  master
MediaWiki\Rest\Handler\SearchHandler Class Reference

Handler class for Core REST API endpoint that handles basic search. More...

Inheritance diagram for MediaWiki\Rest\Handler\SearchHandler:
Collaboration diagram for MediaWiki\Rest\Handler\SearchHandler:

Public Member Functions

 __construct (Config $config, SearchEngineFactory $searchEngineFactory, SearchEngineConfig $searchEngineConfig, PermissionManager $permissionManager, RedirectLookup $redirectLookup, PageStore $pageStore, TitleFormatter $titleFormatter)
 
 execute ()
 
 getParamSettings ()
 Fetch ParamValidator settings for parameters. More...
 
 needsWriteAccess ()
 Indicates whether this route requires write access. More...
 
- Public Member Functions inherited from MediaWiki\Rest\Handler
 applyConditionalResponseHeaders (ResponseInterface $response)
 Modify the response, adding Last-Modified and ETag headers as indicated the values previously returned by ETag and getLastModified(). More...
 
 checkPreconditions ()
 Check the conditional request headers and generate a response if appropriate. More...
 
 getAuthority ()
 Get the current acting authority. More...
 
 getBodyValidator ( $contentType)
 Fetch the BodyValidator. More...
 
 getConfig ()
 Get the configuration array for the current route. More...
 
 getRequest ()
 Get the current request. More...
 
 getResponseFactory ()
 Get the ResponseFactory which can be used to generate Response objects. More...
 
 getValidatedBody ()
 Fetch the validated body. More...
 
 getValidatedParams ()
 Fetch the validated parameters. More...
 
 init (Router $router, RequestInterface $request, array $config, Authority $authority, ResponseFactory $responseFactory, HookContainer $hookContainer)
 Initialise with dependencies from the Router. More...
 
 needsReadAccess ()
 Indicates whether this route requires read rights. More...
 
 validate (Validator $restValidator)
 Validate the request parameters/attributes and body. More...
 

Public Attributes

const COMPLETION_MODE = 'completion'
 Search title completion matches. More...
 
const FULLTEXT_MODE = 'fulltext'
 Search page body and titles. More...
 
- Public Attributes inherited from MediaWiki\Rest\Handler
const PARAM_SOURCE = 'rest-param-source'
 (string) ParamValidator constant to specify the source of the parameter. More...
 

Protected Member Functions

 postInitSetup ()
 The handler can override this to do any necessary setup after init() is called to inject the dependencies. More...
 
- Protected Member Functions inherited from MediaWiki\Rest\Handler
 getConditionalHeaderUtil ()
 Get a ConditionalHeaderUtil object. More...
 
 getETag ()
 The subclass should override this to provide an ETag for the current request. More...
 
 getHookContainer ()
 Get a HookContainer, for running extension hooks or for hook metadata. More...
 
 getHookRunner ()
 Get a HookRunner for running core hooks. More...
 
 getLastModified ()
 The subclass should override this to provide the maximum last modified timestamp for the current request. More...
 
 getRouter ()
 Get the Router. More...
 
 getRouteUrl ( $pathParams=[], $queryParams=[])
 Get the URL of this handler's endpoint. More...
 
 hasRepresentation ()
 The subclass should override this to indicate whether the resource exists. More...
 
 postValidationSetup ()
 The handler can override this to do any necessary setup after validate() has been called. More...
 
 urlEncodeTitle ( $title)
 URL-encode titles in a "pretty" way. More...
 

Private Member Functions

 buildDescriptionsFromPageIdentities (array $pageIdentities)
 Turn page info into serializable array with description field for the page. More...
 
 buildPageObjects (array $searchResponse)
 Build an array of pageInfo objects. More...
 
 buildResultFromPageInfos (array $pageInfos, array $thumbsAndDesc)
 Turn array of page info into serializable array with common information about the page. More...
 
 buildSinglePage ( $title, $result)
 Build one pageInfo object from either a SearchResult or SearchSuggestion. More...
 
 buildThumbnailsFromPageIdentities (array $pageIdentities)
 Turn page info into serializable array with thumbnail information for the page. More...
 
 createSearchEngine ()
 
 doSearch ( $searchEngine)
 Execute search and return info about pages for further processing. More...
 
 getSearchResultsOrThrow ( $results)
 Get SearchResults when results are either SearchResultSet or Status objects. More...
 
 serializeThumbnail (?SearchResultThumbnail $thumbnail)
 Converts SearchResultThumbnail object into serializable array. More...
 

Private Attributes

int null $completionCacheExpiry
 Expiry time for use as max-age value in the cache-control header of completion search responses. More...
 
string $mode = null
 
PageStore $pageStore
 
PermissionManager $permissionManager
 
RedirectLookup $redirectLookup
 
SearchEngineConfig $searchEngineConfig
 
SearchEngineFactory $searchEngineFactory
 
TitleFormatter $titleFormatter
 
const LIMIT = 50
 Limit results to 50 pages per default. More...
 
const MAX_LIMIT = 100
 Hard limit results to 100 pages. More...
 
const OFFSET = 0
 Default to first page. More...
 
const SUPPORTED_MODES = [ self::FULLTEXT_MODE, self::COMPLETION_MODE ]
 Supported modes. More...
 

Detailed Description

Handler class for Core REST API endpoint that handles basic search.

Definition at line 32 of file SearchHandler.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Rest\Handler\SearchHandler::__construct ( Config  $config,
SearchEngineFactory  $searchEngineFactory,
SearchEngineConfig  $searchEngineConfig,
PermissionManager  $permissionManager,
RedirectLookup  $redirectLookup,
PageStore  $pageStore,
TitleFormatter  $titleFormatter 
)

Member Function Documentation

◆ buildDescriptionsFromPageIdentities()

MediaWiki\Rest\Handler\SearchHandler::buildDescriptionsFromPageIdentities ( array  $pageIdentities)
private

Turn page info into serializable array with description field for the page.

The information about description should be provided by extension by implementing 'SearchResultProvideDescription' hook. Description is set to null if no extensions implement the hook.

Parameters
PageIdentity[]$pageIdentities
Returns
array

Definition at line 344 of file SearchHandler.php.

◆ buildPageObjects()

MediaWiki\Rest\Handler\SearchHandler::buildPageObjects ( array  $searchResponse)
private

Build an array of pageInfo objects.

Parameters
SearchSuggestion[] | SearchResult[]$searchResponse@phpcs:ignore Generic.Files.LineLength
Returns
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

Definition at line 212 of file SearchHandler.php.

References $title, MediaWiki\Rest\Handler\SearchHandler\buildSinglePage(), and MediaWiki\Cache\CacheKeyHelper\getKeyForPage().

Referenced by MediaWiki\Rest\Handler\SearchHandler\doSearch().

◆ buildResultFromPageInfos()

MediaWiki\Rest\Handler\SearchHandler::buildResultFromPageInfos ( array  $pageInfos,
array  $thumbsAndDesc 
)
private

Turn array of page info into serializable array with common information about the page.

Parameters
array$pageInfosPage Info objects
array$thumbsAndDescAssociative array mapping pageId to array of description and thumbnail @phpcs:ignore Generic.Files.LineLength

@phpcs:ignore Generic.Files.LineLength

Returns
array[] of [ id, key, title, excerpt, matched_title ]

Definition at line 288 of file SearchHandler.php.

References Page\PageIdentity\canExist().

◆ buildSinglePage()

MediaWiki\Rest\Handler\SearchHandler::buildSinglePage (   $title,
  $result 
)
private

Build one pageInfo object from either a SearchResult or SearchSuggestion.

Parameters
PageIdentity$title
SearchResult | SearchSuggestion$result@phpcs:ignore Generic.Files.LineLength
Returns
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

Definition at line 255 of file SearchHandler.php.

References $title, and MediaWiki\Rest\Handler\getAuthority().

Referenced by MediaWiki\Rest\Handler\SearchHandler\buildPageObjects().

◆ buildThumbnailsFromPageIdentities()

MediaWiki\Rest\Handler\SearchHandler::buildThumbnailsFromPageIdentities ( array  $pageIdentities)
private

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.

Parameters
PageIdentity[]$pageIdentities
Returns
array

Definition at line 365 of file SearchHandler.php.

◆ createSearchEngine()

MediaWiki\Rest\Handler\SearchHandler::createSearchEngine ( )
private
Returns
SearchEngine

Definition at line 132 of file SearchHandler.php.

References MediaWiki\Rest\Handler\getValidatedParams().

◆ doSearch()

MediaWiki\Rest\Handler\SearchHandler::doSearch (   $searchEngine)
private

Execute search and return info about pages for further processing.

Parameters
SearchEngine$searchEngine
Returns
array[]
Exceptions
LocalizedHttpException

Definition at line 182 of file SearchHandler.php.

References MediaWiki\Rest\Handler\SearchHandler\buildPageObjects(), MediaWiki\Rest\Handler\SearchHandler\getSearchResultsOrThrow(), and MediaWiki\Rest\Handler\getValidatedParams().

◆ execute()

MediaWiki\Rest\Handler\SearchHandler::execute ( )
Returns
Response
Exceptions
LocalizedHttpException

Reimplemented from MediaWiki\Rest\Handler.

Definition at line 379 of file SearchHandler.php.

References Page\PageIdentity\exists().

◆ getParamSettings()

MediaWiki\Rest\Handler\SearchHandler::getParamSettings ( )

Fetch ParamValidator settings for parameters.

Every setting must include self::PARAM_SOURCE to specify which part of the request is to contain the parameter.

Can be used for validating parameters inside an application/x-www-form-urlencoded or multipart/form-data POST body (i.e. parameters which would be present in PHP's $_POST array). For validating other kinds of request bodies, override getBodyValidator().

Stability: stable
to override
Returns
array[] Associative array mapping parameter names to ParamValidator settings arrays

Reimplemented from MediaWiki\Rest\Handler.

Definition at line 421 of file SearchHandler.php.

◆ getSearchResultsOrThrow()

MediaWiki\Rest\Handler\SearchHandler::getSearchResultsOrThrow (   $results)
private

Get SearchResults when results are either SearchResultSet or Status objects.

Parameters
ISearchResultSet | Status | null$results
Returns
SearchResult[]
Exceptions
LocalizedHttpException

Definition at line 151 of file SearchHandler.php.

Referenced by MediaWiki\Rest\Handler\SearchHandler\doSearch().

◆ needsWriteAccess()

MediaWiki\Rest\Handler\SearchHandler::needsWriteAccess ( )

Indicates whether this route requires write access.

The handler should override this if the route does not need to write to the database.

This should return true for routes that may require synchronous database writes. Modules that do not need such writes should also not rely on primary database access, since only read queries are needed and each primary DB is a single point of failure.

Stability: stable
to override
Returns
bool

Reimplemented from MediaWiki\Rest\Handler.

Definition at line 141 of file SearchHandler.php.

◆ postInitSetup()

MediaWiki\Rest\Handler\SearchHandler::postInitSetup ( )
protected

The handler can override this to do any necessary setup after init() is called to inject the dependencies.

Stability: stable
to override

Reimplemented from MediaWiki\Rest\Handler.

Definition at line 118 of file SearchHandler.php.

References MediaWiki\Rest\Handler\SearchHandler\FULLTEXT_MODE, and MediaWiki\Rest\Handler\getConfig().

◆ serializeThumbnail()

MediaWiki\Rest\Handler\SearchHandler::serializeThumbnail ( ?SearchResultThumbnail  $thumbnail)
private

Converts SearchResultThumbnail object into serializable array.

Parameters
SearchResultThumbnail | null$thumbnail
Returns
array|null

Definition at line 319 of file SearchHandler.php.

Member Data Documentation

◆ $completionCacheExpiry

int null MediaWiki\Rest\Handler\SearchHandler::$completionCacheExpiry
private

Expiry time for use as max-age value in the cache-control header of completion search responses.

See also
$wgSearchSuggestCacheExpiry

Definition at line 87 of file SearchHandler.php.

◆ $mode

string MediaWiki\Rest\Handler\SearchHandler::$mode = null
private

Definition at line 70 of file SearchHandler.php.

◆ $pageStore

PageStore MediaWiki\Rest\Handler\SearchHandler::$pageStore
private

Definition at line 47 of file SearchHandler.php.

Referenced by MediaWiki\Rest\Handler\SearchHandler\__construct().

◆ $permissionManager

PermissionManager MediaWiki\Rest\Handler\SearchHandler::$permissionManager
private

Definition at line 41 of file SearchHandler.php.

Referenced by MediaWiki\Rest\Handler\SearchHandler\__construct().

◆ $redirectLookup

RedirectLookup MediaWiki\Rest\Handler\SearchHandler::$redirectLookup
private

Definition at line 44 of file SearchHandler.php.

Referenced by MediaWiki\Rest\Handler\SearchHandler\__construct().

◆ $searchEngineConfig

SearchEngineConfig MediaWiki\Rest\Handler\SearchHandler::$searchEngineConfig
private

Definition at line 38 of file SearchHandler.php.

Referenced by MediaWiki\Rest\Handler\SearchHandler\__construct().

◆ $searchEngineFactory

SearchEngineFactory MediaWiki\Rest\Handler\SearchHandler::$searchEngineFactory
private

Definition at line 35 of file SearchHandler.php.

Referenced by MediaWiki\Rest\Handler\SearchHandler\__construct().

◆ $titleFormatter

TitleFormatter MediaWiki\Rest\Handler\SearchHandler::$titleFormatter
private

Definition at line 50 of file SearchHandler.php.

Referenced by MediaWiki\Rest\Handler\SearchHandler\__construct().

◆ COMPLETION_MODE

const MediaWiki\Rest\Handler\SearchHandler::COMPLETION_MODE = 'completion'

Search title completion matches.

Definition at line 60 of file SearchHandler.php.

◆ FULLTEXT_MODE

const MediaWiki\Rest\Handler\SearchHandler::FULLTEXT_MODE = 'fulltext'

Search page body and titles.

Definition at line 55 of file SearchHandler.php.

Referenced by MediaWiki\Rest\Handler\SearchHandler\postInitSetup().

◆ LIMIT

const MediaWiki\Rest\Handler\SearchHandler::LIMIT = 50
private

Limit results to 50 pages per default.

Definition at line 73 of file SearchHandler.php.

◆ MAX_LIMIT

const MediaWiki\Rest\Handler\SearchHandler::MAX_LIMIT = 100
private

Hard limit results to 100 pages.

Definition at line 76 of file SearchHandler.php.

◆ OFFSET

const MediaWiki\Rest\Handler\SearchHandler::OFFSET = 0
private

Default to first page.

Definition at line 79 of file SearchHandler.php.

◆ SUPPORTED_MODES

const MediaWiki\Rest\Handler\SearchHandler::SUPPORTED_MODES = [ self::FULLTEXT_MODE, self::COMPLETION_MODE ]
private

Supported modes.

Definition at line 65 of file SearchHandler.php.


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