45 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
46 $search = trim( $search );
47 if ( $search ==
'' ) {
51 $hasNamespace = SearchEngine::parseNamespacePrefixes( $search,
false,
true );
52 if ( $hasNamespace !==
false ) {
53 [ $search, $namespaces ] = $hasNamespace;
56 return $this->
searchBackend( $namespaces, $search, $limit, $offset );
69 $searches = $this->
search( $search, $limit, $namespaces, $offset );
72 $fallbackLimit = $limit - count( $searches );
73 if ( $fallbackLimit > 0 ) {
74 $services = MediaWikiServices::getInstance();
75 $fallbackSearches = $services->getLanguageConverterFactory()
76 ->getLanguageConverter( $services->getContentLanguage() )
77 ->autoConvertToAllVariants( $search );
78 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
80 foreach ( $fallbackSearches as $fbs ) {
81 $fallbackSearchResult = $this->
search( $fbs, $fallbackLimit, $namespaces );
82 $searches = array_merge( $searches, $fallbackSearchResult );
83 $fallbackLimit -= count( $fallbackSearchResult );
85 if ( $fallbackLimit == 0 ) {
100 abstract protected function titles( array $titles );
109 abstract protected function strings( array $strings );
120 if ( count( $namespaces ) == 1 ) {
121 $ns = $namespaces[0];
129 if ( Hooks::runner()->onPrefixSearchBackend(
130 $namespaces, $search, $limit, $srchres, $offset )
135 $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
138 private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
139 if ( $offset === 0 ) {
145 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
159 $searchParts = explode(
'/', $search, 2 );
160 $searchKey = $searchParts[0];
161 $subpageSearch = $searchParts[1] ??
null;
164 $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
165 if ( $subpageSearch !==
null ) {
167 $specialTitle = Title::makeTitleSafe(
NS_SPECIAL, $searchKey );
168 if ( !$specialTitle ) {
171 $special = $spFactory->getPage( $specialTitle->getText() );
173 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
174 return array_map( [ $specialTitle,
'getSubpage' ], $subpages );
180 # normalize searchKey, so aliases with spaces can be found - T27675
181 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
182 $searchKey = str_replace(
' ',
'_', $searchKey );
183 $searchKey = $contLang->caseFold( $searchKey );
188 foreach ( $spFactory->getNames() as $page ) {
189 $keys[$contLang->caseFold( $page )] = [
'page' => $page,
'rank' => 0 ];
192 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
193 if ( !in_array( $page, $spFactory->getNames() ) ) {# T22885
197 foreach ( $aliases as $key => $alias ) {
198 $keys[$contLang->caseFold( $alias )] = [
'page' => $alias,
'rank' => $key ];
204 foreach (
$keys as $pageKey => $page ) {
205 if ( $searchKey ===
'' || strpos( $pageKey, $searchKey ) === 0 ) {
225 return array_slice(
$matches, $offset, $limit );
241 if ( !$namespaces ) {
254 if ( $search ===
'' ) {
255 $prefixes[$search] = $namespaces;
258 $search = preg_replace( MediaWikiTitleCodec::getTitleInvalidRegex(),
'', $search );
259 foreach ( $namespaces as $namespace ) {
260 $title = Title::makeTitleSafe( $namespace, $search );
262 $prefixes[
$title->getDBkey() ][] = $namespace;
274 foreach ( $prefixes as $prefix => $namespaces ) {
275 $condition = [
'page_namespace' => $namespaces ];
276 if ( $prefix !==
'' ) {
277 $condition[] =
'page_title' .
$dbr->buildLike( $prefix,
$dbr->anyString() );
283 $fields = [
'page_id',
'page_namespace',
'page_title' ];
287 'ORDER BY' => [
'page_title',
'page_namespace' ],
291 $res =
$dbr->select( $table, $fields, $conds, __METHOD__, $options );
293 return iterator_to_array( TitleArray::newFromResult(
$res ) );
304 $validNamespaces = MediaWikiServices::getInstance()->getContentLanguage()->getNamespaces();
305 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
307 foreach ( $namespaces as $ns ) {
308 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
312 if ( count( $valid ) > 0 ) {
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.
validateNamespaces( $namespaces)
Validate an array of numerical namespace indexes.
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...