42 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
43 $search = trim( $search );
44 if ( $search ==
'' ) {
49 if ( $hasNamespace !==
false ) {
50 list( $search, $namespaces ) = $hasNamespace;
53 return $this->
searchBackend( $namespaces, $search, $limit, $offset );
66 $searches = $this->
search( $search, $limit, $namespaces, $offset );
69 $fallbackLimit = $limit - count( $searches );
70 if ( $fallbackLimit > 0 ) {
71 $fallbackSearches = MediaWikiServices::getInstance()->getContentLanguage()->
72 autoConvertToAllVariants( $search );
73 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
75 foreach ( $fallbackSearches as $fbs ) {
76 $fallbackSearchResult = $this->
search( $fbs, $fallbackLimit, $namespaces );
77 $searches = array_merge( $searches, $fallbackSearchResult );
78 $fallbackLimit -= count( $fallbackSearchResult );
80 if ( $fallbackLimit == 0 ) {
95 abstract protected function titles( array $titles );
105 abstract protected function strings( array $strings );
116 if ( count( $namespaces ) == 1 ) {
117 $ns = $namespaces[0];
126 'PrefixSearchBackend',
127 [ $namespaces, $search, $limit, &$srchres, $offset ]
136 if ( $offset === 0 ) {
142 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
156 $searchParts = explode(
'/', $search, 2 );
157 $searchKey = $searchParts[0];
158 $subpageSearch = $searchParts[1] ?? null;
161 $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
162 if ( $subpageSearch !== null ) {
165 if ( !$specialTitle ) {
168 $special = $spFactory->getPage( $specialTitle->getText() );
170 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
171 return array_map(
function ( $sub ) use ( $specialTitle ) {
172 return $specialTitle->getSubpage( $sub );
179 # normalize searchKey, so aliases with spaces can be found - T27675 180 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
181 $searchKey = str_replace(
' ',
'_', $searchKey );
182 $searchKey = $contLang->caseFold( $searchKey );
187 foreach ( $spFactory->getNames() as $page ) {
188 $keys[$contLang->caseFold( $page )] = [
'page' => $page,
'rank' => 0 ];
191 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
192 if ( !in_array( $page, $spFactory->getNames() ) ) {# T22885
196 foreach ( $aliases as $key => $alias ) {
197 $keys[$contLang->caseFold( $alias )] = [
'page' => $alias,
'rank' => $key ];
203 foreach (
$keys as $pageKey => $page ) {
204 if ( $searchKey ===
'' || strpos( $pageKey, $searchKey ) === 0 ) {
224 return array_slice(
$matches, $offset, $limit );
241 if ( $namespaces === null ) {
244 if ( !$namespaces ) {
251 foreach ( $namespaces as $namespace ) {
260 $prefixes[$prefix][] = $namespace;
267 foreach ( $prefixes as $prefix => $namespaces ) {
269 'page_namespace' => $namespaces,
270 'page_title' .
$dbr->buildLike( $prefix,
$dbr->anyString() ),
276 $fields = [
'page_id',
'page_namespace',
'page_title' ];
280 'ORDER BY' => [
'page_title',
'page_namespace' ],
284 $res =
$dbr->select( $table, $fields, $conds, __METHOD__, $options );
297 $validNamespaces = MediaWikiServices::getInstance()->getContentLanguage()->getNamespaces();
298 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
300 foreach ( $namespaces as $ns ) {
301 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
305 if ( count( $valid ) > 0 ) {
static parseNamespacePrefixes( $query, $withAllKeyword=true, $withPrefixSearchExtractNamespaceHook=false)
Parse some common prefixes: all (search everything) or namespace names.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
specialSearch( $search, $limit, $offset)
Prefix search special-case for Special: namespace.
strings(array $strings)
When implemented in a descendant class, receives an array of titles as strings and returns either an ...
An utility class to rescore search results by looking for an exact match in the db and add the page f...
static newFromResult( $res)
searchBackend( $namespaces, $search, $limit, $offset)
Do a prefix search of titles and return a list of matching page names.
Handles searching prefixes of titles and finding any page names that match.
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
search( $search, $limit, $namespaces=[], $offset=0)
Do a prefix search of titles and return a list of matching page names.
handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset)
titles(array $titles)
When implemented in a descendant class, receives an array of Title objects and returns either an unmo...
validateNamespaces( $namespaces)
Validate an array of numerical namespace indexes.
defaultSearchBackend( $namespaces, $search, $limit, $offset)
Unless overridden by PrefixSearchBackend hook...
searchWithVariants( $search, $limit, array $namespaces, $offset=0)
Do a prefix search for all possible variants of the prefix.
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.