31 LanguageConverterFactory $languageConverterFactory,
37 $this->language = $contentLanguage;
38 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $contentLanguage );
39 $this->hookRunner =
new HookRunner( $hookContainer );
40 $this->wikiPageFactory = $wikiPageFactory;
41 $this->repoGroup = $repoGroup;
42 $this->titleFactory = $titleFactory;
76 $allSearchTerms = [ $searchterm ];
78 if ( $this->languageConverter->hasVariants() ) {
79 $allSearchTerms = array_unique( array_merge(
81 $this->languageConverter->autoConvertToAllVariants( $searchterm )
86 if ( !$this->hookRunner->onSearchGetNearMatchBefore( $allSearchTerms, $titleResult ) ) {
93 if ( $searchterm ===
'' || $searchterm[0] ===
'#' ) {
97 foreach ( $allSearchTerms as $term ) {
98 # Exact match? No need to look further.
99 $title = $this->titleFactory->newFromText( $term );
100 if ( $title ===
null ) {
104 # Try files if searching in the Media: namespace
105 if ( $title->getNamespace() ===
NS_MEDIA ) {
109 if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) {
113 # See if it still otherwise has content is some sensible sense
114 if ( $title->canExist() ) {
115 $page = $this->wikiPageFactory->newFromTitle( $title );
116 if ( $page->hasViewableContent() ) {
121 if ( !$this->hookRunner->onSearchAfterNoDirectMatch( $term, $title ) ) {
125 # Now try all lower case (=> first letter capitalized on some wikis)
126 $title = $this->titleFactory->newFromText( $this->language->lc( $term ) );
127 if ( $title && $title->exists() ) {
131 # Now try normalized lowercase (if it's different)
132 $normTerm = Validator::toNFKC( $term );
133 $normDiff = $normTerm !== $term;
135 $title = $this->titleFactory->newFromText( $this->language->lc( $normTerm ) );
136 if ( $title && $title->exists() ) {
141 # Now try capitalized string
142 $title = $this->titleFactory->newFromText( $this->language->ucwords( $term ) );
143 if ( $title && $title->exists() ) {
147 # Now try normalized capitalized (if it's different)
149 $title = $this->titleFactory->newFromText( $this->language->ucwords( $normTerm ) );
150 if ( $title && $title->exists() ) {
155 # Now try all upper case
156 $title = $this->titleFactory->newFromText( $this->language->uc( $term ) );
157 if ( $title && $title->exists() ) {
161 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
162 $title = $this->titleFactory->newFromText( $this->language->ucwordbreaks( $term ) );
163 if ( $title && $title->exists() ) {
170 if ( !$this->hookRunner->onSearchGetNearMatch( $term, $title ) ) {
175 $title = $this->titleFactory->newFromTextThrow( $searchterm );
177 # Entering a user goes to the user page whether it's there or not
178 if ( $title->getNamespace() ===
NS_USER ) {
182 # Go to images that exist even if there's no local page.
183 # There may have been a funny upload, or it may be on a shared
184 # file repository such as Wikimedia Commons.
185 if ( $title->getNamespace() ===
NS_FILE ) {
186 $image = $this->repoGroup->findFile( $title );
192 # MediaWiki namespace? Page may be "implied" if not customized.
193 # Just return it, with caps forced as the message system likes it.
198 # Quoted term? Try without the quotes...
200 if ( preg_match(
'/^"([^"]+)"$/', $searchterm,
$matches ) ) {