Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / search / SearchNearMatcher.php
blob0400021a1a811e485b8551de87565285c4d2b972
1 <?php
3 /**
4 * Implementation of near match title search.
5 * TODO: split into service/implementation.
6 */
7 class SearchNearMatcher {
8 /**
9 * Configuration object.
10 * @param Config $config
12 protected $config;
14 /**
15 * Current language
16 * @var Language
18 private $language;
20 public function __construct( Config $config, Language $lang ) {
21 $this->config = $config;
22 $this->language = $lang;
25 /**
26 * If an exact title match can be found, or a very slightly close match,
27 * return the title. If no match, returns NULL.
29 * @param string $searchterm
30 * @return Title
32 public function getNearMatch( $searchterm ) {
33 $title = $this->getNearMatchInternal( $searchterm );
35 Hooks::run( 'SearchGetNearMatchComplete', [ $searchterm, &$title ] );
36 return $title;
39 /**
40 * Do a near match (see SearchEngine::getNearMatch) and wrap it into a
41 * SearchResultSet.
43 * @param string $searchterm
44 * @return SearchResultSet
46 public function getNearMatchResultSet( $searchterm ) {
47 return new SearchNearMatchResultSet( $this->getNearMatch( $searchterm ) );
50 /**
51 * Really find the title match.
52 * @param string $searchterm
53 * @return null|Title
55 protected function getNearMatchInternal( $searchterm ) {
56 $lang = $this->language;
58 $allSearchTerms = [ $searchterm ];
60 if ( $lang->hasVariants() ) {
61 $allSearchTerms = array_unique( array_merge(
62 $allSearchTerms,
63 $lang->autoConvertToAllVariants( $searchterm )
64 ) );
67 $titleResult = null;
68 if ( !Hooks::run( 'SearchGetNearMatchBefore', [ $allSearchTerms, &$titleResult ] ) ) {
69 return $titleResult;
72 foreach ( $allSearchTerms as $term ) {
74 # Exact match? No need to look further.
75 $title = Title::newFromText( $term );
76 if ( is_null( $title ) ) {
77 return null;
80 # Try files if searching in the Media: namespace
81 if ( $title->getNamespace() == NS_MEDIA ) {
82 $title = Title::makeTitle( NS_FILE, $title->getText() );
85 if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
86 return $title;
89 # See if it still otherwise has content is some sane sense
90 $page = WikiPage::factory( $title );
91 if ( $page->hasViewableContent() ) {
92 return $title;
95 if ( !Hooks::run( 'SearchAfterNoDirectMatch', [ $term, &$title ] ) ) {
96 return $title;
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;
105 # Now try capitalized string
106 $title = Title::newFromText( $lang->ucwords( $term ) );
107 if ( $title && $title->exists() ) {
108 return $title;
111 # Now try all upper case
112 $title = Title::newFromText( $lang->uc( $term ) );
113 if ( $title && $title->exists() ) {
114 return $title;
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;
123 // Give hooks a chance at better match variants
124 $title = null;
125 if ( !Hooks::run( 'SearchGetNearMatch', [ $term, &$title ] ) ) {
126 return $title;
130 $title = Title::newFromText( $searchterm );
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() );
140 # Entering a user goes to the user page whether it's there or not
141 if ( $title->getNamespace() == NS_USER ) {
142 return $title;
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;
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() ) );
161 # Quoted term? Try without the quotes...
162 $matches = [];
163 if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
164 return self::getNearMatch( $matches[1] );
167 return null;