Merge "Add Special:Search sort parameter without ui"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 21 Aug 2018 14:57:13 +0000 (14:57 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 21 Aug 2018 14:57:13 +0000 (14:57 +0000)
1  2 
includes/search/SearchEngine.php
includes/specials/SpecialSearch.php

@@@ -32,6 -32,8 +32,8 @@@ use MediaWiki\MediaWikiServices
   * @ingroup Search
   */
  abstract class SearchEngine {
+       const DEFAULT_SORT = 'relevance';
        /** @var string */
        public $prefix = '';
  
@@@ -49,7 -51,7 +51,7 @@@
  
        /** @var bool */
        protected $showSuggestion = true;
-       private $sort = 'relevance';
+       private $sort = self::DEFAULT_SORT;
  
        /** @var array Feature values */
        protected $features = [];
         * @return string
         */
        public function normalizeText( $string ) {
 -              global $wgContLang;
 -
                // Some languages such as Chinese require word segmentation
 -              return $wgContLang->segmentByWord( $string );
 +              return MediaWikiServices::getInstance()->getContentLanguage()->segmentByWord( $string );
        }
  
        /**
         * @return SearchNearMatcher
         */
        public function getNearMatcher( Config $config ) {
 -              global $wgContLang;
 -              return new SearchNearMatcher( $config, $wgContLang );
 +              return new SearchNearMatcher( $config,
 +                      MediaWikiServices::getInstance()->getContentLanguage() );
        }
  
        /**
         * @return SearchNearMatcher
         */
        protected static function defaultNearMatcher() {
 -              $config = MediaWikiServices::getInstance()->getMainConfig();
 -              return MediaWikiServices::getInstance()->newSearchEngine()->getNearMatcher( $config );
 +              $services = MediaWikiServices::getInstance();
 +              $config = $services->getMainConfig();
 +              return $services->newSearchEngine()->getNearMatcher( $config );
        }
  
        /**
  
        /**
         * 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 ];
        }
  
        /**
                $withAllKeyword = true,
                $withPrefixSearchExtractNamespaceHook = false
        ) {
 -              global $wgContLang;
 -
                $parsed = $query;
                if ( strpos( $query, ':' ) === false ) { // nothing to do
                        return false;
  
                if ( !$allQuery && strpos( $query, ':' ) !== false ) {
                        $prefix = str_replace( ' ', '_', substr( $query, 0, strpos( $query, ':' ) ) );
 -                      $index = $wgContLang->getNsIndex( $prefix );
 +                      $index = MediaWikiServices::getInstance()->getContentLanguage()->getNsIndex( $prefix );
                        if ( $index !== false ) {
                                $extractedNamespace = [ $index ];
                                $parsed = substr( $query, strlen( $prefix ) + 1 );
                $results = $this->completionSearchBackendOverfetch( $search );
                $fallbackLimit = 1 + $this->limit - $results->getSize();
                if ( $fallbackLimit > 0 ) {
 -                      global $wgContLang;
 -
 -                      $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
 +                      $fallbackSearches = MediaWikiServices::getInstance()->getContentLanguage()->
 +                              autoConvertToAllVariants( $search );
                        $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
  
                        foreach ( $fallbackSearches as $fbs ) {
@@@ -76,6 -76,11 +76,11 @@@ class SpecialSearch extends SpecialPag
         */
        protected $fulltext;
  
+       /**
+        * @var string
+        */
+       protected $sort;
        /**
         * @var bool
         */
                        $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
         * @param string $term
         */
        public function showResults( $term ) {
 -              global $wgContLang;
 -
                if ( $this->searchEngineType !== null ) {
                        $this->setExtraParam( 'srbackend', $this->searchEngineType );
                }
                        $this->searchConfig,
                        $this->getSearchProfiles()
                );
 -              $filePrefix = $wgContLang->getFormattedNsText( NS_FILE ) . ':';
 +              $filePrefix = MediaWikiServices::getInstance()->getContentLanguage()->
 +                      getFormattedNsText( NS_FILE ) . ':';
                if ( trim( $term ) === '' || $filePrefix === trim( $term ) ) {
                        // Empty query -- straight view of search form
                        if ( !Hooks::run( 'SpecialSearchResultsPrepend', [ $this, $out, $term ] ) ) {
                $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 ] );
         */
        public function getSearchEngine() {
                if ( $this->searchEngine === null ) {
 +                      $services = MediaWikiServices::getInstance();
                        $this->searchEngine = $this->searchEngineType ?
 -                              MediaWikiServices::getInstance()->getSearchEngineFactory()->create( $this->searchEngineType ) :
 -                              MediaWikiServices::getInstance()->newSearchEngine();
 +                              $services->getSearchEngineFactory()->create( $this->searchEngineType ) :
 +                              $services->newSearchEngine();
                }
  
                return $this->searchEngine;