MediaWiki  1.27.2
SearchEngine.php
Go to the documentation of this file.
1 <?php
29 
34 abstract class SearchEngine {
36  public $prefix = '';
37 
39  public $namespaces = [ NS_MAIN ];
40 
42  protected $limit = 10;
43 
45  protected $offset = 0;
46 
48  protected $searchTerms = [];
49 
51  protected $showSuggestion = true;
52  private $sort = 'relevance';
53 
55  protected $features = [];
56 
65  function searchText( $term ) {
66  return null;
67  }
68 
77  function searchTitle( $term ) {
78  return null;
79  }
80 
86  public function supports( $feature ) {
87  switch ( $feature ) {
88  case 'search-update':
89  return true;
90  case 'title-suffix-filter':
91  default:
92  return false;
93  }
94  }
95 
103  public function setFeatureData( $feature, $data ) {
104  $this->features[$feature] = $data;
105  }
106 
115  public function normalizeText( $string ) {
117 
118  // Some languages such as Chinese require word segmentation
119  return $wgContLang->segmentByWord( $string );
120  }
121 
129  public function transformSearchTerm( $term ) {
130  return $term;
131  }
132 
138  public function getNearMatcher( Config $config ) {
140  return new SearchNearMatcher( $config, $wgContLang );
141  }
142 
147  protected static function defaultNearMatcher() {
148  $config = MediaWikiServices::getInstance()->getMainConfig();
149  return MediaWikiServices::getInstance()->newSearchEngine()->getNearMatcher( $config );
150  }
151 
159  public static function getNearMatch( $searchterm ) {
160  return static::defaultNearMatcher()->getNearMatch( $searchterm );
161  }
162 
170  public static function getNearMatchResultSet( $searchterm ) {
171  return static::defaultNearMatcher()->getNearMatchResultSet( $searchterm );
172  }
173 
179  public static function legalSearchChars() {
180  return "A-Za-z_'.0-9\\x80-\\xFF\\-";
181  }
182 
190  function setLimitOffset( $limit, $offset = 0 ) {
191  $this->limit = intval( $limit );
192  $this->offset = intval( $offset );
193  }
194 
202  if ( $namespaces ) {
203  // Filter namespaces to only keep valid ones
204  $validNs = $this->searchableNamespaces();
205  $namespaces = array_filter( $namespaces, function( $ns ) use( $validNs ) {
206  return $ns < 0 || isset( $validNs[$ns] );
207  } );
208  } else {
209  $namespaces = [];
210  }
211  $this->namespaces = $namespaces;
212  }
213 
222  $this->showSuggestion = $showSuggestion;
223  }
224 
232  public function getValidSorts() {
233  return [ 'relevance' ];
234  }
235 
244  public function setSort( $sort ) {
245  if ( !in_array( $sort, $this->getValidSorts() ) ) {
246  throw new InvalidArgumentException( "Invalid sort: $sort. " .
247  "Must be one of: " . implode( ', ', $this->getValidSorts() ) );
248  }
249  $this->sort = $sort;
250  }
251 
258  public function getSort() {
259  return $this->sort;
260  }
261 
269  function replacePrefixes( $query ) {
271 
272  $parsed = $query;
273  if ( strpos( $query, ':' ) === false ) { // nothing to do
274  return $parsed;
275  }
276 
277  $allkeyword = wfMessage( 'searchall' )->inContentLanguage()->text() . ":";
278  if ( strncmp( $query, $allkeyword, strlen( $allkeyword ) ) == 0 ) {
279  $this->namespaces = null;
280  $parsed = substr( $query, strlen( $allkeyword ) );
281  } elseif ( strpos( $query, ':' ) !== false ) {
282  $prefix = str_replace( ' ', '_', substr( $query, 0, strpos( $query, ':' ) ) );
283  $index = $wgContLang->getNsIndex( $prefix );
284  if ( $index !== false ) {
285  $this->namespaces = [ $index ];
286  $parsed = substr( $query, strlen( $prefix ) + 1 );
287  }
288  }
289  if ( trim( $parsed ) == '' ) {
290  $parsed = $query; // prefix was the whole query
291  }
292 
293  return $parsed;
294  }
295 
300  public static function userHighlightPrefs() {
301  $contextlines = 2; // Hardcode this. Old defaults sucked. :)
302  $contextchars = 75; // same as above.... :P
303  return [ $contextlines, $contextchars ];
304  }
305 
315  function update( $id, $title, $text ) {
316  // no-op
317  }
318 
327  function updateTitle( $id, $title ) {
328  // no-op
329  }
330 
339  function delete( $id, $title ) {
340  // no-op
341  }
342 
349  public static function getOpenSearchTemplate() {
350  wfDeprecated( __METHOD__, '1.25' );
351  return ApiOpenSearch::getOpenSearchTemplate( 'application/x-suggestions+json' );
352  }
353 
364  public function getTextFromContent( Title $t, Content $c = null ) {
365  return $c ? $c->getTextForSearchIndex() : '';
366  }
367 
375  public function textAlreadyUpdatedForIndex() {
376  return false;
377  }
378 
385  protected function normalizeNamespaces( $search ) {
386  // Find a Title which is not an interwiki and is in NS_MAIN
387  $title = Title::newFromText( $search );
388  $ns = $this->namespaces;
389  if ( $title && !$title->isExternal() ) {
390  $ns = [ $title->getNamespace() ];
391  $search = $title->getText();
392  if ( $ns[0] == NS_MAIN ) {
393  $ns = $this->namespaces; // no explicit prefix, use default namespaces
394  Hooks::run( 'PrefixSearchExtractNamespace', [ &$ns, &$search ] );
395  }
396  } else {
397  $title = Title::newFromText( $search . 'Dummy' );
398  if ( $title && $title->getText() == 'Dummy'
399  && $title->getNamespace() != NS_MAIN
400  && !$title->isExternal() )
401  {
402  $ns = [ $title->getNamespace() ];
403  $search = '';
404  } else {
405  Hooks::run( 'PrefixSearchExtractNamespace', [ &$ns, &$search ] );
406  }
407  }
408 
409  $ns = array_map( function( $space ) {
410  return $space == NS_MEDIA ? NS_FILE : $space;
411  }, $ns );
412 
413  $this->setNamespaces( $ns );
414  return $search;
415  }
416 
424  protected function completionSearchBackend( $search ) {
425  $results = [];
426 
427  $search = trim( $search );
428 
429  if ( !in_array( NS_SPECIAL, $this->namespaces ) && // We do not run hook on Special: search
430  !Hooks::run( 'PrefixSearchBackend',
431  [ $this->namespaces, $search, $this->limit, &$results, $this->offset ]
432  ) ) {
433  // False means hook worked.
434  // FIXME: Yes, the API is weird. That's why it is going to be deprecated.
435 
436  return SearchSuggestionSet::fromStrings( $results );
437  } else {
438  // Hook did not do the job, use default simple search
439  $results = $this->simplePrefixSearch( $search );
440  return SearchSuggestionSet::fromTitles( $results );
441  }
442  }
443 
449  public function completionSearch( $search ) {
450  if ( trim( $search ) === '' ) {
451  return SearchSuggestionSet::emptySuggestionSet(); // Return empty result
452  }
453  $search = $this->normalizeNamespaces( $search );
454  return $this->processCompletionResults( $search, $this->completionSearchBackend( $search ) );
455  }
456 
462  public function completionSearchWithVariants( $search ) {
463  if ( trim( $search ) === '' ) {
464  return SearchSuggestionSet::emptySuggestionSet(); // Return empty result
465  }
466  $search = $this->normalizeNamespaces( $search );
467 
468  $results = $this->completionSearchBackend( $search );
469  $fallbackLimit = $this->limit - $results->getSize();
470  if ( $fallbackLimit > 0 ) {
472 
473  $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
474  $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
475 
476  foreach ( $fallbackSearches as $fbs ) {
477  $this->setLimitOffset( $fallbackLimit );
478  $fallbackSearchResult = $this->completionSearch( $fbs );
479  $results->appendAll( $fallbackSearchResult );
480  $fallbackLimit -= count( $fallbackSearchResult );
481  if ( $fallbackLimit <= 0 ) {
482  break;
483  }
484  }
485  }
486  return $this->processCompletionResults( $search, $results );
487  }
488 
494  public function extractTitles( SearchSuggestionSet $completionResults ) {
495  return $completionResults->map( function( SearchSuggestion $sugg ) {
496  return $sugg->getSuggestedTitle();
497  } );
498  }
499 
506  protected function processCompletionResults( $search, SearchSuggestionSet $suggestions ) {
507  $search = trim( $search );
508  // preload the titles with LinkBatch
509  $titles = $suggestions->map( function( SearchSuggestion $sugg ) {
510  return $sugg->getSuggestedTitle();
511  } );
512  $lb = new LinkBatch( $titles );
513  $lb->setCaller( __METHOD__ );
514  $lb->execute();
515 
516  $results = $suggestions->map( function( SearchSuggestion $sugg ) {
517  return $sugg->getSuggestedTitle()->getPrefixedText();
518  } );
519 
520  // Rescore results with an exact title match
521  // NOTE: in some cases like cross-namespace redirects
522  // (frequently used as shortcuts e.g. WP:WP on huwiki) some
523  // backends like Cirrus will return no results. We should still
524  // try an exact title match to workaround this limitation
525  $rescorer = new SearchExactMatchRescorer();
526  $rescoredResults = $rescorer->rescore( $search, $this->namespaces, $results, $this->limit );
527 
528  if ( count( $rescoredResults ) > 0 ) {
529  $found = array_search( $rescoredResults[0], $results );
530  if ( $found === false ) {
531  // If the first result is not in the previous array it
532  // means that we found a new exact match
533  $exactMatch = SearchSuggestion::fromTitle( 0, Title::newFromText( $rescoredResults[0] ) );
534  $suggestions->prepend( $exactMatch );
535  $suggestions->shrink( $this->limit );
536  } else {
537  // if the first result is not the same we need to rescore
538  if ( $found > 0 ) {
539  $suggestions->rescore( $found );
540  }
541  }
542  }
543 
544  return $suggestions;
545  }
546 
552  public function defaultPrefixSearch( $search ) {
553  if ( trim( $search ) === '' ) {
554  return [];
555  }
556 
557  $search = $this->normalizeNamespaces( $search );
558  return $this->simplePrefixSearch( $search );
559  }
560 
567  protected function simplePrefixSearch( $search ) {
568  // Use default database prefix search
569  $backend = new TitlePrefixSearch;
570  return $backend->defaultSearchBackend( $this->namespaces, $search, $this->limit, $this->offset );
571  }
572 
578  public static function searchableNamespaces() {
579  return MediaWikiServices::getInstance()->getSearchEngineConfig()->searchableNamespaces();
580  }
581 
589  public static function userNamespaces( $user ) {
590  return MediaWikiServices::getInstance()->getSearchEngineConfig()->userNamespaces( $user );
591  }
592 
598  public static function defaultNamespaces() {
599  return MediaWikiServices::getInstance()->getSearchEngineConfig()->defaultNamespaces();
600  }
601 
609  public static function namespacesAsText( $namespaces ) {
610  return MediaWikiServices::getInstance()->getSearchEngineConfig()->namespacesAsText( $namespaces );
611  }
612 
620  public static function create( $type = null ) {
621  return MediaWikiServices::getInstance()->getSearchEngineFactory()->create( $type );
622  }
623 
630  public static function getSearchTypes() {
631  return MediaWikiServices::getInstance()->getSearchEngineConfig()->getSearchTypes();
632  }
633 
634 }
635 
643  // no-op
644 }
Dummy class to be used when non-supported Database engine is present.
getSort()
Get the sort direction of the search results.
replacePrefixes($query)
Parse some common prefixes: all (search everything) or namespace names.
string $prefix
static getNearMatchResultSet($searchterm)
Do a near match (see SearchEngine::getNearMatch) and wrap it into a SearchResultSet.
external whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2558
transformSearchTerm($term)
Transform search term in cases when parts of the query came as different GET params (when supported)...
static searchableNamespaces()
Make a list of searchable namespaces and their canonical names.
searchText($term)
Perform a full text search query and return a result set.
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1418
Search suggestion.
completionSearchBackend($search)
Perform a completion search.
static defaultNamespaces()
An array of namespaces indexes to be searched by default.
static namespacesAsText($namespaces)
Get a list of namespace names useful for showing in tooltips and preferences.
const NS_MAIN
Definition: Defines.php:69
to move a page</td >< td > &*You are moving the page across namespaces
static userNamespaces($user)
Extract default namespaces to search from the given user's settings, returning a list of index number...
static legalSearchChars()
Get chars legal for search.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
update($id, $title, $text)
Create or update the search index record for the given page.
setShowSuggestion($showSuggestion)
Set whether the searcher should try to build a suggestion.
defaultPrefixSearch($search)
Simple prefix search for subpages.
const NS_SPECIAL
Definition: Defines.php:58
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
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:277
static fromStrings(array $titles)
Builds a new set of suggestion based on a string array.
Represents a title within MediaWiki.
Definition: Title.php:34
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
getSuggestedTitle()
Title object in the case this suggestion is based on a title.
simplePrefixSearch($search)
Call out to simple search backend.
supports($feature)
completionSearchWithVariants($search)
Perform a completion search with variants.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:31
normalizeNamespaces($search)
Makes search simple string if it was namespaced.
static getOpenSearchTemplate()
Get OpenSearch suggestion template.
setLimitOffset($limit, $offset=0)
Set the maximum number of results to return and how many to skip before returning the first...
normalizeText($string)
When overridden in derived class, performs database-specific conversions on text to be used for searc...
map($callback)
Call array_map on the suggestions array.
prepend(SearchSuggestion $suggestion)
Add a new suggestion at the top.
defaultSearchBackend($namespaces, $search, $limit, $offset)
Unless overridden by PrefixSearchBackend hook...
getTextFromContent(Title $t, Content $c=null)
Get the raw text for updating the index from a content object Nicer search backends could possibly do...
array string $searchTerms
const NS_MEDIA
Definition: Defines.php:57
searchTitle($term)
Perform a title-only search query and return a result set.
An utility class to rescore search results by looking for an exact match in the db and add the page f...
Base interface for content objects.
Definition: Content.php:34
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 noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock()-offset Set to overwrite offset parameter in $wgRequest set to ''to unsetoffset-wrap String Wrap the message in html(usually something like"&lt
textAlreadyUpdatedForIndex()
If an implementation of SearchEngine handles all of its own text processing in getTextFromContent() a...
static getNearMatch($searchterm)
If an exact title match can be found, or a very slightly close match, return the title.
getValidSorts()
Get the valid sort directions.
static defaultNearMatcher()
Get near matcher for default SearchEngine.
wfDeprecated($function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
bool $showSuggestion
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
const NS_FILE
Definition: Defines.php:75
extractTitles(SearchSuggestionSet $completionResults)
Extract titles from completion results.
static getSearchTypes()
Return the search engines we support.
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
Performs prefix search, returning Title objects.
updateTitle($id, $title)
Update a search index record's title only.
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
shrink($limit)
Remove any extra elements in the suggestions set.
setSort($sort)
Set the sort direction of the search results.
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
int[] null $namespaces
setFeatureData($feature, $data)
Way to pass custom data for engines.
Implementation of near match title search.
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
array $features
Feature values.
setNamespaces($namespaces)
Set which namespaces the search should include.
completionSearch($search)
Perform a completion search.
rescore($key)
Move the suggestion at index $key to the first position.
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition: design.txt:56
Search suggestion sets.
static create($type=null)
Load up the appropriate search engine class for the currently active database backend, and return a configured instance.
getNearMatcher(Config $config)
Get service class to finding near matches.
static fromTitle($score, Title $title)
Create suggestion from Title.
static fromTitles(array $titles)
Builds a new set of suggestion based on a title array.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2338
processCompletionResults($search, SearchSuggestionSet $suggestions)
Process completion search results.
static userHighlightPrefs()
Find snippet highlight settings for all users.
static getOpenSearchTemplate($type)
Fetch the template for a type.