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 ) );
150 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
164 $searchParts = explode(
'/', $search, 2 );
165 $searchKey = $searchParts[0];
166 $subpageSearch = $searchParts[1] ??
null;
169 $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
170 if ( $subpageSearch !==
null ) {
172 $specialTitle = Title::makeTitleSafe(
NS_SPECIAL, $searchKey );
173 if ( !$specialTitle ) {
176 $special = $spFactory->getPage( $specialTitle->getText() );
178 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
179 return array_map( [ $specialTitle,
'getSubpage' ], $subpages );
185 # normalize searchKey, so aliases with spaces can be found - T27675
186 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
187 $searchKey = str_replace(
' ',
'_', $searchKey );
188 $searchKey = $contLang->caseFold( $searchKey );
193 $listedPages = $spFactory->getListedPages();
194 foreach ( $listedPages as $page => $_obj ) {
195 $keys[$contLang->caseFold( $page )] = [
'page' => $page,
'rank' => 0 ];
198 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
201 if ( !in_array( $page, $spFactory->getNames() ) ) {
205 if ( !isset( $listedPages[ $page ] ) ) {
209 foreach ( $aliases as $key => $alias ) {
210 $keys[$contLang->caseFold( $alias )] = [
'page' => $alias,
'rank' => $key ];
216 foreach ( $keys as $pageKey => $page ) {
217 if ( $searchKey ===
'' || strpos( $pageKey, $searchKey ) === 0 ) {
237 return array_slice(
$matches, $offset, $limit );
253 if ( !$namespaces ) {
259 return $this->specialSearch( $search, $limit, $offset );
266 if ( $search ===
'' ) {
267 $prefixes[$search] = $namespaces;
270 $search = preg_replace( TitleParser::getTitleInvalidRegex(),
'', $search );
271 foreach ( $namespaces as $namespace ) {
272 $title = Title::makeTitleSafe( $namespace, $search );
274 $prefixes[ $title->getDBkey() ][] = $namespace;
282 $services = MediaWikiServices::getInstance();
283 $dbr = $services->getConnectionProvider()->getReplicaDatabase();
287 foreach ( $prefixes as $prefix => $namespaces ) {
288 $expr = $dbr->expr(
'page_namespace',
'=', $namespaces );
289 if ( $prefix !==
'' ) {
293 new LikeValue( (
string)$prefix, $dbr->anyString() )
299 $queryBuilder = $dbr->newSelectQueryBuilder()
300 ->select( [
'page_id',
'page_namespace',
'page_title' ] )
302 ->where( $dbr->orExpr( $conds ) )
303 ->orderBy( [
'page_title',
'page_namespace' ] )
306 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
308 return iterator_to_array( $services->getTitleFactory()->newTitleArrayFromResult( $res ) );