bb7cd579bed005fd0d92f55abc7cc59e7273d6c1
[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 public function __construct( Config $config ) {
15
16 $this->config = $config;
17 }
18
19 /**
20 * If an exact title match can be found, or a very slightly close match,
21 * return the title. If no match, returns NULL.
22 *
23 * @param string $searchterm
24 * @return Title
25 */
26 public function getNearMatch( $searchterm ) {
27 $title = $this->getNearMatchInternal( $searchterm );
28
29 Hooks::run( 'SearchGetNearMatchComplete', [ $searchterm, &$title ] );
30 return $title;
31 }
32
33 /**
34 * Do a near match (see SearchEngine::getNearMatch) and wrap it into a
35 * SearchResultSet.
36 *
37 * @param string $searchterm
38 * @return SearchResultSet
39 */
40 public function getNearMatchResultSet( $searchterm ) {
41 return new SearchNearMatchResultSet( $this->getNearMatch( $searchterm ) );
42 }
43
44 /**
45 * Really find the title match.
46 * @param string $searchterm
47 * @return null|Title
48 */
49 protected function getNearMatchInternal( $searchterm ) {
50 $lang = $this->config->get( 'ContLang' );
51
52 $allSearchTerms = [ $searchterm ];
53
54 if ( $lang->hasVariants() ) {
55 $allSearchTerms = array_unique( array_merge(
56 $allSearchTerms,
57 $lang->autoConvertToAllVariants( $searchterm )
58 ) );
59 }
60
61 $titleResult = null;
62 if ( !Hooks::run( 'SearchGetNearMatchBefore', [ $allSearchTerms, &$titleResult ] ) ) {
63 return $titleResult;
64 }
65
66 foreach ( $allSearchTerms as $term ) {
67
68 # Exact match? No need to look further.
69 $title = Title::newFromText( $term );
70 if ( is_null( $title ) ) {
71 return null;
72 }
73
74 # Try files if searching in the Media: namespace
75 if ( $title->getNamespace() == NS_MEDIA ) {
76 $title = Title::makeTitle( NS_FILE, $title->getText() );
77 }
78
79 if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
80 return $title;
81 }
82
83 # See if it still otherwise has content is some sane sense
84 $page = WikiPage::factory( $title );
85 if ( $page->hasViewableContent() ) {
86 return $title;
87 }
88
89 if ( !Hooks::run( 'SearchAfterNoDirectMatch', [ $term, &$title ] ) ) {
90 return $title;
91 }
92
93 # Now try all lower case (i.e. first letter capitalized)
94 $title = Title::newFromText( $lang->lc( $term ) );
95 if ( $title && $title->exists() ) {
96 return $title;
97 }
98
99 # Now try capitalized string
100 $title = Title::newFromText( $lang->ucwords( $term ) );
101 if ( $title && $title->exists() ) {
102 return $title;
103 }
104
105 # Now try all upper case
106 $title = Title::newFromText( $lang->uc( $term ) );
107 if ( $title && $title->exists() ) {
108 return $title;
109 }
110
111 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
112 $title = Title::newFromText( $lang->ucwordbreaks( $term ) );
113 if ( $title && $title->exists() ) {
114 return $title;
115 }
116
117 // Give hooks a chance at better match variants
118 $title = null;
119 if ( !Hooks::run( 'SearchGetNearMatch', [ $term, &$title ] ) ) {
120 return $title;
121 }
122 }
123
124 $title = Title::newFromText( $searchterm );
125
126 # Entering an IP address goes to the contributions page
127 if ( $this->config->get( 'EnableSearchContributorsByIP' ) ) {
128 if ( ( $title->getNamespace() == NS_USER && User::isIP( $title->getText() ) )
129 || User::isIP( trim( $searchterm ) ) ) {
130 return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() );
131 }
132 }
133
134 # Entering a user goes to the user page whether it's there or not
135 if ( $title->getNamespace() == NS_USER ) {
136 return $title;
137 }
138
139 # Go to images that exist even if there's no local page.
140 # There may have been a funny upload, or it may be on a shared
141 # file repository such as Wikimedia Commons.
142 if ( $title->getNamespace() == NS_FILE ) {
143 $image = wfFindFile( $title );
144 if ( $image ) {
145 return $title;
146 }
147 }
148
149 # MediaWiki namespace? Page may be "implied" if not customized.
150 # Just return it, with caps forced as the message system likes it.
151 if ( $title->getNamespace() == NS_MEDIAWIKI ) {
152 return Title::makeTitle( NS_MEDIAWIKI, $lang->ucfirst( $title->getText() ) );
153 }
154
155 # Quoted term? Try without the quotes...
156 $matches = [];
157 if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
158 return self::getNearMatch( $matches[1] );
159 }
160
161 return null;
162 }
163 }