From c1f9b1f7b1b77776192048005dcc66dcf3df2bfb Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Sat, 27 Dec 2014 15:41:37 +0100 Subject: Update to MediaWiki 1.24.1 --- includes/PrefixSearch.php | 239 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 177 insertions(+), 62 deletions(-) (limited to 'includes/PrefixSearch.php') diff --git a/includes/PrefixSearch.php b/includes/PrefixSearch.php index 3c464c58..718750f5 100644 --- a/includes/PrefixSearch.php +++ b/includes/PrefixSearch.php @@ -26,30 +26,44 @@ * * @ingroup Search */ -class PrefixSearch { +abstract class PrefixSearch { /** * Do a prefix search of titles and return a list of matching page names. + * @deprecated Since 1.23, use TitlePrefixSearch or StringPrefixSearch classes * - * @param $search String - * @param $limit Integer - * @param array $namespaces used if query is not explicitly prefixed - * @return Array of strings + * @param string $search + * @param int $limit + * @param array $namespaces Used if query is not explicitly prefixed + * @return array Array of strings */ public static function titleSearch( $search, $limit, $namespaces = array() ) { + $prefixSearch = new StringPrefixSearch; + return $prefixSearch->search( $search, $limit, $namespaces ); + } + + /** + * 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 explicitly prefixed + * @return array Array of strings or Title objects + */ + public function search( $search, $limit, $namespaces = array() ) { $search = trim( $search ); if ( $search == '' ) { return array(); // Return empty result } - $namespaces = self::validateNamespaces( $namespaces ); + $namespaces = $this->validateNamespaces( $namespaces ); // Find a Title which is not an interwiki and is in NS_MAIN $title = Title::newFromText( $search ); - if ( $title && $title->getInterwiki() == '' ) { + if ( $title && !$title->isExternal() ) { $ns = array( $title->getNamespace() ); if ( $ns[0] == NS_MAIN ) { $ns = $namespaces; // no explicit prefix, use default namespaces } - return self::searchBackend( + return $this->searchBackend( $ns, $title->getText(), $limit ); } @@ -57,61 +71,136 @@ class PrefixSearch { $title = Title::newFromText( $search . 'Dummy' ); if ( $title && $title->getText() == 'Dummy' && $title->getNamespace() != NS_MAIN - && $title->getInterwiki() == '' ) { - return self::searchBackend( - array( $title->getNamespace() ), '', $limit ); + && !$title->isExternal() ) + { + $namespaces = array( $title->getNamespace() ); + $search = ''; } - return self::searchBackend( $namespaces, $search, $limit ); + return $this->searchBackend( $namespaces, $search, $limit ); + } + + /** + * Do a prefix search for all possible variants of the prefix + * @param string $search + * @param int $limit + * @param array $namespaces + * + * @return array + */ + public function searchWithVariants( $search, $limit, array $namespaces ) { + wfProfileIn( __METHOD__ ); + $searches = $this->search( $search, $limit, $namespaces ); + + // if the content language has variants, try to retrieve fallback results + $fallbackLimit = $limit - count( $searches ); + if ( $fallbackLimit > 0 ) { + global $wgContLang; + + $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search ); + $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) ); + + foreach ( $fallbackSearches as $fbs ) { + $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces ); + $searches = array_merge( $searches, $fallbackSearchResult ); + $fallbackLimit -= count( $fallbackSearchResult ); + + if ( $fallbackLimit == 0 ) { + break; + } + } + } + wfProfileOut( __METHOD__ ); + return $searches; } + /** + * When implemented in a descendant class, receives an array of Title objects and returns + * either an unmodified array or an array of strings corresponding to titles passed to it. + * + * @param array $titles + * @return array + */ + abstract protected function titles( array $titles ); + + /** + * When implemented in a descendant class, receives an array of titles as strings and returns + * either an unmodified array or an array of Title objects corresponding to strings received. + * + * @param array $strings + * + * @return array + */ + abstract protected function strings( array $strings ); + /** * Do a prefix search of titles and return a list of matching page names. - * @param $namespaces Array - * @param $search String - * @param $limit Integer - * @return Array of strings + * @param array $namespaces + * @param string $search + * @param int $limit + * @return array Array of strings */ - protected static function searchBackend( $namespaces, $search, $limit ) { + protected function searchBackend( $namespaces, $search, $limit ) { if ( count( $namespaces ) == 1 ) { $ns = $namespaces[0]; if ( $ns == NS_MEDIA ) { $namespaces = array( NS_FILE ); } elseif ( $ns == NS_SPECIAL ) { - return self::specialSearch( $search, $limit ); + return $this->titles( $this->specialSearch( $search, $limit ) ); } } $srchres = array(); if ( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) { - return self::defaultSearchBackend( $namespaces, $search, $limit ); + return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit ) ); } - return $srchres; + return $this->strings( $srchres ); } /** * Prefix search special-case for Special: namespace. * - * @param string $search term - * @param $limit Integer: max number of items to return - * @return Array + * @param string $search Term + * @param int $limit Max number of items to return + * @return array */ - protected static function specialSearch( $search, $limit ) { + protected function specialSearch( $search, $limit ) { global $wgContLang; - # normalize searchKey, so aliases with spaces can be found - bug 25675 - $search = str_replace( ' ', '_', $search ); + $searchParts = explode( '/', $search, 2 ); + $searchKey = $searchParts[0]; + $subpageSearch = isset( $searchParts[1] ) ? $searchParts[1] : null; - $searchKey = $wgContLang->caseFold( $search ); + // Handle subpage search separately. + if ( $subpageSearch !== null ) { + // Try matching the full search string as a page name + $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey ); + if ( !$specialTitle ) { + return array(); + } + $special = SpecialPageFactory::getPage( $specialTitle->getText() ); + if ( $special ) { + $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit ); + return array_map( function ( $sub ) use ( $specialTitle ) { + return $specialTitle->getSubpage( $sub ); + }, $subpages ); + } else { + return array(); + } + } + + # normalize searchKey, so aliases with spaces can be found - bug 25675 + $searchKey = str_replace( ' ', '_', $searchKey ); + $searchKey = $wgContLang->caseFold( $searchKey ); // Unlike SpecialPage itself, we want the canonical forms of both // canonical and alias title forms... $keys = array(); - foreach ( SpecialPageFactory::getList() as $page => $class ) { + foreach ( SpecialPageFactory::getNames() as $page ) { $keys[$wgContLang->caseFold( $page )] = $page; } foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) { - if ( !array_key_exists( $page, SpecialPageFactory::getList() ) ) {# bug 20885 + if ( !in_array( $page, SpecialPageFactory::getNames() ) ) {# bug 20885 continue; } @@ -124,13 +213,11 @@ class PrefixSearch { $srchres = array(); foreach ( $keys as $pageKey => $page ) { if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) { - wfSuppressWarnings(); // 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' - $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page )->getPrefixedText(); - wfRestoreWarnings(); + $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page ); } if ( count( $srchres ) >= $limit ) { @@ -147,51 +234,43 @@ class PrefixSearch { * be automatically capitalized by Title::secureAndSpit() * later on depending on $wgCapitalLinks) * - * @param array $namespaces namespaces to search in - * @param string $search term - * @param $limit Integer: max number of items to return - * @return Array of title strings + * @param array $namespaces Namespaces to search in + * @param string $search Term + * @param int $limit Max number of items to return + * @return array Array of Title objects */ - protected static function defaultSearchBackend( $namespaces, $search, $limit ) { + protected function defaultSearchBackend( $namespaces, $search, $limit ) { $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( - 'action' => 'query', - 'list' => 'allpages', - 'apnamespace' => $ns, - 'aplimit' => $limit, - 'apprefix' => $search - )); - - // Execute - $module = new ApiMain( $req ); - $module->execute(); - - // Get resulting data - $data = $module->getResultData(); - - // Reformat useful data for future printing by JSON engine + $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' ) + ); $srchres = array(); - foreach ( (array)$data['query']['allpages'] as $pageinfo ) { - // Note: this data will no be printable by the xml engine - // because it does not support lists of unnamed items - $srchres[] = $pageinfo['title']; + foreach ( $res as $row ) { + $srchres[] = Title::newFromRow( $row ); } - return $srchres; } /** * Validate an array of numerical namespace indexes * - * @param $namespaces Array - * @return Array (default: contains only NS_MAIN) + * @param array $namespaces + * @return array (default: contains only NS_MAIN) */ - protected static function validateNamespaces( $namespaces ) { + protected function validateNamespaces( $namespaces ) { global $wgContLang; // We will look at each given namespace against wgContLang namespaces @@ -211,3 +290,39 @@ class PrefixSearch { return array( NS_MAIN ); } } + +/** + * Performs prefix search, returning Title objects + * @ingroup Search + */ +class TitlePrefixSearch extends PrefixSearch { + + protected function titles( array $titles ) { + return $titles; + } + + protected function strings( array $strings ) { + $titles = array_map( 'Title::newFromText', $strings ); + $lb = new LinkBatch( $titles ); + $lb->setCaller( __METHOD__ ); + $lb->execute(); + return $titles; + } +} + +/** + * Performs prefix search, returning strings + * @ingroup Search + */ +class StringPrefixSearch extends PrefixSearch { + + protected function titles( array $titles ) { + return array_map( function ( Title $t ) { + return $t->getPrefixedText(); + }, $titles ); + } + + protected function strings( array $strings ) { + return $strings; + } +} -- cgit v1.2.3-54-g00ecf