X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FPrefixSearch.php;h=f6c4147118021d7ffa0dac37713a57394606db2e;hb=2f5ee9518857b4384518a283ff2d963145a8b119;hp=5f36cf507dd580f58ed6d6bd2754e83078c64774;hpb=04ca989e86ff82b2a2336f91b0c19d72a592ba18;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/PrefixSearch.php b/includes/PrefixSearch.php index 5f36cf507d..f6c4147118 100644 --- a/includes/PrefixSearch.php +++ b/includes/PrefixSearch.php @@ -38,7 +38,7 @@ abstract class PrefixSearch { * @param int $offset How many results to offset from the beginning * @return array Array of strings */ - public static function titleSearch( $search, $limit, $namespaces = array(), $offset = 0 ) { + public static function titleSearch( $search, $limit, $namespaces = [], $offset = 0 ) { $prefixSearch = new StringPrefixSearch; return $prefixSearch->search( $search, $limit, $namespaces, $offset ); } @@ -52,40 +52,60 @@ abstract class PrefixSearch { * @param int $offset How many results to offset from the beginning * @return array Array of strings or Title objects */ - public function search( $search, $limit, $namespaces = array(), $offset = 0 ) { + public function search( $search, $limit, $namespaces = [], $offset = 0 ) { $search = trim( $search ); if ( $search == '' ) { - return array(); // Return empty result - } - $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() ); - $search = $title->getText(); - if ( $ns[0] == NS_MAIN ) { - $ns = $namespaces; // no explicit prefix, use default namespaces - Hooks::run( 'PrefixSearchExtractNamespace', array( &$ns, &$search ) ); - } - return $this->searchBackend( $ns, $search, $limit, $offset ); + return []; // Return empty result } - // 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 = ''; + $hasNamespace = $this->extractNamespace( $search ); + if ( $hasNamespace ) { + list( $namespace, $search ) = $hasNamespace; + $namespaces = [ $namespace ]; } else { - Hooks::run( 'PrefixSearchExtractNamespace', array( &$namespaces, &$search ) ); + $namespaces = $this->validateNamespaces( $namespaces ); + Hooks::run( 'PrefixSearchExtractNamespace', [ &$namespaces, &$search ] ); } return $this->searchBackend( $namespaces, $search, $limit, $offset ); } + /** + * Figure out if given input contains an explicit namespace. + * + * @param string $input + * @return false|array Array of namespace and remaining text, or false if no namespace given. + */ + protected function extractNamespace( $input ) { + if ( strpos( $input, ':' ) === false ) { + return false; + } + + // Namespace prefix only + $title = Title::newFromText( $input . 'Dummy' ); + if ( + $title && + $title->getText() === 'Dummy' && + !$title->inNamespace( NS_MAIN ) && + !$title->isExternal() + ) { + return [ $title->getNamespace(), '' ]; + } + + // Namespace prefix with additional input + $title = Title::newFromText( $input ); + if ( + $title && + !$title->inNamespace( NS_MAIN ) && + !$title->isExternal() + ) { + // getText provides correct capitalization + return [ $title->getNamespace(), $title->getText() ]; + } + + return false; + } + /** * Do a prefix search for all possible variants of the prefix * @param string $search @@ -104,7 +124,7 @@ abstract class PrefixSearch { global $wgContLang; $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search ); - $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) ); + $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] ); foreach ( $fallbackSearches as $fbs ) { $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces ); @@ -150,24 +170,32 @@ abstract class PrefixSearch { if ( count( $namespaces ) == 1 ) { $ns = $namespaces[0]; if ( $ns == NS_MEDIA ) { - $namespaces = array( NS_FILE ); + $namespaces = [ NS_FILE ]; } elseif ( $ns == NS_SPECIAL ) { return $this->titles( $this->specialSearch( $search, $limit, $offset ) ); } } - $srchres = array(); + $srchres = []; if ( Hooks::run( 'PrefixSearchBackend', - array( $namespaces, $search, $limit, &$srchres, $offset ) + [ $namespaces, $search, $limit, &$srchres, $offset ] ) ) { return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) ); } - return $this->strings( $this->handleResultFromHook( $srchres, $namespaces, $search, $limit ) ); + return $this->strings( + $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) ); } - private function handleResultFromHook( $srchres, $namespaces, $search, $limit ) { - $rescorer = new SearchExactMatchRescorer(); - return $rescorer->rescore( $search, $namespaces, $srchres, $limit ); + private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) { + if ( $offset === 0 ) { + // Only perform exact db match if offset === 0 + // This is still far from perfect but at least we avoid returning the + // same title afain and again when the user is scrolling with a query + // that matches a title in the db. + $rescorer = new SearchExactMatchRescorer(); + $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit ); + } + return $srchres; } /** @@ -190,7 +218,7 @@ abstract class PrefixSearch { // Try matching the full search string as a page name $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey ); if ( !$specialTitle ) { - return array(); + return []; } $special = SpecialPageFactory::getPage( $specialTitle->getText() ); if ( $special ) { @@ -199,7 +227,7 @@ abstract class PrefixSearch { return $specialTitle->getSubpage( $sub ); }, $subpages ); } else { - return array(); + return []; } } @@ -209,7 +237,7 @@ abstract class PrefixSearch { // Unlike SpecialPage itself, we want the canonical forms of both // canonical and alias title forms... - $keys = array(); + $keys = []; foreach ( SpecialPageFactory::getNames() as $page ) { $keys[$wgContLang->caseFold( $page )] = $page; } @@ -225,7 +253,7 @@ abstract class PrefixSearch { } ksort( $keys ); - $srchres = array(); + $srchres = []; $skipped = 0; foreach ( $keys as $pageKey => $page ) { if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) { @@ -254,43 +282,60 @@ abstract class PrefixSearch { * be automatically capitalized by Title::secureAndSpit() * later on depending on $wgCapitalLinks) * - * @param array $namespaces Namespaces to search in + * @param array|null $namespaces Namespaces to search in * @param string $search Term * @param int $limit Max number of items to return * @param int $offset Number of items to skip - * @return array Array of Title objects + * @return Title[] Array of Title objects */ public function defaultSearchBackend( $namespaces, $search, $limit, $offset ) { - $ns = array_shift( $namespaces ); // support only one namespace - if ( is_null( $ns ) || in_array( NS_MAIN, $namespaces ) ) { - $ns = NS_MAIN; // if searching on many always default to main + // Backwards compatability with old code. Default to NS_MAIN if no namespaces provided. + if ( $namespaces === null ) { + $namespaces = []; + } + if ( !$namespaces ) { + $namespaces[] = NS_MAIN; } - if ( $ns == NS_SPECIAL ) { - return $this->specialSearch( $search, $limit, $offset ); + // Construct suitable prefix for each namespace. They differ in cases where + // some namespaces always capitalize and some don't. + $prefixes = []; + foreach ( $namespaces as $namespace ) { + // For now, if special is included, ignore the other namespaces + if ( $namespace == NS_SPECIAL ) { + return $this->specialSearch( $search, $limit, $offset ); + } + + $title = Title::makeTitleSafe( $namespace, $search ); + // Why does the prefix default to empty? + $prefix = $title ? $title->getDBkey() : ''; + $prefixes[$prefix][] = $namespace; } - $t = Title::newFromText( $search, $ns ); - $prefix = $t ? $t->getDBkey() : ''; - $dbr = wfGetDB( DB_SLAVE ); - $res = $dbr->select( 'page', - array( 'page_id', 'page_namespace', 'page_title' ), - array( - 'page_namespace' => $ns, - 'page_title ' . $dbr->buildLike( $prefix, $dbr->anyString() ) - ), - __METHOD__, - array( - 'LIMIT' => $limit, - 'ORDER BY' => 'page_title', - 'OFFSET' => $offset - ) - ); - $srchres = array(); - foreach ( $res as $row ) { - $srchres[] = Title::newFromRow( $row ); + $dbr = wfGetDB( DB_REPLICA ); + // Often there is only one prefix that applies to all requested namespaces, + // but sometimes there are two if some namespaces do not always capitalize. + $conds = []; + foreach ( $prefixes as $prefix => $namespaces ) { + $condition = [ + 'page_namespace' => $namespaces, + 'page_title' . $dbr->buildLike( $prefix, $dbr->anyString() ), + ]; + $conds[] = $dbr->makeList( $condition, LIST_AND ); } - return $srchres; + + $table = 'page'; + $fields = [ 'page_id', 'page_namespace', 'page_title' ]; + $conds = $dbr->makeList( $conds, LIST_OR ); + $options = [ + 'LIMIT' => $limit, + 'ORDER BY' => [ 'page_title', 'page_namespace' ], + 'OFFSET' => $offset + ]; + + $res = $dbr->select( $table, $fields, $conds, __METHOD__, $options ); + + return iterator_to_array( TitleArray::newFromResult( $res ) ); } /** @@ -305,7 +350,7 @@ abstract class PrefixSearch { // We will look at each given namespace against wgContLang namespaces $validNamespaces = $wgContLang->getNamespaces(); if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) { - $valid = array(); + $valid = []; foreach ( $namespaces as $ns ) { if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) { $valid[] = $ns; @@ -316,7 +361,7 @@ abstract class PrefixSearch { } } - return array( NS_MAIN ); + return [ NS_MAIN ]; } }