Add a sort parameter to SearchEngine
authorNik Everett <neverett@wikimedia.org>
Fri, 24 Jan 2014 08:15:16 +0000 (00:15 -0800)
committerChad Horohoe <chadh@wikimedia.org>
Wed, 7 Jan 2015 21:41:18 +0000 (13:41 -0800)
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

index cd6cf7d..5770276 100644 (file)
@@ -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