From c6a83871b59d2bfb6a8c77160b37fbfb2ca8edd6 Mon Sep 17 00:00:00 2001 From: Erik Bernhardson Date: Wed, 1 Aug 2018 19:45:02 -0700 Subject: [PATCH] Add Special:Search sort parameter without ui Search engines support the sort parameter, it would be nice if we could pass it to be rendered in the web ui. This avoids implementing any ui, which can be done at a later time. Bug: T195071 Change-Id: Id7bd35bd4259bb1f1f856933d279dbd6eddfa2e2 --- includes/search/SearchEngine.php | 8 +++++--- includes/specials/SpecialSearch.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/includes/search/SearchEngine.php b/includes/search/SearchEngine.php index 30c2271916..99bdabea7c 100644 --- a/includes/search/SearchEngine.php +++ b/includes/search/SearchEngine.php @@ -32,6 +32,8 @@ use MediaWiki\MediaWikiServices; * @ingroup Search */ abstract class SearchEngine { + const DEFAULT_SORT = 'relevance'; + /** @var string */ public $prefix = ''; @@ -49,7 +51,7 @@ abstract class SearchEngine { /** @var bool */ protected $showSuggestion = true; - private $sort = 'relevance'; + private $sort = self::DEFAULT_SORT; /** @var array Feature values */ protected $features = []; @@ -347,13 +349,13 @@ abstract class SearchEngine { /** * Get the valid sort directions. All search engines support 'relevance' but others - * might support more. The default in all implementations should be 'relevance.' + * might support more. The default in all implementations must be 'relevance.' * * @since 1.25 * @return string[] the valid sort directions for setSort */ public function getValidSorts() { - return [ 'relevance' ]; + return [ self::DEFAULT_SORT ]; } /** diff --git a/includes/specials/SpecialSearch.php b/includes/specials/SpecialSearch.php index 2cff90e396..f595b19172 100644 --- a/includes/specials/SpecialSearch.php +++ b/includes/specials/SpecialSearch.php @@ -76,6 +76,11 @@ class SpecialSearch extends SpecialPage { */ protected $fulltext; + /** + * @var string + */ + protected $sort; + /** * @var bool */ @@ -198,6 +203,11 @@ class SpecialSearch extends SpecialPage { $this->setExtraParam( 'prefix', $this->mPrefix ); } + $this->sort = $request->getVal( 'sort', SearchEngine::DEFAULT_SORT ); + if ( $this->sort !== SearchEngine::DEFAULT_SORT ) { + $this->setExtraParam( 'sort', $this->sort ); + } + $user = $this->getUser(); # Extract manually requested namespaces @@ -302,6 +312,7 @@ class SpecialSearch extends SpecialPage { $search->setFeatureData( 'rewrite', $this->runSuggestion ); $search->setLimitOffset( $this->limit, $this->offset ); $search->setNamespaces( $this->namespaces ); + $search->setSort( $this->sort ); $search->prefix = $this->mPrefix; Hooks::run( 'SpecialSearchSetupEngine', [ $this, $this->profile, $search ] ); -- 2.20.1