49 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
50 $search = trim( $search );
51 if ( $search ==
'' ) {
55 $hasNamespace = SearchEngine::parseNamespacePrefixes( $search,
false,
true );
56 if ( $hasNamespace !==
false ) {
57 [ $search, $namespaces ] = $hasNamespace;
60 return $this->
searchBackend( $namespaces, $search, $limit, $offset );
73 $searches = $this->
search( $search, $limit, $namespaces, $offset );
76 $fallbackLimit = $limit - count( $searches );
77 if ( $fallbackLimit > 0 ) {
78 $services = MediaWikiServices::getInstance();
79 $fallbackSearches = $services->getLanguageConverterFactory()
80 ->getLanguageConverter( $services->getContentLanguage() )
81 ->autoConvertToAllVariants( $search );
82 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
84 foreach ( $fallbackSearches as $fbs ) {
85 $fallbackSearchResult = $this->
search( $fbs, $fallbackLimit, $namespaces );
86 $searches = array_merge( $searches, $fallbackSearchResult );
87 $fallbackLimit -= count( $fallbackSearchResult );
89 if ( $fallbackLimit == 0 ) {
124 if ( count( $namespaces ) == 1 ) {
125 $ns = $namespaces[0];
133 if ( (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onPrefixSearchBackend(
134 $namespaces, $search, $limit, $srchres, $offset )
139 $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
142 private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
149 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
163 $searchParts = explode(
'/', $search, 2 );
164 $searchKey = $searchParts[0];
165 $subpageSearch = $searchParts[1] ??
null;
168 $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
169 if ( $subpageSearch !==
null ) {
171 $specialTitle = Title::makeTitleSafe(
NS_SPECIAL, $searchKey );
172 if ( !$specialTitle ) {
175 $special = $spFactory->getPage( $specialTitle->getText() );
177 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
178 return array_map( [ $specialTitle,
'getSubpage' ], $subpages );
184 # normalize searchKey, so aliases with spaces can be found - T27675
185 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
186 $searchKey = str_replace(
' ',
'_', $searchKey );
187 $searchKey = $contLang->caseFold( $searchKey );
192 $listedPages = $spFactory->getListedPages();
193 foreach ( $listedPages as $page => $_obj ) {
194 $keys[$contLang->caseFold( $page )] = [
'page' => $page,
'rank' => 0 ];
197 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
200 if ( !in_array( $page, $spFactory->getNames() ) ) {
204 if ( !isset( $listedPages[ $page ] ) ) {
208 foreach ( $aliases as $key => $alias ) {
209 $keys[$contLang->caseFold( $alias )] = [
'page' => $alias,
'rank' => $key ];
215 foreach ( $keys as $pageKey => $page ) {
216 if ( $searchKey ===
'' || strpos( $pageKey, $searchKey ) === 0 ) {
236 return array_slice(
$matches, $offset, $limit );
252 if ( !$namespaces ) {
265 if ( $search ===
'' ) {
266 $prefixes[$search] = $namespaces;
269 $search = preg_replace( MediaWikiTitleCodec::getTitleInvalidRegex(),
'', $search );
270 foreach ( $namespaces as $namespace ) {
271 $title = Title::makeTitleSafe( $namespace, $search );
273 $prefixes[ $title->getDBkey() ][] = $namespace;
281 $services = MediaWikiServices::getInstance();
282 $dbr = $services->getConnectionProvider()->getReplicaDatabase();
286 foreach ( $prefixes as $prefix => $namespaces ) {
287 $expr = $dbr->expr(
'page_namespace',
'=', $namespaces );
288 if ( $prefix !==
'' ) {
292 new LikeValue( $prefix, $dbr->anyString() )
298 $queryBuilder = $dbr->newSelectQueryBuilder()
299 ->select( [
'page_id',
'page_namespace',
'page_title' ] )
302 ->orderBy( [
'page_title',
'page_namespace' ] )
305 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
307 return iterator_to_array( $services->getTitleFactory()->newTitleArrayFromResult( $res ) );