MediaWiki  master
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 }
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
const NS_MAIN
Definition: Defines.php:60
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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.
const NS_SPECIAL
Definition: Defines.php:49
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
strings(array $strings)
When implemented in a descendant class, receives an array of titles as strings and returns either an ...
const LIST_AND
Definition: Defines.php:39
const NS_MEDIA
Definition: Defines.php:48
$res
Definition: database.txt:21
An utility class to rescore search results by looking for an exact match in the db and add the page f...
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1972
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
static newFromResult( $res)
Definition: TitleArray.php:40
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
searchBackend( $namespaces, $search, $limit, $offset)
Do a prefix search of titles and return a list of matching page names.
const NS_FILE
Definition: Defines.php:66
namespace and then decline to actually register it & $namespaces
Definition: hooks.txt:912
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Handles searching prefixes of titles and finding any page names that match.
const LIST_OR
Definition: Defines.php:42
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:620
search( $search, $limit, $namespaces=[], $offset=0)
Do a prefix search of titles and return a list of matching page names.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a you can use the following $titles
Definition: linkcache.txt:17
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.
const DB_REPLICA
Definition: defines.php:25
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.
Definition: Hooks.php:200
$matches