MediaWiki REL1_33
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 $allSearchTerms = [ $searchterm ];
57
58 if ( $lang->hasVariants() ) {
59 $allSearchTerms = array_unique( array_merge(
60 $allSearchTerms,
61 $lang->autoConvertToAllVariants( $searchterm )
62 ) );
63 }
64
65 $titleResult = null;
66 if ( !Hooks::run( 'SearchGetNearMatchBefore', [ $allSearchTerms, &$titleResult ] ) ) {
67 return $titleResult;
68 }
69
70 // Most of our handling here deals with finding a valid title for the search term,
71 // but almost anything starting with '#' is "valid" and points to Main_Page#searchterm.
72 // Rather than doing something completely wrong, do nothing.
73 if ( $searchterm === '' || $searchterm[0] === '#' ) {
74 return null;
75 }
76
77 foreach ( $allSearchTerms as $term ) {
78 # Exact match? No need to look further.
79 $title = Title::newFromText( $term );
80 if ( is_null( $title ) ) {
81 return null;
82 }
83
84 # Try files if searching in the Media: namespace
85 if ( $title->getNamespace() == NS_MEDIA ) {
86 $title = Title::makeTitle( NS_FILE, $title->getText() );
87 }
88
89 if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
90 return $title;
91 }
92
93 # See if it still otherwise has content is some sane sense
94 $page = WikiPage::factory( $title );
95 if ( $page->hasViewableContent() ) {
96 return $title;
97 }
98
99 if ( !Hooks::run( 'SearchAfterNoDirectMatch', [ $term, &$title ] ) ) {
100 return $title;
101 }
102
103 # Now try all lower case (i.e. first letter capitalized)
104 $title = Title::newFromText( $lang->lc( $term ) );
105 if ( $title && $title->exists() ) {
106 return $title;
107 }
108
109 # Now try capitalized string
110 $title = Title::newFromText( $lang->ucwords( $term ) );
111 if ( $title && $title->exists() ) {
112 return $title;
113 }
114
115 # Now try all upper case
116 $title = Title::newFromText( $lang->uc( $term ) );
117 if ( $title && $title->exists() ) {
118 return $title;
119 }
120
121 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
122 $title = Title::newFromText( $lang->ucwordbreaks( $term ) );
123 if ( $title && $title->exists() ) {
124 return $title;
125 }
126
127 // Give hooks a chance at better match variants
128 $title = null;
129 if ( !Hooks::run( 'SearchGetNearMatch', [ $term, &$title ] ) ) {
130 return $title;
131 }
132 }
133
134 $title = Title::newFromText( $searchterm );
135
136 # Entering an IP address goes to the contributions page
137 if ( $this->config->get( 'EnableSearchContributorsByIP' ) ) {
138 if ( ( $title->getNamespace() == NS_USER && User::isIP( $title->getText() ) )
139 || User::isIP( trim( $searchterm ) ) ) {
140 return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() );
141 }
142 }
143
144 # Entering a user goes to the user page whether it's there or not
145 if ( $title->getNamespace() == NS_USER ) {
146 return $title;
147 }
148
149 # Go to images that exist even if there's no local page.
150 # There may have been a funny upload, or it may be on a shared
151 # file repository such as Wikimedia Commons.
152 if ( $title->getNamespace() == NS_FILE ) {
154 if ( $image ) {
155 return $title;
156 }
157 }
158
159 # MediaWiki namespace? Page may be "implied" if not customized.
160 # Just return it, with caps forced as the message system likes it.
161 if ( $title->getNamespace() == NS_MEDIAWIKI ) {
162 return Title::makeTitle( NS_MEDIAWIKI, $lang->ucfirst( $title->getText() ) );
163 }
164
165 # Quoted term? Try without the quotes...
166 $matches = [];
167 if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
168 return self::getNearMatch( $matches[1] );
169 }
170
171 return null;
172 }
173}
wfFindFile( $title, $options=[])
Find a file.
Internationalisation code.
Definition Language.php:36
A SearchResultSet wrapper for SearchNearMatcher.
Implementation of near match title search.
getNearMatch( $searchterm)
If an exact title match can be found, or a very slightly close match, return the title.
Language $language
Current language.
getNearMatchInternal( $searchterm)
Really find the title match.
__construct(Config $config, Language $lang)
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:967
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
const NS_USER
Definition Defines.php:75
const NS_FILE
Definition Defines.php:79
const NS_MEDIAWIKI
Definition Defines.php:81
const NS_MEDIA
Definition Defines.php:61
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 it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:886
whereas SearchGetNearMatch runs after $term
Definition hooks.txt:2889
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
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
Interface for configuration instances.
Definition Config.php:28
if(!isset( $args[0])) $lang