MediaWiki  master
PrefixSearch.php
Go to the documentation of this file.
1 <?php
28 
37 abstract class PrefixSearch {
47  public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
48  $search = trim( $search );
49  if ( $search == '' ) {
50  return []; // Return empty result
51  }
52 
53  $hasNamespace = SearchEngine::parseNamespacePrefixes( $search, false, true );
54  if ( $hasNamespace !== false ) {
55  [ $search, $namespaces ] = $hasNamespace;
56  }
57 
58  return $this->searchBackend( $namespaces, $search, $limit, $offset );
59  }
60 
70  public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
71  $searches = $this->search( $search, $limit, $namespaces, $offset );
72 
73  // if the content language has variants, try to retrieve fallback results
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 ] );
81 
82  foreach ( $fallbackSearches as $fbs ) {
83  $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
84  $searches = array_merge( $searches, $fallbackSearchResult );
85  $fallbackLimit -= count( $fallbackSearchResult );
86 
87  if ( $fallbackLimit == 0 ) {
88  break;
89  }
90  }
91  }
92  return $searches;
93  }
94 
102  abstract protected function titles( array $titles );
103 
111  abstract protected function strings( array $strings );
112 
121  protected function searchBackend( $namespaces, $search, $limit, $offset ) {
122  if ( count( $namespaces ) == 1 ) {
123  $ns = $namespaces[0];
124  if ( $ns == NS_MEDIA ) {
125  $namespaces = [ NS_FILE ];
126  } elseif ( $ns == NS_SPECIAL ) {
127  return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
128  }
129  }
130  $srchres = [];
131  if ( ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onPrefixSearchBackend(
132  $namespaces, $search, $limit, $srchres, $offset )
133  ) {
134  return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
135  }
136  return $this->strings(
137  $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
138  }
139 
140  private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
141  if ( $offset === 0 ) {
142  // Only perform exact db match if offset === 0
143  // This is still far from perfect but at least we avoid returning the
144  // same title again and again when the user is scrolling with a query
145  // that matches a title in the db.
146  $rescorer = new SearchExactMatchRescorer();
147  $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
148  }
149  return $srchres;
150  }
151 
160  protected function specialSearch( $search, $limit, $offset ) {
161  $searchParts = explode( '/', $search, 2 );
162  $searchKey = $searchParts[0];
163  $subpageSearch = $searchParts[1] ?? null;
164 
165  // Handle subpage search separately.
166  $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
167  if ( $subpageSearch !== null ) {
168  // Try matching the full search string as a page name
169  $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
170  if ( !$specialTitle ) {
171  return [];
172  }
173  $special = $spFactory->getPage( $specialTitle->getText() );
174  if ( $special ) {
175  $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
176  return array_map( [ $specialTitle, 'getSubpage' ], $subpages );
177  } else {
178  return [];
179  }
180  }
181 
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 );
186 
187  // Unlike SpecialPage itself, we want the canonical forms of both
188  // canonical and alias title forms...
189  $keys = [];
190  foreach ( $spFactory->getNames() as $page ) {
191  $keys[$contLang->caseFold( $page )] = [ 'page' => $page, 'rank' => 0 ];
192  }
193 
194  foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
195  if ( !in_array( $page, $spFactory->getNames() ) ) {# T22885
196  continue;
197  }
198 
199  foreach ( $aliases as $key => $alias ) {
200  $keys[$contLang->caseFold( $alias )] = [ 'page' => $alias, 'rank' => $key ];
201  }
202  }
203  ksort( $keys );
204 
205  $matches = [];
206  foreach ( $keys as $pageKey => $page ) {
207  if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
208  // T29671: Don't use SpecialPage::getTitleFor() here because it
209  // localizes its input leading to searches for e.g. Special:All
210  // returning Spezial:MediaWiki-Systemnachrichten and returning
211  // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
212  $matches[$page['rank']][] = Title::makeTitleSafe( NS_SPECIAL, $page['page'] );
213 
214  if ( isset( $matches[0] ) && count( $matches[0] ) >= $limit + $offset ) {
215  // We have enough items in primary rank, no use to continue
216  break;
217  }
218  }
219 
220  }
221 
222  // Ensure keys are in order
223  ksort( $matches );
224  // Flatten the array
225  $matches = array_reduce( $matches, 'array_merge', [] );
226 
227  return array_slice( $matches, $offset, $limit );
228  }
229 
242  public function defaultSearchBackend( $namespaces, $search, $limit, $offset ) {
243  if ( !$namespaces ) {
244  $namespaces = [ NS_MAIN ];
245  }
246 
247  if ( in_array( NS_SPECIAL, $namespaces ) ) {
248  // For now, if special is included, ignore the other namespaces
249  return $this->specialSearch( $search, $limit, $offset );
250  }
251 
252  // Construct suitable prefix for each namespace. They differ in cases where
253  // some namespaces always capitalize and some don't.
254  $prefixes = [];
255  // Allow to do a prefix search for e.g. "Talk:"
256  if ( $search === '' ) {
257  $prefixes[$search] = $namespaces;
258  } else {
259  // Don't just ignore input like "[[Foo]]", but try to search for "Foo"
260  $search = preg_replace( MediaWikiTitleCodec::getTitleInvalidRegex(), '', $search );
261  foreach ( $namespaces as $namespace ) {
262  $title = Title::makeTitleSafe( $namespace, $search );
263  if ( $title ) {
264  $prefixes[ $title->getDBkey() ][] = $namespace;
265  }
266  }
267  }
268  if ( !$prefixes ) {
269  return [];
270  }
271 
272  $dbr = wfGetDB( DB_REPLICA );
273  // Often there is only one prefix that applies to all requested namespaces,
274  // but sometimes there are two if some namespaces do not always capitalize.
275  $conds = [];
276  foreach ( $prefixes as $prefix => $namespaces ) {
277  $condition = [ 'page_namespace' => $namespaces ];
278  if ( $prefix !== '' ) {
279  $condition[] = 'page_title' . $dbr->buildLike( $prefix, $dbr->anyString() );
280  }
281  $conds[] = $dbr->makeList( $condition, LIST_AND );
282  }
283 
284  $queryBuilder = $dbr->newSelectQueryBuilder()
285  ->select( [ 'page_id', 'page_namespace', 'page_title' ] )
286  ->from( 'page' )
287  ->where( $dbr->makeList( $conds, LIST_OR ) )
288  ->orderBy( [ 'page_title', 'page_namespace' ] )
289  ->limit( $limit )
290  ->offset( $offset );
291  $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
292 
293  return iterator_to_array( TitleArray::newFromResult( $res ) );
294  }
295 }
const NS_FILE
Definition: Defines.php:70
const NS_MAIN
Definition: Defines.php:64
const NS_SPECIAL
Definition: Defines.php:53
const LIST_OR
Definition: Defines.php:46
const NS_MEDIA
Definition: Defines.php:52
const LIST_AND
Definition: Defines.php:43
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
$matches
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:568
Service locator for MediaWiki core services.
A codec for MediaWiki page titles.
The TitleArray class only exists to provide the newFromResult method at pre- sent.
Definition: TitleArray.php:40
Represents a title within MediaWiki.
Definition: Title.php:76
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...
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.
static parseNamespacePrefixes( $query, $withAllKeyword=true, $withPrefixSearchExtractNamespaceHook=false)
Parse some common prefixes: all (search everything) or namespace names.
An utility class to rescore search results by looking for an exact match in the db and add the page f...
const DB_REPLICA
Definition: defines.php:26