47 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
48 $search = trim( $search );
49 if ( $search ==
'' ) {
53 $hasNamespace = SearchEngine::parseNamespacePrefixes( $search,
false,
true );
54 if ( $hasNamespace !==
false ) {
55 [ $search, $namespaces ] = $hasNamespace;
58 return $this->
searchBackend( $namespaces, $search, $limit, $offset );
71 $searches = $this->
search( $search, $limit, $namespaces, $offset );
74 $fallbackLimit = $limit - count( $searches );
75 if ( $fallbackLimit > 0 ) {
76 $services = MediaWikiServices::getInstance();
77 $fallbackSearches = $services->getLanguageConverterFactory()
78 ->getLanguageConverter( $services->getContentLanguage() )
79 ->autoConvertToAllVariants( $search );
80 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
82 foreach ( $fallbackSearches as $fbs ) {
83 $fallbackSearchResult = $this->
search( $fbs, $fallbackLimit, $namespaces );
84 $searches = array_merge( $searches, $fallbackSearchResult );
85 $fallbackLimit -= count( $fallbackSearchResult );
87 if ( $fallbackLimit == 0 ) {
102 abstract protected function titles( array $titles );
111 abstract protected function strings( array $strings );
122 if ( count( $namespaces ) == 1 ) {
123 $ns = $namespaces[0];
131 if ( (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onPrefixSearchBackend(
132 $namespaces, $search, $limit, $srchres, $offset )
137 $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
140 private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
141 if ( $offset === 0 ) {
147 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
161 $searchParts = explode(
'/', $search, 2 );
162 $searchKey = $searchParts[0];
163 $subpageSearch = $searchParts[1] ??
null;
166 $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
167 if ( $subpageSearch !==
null ) {
169 $specialTitle = Title::makeTitleSafe(
NS_SPECIAL, $searchKey );
170 if ( !$specialTitle ) {
173 $special = $spFactory->getPage( $specialTitle->getText() );
175 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
176 return array_map( [ $specialTitle,
'getSubpage' ], $subpages );
182 # normalize searchKey, so aliases with spaces can be found - T27675
183 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
184 $searchKey = str_replace(
' ',
'_', $searchKey );
185 $searchKey = $contLang->caseFold( $searchKey );
190 foreach ( $spFactory->getNames() as $page ) {
191 $keys[$contLang->caseFold( $page )] = [
'page' => $page,
'rank' => 0 ];
194 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
195 if ( !in_array( $page, $spFactory->getNames() ) ) {# T22885
199 foreach ( $aliases as $key => $alias ) {
200 $keys[$contLang->caseFold( $alias )] = [
'page' => $alias,
'rank' => $key ];
206 foreach ( $keys as $pageKey => $page ) {
207 if ( $searchKey ===
'' || strpos( $pageKey, $searchKey ) === 0 ) {
227 return array_slice(
$matches, $offset, $limit );
243 if ( !$namespaces ) {
256 if ( $search ===
'' ) {
257 $prefixes[$search] = $namespaces;
260 $search = preg_replace( MediaWikiTitleCodec::getTitleInvalidRegex(),
'', $search );
261 foreach ( $namespaces as $namespace ) {
262 $title = Title::makeTitleSafe( $namespace, $search );
264 $prefixes[ $title->getDBkey() ][] = $namespace;
276 foreach ( $prefixes as $prefix => $namespaces ) {
277 $condition = [
'page_namespace' => $namespaces ];
278 if ( $prefix !==
'' ) {
279 $condition[] =
'page_title' . $dbr->buildLike( $prefix, $dbr->anyString() );
281 $conds[] = $dbr->makeList( $condition,
LIST_AND );
284 $queryBuilder = $dbr->newSelectQueryBuilder()
285 ->select( [
'page_id',
'page_namespace',
'page_title' ] )
287 ->where( $dbr->makeList( $conds,
LIST_OR ) )
288 ->orderBy( [
'page_title',
'page_namespace' ] )
291 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Handles searching prefixes of titles and finding any page names that match.
searchWithVariants( $search, $limit, array $namespaces, $offset=0)
Do a prefix search for all possible variants of the prefix.
search( $search, $limit, $namespaces=[], $offset=0)
Do a prefix search of titles and return a list of matching page names.
specialSearch( $search, $limit, $offset)
Prefix search special-case for Special: namespace.
titles(array $titles)
When implemented in a descendant class, receives an array of Title objects and returns either an unmo...
defaultSearchBackend( $namespaces, $search, $limit, $offset)
Unless overridden by PrefixSearchBackend hook... This is case-sensitive (First character may be autom...
strings(array $strings)
When implemented in a descendant class, receives an array of titles as strings and returns either an ...
searchBackend( $namespaces, $search, $limit, $offset)
Do a prefix search of titles and return a list of matching page names.
An utility class to rescore search results by looking for an exact match in the db and add the page f...