]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/PrefixSearch.php
MediaWiki 1.17.0
[autoinstallsdev/mediawiki.git] / includes / PrefixSearch.php
index af5691125f5167089cf64a96aa0dd7d8d0334ed4..236e4370993f1dc16e87ddb2f64183b2c278855e 100644 (file)
@@ -1,19 +1,19 @@
 <?php
-
 /**
  * PrefixSearch - Handles searching prefixes of titles and finding any page
  * names that match. Used largely by the OpenSearch implementation.
- * 
+ *
  * @ingroup Search
  */
 
 class PrefixSearch {
        /**
         * Do a prefix search of titles and return a list of matching page names.
-        * @param string $search
-        * @param int $limit
-        * @param array $namespaces - used if query is not explicitely prefixed
-        * @return array of strings
+        *
+        * @param $search String
+        * @param $limit Integer
+        * @param $namespaces Array: used if query is not explicitely prefixed
+        * @return Array of strings
         */
        public static function titleSearch( $search, $limit, $namespaces=array() ) {
                $search = trim( $search );
@@ -21,11 +21,12 @@ class PrefixSearch {
                        return array(); // Return empty result
                }
                $namespaces = self::validateNamespaces( $namespaces );
-               
+
+               // Find a Title which is not an interwiki and is in NS_MAIN
                $title = Title::newFromText( $search );
                if( $title && $title->getInterwiki() == '' ) {
                        $ns = array($title->getNamespace());
-                       if($ns[0] == NS_MAIN) 
+                       if($ns[0] == NS_MAIN)
                                $ns = $namespaces; // no explicit prefix, use default namespaces
                        return self::searchBackend(
                                $ns, $title->getText(), $limit );
@@ -39,17 +40,17 @@ class PrefixSearch {
                        return self::searchBackend(
                                array($title->getNamespace()), '', $limit );
                }
-                               
+
                return self::searchBackend( $namespaces, $search, $limit );
        }
 
 
        /**
         * Do a prefix search of titles and return a list of matching page names.
-        * @param array $namespaces
-        * @param string $search
-        * @param int $limit
-        * @return array of strings
+        * @param $namespaces Array
+        * @param $search String
+        * @param $limit Integer
+        * @return Array of strings
         */
        protected static function searchBackend( $namespaces, $search, $limit ) {
                if( count($namespaces) == 1 ){
@@ -69,9 +70,17 @@ class PrefixSearch {
 
        /**
         * Prefix search special-case for Special: namespace.
+        *
+        * @param $search String: term
+        * @param $limit Integer: max number of items to return
+        * @return Array
         */
        protected static function specialSearch( $search, $limit ) {
                global $wgContLang;
+
+               # normalize searchKey, so aliases with spaces can be found - bug 25675
+               $search = str_replace( ' ', '_', $search );
+
                $searchKey = $wgContLang->caseFold( $search );
 
                // Unlike SpecialPage itself, we want the canonical forms of both
@@ -82,7 +91,12 @@ class PrefixSearch {
                foreach( array_keys( SpecialPage::$mList ) as $page ) {
                        $keys[$wgContLang->caseFold( $page )] = $page;
                }
+
                foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
+                       if( !array_key_exists( $page, SpecialPage::$mList ) ) {# bug 20885
+                               continue;
+                       }
+
                        foreach( $aliases as $alias ) {
                                $keys[$wgContLang->caseFold( $alias )] = $alias;
                        }
@@ -92,12 +106,20 @@ class PrefixSearch {
                $srchres = array();
                foreach( $keys as $pageKey => $page ) {
                        if( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
-                               $srchres[] = Title::makeTitle( NS_SPECIAL, $page )->getPrefixedText();
+                               wfSuppressWarnings();
+                               // bug 27671: Don't use SpecialPage::getTitleFor() here because it
+                               // localizes its input leading to searches for e.g. Special:All
+                               // returning Spezial:MediaWiki-Systemnachrichten and returning
+                               // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
+                               $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page )->getPrefixedText();
+                               wfRestoreWarnings();
                        }
+
                        if( count( $srchres ) >= $limit ) {
                                break;
                        }
                }
+
                return $srchres;
        }
 
@@ -107,16 +129,16 @@ class PrefixSearch {
         * be automatically capitalized by Title::secureAndSpit()
         * later on depending on $wgCapitalLinks)
         *
-        * @param array $namespaces Namespaces to search in
-        * @param string $search term
-        * @param int $limit max number of items to return
-        * @return array of title strings
+        * @param $namespaces Array: namespaces to search in
+        * @param $search String: term
+        * @param $limit Integer: max number of items to return
+        * @return Array of title strings
         */
        protected static function defaultSearchBackend( $namespaces, $search, $limit ) {
                $ns = array_shift($namespaces); // support only one namespace
                if( in_array(NS_MAIN,$namespaces))
-                       $ns = NS_MAIN; // if searching on many always default to main 
-               
+                       $ns = NS_MAIN; // if searching on many always default to main
+
                // Prepare nested request
                $req = new FauxRequest(array (
                        'action' => 'query',
@@ -135,7 +157,7 @@ class PrefixSearch {
 
                // Reformat useful data for future printing by JSON engine
                $srchres = array ();
-               foreach ($data['query']['allpages'] as & $pageinfo) {
+               foreach ((array)$data['query']['allpages'] as $pageinfo) {
                        // Note: this data will no be printable by the xml engine
                        // because it does not support lists of unnamed items
                        $srchres[] = $pageinfo['title'];
@@ -143,14 +165,17 @@ class PrefixSearch {
 
                return $srchres;
        }
-       
+
        /**
         * Validate an array of numerical namespace indexes
-        * 
-        * @param array $namespaces
+        *
+        * @param $namespaces Array
+        * @return Array (default: contains only NS_MAIN)
         */
        protected static function validateNamespaces($namespaces){
                global $wgContLang;
+
+               // We will look at each given namespace against wgContLang namespaces
                $validNamespaces = $wgContLang->getNamespaces();
                if( is_array($namespaces) && count($namespaces)>0 ){
                        $valid = array();
@@ -158,10 +183,11 @@ class PrefixSearch {
                                if( is_numeric($ns) && array_key_exists($ns, $validNamespaces) )
                                        $valid[] = $ns;
                        }
-                       if( count($valid) > 0 )
+                       if( count($valid) > 0 ) {
                                return $valid;
+                       }
                }
-               
+
                return array( NS_MAIN );
        }
 }