3 use MediaWiki\MediaWikiServices
;
6 * Implementation of near match title search.
7 * TODO: split into service/implementation.
9 class SearchNearMatcher
{
21 public function __construct( Config
$config, Language
$lang ) {
22 $this->config
= $config;
23 $this->language
= $lang;
27 * If an exact title match can be found, or a very slightly close match,
28 * return the title. If no match, returns NULL.
30 * @param string $searchterm
33 public function getNearMatch( $searchterm ) {
34 $title = $this->getNearMatchInternal( $searchterm );
36 Hooks
::run( 'SearchGetNearMatchComplete', [ $searchterm, &$title ] );
41 * Do a near match (see SearchEngine::getNearMatch) and wrap it into a
44 * @param string $searchterm
45 * @return ISearchResultSet
47 public function getNearMatchResultSet( $searchterm ) {
48 return new SearchNearMatchResultSet( $this->getNearMatch( $searchterm ) );
52 * Really find the title match.
53 * @param string $searchterm
56 protected function getNearMatchInternal( $searchterm ) {
57 $lang = $this->language
;
58 $allSearchTerms = [ $searchterm ];
60 if ( $lang->hasVariants() ) {
61 $allSearchTerms = array_unique( array_merge(
63 $lang->autoConvertToAllVariants( $searchterm )
68 if ( !Hooks
::run( 'SearchGetNearMatchBefore', [ $allSearchTerms, &$titleResult ] ) ) {
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] === '#' ) {
79 foreach ( $allSearchTerms as $term ) {
80 # Exact match? No need to look further.
81 $title = Title
::newFromText( $term );
82 if ( is_null( $title ) ) {
86 # Try files if searching in the Media: namespace
87 if ( $title->getNamespace() == NS_MEDIA
) {
88 $title = Title
::makeTitle( NS_FILE
, $title->getText() );
91 if ( $title->isSpecialPage() ||
$title->isExternal() ||
$title->exists() ) {
95 # See if it still otherwise has content is some sane sense
96 $page = WikiPage
::factory( $title );
97 if ( $page->hasViewableContent() ) {
101 if ( !Hooks
::run( 'SearchAfterNoDirectMatch', [ $term, &$title ] ) ) {
105 # Now try all lower case (i.e. first letter capitalized)
106 $title = Title
::newFromText( $lang->lc( $term ) );
107 if ( $title && $title->exists() ) {
111 # Now try capitalized string
112 $title = Title
::newFromText( $lang->ucwords( $term ) );
113 if ( $title && $title->exists() ) {
117 # Now try all upper case
118 $title = Title
::newFromText( $lang->uc( $term ) );
119 if ( $title && $title->exists() ) {
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() ) {
129 // Give hooks a chance at better match variants
131 if ( !Hooks
::run( 'SearchGetNearMatch', [ $term, &$title ] ) ) {
136 $title = Title
::newFromText( $searchterm );
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() );
146 # Entering a user goes to the user page whether it's there or not
147 if ( $title->getNamespace() == NS_USER
) {
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 );
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() ) );
167 # Quoted term? Try without the quotes...
169 if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
170 return $this->getNearMatch( $matches[1] );