Merge "Salt the "nsToken" used for Special:Search namespace remembering"
[lhc/web/wiklou.git] / includes / search / SearchEngine.php
index a96ade0..56bb0ac 100644 (file)
  * @ingroup Search
  */
 class SearchEngine {
-       var $limit = 10;
-       var $offset = 0;
-       var $prefix = '';
-       var $searchTerms = array();
-       var $namespaces = array( NS_MAIN );
-       var $showRedirects = false;
+       /** @var string */
+       public $prefix = '';
+
+       /** @var int[] */
+       public $namespaces = array( NS_MAIN );
+
+       /** @var int */
+       protected $limit = 10;
+
+       /** @var int */
+       protected $offset = 0;
+
+       /** @var array|string */
+       protected $searchTerms = array();
+
+       /** @var bool */
        protected $showSuggestion = true;
 
-       /// Feature values
+       /** @var array Feature values */
        protected $features = array();
 
        /**
@@ -46,7 +56,7 @@ class SearchEngine {
         * If title searches are not supported or disabled, return null.
         * STUB
         *
-        * @param string $term raw search term
+        * @param string $term Raw search term
         * @return SearchResultSet|Status|null
         */
        function searchText( $term ) {
@@ -58,7 +68,7 @@ class SearchEngine {
         * If title searches are not supported or disabled, return null.
         * STUB
         *
-        * @param string $term raw search term
+        * @param string $term Raw search term
         * @return SearchResultSet|null
         */
        function searchTitle( $term ) {
@@ -67,12 +77,11 @@ class SearchEngine {
 
        /**
         * @since 1.18
-        * @param $feature String
-        * @return Boolean
+        * @param string $feature
+        * @return bool
         */
        public function supports( $feature ) {
                switch ( $feature ) {
-               case 'list-redirects':
                case 'search-update':
                        return true;
                case 'title-suffix-filter':
@@ -84,8 +93,8 @@ class SearchEngine {
        /**
         * Way to pass custom data for engines
         * @since 1.18
-        * @param $feature String
-        * @param $data Mixed
+        * @param string $feature
+        * @param mixed $data
         * @return bool
         */
        public function setFeatureData( $feature, $data ) {
@@ -108,8 +117,9 @@ class SearchEngine {
        }
 
        /**
-        * Transform search term in cases when parts of the query came as different GET params (when supported)
-        * e.g. for prefix queries: search=test&prefix=Main_Page/Archive -> test prefix:Main Page/Archive
+        * Transform search term in cases when parts of the query came as different
+        * GET params (when supported), e.g. for prefix queries:
+        * search=test&prefix=Main_Page/Archive -> test prefix:Main Page/Archive
         */
        function transformSearchTerm( $term ) {
                return $term;
@@ -119,7 +129,7 @@ class SearchEngine {
         * If an exact title match can be found, or a very slightly close match,
         * return the title. If no match, returns NULL.
         *
-        * @param $searchterm String
+        * @param string $searchterm
         * @return Title
         */
        public static function getNearMatch( $searchterm ) {
@@ -133,7 +143,7 @@ class SearchEngine {
         * Do a near match (see SearchEngine::getNearMatch) and wrap it into a
         * SearchResultSet.
         *
-        * @param $searchterm string
+        * @param string $searchterm
         * @return SearchResultSet
         */
        public static function getNearMatchResultSet( $searchterm ) {
@@ -150,7 +160,10 @@ class SearchEngine {
                $allSearchTerms = array( $searchterm );
 
                if ( $wgContLang->hasVariants() ) {
-                       $allSearchTerms = array_merge( $allSearchTerms, $wgContLang->autoConvertToAllVariants( $searchterm ) );
+                       $allSearchTerms = array_merge(
+                               $allSearchTerms,
+                               $wgContLang->autoConvertToAllVariants( $searchterm )
+                       );
                }
 
                $titleResult = null;
@@ -264,8 +277,8 @@ class SearchEngine {
         * Set the maximum number of results to return
         * and how many to skip before returning the first.
         *
-        * @param $limit Integer
-        * @param $offset Integer
+        * @param int $limit
+        * @param int $offset
         */
        function setLimitOffset( $limit, $offset = 0 ) {
                $this->limit = intval( $limit );
@@ -276,7 +289,7 @@ class SearchEngine {
         * Set which namespaces the search should include.
         * Give an array of namespace index numbers.
         *
-        * @param $namespaces Array
+        * @param array $namespaces
         */
        function setNamespaces( $namespaces ) {
                $this->namespaces = $namespaces;
@@ -287,7 +300,7 @@ class SearchEngine {
         * don't support building a suggestion in the first place and others don't respect
         * this flag.
         *
-        * @param boolean $showSuggestion should the searcher try to build suggestions
+        * @param bool $showSuggestion Should the searcher try to build suggestions
         */
        function setShowSuggestion( $showSuggestion ) {
                $this->showSuggestion = $showSuggestion;
@@ -297,7 +310,7 @@ class SearchEngine {
         * Parse some common prefixes: all (search everything)
         * or namespace names
         *
-        * @param $query String
+        * @param string $query
         * @return string
         */
        function replacePrefixes( $query ) {
@@ -305,7 +318,6 @@ class SearchEngine {
 
                $parsed = $query;
                if ( strpos( $query, ':' ) === false ) { // nothing to do
-                       wfRunHooks( 'SearchEngineReplacePrefixesComplete', array( $this, $query, &$parsed ) );
                        return $parsed;
                }
 
@@ -325,14 +337,12 @@ class SearchEngine {
                        $parsed = $query; // prefix was the whole query
                }
 
-               wfRunHooks( 'SearchEngineReplacePrefixesComplete', array( $this, $query, &$parsed ) );
-
                return $parsed;
        }
 
        /**
         * Make a list of searchable namespaces and their canonical names.
-        * @return Array
+        * @return array
         */
        public static function searchableNamespaces() {
                global $wgContLang;
@@ -351,24 +361,12 @@ class SearchEngine {
         * Extract default namespaces to search from the given user's
         * settings, returning a list of index numbers.
         *
-        * @param $user User
-        * @return Array
+        * @param user $user
+        * @return array
         */
        public static function userNamespaces( $user ) {
-               global $wgSearchEverythingOnlyLoggedIn;
-
-               $searchableNamespaces = SearchEngine::searchableNamespaces();
-
-               // get search everything preference, that can be set to be read for logged-in users
-               // it overrides other options
-               if ( !$wgSearchEverythingOnlyLoggedIn || $user->isLoggedIn() ) {
-                       if ( $user->getOption( 'searcheverything' ) ) {
-                               return array_keys( $searchableNamespaces );
-                       }
-               }
-
                $arr = array();
-               foreach ( $searchableNamespaces as $ns => $name ) {
+               foreach ( SearchEngine::searchableNamespaces() as $ns => $name ) {
                        if ( $user->getOption( 'searchNs' . $ns ) ) {
                                $arr[] = $ns;
                        }
@@ -380,7 +378,7 @@ class SearchEngine {
        /**
         * Find snippet highlight settings for all users
         *
-        * @return Array contextlines, contextchars
+        * @return array Contextlines, contextchars
         */
        public static function userHighlightPrefs() {
                $contextlines = 2; // Hardcode this. Old defaults sucked. :)
@@ -391,7 +389,7 @@ class SearchEngine {
        /**
         * An array of namespaces indexes to be searched by default
         *
-        * @return Array
+        * @return array
         */
        public static function defaultNamespaces() {
                global $wgNamespacesToBeSearchedDefault;
@@ -403,7 +401,7 @@ class SearchEngine {
         * Get a list of namespace names useful for showing in tooltips
         * and preferences
         *
-        * @param $namespaces Array
+        * @param array $namespaces
         * @return array
         */
        public static function namespacesAsText( $namespaces ) {
@@ -418,33 +416,11 @@ class SearchEngine {
                return $formatted;
        }
 
-       /**
-        * Return the help namespaces to be shown on Special:Search
-        *
-        * @return Array
-        */
-       public static function helpNamespaces() {
-               global $wgNamespacesToBeSearchedHelp;
-
-               return array_keys( $wgNamespacesToBeSearchedHelp, true );
-       }
-
-       /**
-        * Return a 'cleaned up' search string
-        *
-        * @param $text String
-        * @return String
-        */
-       function filter( $text ) {
-               $lc = $this->legalSearchChars();
-               return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
-       }
-
        /**
         * Load up the appropriate search engine class for the currently
         * active database backend, and return a configured instance.
         *
-        * @param String $type Type of search backend, if not the default
+        * @param string $type Type of search backend, if not the default
         * @return SearchEngine
         */
        public static function create( $type = null ) {
@@ -486,9 +462,9 @@ class SearchEngine {
         * Title and text should be pre-processed.
         * STUB
         *
-        * @param $id Integer
-        * @param $title String
-        * @param $text String
+        * @param int $id
+        * @param string $title
+        * @param string $text
         */
        function update( $id, $title, $text ) {
                // no-op
@@ -499,8 +475,8 @@ class SearchEngine {
         * Title should be pre-processed.
         * STUB
         *
-        * @param $id Integer
-        * @param $title String
+        * @param int $id
+        * @param string $title
         */
        function updateTitle( $id, $title ) {
                // no-op
@@ -511,8 +487,8 @@ class SearchEngine {
         * Title should be pre-processed.
         * STUB
         *
-        * @param Integer $id Page id that was deleted
-        * @param String $title Title of page that was deleted
+        * @param int $id Page id that was deleted
+        * @param string $title Title of page that was deleted
         */
        function delete( $id, $title ) {
                // no-op
@@ -521,10 +497,11 @@ class SearchEngine {
        /**
         * Get OpenSearch suggestion template
         *
-        * @return String
+        * @return string
         */
        public static function getOpenSearchTemplate() {
                global $wgOpenSearchTemplate, $wgCanonicalServer;
+
                if ( $wgOpenSearchTemplate ) {
                        return $wgOpenSearchTemplate;
                } else {
@@ -532,7 +509,9 @@ class SearchEngine {
                        if ( !$ns ) {
                                $ns = "0";
                        }
-                       return $wgCanonicalServer . wfScript( 'api' ) . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
+
+                       return $wgCanonicalServer . wfScript( 'api' )
+                               . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
                }
        }
 
@@ -562,13 +541,6 @@ class SearchEngine {
        }
 }
 
-/**
- * @ingroup Search
- */
-class SearchResultTooMany {
-       # # Some search engines may bail out if too many matches are found
-}
-
 /**
  * Dummy class to be used when non-supported Database engine is present.
  * @todo FIXME: Dummy class should probably try something at least mildly useful,