X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FPrefixSearch.php;h=839fedcd1579bd7e6d505c9639f80481e7fa3e36;hb=17cc78767ab099f9d00d267f230be81c202b1795;hp=718750f58ccf497d4a13418084ff3a36f0ba3ac1;hpb=91f7c1996d5169ac7d712ddc070fd0113ce9afb6;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/PrefixSearch.php b/includes/PrefixSearch.php index 718750f58c..839fedcd15 100644 --- a/includes/PrefixSearch.php +++ b/includes/PrefixSearch.php @@ -60,11 +60,12 @@ abstract class PrefixSearch { $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 + wfRunHooks( 'PrefixSearchExtractNamespace', array( &$ns, &$search ) ); } - return $this->searchBackend( - $ns, $title->getText(), $limit ); + return $this->searchBackend( $ns, $search, $limit ); } // Is this a namespace prefix? @@ -75,6 +76,8 @@ abstract class PrefixSearch { { $namespaces = array( $title->getNamespace() ); $search = ''; + } else { + wfRunHooks( 'PrefixSearchExtractNamespace', array( &$namespaces, &$search ) ); } return $this->searchBackend( $namespaces, $search, $limit ); @@ -152,7 +155,33 @@ 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 ); + if ( $t ) { + // If text is a valid title and is in the search results + $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 ); } @@ -195,7 +224,7 @@ abstract class PrefixSearch { // Unlike SpecialPage itself, we want the canonical forms of both // canonical and alias title forms... $keys = array(); - foreach ( SpecialPageFactory::getNames() as $page ) { + foreach ( SpecialPageFactory::getNames() as $page ) { $keys[$wgContLang->caseFold( $page )] = $page; }