From 370e83bb0dfd0c70de268c93bf07ad5ee0897192 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Fri, 15 Aug 2008 01:29:47 +0200 Subject: Update auf 1.13.0 --- includes/PrefixSearch.php | 85 +++++++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 28 deletions(-) (limited to 'includes/PrefixSearch.php') diff --git a/includes/PrefixSearch.php b/includes/PrefixSearch.php index bddfb9f1..a3ff05e2 100644 --- a/includes/PrefixSearch.php +++ b/includes/PrefixSearch.php @@ -5,19 +5,23 @@ class PrefixSearch { * Do a prefix search of titles and return a list of matching page names. * @param string $search * @param int $limit + * @param array $namespaces - used if query is not explicitely prefixed * @return array of strings */ - public static function titleSearch( $search, $limit ) { + public static function titleSearch( $search, $limit, $namespaces=array() ) { $search = trim( $search ); if( $search == '' ) { return array(); // Return empty result } - + $namespaces = self::validateNamespaces( $namespaces ); + $title = Title::newFromText( $search ); if( $title && $title->getInterwiki() == '' ) { - $ns = $title->getNamespace(); + $ns = array($title->getNamespace()); + if($ns[0] == NS_MAIN) + $ns = $namespaces; // no explicit prefix, use default namespaces return self::searchBackend( - $title->getNamespace(), $title->getText(), $limit ); + $ns, $title->getText(), $limit ); } // Is this a namespace prefix? @@ -26,40 +30,43 @@ class PrefixSearch { && $title->getNamespace() != NS_MAIN && $title->getInterwiki() == '' ) { return self::searchBackend( - $title->getNamespace(), '', $limit ); + array($title->getNamespace()), '', $limit ); } - - return self::searchBackend( 0, $search, $limit ); + + return self::searchBackend( $namespaces, $search, $limit ); } - - + + /** * Do a prefix search of titles and return a list of matching page names. + * @param array $namespaces * @param string $search * @param int $limit * @return array of strings */ - protected static function searchBackend( $ns, $search, $limit ) { - if( $ns == NS_MEDIA ) { - $ns = NS_IMAGE; - } elseif( $ns == NS_SPECIAL ) { - return self::specialSearch( $search, $limit ); + protected static function searchBackend( $namespaces, $search, $limit ) { + if( count($namespaces) == 1 ){ + $ns = $namespaces[0]; + if( $ns == NS_MEDIA ) { + $namespaces = array(NS_IMAGE); + } elseif( $ns == NS_SPECIAL ) { + return self::specialSearch( $search, $limit ); + } } - $srchres = array(); - if( wfRunHooks( 'PrefixSearchBackend', array( $ns, $search, $limit, &$srchres ) ) ) { - return self::defaultSearchBackend( $ns, $search, $limit ); + if( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) { + return self::defaultSearchBackend( $namespaces, $search, $limit ); } return $srchres; } - + /** * Prefix search special-case for Special: namespace. */ protected static function specialSearch( $search, $limit ) { global $wgContLang; $searchKey = $wgContLang->caseFold( $search ); - + // Unlike SpecialPage itself, we want the canonical forms of both // canonical and alias title forms... SpecialPage::initList(); @@ -74,7 +81,7 @@ class PrefixSearch { } } ksort( $keys ); - + $srchres = array(); foreach( $keys as $pageKey => $page ) { if( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) { @@ -86,22 +93,26 @@ class PrefixSearch { } return $srchres; } - + /** * Unless overridden by PrefixSearchBackend hook... * This is case-sensitive except the first letter (per $wgCapitalLinks) * - * @param int $ns Namespace to search in + * @param array $namespaces Namespaces to search in * @param string $search term * @param int $limit max number of items to return * @return array of title strings */ - protected static function defaultSearchBackend( $ns, $search, $limit ) { + protected static function defaultSearchBackend( $namespaces, $search, $limit ) { global $wgCapitalLinks, $wgContLang; - + if( $wgCapitalLinks ) { $search = $wgContLang->ucfirst( $search ); } + + $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 // Prepare nested request $req = new FauxRequest(array ( @@ -126,10 +137,28 @@ class PrefixSearch { // because it does not support lists of unnamed items $srchres[] = $pageinfo['title']; } - + return $srchres; } - + + /** + * Validate an array of numerical namespace indexes + * + * @param array $namespaces + */ + protected static function validateNamespaces($namespaces){ + global $wgContLang; + $validNamespaces = $wgContLang->getNamespaces(); + if( is_array($namespaces) && count($namespaces)>0 ){ + $valid = array(); + foreach ($namespaces as $ns){ + if( is_numeric($ns) && array_key_exists($ns, $validNamespaces) ) + $valid[] = $ns; + } + if( count($valid) > 0 ) + return $valid; + } + + return array( NS_MAIN ); + } } - -?> \ No newline at end of file -- cgit v1.2.3-54-g00ecf