From e421158b1adb642fa07c9dd33404555976a7bf7f Mon Sep 17 00:00:00 2001 From: Stanislav Malyshev Date: Tue, 29 Nov 2016 13:54:15 -0800 Subject: [PATCH] Rank aliases in search in order they appear in the messages file. This will ensure the local page name (which is the first alias) is prioritized. Bug: T151660 Change-Id: I3cc266024f5e052d20ff55c4b2c6cbd2945e3dd3 --- includes/PrefixSearch.php | 28 ++++++++++--------- tests/phpunit/includes/PrefixSearchTest.php | 6 ++-- .../search/SearchEnginePrefixTest.php | 6 ++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/includes/PrefixSearch.php b/includes/PrefixSearch.php index f6c4147118..04c17e4021 100644 --- a/includes/PrefixSearch.php +++ b/includes/PrefixSearch.php @@ -239,7 +239,7 @@ abstract class PrefixSearch { // canonical and alias title forms... $keys = []; foreach ( SpecialPageFactory::getNames() as $page ) { - $keys[$wgContLang->caseFold( $page )] = $page; + $keys[$wgContLang->caseFold( $page )] = [ 'page' => $page, 'rank' => 0 ]; } foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) { @@ -247,33 +247,35 @@ abstract class PrefixSearch { continue; } - foreach ( $aliases as $alias ) { - $keys[$wgContLang->caseFold( $alias )] = $alias; + foreach ( $aliases as $key => $alias ) { + $keys[$wgContLang->caseFold( $alias )] = [ 'page' => $alias, 'rank' => $key ]; } } ksort( $keys ); - $srchres = []; - $skipped = 0; + $matches = []; foreach ( $keys as $pageKey => $page ) { if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) { // 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' - if ( $offset > 0 && $skipped < $offset ) { - $skipped++; - continue; + $matches[$page['rank']][] = Title::makeTitleSafe( NS_SPECIAL, $page['page'] ); + + if ( isset( $matches[0] ) && count( $matches[0] ) >= $limit + $offset ) { + // We have enough items in primary rank, no use to continue + break; } - $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page ); } - if ( count( $srchres ) >= $limit ) { - break; - } } - return $srchres; + // Ensure keys are in order + ksort( $matches ); + // Flatten the array + $matches = array_reduce( $matches, 'array_merge', [] ); + + return array_slice( $matches, $offset, $limit ); } /** diff --git a/tests/phpunit/includes/PrefixSearchTest.php b/tests/phpunit/includes/PrefixSearchTest.php index bc43709fde..c5a7e04e30 100644 --- a/tests/phpunit/includes/PrefixSearchTest.php +++ b/tests/phpunit/includes/PrefixSearchTest.php @@ -129,11 +129,11 @@ class PrefixSearchTest extends MediaWikiLangTestCase { 'results' => [ 'Special:ActiveUsers', 'Special:AllMessages', - 'Special:AllMyFiles', + 'Special:AllMyUploads', ], // Third result when testing offset 'offsetresult' => [ - 'Special:AllMyUploads', + 'Special:AllPages', ], ] ], [ [ @@ -146,7 +146,7 @@ class PrefixSearchTest extends MediaWikiLangTestCase { ], // Third result when testing offset 'offsetresult' => [ - 'Special:UncategorizedImages', + 'Special:UncategorizedPages', ], ] ], [ [ diff --git a/tests/phpunit/includes/search/SearchEnginePrefixTest.php b/tests/phpunit/includes/search/SearchEnginePrefixTest.php index e0de58866f..a88264bb78 100644 --- a/tests/phpunit/includes/search/SearchEnginePrefixTest.php +++ b/tests/phpunit/includes/search/SearchEnginePrefixTest.php @@ -126,11 +126,11 @@ class SearchEnginePrefixTest extends MediaWikiLangTestCase { 'results' => [ 'Special:ActiveUsers', 'Special:AllMessages', - 'Special:AllMyFiles', + 'Special:AllMyUploads', ], // Third result when testing offset 'offsetresult' => [ - 'Special:AllMyUploads', + 'Special:AllPages', ], ] ], [ [ @@ -143,7 +143,7 @@ class SearchEnginePrefixTest extends MediaWikiLangTestCase { ], // Third result when testing offset 'offsetresult' => [ - 'Special:UncategorizedImages', + 'Special:UncategorizedPages', ], ] ], [ [ -- 2.20.1