Chad has submitted this change and it was merged.

Change subject: PrefixSearch (ApiOpenSearch) now supports searching in multiple 
namespaces
......................................................................


PrefixSearch (ApiOpenSearch) now supports searching in multiple namespaces

I thought there was just an issue with capitalization, but in fact the code
explicitly only searched one namespace anyway. Fixed that while taking
capitalization differences in namespaces into account.

Bug: 65752
Bug: 30323
Change-Id: I3487bb69eae1867832e0ec9ece497538eca32a2a
---
M includes/PrefixSearch.php
1 file changed, 41 insertions(+), 35 deletions(-)

Approvals:
  Chad: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/PrefixSearch.php b/includes/PrefixSearch.php
index a796d35..15d48cb 100644
--- a/includes/PrefixSearch.php
+++ b/includes/PrefixSearch.php
@@ -51,32 +51,33 @@
         */
        public function search( $search, $limit, $namespaces = array() ) {
                $search = trim( $search );
-               if ( $search == '' ) {
-                       return array(); // Return empty result
+               if ( $search === '' ) {
+                       return array();
                }
+
                $namespaces = $this->validateNamespaces( $namespaces );
 
-               // 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 );
-               }
-
-               // Is this a namespace prefix?
+               // Is this a namespace prefix? Start listing all pages in it.
                $title = Title::newFromText( $search . 'Dummy' );
-               if ( $title && $title->getText() == 'Dummy'
-                       && $title->getNamespace() != NS_MAIN
-                       && !$title->isExternal() )
-               {
-                       $namespaces = array( $title->getNamespace() );
-                       $search = '';
+               if ( $title
+                       && $title->getText() === 'Dummy'
+                       && !$title->inNamespace( NS_MAIN )
+                       && !$title->isExternal()
+               ) {
+                       $this->searchBackend( array( $title->getNamespace() ), 
'', $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 );
+               }
+
+               // Search in all requested namespaces
                return $this->searchBackend( $namespaces, $search, $limit );
        }
 
@@ -219,28 +220,33 @@
         * @return array Array of Title objects
         */
        protected 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
+               $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;
                }
 
-               $t = Title::newFromText( $search, $ns );
-               $prefix = $t ? $t->getDBkey() : '';
-               $dbr = wfGetDB( DB_SLAVE );
+               $conds = array();
+               foreach ( $prefixes as $prefix => $nss ) {
+                       $conds[] = $dbr->makeList( array(
+                               'page_namespace' => $nss,
+                               'page_title' . $dbr->buildLike( $prefix, 
$dbr->anyString() ),
+                       ), LIST_AND );
+               }
+
                $res = $dbr->select( 'page',
                        array( 'page_id', 'page_namespace', 'page_title' ),
-                       array(
-                               'page_namespace' => $ns,
-                               'page_title ' . $dbr->buildLike( $prefix, 
$dbr->anyString() )
-                       ),
+                       $dbr->makeList( $conds, LIST_OR ),
                        __METHOD__,
                        array( 'LIMIT' => $limit, 'ORDER BY' => 'page_title' )
                );
-               $srchres = array();
-               foreach ( $res as $row ) {
-                       $srchres[] = Title::newFromRow( $row );
-               }
-               return $srchres;
+
+               // Shorter than a loop, and doesn't break class api
+               return iterator_to_array( TitleArray::newFromResult( $res ) );
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/136796
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I3487bb69eae1867832e0ec9ece497538eca32a2a
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>
Gerrit-Reviewer: Brion VIBBER <[email protected]>
Gerrit-Reviewer: Chad <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to