48 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
49 $search = trim( $search );
50 if ( $search ==
'' ) {
54 $hasNamespace = SearchEngine::parseNamespacePrefixes( $search,
false,
true );
55 if ( $hasNamespace !==
false ) {
56 [ $search, $namespaces ] = $hasNamespace;
59 return $this->
searchBackend( $namespaces, $search, $limit, $offset );
72 $searches = $this->
search( $search, $limit, $namespaces, $offset );
75 $fallbackLimit = $limit - count( $searches );
76 if ( $fallbackLimit > 0 ) {
77 $services = MediaWikiServices::getInstance();
78 $fallbackSearches = $services->getLanguageConverterFactory()
79 ->getLanguageConverter( $services->getContentLanguage() )
80 ->autoConvertToAllVariants( $search );
81 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
83 foreach ( $fallbackSearches as $fbs ) {
84 $fallbackSearchResult = $this->
search( $fbs, $fallbackLimit, $namespaces );
85 $searches = array_merge( $searches, $fallbackSearchResult );
86 $fallbackLimit -= count( $fallbackSearchResult );
88 if ( $fallbackLimit == 0 ) {
123 if ( count( $namespaces ) == 1 ) {
124 $ns = $namespaces[0];
132 if ( (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onPrefixSearchBackend(
133 $namespaces, $search, $limit, $srchres, $offset )
138 $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
141 private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
148 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
162 $searchParts = explode(
'/', $search, 2 );
163 $searchKey = $searchParts[0];
164 $subpageSearch = $searchParts[1] ??
null;
167 $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
168 if ( $subpageSearch !==
null ) {
170 $specialTitle = Title::makeTitleSafe(
NS_SPECIAL, $searchKey );
171 if ( !$specialTitle ) {
174 $special = $spFactory->getPage( $specialTitle->getText() );
176 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
177 return array_map( [ $specialTitle,
'getSubpage' ], $subpages );
183 # normalize searchKey, so aliases with spaces can be found - T27675
184 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
185 $searchKey = str_replace(
' ',
'_', $searchKey );
186 $searchKey = $contLang->caseFold( $searchKey );
191 $listedPages = $spFactory->getListedPages();
192 foreach ( $listedPages as $page => $_obj ) {
193 $keys[$contLang->caseFold( $page )] = [
'page' => $page,
'rank' => 0 ];
196 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
199 if ( !in_array( $page, $spFactory->getNames() ) ) {
203 if ( !isset( $listedPages[ $page ] ) ) {
207 foreach ( $aliases as $key => $alias ) {
208 $keys[$contLang->caseFold( $alias )] = [
'page' => $alias,
'rank' => $key ];
214 foreach ( $keys as $pageKey => $page ) {
215 if ( $searchKey ===
'' || strpos( $pageKey, $searchKey ) === 0 ) {
235 return array_slice(
$matches, $offset, $limit );
251 if ( !$namespaces ) {
264 if ( $search ===
'' ) {
265 $prefixes[$search] = $namespaces;
268 $search = preg_replace( MediaWikiTitleCodec::getTitleInvalidRegex(),
'', $search );
269 foreach ( $namespaces as $namespace ) {
270 $title = Title::makeTitleSafe( $namespace, $search );
272 $prefixes[ $title->getDBkey() ][] = $namespace;
280 $services = MediaWikiServices::getInstance();
281 $dbr = $services->getConnectionProvider()->getReplicaDatabase();
285 foreach ( $prefixes as $prefix => $namespaces ) {
286 $expr = $dbr->expr(
'page_namespace',
'=', $namespaces );
287 if ( $prefix !==
'' ) {
291 new LikeValue( (
string)$prefix, $dbr->anyString() )
297 $queryBuilder = $dbr->newSelectQueryBuilder()
298 ->select( [
'page_id',
'page_namespace',
'page_title' ] )
300 ->where( $dbr->orExpr( $conds ) )
301 ->orderBy( [
'page_title',
'page_namespace' ] )
304 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
306 return iterator_to_array( $services->getTitleFactory()->newTitleArrayFromResult( $res ) );