From 74b1c0d2563ff3e5d3de53b6bf6ee5d08a29fba1 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Fri, 24 Jan 2014 00:15:16 -0800 Subject: [PATCH] Add a sort parameter to SearchEngine SearchEngine grows a method to list valid sort orders one to set the sort for the next search, and one to read it. The default implemenation only supports 'relevance' and the documenation strongly urges that all other implemenations leave that as the default. Other implementations can support other orders. Cirrus already supports title_asc and title_desc. Change-Id: Ie946150c6796139201221dfa6f7750c210e97166 --- includes/search/SearchEngine.php | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/includes/search/SearchEngine.php b/includes/search/SearchEngine.php index cd6cf7dd70..5770276ac6 100644 --- a/includes/search/SearchEngine.php +++ b/includes/search/SearchEngine.php @@ -47,6 +47,7 @@ class SearchEngine { /** @var bool */ protected $showSuggestion = true; + private $sort = 'relevance'; /** @var array Feature values */ protected $features = array(); @@ -309,6 +310,43 @@ class SearchEngine { $this->showSuggestion = $showSuggestion; } + /** + * 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 + * @return array(string) the valid sort directions for setSort + */ + public function getValidSorts() { + return array( 'relevance' ); + } + + /** + * Set the sort direction of the search results. Must be one returned by + * SearchEngine::getValidSorts() + * + * @since 1.25 + * @throws InvalidArgumentException + * @param string $sort sort direction for query result + */ + public function setSort( $sort ) { + if ( !in_array( $sort, $this->getValidSorts() ) ) { + throw new InvalidArgumentException( "Invalid sort: $sort. " . + "Must be one of: " . implode( ', ', $this->getValidSorts() ) ); + } + $this->sort = $sort; + } + + /** + * Get the sort direction of the search results + * + * @since 1.25 + * @return string + */ + public function getSort() { + return $this->sort; + } + /** * Parse some common prefixes: all (search everything) * or namespace names -- 2.20.1