MediaWiki  REL1_31
SearchNearMatcher.php
Go to the documentation of this file.
1 <?php
2 
11  protected $config;
12 
17  private $language;
18 
19  public function __construct( Config $config, Language $lang ) {
20  $this->config = $config;
21  $this->language = $lang;
22  }
23 
31  public function getNearMatch( $searchterm ) {
32  $title = $this->getNearMatchInternal( $searchterm );
33 
34  Hooks::run( 'SearchGetNearMatchComplete', [ $searchterm, &$title ] );
35  return $title;
36  }
37 
45  public function getNearMatchResultSet( $searchterm ) {
46  return new SearchNearMatchResultSet( $this->getNearMatch( $searchterm ) );
47  }
48 
54  protected function getNearMatchInternal( $searchterm ) {
56 
57  $allSearchTerms = [ $searchterm ];
58 
59  if ( $lang->hasVariants() ) {
60  $allSearchTerms = array_unique( array_merge(
61  $allSearchTerms,
62  $lang->autoConvertToAllVariants( $searchterm )
63  ) );
64  }
65 
66  $titleResult = null;
67  if ( !Hooks::run( 'SearchGetNearMatchBefore', [ $allSearchTerms, &$titleResult ] ) ) {
68  return $titleResult;
69  }
70 
71  foreach ( $allSearchTerms as $term ) {
72  # Exact match? No need to look further.
74  if ( is_null( $title ) ) {
75  return null;
76  }
77 
78  # Try files if searching in the Media: namespace
79  if ( $title->getNamespace() == NS_MEDIA ) {
80  $title = Title::makeTitle( NS_FILE, $title->getText() );
81  }
82 
83  if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
84  return $title;
85  }
86 
87  # See if it still otherwise has content is some sane sense
88  $page = WikiPage::factory( $title );
89  if ( $page->hasViewableContent() ) {
90  return $title;
91  }
92 
93  if ( !Hooks::run( 'SearchAfterNoDirectMatch', [ $term, &$title ] ) ) {
94  return $title;
95  }
96 
97  # Now try all lower case (i.e. first letter capitalized)
99  if ( $title && $title->exists() ) {
100  return $title;
101  }
102 
103  # Now try capitalized string
104  $title = Title::newFromText( $lang->ucwords( $term ) );
105  if ( $title && $title->exists() ) {
106  return $title;
107  }
108 
109  # Now try all upper case
110  $title = Title::newFromText( $lang->uc( $term ) );
111  if ( $title && $title->exists() ) {
112  return $title;
113  }
114 
115  # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
116  $title = Title::newFromText( $lang->ucwordbreaks( $term ) );
117  if ( $title && $title->exists() ) {
118  return $title;
119  }
120 
121  // Give hooks a chance at better match variants
122  $title = null;
123  if ( !Hooks::run( 'SearchGetNearMatch', [ $term, &$title ] ) ) {
124  return $title;
125  }
126  }
127 
128  $title = Title::newFromText( $searchterm );
129 
130  # Entering an IP address goes to the contributions page
131  if ( $this->config->get( 'EnableSearchContributorsByIP' ) ) {
132  if ( ( $title->getNamespace() == NS_USER && User::isIP( $title->getText() ) )
133  || User::isIP( trim( $searchterm ) ) ) {
134  return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() );
135  }
136  }
137 
138  # Entering a user goes to the user page whether it's there or not
139  if ( $title->getNamespace() == NS_USER ) {
140  return $title;
141  }
142 
143  # Go to images that exist even if there's no local page.
144  # There may have been a funny upload, or it may be on a shared
145  # file repository such as Wikimedia Commons.
146  if ( $title->getNamespace() == NS_FILE ) {
147  $image = wfFindFile( $title );
148  if ( $image ) {
149  return $title;
150  }
151  }
152 
153  # MediaWiki namespace? Page may be "implied" if not customized.
154  # Just return it, with caps forced as the message system likes it.
155  if ( $title->getNamespace() == NS_MEDIAWIKI ) {
156  return Title::makeTitle( NS_MEDIAWIKI, $lang->ucfirst( $title->getText() ) );
157  }
158 
159  # Quoted term? Try without the quotes...
160  $matches = [];
161  if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
162  return self::getNearMatch( $matches[1] );
163  }
164 
165  return null;
166  }
167 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:273
SearchNearMatcher\getNearMatchInternal
getNearMatchInternal( $searchterm)
Really find the title match.
Definition: SearchNearMatcher.php:54
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
NS_FILE
const NS_FILE
Definition: Defines.php:80
SearchNearMatcher\getNearMatch
getNearMatch( $searchterm)
If an exact title match can be found, or a very slightly close match, return the title.
Definition: SearchNearMatcher.php:31
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Definition: SpecialPage.php:82
php
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:37
Config
Interface for configuration instances.
Definition: Config.php:28
$term
For QUnit the mediawiki tests qunit testrunner dependency will be added to any module whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2845
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:115
SearchNearMatcher
Implementation of near match title search.
Definition: SearchNearMatcher.php:7
$matches
$matches
Definition: NoLocalSettings.php:24
User\isIP
static isIP( $name)
Does the string match an anonymous IP address?
Definition: User.php:943
SearchNearMatchResultSet
A SearchResultSet wrapper for SearchNearMatcher.
Definition: SearchNearMatchResultSet.php:5
SearchNearMatcher\getNearMatchResultSet
getNearMatchResultSet( $searchterm)
Do a near match (see SearchEngine::getNearMatch) and wrap it into a SearchResultSet.
Definition: SearchNearMatcher.php:45
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
SearchNearMatcher\$language
Language $language
Current language.
Definition: SearchNearMatcher.php:17
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:534
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:62
wfFindFile
wfFindFile( $title, $options=[])
Find a file.
Definition: GlobalFunctions.php:2853
$image
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition: hooks.txt:895
SearchNearMatcher\$config
Config $config
Definition: SearchNearMatcher.php:11
as
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:22
SearchNearMatcher\__construct
__construct(Config $config, Language $lang)
Definition: SearchNearMatcher.php:19
NS_USER
const NS_USER
Definition: Defines.php:76
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:82
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:203
Language
Internationalisation code.
Definition: Language.php:35