MediaWiki  1.28.0
SearchNearMatcher.php
Go to the documentation of this file.
1 <?php
2 
12  protected $config;
13 
18  private $language;
19 
20  public function __construct( Config $config, Language $lang ) {
21  $this->config = $config;
22  $this->language = $lang;
23  }
24 
32  public function getNearMatch( $searchterm ) {
33  $title = $this->getNearMatchInternal( $searchterm );
34 
35  Hooks::run( 'SearchGetNearMatchComplete', [ $searchterm, &$title ] );
36  return $title;
37  }
38 
46  public function getNearMatchResultSet( $searchterm ) {
47  return new SearchNearMatchResultSet( $this->getNearMatch( $searchterm ) );
48  }
49 
55  protected function getNearMatchInternal( $searchterm ) {
57 
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  foreach ( $allSearchTerms as $term ) {
73 
74  # Exact match? No need to look further.
75  $title = Title::newFromText( $term );
76  if ( is_null( $title ) ) {
77  return null;
78  }
79 
80  # Try files if searching in the Media: namespace
81  if ( $title->getNamespace() == NS_MEDIA ) {
82  $title = Title::makeTitle( NS_FILE, $title->getText() );
83  }
84 
85  if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
86  return $title;
87  }
88 
89  # See if it still otherwise has content is some sane sense
91  if ( $page->hasViewableContent() ) {
92  return $title;
93  }
94 
95  if ( !Hooks::run( 'SearchAfterNoDirectMatch', [ $term, &$title ] ) ) {
96  return $title;
97  }
98 
99  # Now try all lower case (i.e. first letter capitalized)
100  $title = Title::newFromText( $lang->lc( $term ) );
101  if ( $title && $title->exists() ) {
102  return $title;
103  }
104 
105  # Now try capitalized string
106  $title = Title::newFromText( $lang->ucwords( $term ) );
107  if ( $title && $title->exists() ) {
108  return $title;
109  }
110 
111  # Now try all upper case
112  $title = Title::newFromText( $lang->uc( $term ) );
113  if ( $title && $title->exists() ) {
114  return $title;
115  }
116 
117  # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
118  $title = Title::newFromText( $lang->ucwordbreaks( $term ) );
119  if ( $title && $title->exists() ) {
120  return $title;
121  }
122 
123  // Give hooks a chance at better match variants
124  $title = null;
125  if ( !Hooks::run( 'SearchGetNearMatch', [ $term, &$title ] ) ) {
126  return $title;
127  }
128  }
129 
130  $title = Title::newFromText( $searchterm );
131 
132  # Entering an IP address goes to the contributions page
133  if ( $this->config->get( 'EnableSearchContributorsByIP' ) ) {
134  if ( ( $title->getNamespace() == NS_USER && User::isIP( $title->getText() ) )
135  || User::isIP( trim( $searchterm ) ) ) {
136  return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() );
137  }
138  }
139 
140  # Entering a user goes to the user page whether it's there or not
141  if ( $title->getNamespace() == NS_USER ) {
142  return $title;
143  }
144 
145  # Go to images that exist even if there's no local page.
146  # There may have been a funny upload, or it may be on a shared
147  # file repository such as Wikimedia Commons.
148  if ( $title->getNamespace() == NS_FILE ) {
149  $image = wfFindFile( $title );
150  if ( $image ) {
151  return $title;
152  }
153  }
154 
155  # MediaWiki namespace? Page may be "implied" if not customized.
156  # Just return it, with caps forced as the message system likes it.
157  if ( $title->getNamespace() == NS_MEDIAWIKI ) {
158  return Title::makeTitle( NS_MEDIAWIKI, $lang->ucfirst( $title->getText() ) );
159  }
160 
161  # Quoted term? Try without the quotes...
162  $matches = [];
163  if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
164  return self::getNearMatch( $matches[1] );
165  }
166 
167  return null;
168  }
169 }
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:115
external whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2713
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
if(!isset($args[0])) $lang
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
getNearMatch($searchterm)
If an exact title match can be found, or a very slightly close match, return the title.
getNearMatchInternal($searchterm)
Really find the title match.
const NS_MEDIA
Definition: Defines.php:44
getNearMatchResultSet($searchterm)
Do a near match (see SearchEngine::getNearMatch) and wrap it into a SearchResultSet.
static isIP($name)
Does the string match an anonymous IP address?
Definition: User.php:788
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
Language $language
Current language.
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:62
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
const NS_MEDIAWIKI
Definition: Defines.php:64
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
Implementation of near match title search.
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:802
__construct(Config $config, Language $lang)
A SearchResultSet wrapper for SearchNearMatcher.
wfFindFile($title, $options=[])
Find a file.
$config
Configuration object.
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511
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 $page
Definition: hooks.txt:2491
$matches