Add a sort parameter to SearchEngine
[lhc/web/wiklou.git] / 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