MediaWiki  1.34.0
PrefixSearch.php
Go to the documentation of this file.
1 <?php
24 
32 abstract class PrefixSearch {
42  public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
43  $search = trim( $search );
44  if ( $search == '' ) {
45  return []; // Return empty result
46  }
47 
48  $hasNamespace = SearchEngine::parseNamespacePrefixes( $search, false, true );
49  if ( $hasNamespace !== false ) {
50  list( $search, $namespaces ) = $hasNamespace;
51  }
52 
53  return $this->searchBackend( $namespaces, $search, $limit, $offset );
54  }
55 
65  public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
66  $searches = $this->search( $search, $limit, $namespaces, $offset );
67 
68  // if the content language has variants, try to retrieve fallback results
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 ] );
74 
75  foreach ( $fallbackSearches as $fbs ) {
76  $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
77  $searches = array_merge( $searches, $fallbackSearchResult );
78  $fallbackLimit -= count( $fallbackSearchResult );
79 
80  if ( $fallbackLimit == 0 ) {
81  break;
82  }
83  }
84  }
85  return $searches;
86  }
87 
95  abstract protected function titles( array $titles );
96 
105  abstract protected function strings( array $strings );
106 
115  protected function searchBackend( $namespaces, $search, $limit, $offset ) {
116  if ( count( $namespaces ) == 1 ) {
117  $ns = $namespaces[0];
118  if ( $ns == NS_MEDIA ) {
119  $namespaces = [ NS_FILE ];
120  } elseif ( $ns == NS_SPECIAL ) {
121  return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
122  }
123  }
124  $srchres = [];
125  if ( Hooks::run(
126  'PrefixSearchBackend',
127  [ $namespaces, $search, $limit, &$srchres, $offset ]
128  ) ) {
129  return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
130  }
131  return $this->strings(
132  $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
133  }
134 
135  private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
136  if ( $offset === 0 ) {
137  // Only perform exact db match if offset === 0
138  // This is still far from perfect but at least we avoid returning the
139  // same title afain and again when the user is scrolling with a query
140  // that matches a title in the db.
141  $rescorer = new SearchExactMatchRescorer();
142  $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
143  }
144  return $srchres;
145  }
146 
155  protected function specialSearch( $search, $limit, $offset ) {
156  $searchParts = explode( '/', $search, 2 );
157  $searchKey = $searchParts[0];
158  $subpageSearch = $searchParts[1] ?? null;
159 
160  // Handle subpage search separately.
161  $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
162  if ( $subpageSearch !== null ) {
163  // Try matching the full search string as a page name
164  $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
165  if ( !$specialTitle ) {
166  return [];
167  }
168  $special = $spFactory->getPage( $specialTitle->getText() );
169  if ( $special ) {
170  $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
171  return array_map( function ( $sub ) use ( $specialTitle ) {
172  return $specialTitle->getSubpage( $sub );
173  }, $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  // Backwards compatability with old code. Default to NS_MAIN if no namespaces provided.
241  if ( $namespaces === null ) {
242  $namespaces = [];
243  }
244  if ( !$namespaces ) {
245  $namespaces[] = NS_MAIN;
246  }
247 
248  // Construct suitable prefix for each namespace. They differ in cases where
249  // some namespaces always capitalize and some don't.
250  $prefixes = [];
251  foreach ( $namespaces as $namespace ) {
252  // For now, if special is included, ignore the other namespaces
253  if ( $namespace == NS_SPECIAL ) {
254  return $this->specialSearch( $search, $limit, $offset );
255  }
256 
257  $title = Title::makeTitleSafe( $namespace, $search );
258  // Why does the prefix default to empty?
259  $prefix = $title ? $title->getDBkey() : '';
260  $prefixes[$prefix][] = $namespace;
261  }
262 
263  $dbr = wfGetDB( DB_REPLICA );
264  // Often there is only one prefix that applies to all requested namespaces,
265  // but sometimes there are two if some namespaces do not always capitalize.
266  $conds = [];
267  foreach ( $prefixes as $prefix => $namespaces ) {
268  $condition = [
269  'page_namespace' => $namespaces,
270  'page_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
271  ];
272  $conds[] = $dbr->makeList( $condition, LIST_AND );
273  }
274 
275  $table = 'page';
276  $fields = [ 'page_id', 'page_namespace', 'page_title' ];
277  $conds = $dbr->makeList( $conds, LIST_OR );
278  $options = [
279  'LIMIT' => $limit,
280  'ORDER BY' => [ 'page_title', 'page_namespace' ],
281  'OFFSET' => $offset
282  ];
283 
284  $res = $dbr->select( $table, $fields, $conds, __METHOD__, $options );
285 
286  return iterator_to_array( TitleArray::newFromResult( $res ) );
287  }
288 
295  protected function validateNamespaces( $namespaces ) {
296  // We will look at each given namespace against content language namespaces
297  $validNamespaces = MediaWikiServices::getInstance()->getContentLanguage()->getNamespaces();
298  if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
299  $valid = [];
300  foreach ( $namespaces as $ns ) {
301  if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
302  $valid[] = $ns;
303  }
304  }
305  if ( count( $valid ) > 0 ) {
306  return $valid;
307  }
308  }
309 
310  return [ NS_MAIN ];
311  }
312 }
PrefixSearch\specialSearch
specialSearch( $search, $limit, $offset)
Prefix search special-case for Special: namespace.
Definition: PrefixSearch.php:155
TitleArray\newFromResult
static newFromResult( $res)
Definition: TitleArray.php:42
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
NS_FILE
const NS_FILE
Definition: Defines.php:66
PrefixSearch\handleResultFromHook
handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset)
Definition: PrefixSearch.php:135
SearchEngine\parseNamespacePrefixes
static parseNamespacePrefixes( $query, $withAllKeyword=true, $withPrefixSearchExtractNamespaceHook=false)
Parse some common prefixes: all (search everything) or namespace names.
Definition: SearchEngine.php:378
$res
$res
Definition: testCompression.php:52
LIST_AND
const LIST_AND
Definition: Defines.php:39
$dbr
$dbr
Definition: testCompression.php:50
NS_MAIN
const NS_MAIN
Definition: Defines.php:60
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:49
SearchExactMatchRescorer
An utility class to rescore search results by looking for an exact match in the db and add the page f...
Definition: SearchExactMatchRescorer.php:31
LIST_OR
const LIST_OR
Definition: Defines.php:42
PrefixSearch\searchWithVariants
searchWithVariants( $search, $limit, array $namespaces, $offset=0)
Do a prefix search for all possible variants of the prefix.
Definition: PrefixSearch.php:65
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
$matches
$matches
Definition: NoLocalSettings.php:24
PrefixSearch\search
search( $search, $limit, $namespaces=[], $offset=0)
Do a prefix search of titles and return a list of matching page names.
Definition: PrefixSearch.php:42
PrefixSearch\strings
strings(array $strings)
When implemented in a descendant class, receives an array of titles as strings and returns either an ...
$title
$title
Definition: testCompression.php:34
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:613
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:48
PrefixSearch\validateNamespaces
validateNamespaces( $namespaces)
Validate an array of numerical namespace indexes.
Definition: PrefixSearch.php:295
PrefixSearch\searchBackend
searchBackend( $namespaces, $search, $limit, $offset)
Do a prefix search of titles and return a list of matching page names.
Definition: PrefixSearch.php:115
PrefixSearch\defaultSearchBackend
defaultSearchBackend( $namespaces, $search, $limit, $offset)
Unless overridden by PrefixSearchBackend hook...
Definition: PrefixSearch.php:239
PrefixSearch
Handles searching prefixes of titles and finding any page names that match.
Definition: PrefixSearch.php:32
$keys
$keys
Definition: testCompression.php:67
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
PrefixSearch\titles
titles(array $titles)
When implemented in a descendant class, receives an array of Title objects and returns either an unmo...