MediaWiki  master
PrefixSearch.php
Go to the documentation of this file.
1 <?php
25 
34 abstract class PrefixSearch {
44  public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
45  $search = trim( $search );
46  if ( $search == '' ) {
47  return []; // Return empty result
48  }
49 
50  $hasNamespace = SearchEngine::parseNamespacePrefixes( $search, false, true );
51  if ( $hasNamespace !== false ) {
52  [ $search, $namespaces ] = $hasNamespace;
53  }
54 
55  return $this->searchBackend( $namespaces, $search, $limit, $offset );
56  }
57 
67  public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
68  $searches = $this->search( $search, $limit, $namespaces, $offset );
69 
70  // if the content language has variants, try to retrieve fallback results
71  $fallbackLimit = $limit - count( $searches );
72  if ( $fallbackLimit > 0 ) {
73  $services = MediaWikiServices::getInstance();
74  $fallbackSearches = $services->getLanguageConverterFactory()
75  ->getLanguageConverter( $services->getContentLanguage() )
76  ->autoConvertToAllVariants( $search );
77  $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
78 
79  foreach ( $fallbackSearches as $fbs ) {
80  $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
81  $searches = array_merge( $searches, $fallbackSearchResult );
82  $fallbackLimit -= count( $fallbackSearchResult );
83 
84  if ( $fallbackLimit == 0 ) {
85  break;
86  }
87  }
88  }
89  return $searches;
90  }
91 
99  abstract protected function titles( array $titles );
100 
108  abstract protected function strings( array $strings );
109 
118  protected function searchBackend( $namespaces, $search, $limit, $offset ) {
119  if ( count( $namespaces ) == 1 ) {
120  $ns = $namespaces[0];
121  if ( $ns == NS_MEDIA ) {
122  $namespaces = [ NS_FILE ];
123  } elseif ( $ns == NS_SPECIAL ) {
124  return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
125  }
126  }
127  $srchres = [];
128  if ( Hooks::runner()->onPrefixSearchBackend(
129  $namespaces, $search, $limit, $srchres, $offset )
130  ) {
131  return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
132  }
133  return $this->strings(
134  $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
135  }
136 
137  private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
138  if ( $offset === 0 ) {
139  // Only perform exact db match if offset === 0
140  // This is still far from perfect but at least we avoid returning the
141  // same title again and again when the user is scrolling with a query
142  // that matches a title in the db.
143  $rescorer = new SearchExactMatchRescorer();
144  $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
145  }
146  return $srchres;
147  }
148 
157  protected function specialSearch( $search, $limit, $offset ) {
158  $searchParts = explode( '/', $search, 2 );
159  $searchKey = $searchParts[0];
160  $subpageSearch = $searchParts[1] ?? null;
161 
162  // Handle subpage search separately.
163  $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
164  if ( $subpageSearch !== null ) {
165  // Try matching the full search string as a page name
166  $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
167  if ( !$specialTitle ) {
168  return [];
169  }
170  $special = $spFactory->getPage( $specialTitle->getText() );
171  if ( $special ) {
172  $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
173  return array_map( [ $specialTitle, 'getSubpage' ], $subpages );
174  } else {
175  return [];
176  }
177  }
178 
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 );
183 
184  // Unlike SpecialPage itself, we want the canonical forms of both
185  // canonical and alias title forms...
186  $keys = [];
187  foreach ( $spFactory->getNames() as $page ) {
188  $keys[$contLang->caseFold( $page )] = [ 'page' => $page, 'rank' => 0 ];
189  }
190 
191  foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
192  if ( !in_array( $page, $spFactory->getNames() ) ) {# T22885
193  continue;
194  }
195 
196  foreach ( $aliases as $key => $alias ) {
197  $keys[$contLang->caseFold( $alias )] = [ 'page' => $alias, 'rank' => $key ];
198  }
199  }
200  ksort( $keys );
201 
202  $matches = [];
203  foreach ( $keys as $pageKey => $page ) {
204  if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
205  // T29671: Don't use SpecialPage::getTitleFor() here because it
206  // localizes its input leading to searches for e.g. Special:All
207  // returning Spezial:MediaWiki-Systemnachrichten and returning
208  // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
209  $matches[$page['rank']][] = Title::makeTitleSafe( NS_SPECIAL, $page['page'] );
210 
211  if ( isset( $matches[0] ) && count( $matches[0] ) >= $limit + $offset ) {
212  // We have enough items in primary rank, no use to continue
213  break;
214  }
215  }
216 
217  }
218 
219  // Ensure keys are in order
220  ksort( $matches );
221  // Flatten the array
222  $matches = array_reduce( $matches, 'array_merge', [] );
223 
224  return array_slice( $matches, $offset, $limit );
225  }
226 
239  public function defaultSearchBackend( $namespaces, $search, $limit, $offset ) {
240  if ( !$namespaces ) {
241  $namespaces = [ NS_MAIN ];
242  }
243 
244  if ( in_array( NS_SPECIAL, $namespaces ) ) {
245  // For now, if special is included, ignore the other namespaces
246  return $this->specialSearch( $search, $limit, $offset );
247  }
248 
249  // Construct suitable prefix for each namespace. They differ in cases where
250  // some namespaces always capitalize and some don't.
251  $prefixes = [];
252  // Allow to do a prefix search for e.g. "Talk:"
253  if ( $search === '' ) {
254  $prefixes[$search] = $namespaces;
255  } else {
256  // Don't just ignore input like "[[Foo]]", but try to search for "Foo"
257  $search = preg_replace( MediaWikiTitleCodec::getTitleInvalidRegex(), '', $search );
258  foreach ( $namespaces as $namespace ) {
259  $title = Title::makeTitleSafe( $namespace, $search );
260  if ( $title ) {
261  $prefixes[ $title->getDBkey() ][] = $namespace;
262  }
263  }
264  }
265  if ( !$prefixes ) {
266  return [];
267  }
268 
269  $dbr = wfGetDB( DB_REPLICA );
270  // Often there is only one prefix that applies to all requested namespaces,
271  // but sometimes there are two if some namespaces do not always capitalize.
272  $conds = [];
273  foreach ( $prefixes as $prefix => $namespaces ) {
274  $condition = [ 'page_namespace' => $namespaces ];
275  if ( $prefix !== '' ) {
276  $condition[] = 'page_title' . $dbr->buildLike( $prefix, $dbr->anyString() );
277  }
278  $conds[] = $dbr->makeList( $condition, LIST_AND );
279  }
280 
281  $table = 'page';
282  $fields = [ 'page_id', 'page_namespace', 'page_title' ];
283  $conds = $dbr->makeList( $conds, LIST_OR );
284  $options = [
285  'LIMIT' => $limit,
286  'ORDER BY' => [ 'page_title', 'page_namespace' ],
287  'OFFSET' => $offset
288  ];
289 
290  $res = $dbr->select( $table, $fields, $conds, __METHOD__, $options );
291 
292  return iterator_to_array( TitleArray::newFromResult( $res ) );
293  }
294 
301  protected function validateNamespaces( $namespaces ) {
302  // We will look at each given namespace against content language namespaces
303  $validNamespaces = MediaWikiServices::getInstance()->getContentLanguage()->getNamespaces();
304  if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
305  $valid = [];
306  foreach ( $namespaces as $ns ) {
307  if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
308  $valid[] = $ns;
309  }
310  }
311  if ( count( $valid ) > 0 ) {
312  return $valid;
313  }
314  }
315 
316  return [ NS_MAIN ];
317  }
318 }
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
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
static getTitleInvalidRegex()
Returns a simple regex that will match on characters and sequences invalid in titles.
Service locator for MediaWiki core services.
The TitleArray class only exists to provide the newFromResult method at pre- sent.
Definition: TitleArray.php:41
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...
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...
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:667
const DB_REPLICA
Definition: defines.php:26