Preferences: Disable the 'skin' preference if there are no skins
[lhc/web/wiklou.git] / includes / PrefixSearch.php
index 15d48cb..35be2a9 100644 (file)
@@ -29,7 +29,7 @@
 abstract class PrefixSearch {
        /**
         * Do a prefix search of titles and return a list of matching page names.
-        * @deprecated: Since 1.23, use TitlePrefixSearch or StringPrefixSearch classes
+        * @deprecated Since 1.23, use TitlePrefixSearch or StringPrefixSearch classes
         *
         * @param string $search
         * @param int $limit
@@ -51,33 +51,32 @@ abstract class PrefixSearch {
         */
        public function search( $search, $limit, $namespaces = array() ) {
                $search = trim( $search );
-               if ( $search === '' ) {
-                       return array();
+               if ( $search == '' ) {
+                       return array(); // Return empty result
                }
-
                $namespaces = $this->validateNamespaces( $namespaces );
 
-               // Is this a namespace prefix? Start listing all pages in it.
-               $title = Title::newFromText( $search . 'Dummy' );
-               if ( $title
-                       && $title->getText() === 'Dummy'
-                       && !$title->inNamespace( NS_MAIN )
-                       && !$title->isExternal()
-               ) {
-                       $this->searchBackend( array( $title->getNamespace() ), '', $limit );
+               // Find a Title which is not an interwiki and is in NS_MAIN
+               $title = Title::newFromText( $search );
+               if ( $title && !$title->isExternal() ) {
+                       $ns = array( $title->getNamespace() );
+                       if ( $ns[0] == NS_MAIN ) {
+                               $ns = $namespaces; // no explicit prefix, use default namespaces
+                       }
+                       return $this->searchBackend(
+                               $ns, $title->getText(), $limit );
                }
 
-               // Explicit namespace prefix? Limit search to that namespace.
-               $title = Title::newFromText( $search );
-               if ( $title
-                       && !$title->isExternal()
-                       && !$title->inNamespace( NS_MAIN )
-               {
-                       // This will convert first letter to uppercase if appropriate for the namespace
-                       $this->searchBackend( array( $title->getNamespace() ), $title->getText(), $limit );
+               // Is this a namespace prefix?
+               $title = Title::newFromText( $search . 'Dummy' );
+               if ( $title && $title->getText() == 'Dummy'
+                       && $title->getNamespace() != NS_MAIN
+                       && !$title->isExternal() )
+               {
+                       $namespaces = array( $title->getNamespace() );
+                       $search = '';
                }
 
-               // Search in all requested namespaces
                return $this->searchBackend( $namespaces, $search, $limit );
        }
 
@@ -167,10 +166,28 @@ abstract class PrefixSearch {
        protected function specialSearch( $search, $limit ) {
                global $wgContLang;
 
-               # normalize searchKey, so aliases with spaces can be found - bug 25675
-               $search = str_replace( ' ', '_', $search );
+               $searchParts = explode( '/', $search, 2 );
+               $searchKey = $searchParts[0];
+               $subpageSearch = isset( $searchParts[1] ) ? $searchParts[1] : null;
+
+               // Handle subpage search separately.
+               if ( $subpageSearch !== null ) {
+                       // Try matching the full search string as a page name
+                       $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
+                       $special = SpecialPageFactory::getPage( $specialTitle->getText() );
+                       if ( $special ) {
+                               $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit );
+                               return array_map( function ( $sub ) use ( $specialTitle ) {
+                                       return $specialTitle->getSubpage( $sub );
+                               }, $subpages );
+                       } else {
+                               return array();
+                       }
+               }
 
-               $searchKey = $wgContLang->caseFold( $search );
+               # normalize searchKey, so aliases with spaces can be found - bug 25675
+               $searchKey = str_replace( ' ', '_', $searchKey );
+               $searchKey = $wgContLang->caseFold( $searchKey );
 
                // Unlike SpecialPage itself, we want the canonical forms of both
                // canonical and alias title forms...
@@ -220,33 +237,28 @@ abstract class PrefixSearch {
         * @return array Array of Title objects
         */
        protected function defaultSearchBackend( $namespaces, $search, $limit ) {
-               $dbr = wfGetDB( DB_SLAVE );
-
-               // Construct suitable prefix for each namespace, they might differ
-               $prefixes = array();
-               foreach ( $namespaces as $ns ) {
-                       $title = Title::makeTitleSafe( $ns, $search );
-                       $prefix = $title ? $title->getDBkey() : '';
-                       $prefixes[$prefix][] = $ns;
-               }
-
-               $conds = array();
-               foreach ( $prefixes as $prefix => $nss ) {
-                       $conds[] = $dbr->makeList( array(
-                               'page_namespace' => $nss,
-                               'page_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
-                       ), LIST_AND );
+               $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
                }
 
+               $t = Title::newFromText( $search, $ns );
+               $prefix = $t ? $t->getDBkey() : '';
+               $dbr = wfGetDB( DB_SLAVE );
                $res = $dbr->select( 'page',
                        array( 'page_id', 'page_namespace', 'page_title' ),
-                       $dbr->makeList( $conds, LIST_OR ),
+                       array(
+                               'page_namespace' => $ns,
+                               'page_title ' . $dbr->buildLike( $prefix, $dbr->anyString() )
+                       ),
                        __METHOD__,
                        array( 'LIMIT' => $limit, 'ORDER BY' => 'page_title' )
                );
-
-               // Shorter than a loop, and doesn't break class api
-               return iterator_to_array( TitleArray::newFromResult( $res ) );
+               $srchres = array();
+               foreach ( $res as $row ) {
+                       $srchres[] = Title::newFromRow( $row );
+               }
+               return $srchres;
        }
 
        /**