51 $this->options = $options;
53 $this->language = $contentLanguage;
55 $this->hookRunner =
new HookRunner( $hookContainer );
56 $this->wikiPageFactory = $wikiPageFactory;
57 $this->userNameUtils = $userNameUtils;
58 $this->repoGroup = $repoGroup;
59 $this->titleFactory = $titleFactory;
93 $allSearchTerms = [ $searchterm ];
95 if ( $this->languageConverter->hasVariants() ) {
96 $allSearchTerms = array_unique( array_merge(
98 $this->languageConverter->autoConvertToAllVariants( $searchterm )
103 if ( !$this->hookRunner->onSearchGetNearMatchBefore( $allSearchTerms, $titleResult ) ) {
110 if ( $searchterm ===
'' || $searchterm[0] ===
'#' ) {
114 foreach ( $allSearchTerms as $term ) {
115 # Exact match? No need to look further.
116 $title = $this->titleFactory->newFromText( $term );
117 if ( $title ===
null ) {
121 # Try files if searching in the Media: namespace
122 if ( $title->getNamespace() ===
NS_MEDIA ) {
123 $title = Title::makeTitle(
NS_FILE, $title->getText() );
126 if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
130 # See if it still otherwise has content is some sensible sense
131 if ( $title->canExist() ) {
132 $page = $this->wikiPageFactory->newFromTitle( $title );
133 if ( $page->hasViewableContent() ) {
138 if ( !$this->hookRunner->onSearchAfterNoDirectMatch( $term, $title ) ) {
142 # Now try all lower case (i.e. first letter capitalized)
143 $title = $this->titleFactory->newFromText( $this->language->lc( $term ) );
144 if ( $title && $title->exists() ) {
148 # Now try capitalized string
149 $title = $this->titleFactory->newFromText( $this->language->ucwords( $term ) );
150 if ( $title && $title->exists() ) {
154 # Now try all upper case
155 $title = $this->titleFactory->newFromText( $this->language->uc( $term ) );
156 if ( $title && $title->exists() ) {
160 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
161 $title = $this->titleFactory->newFromText( $this->language->ucwordbreaks( $term ) );
162 if ( $title && $title->exists() ) {
169 if ( !$this->hookRunner->onSearchGetNearMatch( $term, $title ) ) {
174 $title = $this->titleFactory->newFromTextThrow( $searchterm );
176 # Entering an IP address goes to the contributions page
178 if ( ( $title->getNamespace() ===
NS_USER && $this->userNameUtils->isIP( $title->getText() ) )
179 || $this->userNameUtils->isIP( trim( $searchterm ) ) ) {
184 # Entering a user goes to the user page whether it's there or not
185 if ( $title->getNamespace() ===
NS_USER ) {
189 # Go to images that exist even if there's no local page.
190 # There may have been a funny upload, or it may be on a shared
191 # file repository such as Wikimedia Commons.
192 if ( $title->getNamespace() ===
NS_FILE ) {
193 $image = $this->repoGroup->findFile( $title );
199 # MediaWiki namespace? Page may be "implied" if not customized.
200 # Just return it, with caps forced as the message system likes it.
202 return Title::makeTitle(
NS_MEDIAWIKI, $this->language->ucfirst( $title->getText() ) );
205 # Quoted term? Try without the quotes...
207 if ( preg_match(
'/^"([^"]+)"$/', $searchterm,
$matches ) ) {