4 * Implementation of near match title search.
5 * TODO: split into service/implementation.
7 class SearchNearMatcher
{
9 * Configuration object.
10 * @param Config $config
20 public function __construct( Config
$config, Language
$lang ) {
21 $this->config
= $config;
22 $this->language
= $lang;
26 * If an exact title match can be found, or a very slightly close match,
27 * return the title. If no match, returns NULL.
29 * @param string $searchterm
32 public function getNearMatch( $searchterm ) {
33 $title = $this->getNearMatchInternal( $searchterm );
35 Hooks
::run( 'SearchGetNearMatchComplete', [ $searchterm, &$title ] );
40 * Do a near match (see SearchEngine::getNearMatch) and wrap it into a
43 * @param string $searchterm
44 * @return SearchResultSet
46 public function getNearMatchResultSet( $searchterm ) {
47 return new SearchNearMatchResultSet( $this->getNearMatch( $searchterm ) );
51 * Really find the title match.
52 * @param string $searchterm
55 protected function getNearMatchInternal( $searchterm ) {
56 $lang = $this->language
;
58 $allSearchTerms = [ $searchterm ];
60 if ( $lang->hasVariants() ) {
61 $allSearchTerms = array_unique( array_merge(
63 $lang->autoConvertToAllVariants( $searchterm )
68 if ( !Hooks
::run( 'SearchGetNearMatchBefore', [ $allSearchTerms, &$titleResult ] ) ) {
72 foreach ( $allSearchTerms as $term ) {
74 # Exact match? No need to look further.
75 $title = Title
::newFromText( $term );
76 if ( is_null( $title ) ) {
80 # Try files if searching in the Media: namespace
81 if ( $title->getNamespace() == NS_MEDIA
) {
82 $title = Title
::makeTitle( NS_FILE
, $title->getText() );
85 if ( $title->isSpecialPage() ||
$title->isExternal() ||
$title->exists() ) {
89 # See if it still otherwise has content is some sane sense
90 $page = WikiPage
::factory( $title );
91 if ( $page->hasViewableContent() ) {
95 if ( !Hooks
::run( 'SearchAfterNoDirectMatch', [ $term, &$title ] ) ) {
99 # Now try all lower case (i.e. first letter capitalized)
100 $title = Title
::newFromText( $lang->lc( $term ) );
101 if ( $title && $title->exists() ) {
105 # Now try capitalized string
106 $title = Title
::newFromText( $lang->ucwords( $term ) );
107 if ( $title && $title->exists() ) {
111 # Now try all upper case
112 $title = Title
::newFromText( $lang->uc( $term ) );
113 if ( $title && $title->exists() ) {
117 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
118 $title = Title
::newFromText( $lang->ucwordbreaks( $term ) );
119 if ( $title && $title->exists() ) {
123 // Give hooks a chance at better match variants
125 if ( !Hooks
::run( 'SearchGetNearMatch', [ $term, &$title ] ) ) {
130 $title = Title
::newFromText( $searchterm );
132 # Entering an IP address goes to the contributions page
133 if ( $this->config
->get( 'EnableSearchContributorsByIP' ) ) {
134 if ( ( $title->getNamespace() == NS_USER
&& User
::isIP( $title->getText() ) )
135 || User
::isIP( trim( $searchterm ) ) ) {
136 return SpecialPage
::getTitleFor( 'Contributions', $title->getDBkey() );
140 # Entering a user goes to the user page whether it's there or not
141 if ( $title->getNamespace() == NS_USER
) {
145 # Go to images that exist even if there's no local page.
146 # There may have been a funny upload, or it may be on a shared
147 # file repository such as Wikimedia Commons.
148 if ( $title->getNamespace() == NS_FILE
) {
149 $image = wfFindFile( $title );
155 # MediaWiki namespace? Page may be "implied" if not customized.
156 # Just return it, with caps forced as the message system likes it.
157 if ( $title->getNamespace() == NS_MEDIAWIKI
) {
158 return Title
::makeTitle( NS_MEDIAWIKI
, $lang->ucfirst( $title->getText() ) );
161 # Quoted term? Try without the quotes...
163 if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
164 return self
::getNearMatch( $matches[1] );