91 $this->options = $options;
93 $this->language = $contentLanguage;
95 $this->hookRunner =
new HookRunner( $hookContainer );
96 $this->wikiPageFactory = $wikiPageFactory;
97 $this->userNameUtils = $userNameUtils;
98 $this->repoGroup = $repoGroup;
99 $this->titleFactory = $titleFactory;
133 $allSearchTerms = [ $searchterm ];
135 if ( $this->languageConverter->hasVariants() ) {
136 $allSearchTerms = array_unique( array_merge(
138 $this->languageConverter->autoConvertToAllVariants( $searchterm )
143 if ( !$this->hookRunner->onSearchGetNearMatchBefore( $allSearchTerms, $titleResult ) ) {
150 if ( $searchterm ===
'' || $searchterm[0] ===
'#' ) {
154 foreach ( $allSearchTerms as $term ) {
155 # Exact match? No need to look further.
156 $title = $this->titleFactory->newFromText( $term );
157 if ( $title ===
null ) {
161 # Try files if searching in the Media: namespace
162 if ( $title->getNamespace() ===
NS_MEDIA ) {
163 $title = Title::makeTitle(
NS_FILE, $title->getText() );
166 if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
170 # See if it still otherwise has content is some sensible sense
171 if ( $title->canExist() ) {
172 $page = $this->wikiPageFactory->newFromTitle( $title );
173 if ( $page->hasViewableContent() ) {
178 if ( !$this->hookRunner->onSearchAfterNoDirectMatch( $term, $title ) ) {
182 # Now try all lower case (i.e. first letter capitalized)
183 $title = $this->titleFactory->newFromText( $this->language->lc( $term ) );
184 if ( $title && $title->exists() ) {
188 # Now try capitalized string
189 $title = $this->titleFactory->newFromText( $this->language->ucwords( $term ) );
190 if ( $title && $title->exists() ) {
194 # Now try all upper case
195 $title = $this->titleFactory->newFromText( $this->language->uc( $term ) );
196 if ( $title && $title->exists() ) {
200 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
201 $title = $this->titleFactory->newFromText( $this->language->ucwordbreaks( $term ) );
202 if ( $title && $title->exists() ) {
209 if ( !$this->hookRunner->onSearchGetNearMatch( $term, $title ) ) {
214 $title = $this->titleFactory->newFromTextThrow( $searchterm );
216 # Entering an IP address goes to the contributions page
218 if ( ( $title->getNamespace() ===
NS_USER && $this->userNameUtils->isIP( $title->getText() ) )
219 || $this->userNameUtils->isIP( trim( $searchterm ) ) ) {
224 # Entering a user goes to the user page whether it's there or not
225 if ( $title->getNamespace() ===
NS_USER ) {
229 # Go to images that exist even if there's no local page.
230 # There may have been a funny upload, or it may be on a shared
231 # file repository such as Wikimedia Commons.
232 if ( $title->getNamespace() ===
NS_FILE ) {
233 $image = $this->repoGroup->findFile( $title );
239 # MediaWiki namespace? Page may be "implied" if not customized.
240 # Just return it, with caps forced as the message system likes it.
242 return Title::makeTitle(
NS_MEDIAWIKI, $this->language->ucfirst( $title->getText() ) );
245 # Quoted term? Try without the quotes...
247 if ( preg_match(
'/^"([^"]+)"$/', $searchterm,
$matches ) ) {