36 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
37 $search = trim( $search );
38 if ( $search ==
'' ) {
43 if ( $hasNamespace !==
false ) {
44 [ $search, $namespaces ] = $hasNamespace;
47 return $this->
searchBackend( $namespaces, $search, $limit, $offset );
60 $searches = $this->
search( $search, $limit, $namespaces, $offset );
63 $fallbackLimit = $limit - count( $searches );
64 if ( $fallbackLimit > 0 ) {
66 $fallbackSearches = $services->getLanguageConverterFactory()
67 ->getLanguageConverter( $services->getContentLanguage() )
68 ->autoConvertToAllVariants( $search );
69 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
71 foreach ( $fallbackSearches as $fbs ) {
72 $fallbackSearchResult = $this->
search( $fbs, $fallbackLimit, $namespaces );
73 $searches = array_merge( $searches, $fallbackSearchResult );
74 $fallbackLimit -= count( $fallbackSearchResult );
76 if ( $fallbackLimit == 0 ) {
91 abstract protected function titles( array $titles );
100 abstract protected function strings( array $strings );
111 if ( count( $namespaces ) == 1 ) {
112 $ns = $namespaces[0];
121 $namespaces, $search, $limit, $srchres, $offset )
126 $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
129 private function handleResultFromHook(
130 array $srchres, array $namespaces,
string $search,
int $limit,
int $offset
132 if ( $offset === 0 ) {
138 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
152 $searchParts = explode(
'/', $search, 2 );
153 $searchKey = $searchParts[0];
154 $subpageSearch = $searchParts[1] ??
null;
158 if ( $subpageSearch !==
null ) {
160 $specialTitle = Title::makeTitleSafe(
NS_SPECIAL, $searchKey );
161 if ( !$specialTitle ) {
164 $special = $spFactory->getPage( $specialTitle->getText() );
166 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
167 return array_map( $specialTitle->getSubpage( ... ), $subpages );
173 # normalize searchKey, so aliases with spaces can be found - T27675
175 $searchKey = str_replace(
' ',
'_', $searchKey );
176 $searchKey = $contLang->caseFold( $searchKey );
181 $listedPages = $spFactory->getListedPages();
182 foreach ( $listedPages as $page => $_obj ) {
183 $keys[$contLang->caseFold( $page )] = [
'page' => $page,
'rank' => 0 ];
186 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
189 if ( !in_array( $page, $spFactory->getNames() ) ) {
193 if ( !isset( $listedPages[$page] ) ) {
197 foreach ( $aliases as $key => $alias ) {
198 $keys[$contLang->caseFold( $alias )] = [
'page' => $alias,
'rank' => $key ];
204 foreach ( $keys as $pageKey => $page ) {
205 if ( $searchKey ===
'' || str_starts_with( $pageKey, $searchKey ) ) {
225 return array_slice(
$matches, $offset, $limit );
241 if ( !$namespaces ) {
247 return $this->specialSearch( $search, $limit, $offset );
254 if ( $search ===
'' ) {
255 $prefixes[$search] = $namespaces;
258 $search = preg_replace( TitleParser::getTitleInvalidRegex(),
'', $search );
259 foreach ( $namespaces as $namespace ) {
260 $title = Title::makeTitleSafe( $namespace, $search );
262 $prefixes[$title->getDBkey()][] = $namespace;
271 $dbr = $services->getConnectionProvider()->getReplicaDatabase();
275 foreach ( $prefixes as $prefix => $namespaces ) {
276 $expr = $dbr->expr(
'page_namespace',
'=', $namespaces );
277 if ( $prefix !==
'' ) {
281 new LikeValue( (
string)$prefix, $dbr->anyString() )
287 $queryBuilder = $dbr->newSelectQueryBuilder()
288 ->select( [
'page_id',
'page_namespace',
'page_title' ] )
290 ->where( $dbr->orExpr( $conds ) )
291 ->orderBy( [
'page_title',
'page_namespace' ] )
294 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
296 return iterator_to_array( $services->getTitleFactory()->newTitleArrayFromResult( $res ) );
301class_alias( PrefixSearch::class,
'PrefixSearch' );
if(!defined('MW_SETUP_CALLBACK'))