29 LanguageConverterFactory $languageConverterFactory,
35 $this->language = $contentLanguage;
36 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $contentLanguage );
37 $this->hookRunner =
new HookRunner( $hookContainer );
38 $this->wikiPageFactory = $wikiPageFactory;
39 $this->repoGroup = $repoGroup;
40 $this->titleFactory = $titleFactory;
74 $allSearchTerms = [ $searchterm ];
76 if ( $this->languageConverter->hasVariants() ) {
77 $allSearchTerms = array_unique( array_merge(
79 $this->languageConverter->autoConvertToAllVariants( $searchterm )
84 if ( !$this->hookRunner->onSearchGetNearMatchBefore( $allSearchTerms, $titleResult ) ) {
91 if ( $searchterm ===
'' || $searchterm[0] ===
'#' ) {
95 foreach ( $allSearchTerms as $term ) {
96 # Exact match? No need to look further.
97 $title = $this->titleFactory->newFromText( $term );
98 if ( $title ===
null ) {
102 # Try files if searching in the Media: namespace
103 if ( $title->getNamespace() ===
NS_MEDIA ) {
107 if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
111 # See if it still otherwise has content is some sensible sense
112 if ( $title->canExist() ) {
113 $page = $this->wikiPageFactory->newFromTitle( $title );
114 if ( $page->hasViewableContent() ) {
119 if ( !$this->hookRunner->onSearchAfterNoDirectMatch( $term, $title ) ) {
123 # Now try all lower case (=> first letter capitalized on some wikis)
124 $title = $this->titleFactory->newFromText( $this->language->lc( $term ) );
125 if ( $title && $title->exists() ) {
129 # Now try normalized lowercase (if it's different)
130 $normTerm = Validator::toNFKC( $term );
131 $normDiff = $normTerm !== $term;
133 $title = $this->titleFactory->newFromText( $this->language->lc( $normTerm ) );
134 if ( $title && $title->exists() ) {
139 # Now try capitalized string
140 $title = $this->titleFactory->newFromText( $this->language->ucwords( $term ) );
141 if ( $title && $title->exists() ) {
145 # Now try normalized capitalized (if it's different)
147 $title = $this->titleFactory->newFromText( $this->language->ucwords( $normTerm ) );
148 if ( $title && $title->exists() ) {
153 # Now try all upper case
154 $title = $this->titleFactory->newFromText( $this->language->uc( $term ) );
155 if ( $title && $title->exists() ) {
159 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
160 $title = $this->titleFactory->newFromText( $this->language->ucwordbreaks( $term ) );
161 if ( $title && $title->exists() ) {
168 if ( !$this->hookRunner->onSearchGetNearMatch( $term, $title ) ) {
173 $title = $this->titleFactory->newFromTextThrow( $searchterm );
175 # Entering a user goes to the user page whether it's there or not
176 if ( $title->getNamespace() ===
NS_USER ) {
180 # Go to images that exist even if there's no local page.
181 # There may have been a funny upload, or it may be on a shared
182 # file repository such as Wikimedia Commons.
183 if ( $title->getNamespace() ===
NS_FILE ) {
184 $image = $this->repoGroup->findFile( $title );
190 # MediaWiki namespace? Page may be "implied" if not customized.
191 # Just return it, with caps forced as the message system likes it.
196 # Quoted term? Try without the quotes...
198 if ( preg_match(
'/^"([^"]+)"$/', $searchterm,
$matches ) ) {