43 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
44 $search = trim( $search );
45 if ( $search ==
'' ) {
49 $hasNamespace = SearchEngine::parseNamespacePrefixes( $search,
false,
true );
50 if ( $hasNamespace !==
false ) {
51 list( $search, $namespaces ) = $hasNamespace;
54 return $this->
searchBackend( $namespaces, $search, $limit, $offset );
67 $searches = $this->
search( $search, $limit, $namespaces, $offset );
70 $fallbackLimit = $limit - count( $searches );
71 if ( $fallbackLimit > 0 ) {
72 $services = MediaWikiServices::getInstance();
73 $fallbackSearches = $services->getLanguageConverterFactory()
74 ->getLanguageConverter( $services->getContentLanguage() )
75 ->autoConvertToAllVariants( $search );
76 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
78 foreach ( $fallbackSearches as $fbs ) {
79 $fallbackSearchResult = $this->
search( $fbs, $fallbackLimit, $namespaces );
80 $searches = array_merge( $searches, $fallbackSearchResult );
81 $fallbackLimit -= count( $fallbackSearchResult );
83 if ( $fallbackLimit == 0 ) {
98 abstract protected function titles( array $titles );
107 abstract protected function strings( array $strings );
118 if ( count( $namespaces ) == 1 ) {
119 $ns = $namespaces[0];
127 if ( Hooks::runner()->onPrefixSearchBackend(
128 $namespaces, $search, $limit, $srchres, $offset )
133 $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
136 private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
137 if ( $offset === 0 ) {
143 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
157 $searchParts = explode(
'/', $search, 2 );
158 $searchKey = $searchParts[0];
159 $subpageSearch = $searchParts[1] ??
null;
162 $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
163 if ( $subpageSearch !==
null ) {
165 $specialTitle = Title::makeTitleSafe(
NS_SPECIAL, $searchKey );
166 if ( !$specialTitle ) {
169 $special = $spFactory->getPage( $specialTitle->getText() );
171 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
172 return array_map( [ $specialTitle,
'getSubpage' ], $subpages );
178 # normalize searchKey, so aliases with spaces can be found - T27675
179 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
180 $searchKey = str_replace(
' ',
'_', $searchKey );
181 $searchKey = $contLang->caseFold( $searchKey );
186 foreach ( $spFactory->getNames() as $page ) {
187 $keys[$contLang->caseFold( $page )] = [
'page' => $page,
'rank' => 0 ];
190 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
191 if ( !in_array( $page, $spFactory->getNames() ) ) {# T22885
195 foreach ( $aliases as $key => $alias ) {
196 $keys[$contLang->caseFold( $alias )] = [
'page' => $alias,
'rank' => $key ];
202 foreach (
$keys as $pageKey => $page ) {
203 if ( $searchKey ===
'' || strpos( $pageKey, $searchKey ) === 0 ) {
223 return array_slice(
$matches, $offset, $limit );
239 if ( !$namespaces ) {
252 if ( $search ===
'' ) {
253 $prefixes[$search] = $namespaces;
256 $search = preg_replace( MediaWikiTitleCodec::getTitleInvalidRegex(),
'', $search );
257 foreach ( $namespaces as $namespace ) {
258 $title = Title::makeTitleSafe( $namespace, $search );
260 $prefixes[
$title->getDBkey() ][] = $namespace;
272 foreach ( $prefixes as $prefix => $namespaces ) {
273 $condition = [
'page_namespace' => $namespaces ];
274 if ( $prefix !==
'' ) {
275 $condition[] =
'page_title' .
$dbr->buildLike( $prefix,
$dbr->anyString() );
281 $fields = [
'page_id',
'page_namespace',
'page_title' ];
285 'ORDER BY' => [
'page_title',
'page_namespace' ],
289 $res =
$dbr->select( $table, $fields, $conds, __METHOD__, $options );
302 $validNamespaces = MediaWikiServices::getInstance()->getContentLanguage()->getNamespaces();
303 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
305 foreach ( $namespaces as $ns ) {
306 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
310 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...
static newFromResult( $res)