Merge "Move up devunt's name to Developers"
[lhc/web/wiklou.git] / includes / search / SearchNearMatcher.php
1 <?php
2
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
11 */
12 protected $config;
13
14 /**
15 * Current language
16 * @var Language
17 */
18 private $language;
19
20 public function __construct( Config $config, Language $lang ) {
21 $this->config = $config;
22 $this->language = $lang;
23 }
24
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.
28 *
29 * @param string $searchterm
30 * @return Title
31 */
32 public function getNearMatch( $searchterm ) {
33 $title = $this->getNearMatchInternal( $searchterm );
34
35 Hooks::run( 'SearchGetNearMatchComplete', [ $searchterm, &$title ] );
36 return $title;
37 }
38
39 /**
40 * Do a near match (see SearchEngine::getNearMatch) and wrap it into a
41 * SearchResultSet.
42 *
43 * @param string $searchterm
44 * @return SearchResultSet
45 */
46 public function getNearMatchResultSet( $searchterm ) {
47 return new SearchNearMatchResultSet( $this->getNearMatch( $searchterm ) );
48 }
49
50 /**
51 * Really find the title match.
52 * @param string $searchterm
53 * @return null|Title
54 */
55 protected function getNearMatchInternal( $searchterm ) {
56 $lang = $this->language;
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
90 $page = WikiPage::factory( $title );
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 }