35 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
36 $search = trim( $search );
37 if ( $search ==
'' ) {
42 if ( $hasNamespace !==
false ) {
43 [ $search, $namespaces ] = $hasNamespace;
46 return $this->
searchBackend( $namespaces, $search, $limit, $offset );
59 $searches = $this->
search( $search, $limit, $namespaces, $offset );
62 $fallbackLimit = $limit - count( $searches );
63 if ( $fallbackLimit > 0 ) {
65 $fallbackSearches = $services->getLanguageConverterFactory()
66 ->getLanguageConverter( $services->getContentLanguage() )
67 ->autoConvertToAllVariants( $search );
68 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
70 foreach ( $fallbackSearches as $fbs ) {
71 $fallbackSearchResult = $this->
search( $fbs, $fallbackLimit, $namespaces );
72 $searches = array_merge( $searches, $fallbackSearchResult );
73 $fallbackLimit -= count( $fallbackSearchResult );
75 if ( $fallbackLimit == 0 ) {
90 abstract protected function titles( array $titles );
99 abstract protected function strings( array $strings );
110 if ( count( $namespaces ) == 1 ) {
111 $ns = $namespaces[0];
130 $searchParts = explode(
'/', $search, 2 );
131 $searchKey = $searchParts[0];
132 $subpageSearch = $searchParts[1] ??
null;
136 if ( $subpageSearch !==
null ) {
138 $specialTitle = Title::makeTitleSafe(
NS_SPECIAL, $searchKey );
139 if ( !$specialTitle ) {
142 $special = $spFactory->getPage( $specialTitle->getText() );
144 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
145 return array_map( $specialTitle->getSubpage( ... ), $subpages );
151 # normalize searchKey, so aliases with spaces can be found - T27675
153 $searchKey = str_replace(
' ',
'_', $searchKey );
154 $searchKey = $contLang->caseFold( $searchKey );
159 $listedPages = $spFactory->getListedPages();
160 foreach ( $listedPages as $page => $_obj ) {
161 $keys[$contLang->caseFold( $page )] = [
'page' => $page,
'rank' => 0 ];
164 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
167 if ( !in_array( $page, $spFactory->getNames() ) ) {
171 if ( !isset( $listedPages[$page] ) ) {
175 foreach ( $aliases as $key => $alias ) {
176 $keys[$contLang->caseFold( $alias )] = [
'page' => $alias,
'rank' => $key ];
182 foreach ( $keys as $pageKey => $page ) {
183 if ( $searchKey ===
'' || str_starts_with( $pageKey, $searchKey ) ) {
203 return array_slice(
$matches, $offset, $limit );
218 if ( !$namespaces ) {
231 if ( $search ===
'' ) {
232 $prefixes[$search] = $namespaces;
235 $search = preg_replace( TitleParser::getTitleInvalidRegex(),
'', $search );
236 foreach ( $namespaces as $namespace ) {
237 $title = Title::makeTitleSafe( $namespace, $search );
239 $prefixes[$title->getDBkey()][] = $namespace;
248 $dbr = $services->getConnectionProvider()->getReplicaDatabase();
252 foreach ( $prefixes as $prefix => $namespaces ) {
253 $expr = $dbr->expr(
'page_namespace',
'=', $namespaces );
254 if ( $prefix !==
'' ) {
258 new LikeValue( (
string)$prefix, $dbr->anyString() )
264 $queryBuilder = $dbr->newSelectQueryBuilder()
265 ->select( [
'page_id',
'page_namespace',
'page_title' ] )
267 ->where( $dbr->orExpr( $conds ) )
268 ->orderBy( [
'page_title',
'page_namespace' ] )
271 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
273 return iterator_to_array( $services->getTitleFactory()->newTitleArrayFromResult( $res ) );
278class_alias( PrefixSearch::class,
'PrefixSearch' );