Add Special:Search sort parameter without ui
authorErik Bernhardson <ebernhardson@wikimedia.org>
Thu, 2 Aug 2018 02:45:02 +0000 (19:45 -0700)
committerErik Bernhardson <ebernhardson@wikimedia.org>
Fri, 17 Aug 2018 00:12:05 +0000 (17:12 -0700)
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
includes/specials/SpecialSearch.php

index 30c2271..99bdabe 100644 (file)
@@ -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 ];
        }
 
        /**
index 2cff90e..f595b19 100644 (file)
@@ -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 ] );