MediaWiki  1.34.0
SearchNearMatcher.php
Go to the documentation of this file.
1 <?php
2 
4 
13  protected $config;
14 
19  private $language;
20 
21  public function __construct( Config $config, Language $lang ) {
22  $this->config = $config;
23  $this->language = $lang;
24  }
25 
33  public function getNearMatch( $searchterm ) {
34  $title = $this->getNearMatchInternal( $searchterm );
35 
36  Hooks::run( 'SearchGetNearMatchComplete', [ $searchterm, &$title ] );
37  return $title;
38  }
39 
47  public function getNearMatchResultSet( $searchterm ) {
48  return new SearchNearMatchResultSet( $this->getNearMatch( $searchterm ) );
49  }
50 
56  protected function getNearMatchInternal( $searchterm ) {
58  $allSearchTerms = [ $searchterm ];
59 
60  if ( $lang->hasVariants() ) {
61  $allSearchTerms = array_unique( array_merge(
62  $allSearchTerms,
63  $lang->autoConvertToAllVariants( $searchterm )
64  ) );
65  }
66 
67  $titleResult = null;
68  if ( !Hooks::run( 'SearchGetNearMatchBefore', [ $allSearchTerms, &$titleResult ] ) ) {
69  return $titleResult;
70  }
71 
72  // Most of our handling here deals with finding a valid title for the search term,
73  // but almost anything starting with '#' is "valid" and points to Main_Page#searchterm.
74  // Rather than doing something completely wrong, do nothing.
75  if ( $searchterm === '' || $searchterm[0] === '#' ) {
76  return null;
77  }
78 
79  foreach ( $allSearchTerms as $term ) {
80  # Exact match? No need to look further.
81  $title = Title::newFromText( $term );
82  if ( is_null( $title ) ) {
83  return null;
84  }
85 
86  # Try files if searching in the Media: namespace
87  if ( $title->getNamespace() == NS_MEDIA ) {
88  $title = Title::makeTitle( NS_FILE, $title->getText() );
89  }
90 
91  if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
92  return $title;
93  }
94 
95  # See if it still otherwise has content is some sane sense
96  $page = WikiPage::factory( $title );
97  if ( $page->hasViewableContent() ) {
98  return $title;
99  }
100 
101  if ( !Hooks::run( 'SearchAfterNoDirectMatch', [ $term, &$title ] ) ) {
102  return $title;
103  }
104 
105  # Now try all lower case (i.e. first letter capitalized)
106  $title = Title::newFromText( $lang->lc( $term ) );
107  if ( $title && $title->exists() ) {
108  return $title;
109  }
110 
111  # Now try capitalized string
112  $title = Title::newFromText( $lang->ucwords( $term ) );
113  if ( $title && $title->exists() ) {
114  return $title;
115  }
116 
117  # Now try all upper case
118  $title = Title::newFromText( $lang->uc( $term ) );
119  if ( $title && $title->exists() ) {
120  return $title;
121  }
122 
123  # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
124  $title = Title::newFromText( $lang->ucwordbreaks( $term ) );
125  if ( $title && $title->exists() ) {
126  return $title;
127  }
128 
129  // Give hooks a chance at better match variants
130  $title = null;
131  if ( !Hooks::run( 'SearchGetNearMatch', [ $term, &$title ] ) ) {
132  return $title;
133  }
134  }
135 
136  $title = Title::newFromText( $searchterm );
137 
138  # Entering an IP address goes to the contributions page
139  if ( $this->config->get( 'EnableSearchContributorsByIP' ) ) {
140  if ( ( $title->getNamespace() == NS_USER && User::isIP( $title->getText() ) )
141  || User::isIP( trim( $searchterm ) ) ) {
142  return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() );
143  }
144  }
145 
146  # Entering a user goes to the user page whether it's there or not
147  if ( $title->getNamespace() == NS_USER ) {
148  return $title;
149  }
150 
151  # Go to images that exist even if there's no local page.
152  # There may have been a funny upload, or it may be on a shared
153  # file repository such as Wikimedia Commons.
154  if ( $title->getNamespace() == NS_FILE ) {
155  $image = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $title );
156  if ( $image ) {
157  return $title;
158  }
159  }
160 
161  # MediaWiki namespace? Page may be "implied" if not customized.
162  # Just return it, with caps forced as the message system likes it.
163  if ( $title->getNamespace() == NS_MEDIAWIKI ) {
164  return Title::makeTitle( NS_MEDIAWIKI, $lang->ucfirst( $title->getText() ) );
165  }
166 
167  # Quoted term? Try without the quotes...
168  $matches = [];
169  if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
170  return $this->getNearMatch( $matches[1] );
171  }
172 
173  return null;
174  }
175 }
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:316
SearchNearMatcher\getNearMatchInternal
getNearMatchInternal( $searchterm)
Really find the title match.
Definition: SearchNearMatcher.php:56
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
NS_FILE
const NS_FILE
Definition: Defines.php:66
SearchNearMatcher\getNearMatch
getNearMatch( $searchterm)
If an exact title match can be found, or a very slightly close match, return the title.
Definition: SearchNearMatcher.php:33
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:83
Config
Interface for configuration instances.
Definition: Config.php:28
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
SearchNearMatcher
Implementation of near match title search.
Definition: SearchNearMatcher.php:9
$matches
$matches
Definition: NoLocalSettings.php:24
User\isIP
static isIP( $name)
Does the string match an anonymous IP address?
Definition: User.php:891
SearchNearMatchResultSet
A ISearchResultSet wrapper for SearchNearMatcher.
Definition: SearchNearMatchResultSet.php:5
SearchNearMatcher\getNearMatchResultSet
getNearMatchResultSet( $searchterm)
Do a near match (see SearchEngine::getNearMatch) and wrap it into a ISearchResultSet.
Definition: SearchNearMatcher.php:47
$title
$title
Definition: testCompression.php:34
SearchNearMatcher\$language
Language $language
Current language.
Definition: SearchNearMatcher.php:19
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:48
SearchNearMatcher\$config
Config $config
Definition: SearchNearMatcher.php:13
SearchNearMatcher\__construct
__construct(Config $config, Language $lang)
Definition: SearchNearMatcher.php:21
NS_USER
const NS_USER
Definition: Defines.php:62
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:68
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
Language
Internationalisation code.
Definition: Language.php:37