Merge "PrefixSearch: Enforce including the exact match as first result"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 10 Oct 2014 21:34:23 +0000 (21:34 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 10 Oct 2014 21:34:23 +0000 (21:34 +0000)
includes/PrefixSearch.php
tests/phpunit/includes/PrefixSearchTest.php

index 36196f2..fe78e23 100644 (file)
@@ -151,7 +151,31 @@ abstract class PrefixSearch {
                $srchres = array();
                if ( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
                        return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit ) );
+               } else {
+                       // Default search backend does proper prefix searching, but custom backends
+                       // may sort based on other algorythms that may cause the exact title match
+                       // to not be in the results or be lower down the list.
+
+                       // Pick namespace (based on PrefixSearch::defaultSearchBackend)
+                       $ns = in_array( NS_MAIN, $namespaces ) ? NS_MAIN : $namespaces[0];
+                       $t = Title::newFromText( $search, $ns );
+                       $string = $t->getPrefixedText();
+
+                       $key = array_search( $string, $srchres );
+                       if ( $key !== false ) {
+                               // Move it to the front
+                               $cut = array_splice( $srchres, $key, 1 );
+                               array_unshift( $srchres, $cut[0] );
+                       } elseif ( $t->exists() ) {
+                               // Add it in front
+                               array_unshift( $srchres, $string );
+
+                               if ( count( $srchres ) > $limit ) {
+                                       array_pop( $srchres );
+                               }
+                       }
                }
+
                return $this->strings( $srchres );
        }
 
index 63dcc3f..5390dba 100644 (file)
@@ -29,7 +29,7 @@ class PrefixSearchTest extends MediaWikiTestCase {
 
        public function addDBData() {
                $this->insertPage( 'Sandbox' );
-
+               $this->insertPage( 'Bar' );
                $this->insertPage( 'Example' );
                $this->insertPage( 'Example Bar' );
                $this->insertPage( 'Example Foo' );
@@ -131,4 +131,80 @@ class PrefixSearchTest extends MediaWikiTestCase {
                        $case[0]
                );
        }
+
+       public static function provideSearchBackend() {
+               return array(
+                       array( array(
+                               'Simple case',
+                               'provision' => array(
+                                       'Bar',
+                                       'Barcelona',
+                                       'Barbara',
+                               ),
+                               'query' => 'Bar',
+                               'results' => array(
+                                       'Bar',
+                                       'Barcelona',
+                                       'Barbara',
+                               ),
+                       ) ),
+                       array( array(
+                               'Exact match not on top (bug 70958)',
+                               'provision' => array(
+                                       'Barcelona',
+                                       'Bar',
+                                       'Barbara',
+                               ),
+                               'query' => 'Bar',
+                               'results' => array(
+                                       'Bar',
+                                       'Barcelona',
+                                       'Barbara',
+                               ),
+                       ) ),
+                       array( array(
+                               'Exact match missing (bug 70958)',
+                               'provision' => array(
+                                       'Barcelona',
+                                       'Barbara',
+                                       'Bart',
+                               ),
+                               'query' => 'Bar',
+                               'results' => array(
+                                       'Bar',
+                                       'Barcelona',
+                                       'Barbara',
+                               ),
+                       ) ),
+                       array( array(
+                               'Exact match missing and not existing',
+                               'provision' => array(
+                                       'Exile',
+                                       'Exist',
+                                       'External',
+                               ),
+                               'query' => 'Ex',
+                               'results' => array(
+                                       'Exile',
+                                       'Exist',
+                                       'External',
+                               ),
+                       ) ),
+               );
+       }
+
+       /**
+        * @dataProvider provideSearchBackend
+        * @covers PrefixSearch::searchBackend
+        */
+       public function testSearchBackend( Array $case ) {
+               $this->searchProvision( $case['provision'] );
+               $searcher = new StringPrefixSearch;
+               $results = $searcher->search( $case['query'], 3 );
+               $this->assertEquals(
+                       $case['results'],
+                       $results,
+                       $case[0]
+               );
+       }
 }