Merge "Added a separate error message for mkdir failures"
[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 # Exact match? No need to look further.
74 $title = Title::newFromText( $term );
75 if ( is_null( $title ) ) {
76 return null;
77 }
78
79 # Try files if searching in the Media: namespace
80 if ( $title->getNamespace() == NS_MEDIA ) {
81 $title = Title::makeTitle( NS_FILE, $title->getText() );
82 }
83
84 if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
85 return $title;
86 }
87
88 # See if it still otherwise has content is some sane sense
89 $page = WikiPage::factory( $title );
90 if ( $page->hasViewableContent() ) {
91 return $title;
92 }
93
94 if ( !Hooks::run( 'SearchAfterNoDirectMatch', [ $term, &$title ] ) ) {
95 return $title;
96 }
97
98 # Now try all lower case (i.e. first letter capitalized)
99 $title = Title::newFromText( $lang->lc( $term ) );
100 if ( $title && $title->exists() ) {
101 return $title;
102 }
103
104 # Now try capitalized string
105 $title = Title::newFromText( $lang->ucwords( $term ) );
106 if ( $title && $title->exists() ) {
107 return $title;
108 }
109
110 # Now try all upper case
111 $title = Title::newFromText( $lang->uc( $term ) );
112 if ( $title && $title->exists() ) {
113 return $title;
114 }
115
116 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
117 $title = Title::newFromText( $lang->ucwordbreaks( $term ) );
118 if ( $title && $title->exists() ) {
119 return $title;
120 }
121
122 // Give hooks a chance at better match variants
123 $title = null;
124 if ( !Hooks::run( 'SearchGetNearMatch', [ $term, &$title ] ) ) {
125 return $title;
126 }
127 }
128
129 $title = Title::newFromText( $searchterm );
130
131 # Entering an IP address goes to the contributions page
132 if ( $this->config->get( 'EnableSearchContributorsByIP' ) ) {
133 if ( ( $title->getNamespace() == NS_USER && User::isIP( $title->getText() ) )
134 || User::isIP( trim( $searchterm ) ) ) {
135 return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() );
136 }
137 }
138
139 # Entering a user goes to the user page whether it's there or not
140 if ( $title->getNamespace() == NS_USER ) {
141 return $title;
142 }
143
144 # Go to images that exist even if there's no local page.
145 # There may have been a funny upload, or it may be on a shared
146 # file repository such as Wikimedia Commons.
147 if ( $title->getNamespace() == NS_FILE ) {
148 $image = wfFindFile( $title );
149 if ( $image ) {
150 return $title;
151 }
152 }
153
154 # MediaWiki namespace? Page may be "implied" if not customized.
155 # Just return it, with caps forced as the message system likes it.
156 if ( $title->getNamespace() == NS_MEDIAWIKI ) {
157 return Title::makeTitle( NS_MEDIAWIKI, $lang->ucfirst( $title->getText() ) );
158 }
159
160 # Quoted term? Try without the quotes...
161 $matches = [];
162 if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
163 return self::getNearMatch( $matches[1] );
164 }
165
166 return null;
167 }
168 }