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 ) {
73 # Exact match? No need to look further.
74 $title = Title
::newFromText( $term );
75 if ( is_null( $title ) ) {
79 # Try files if searching in the Media: namespace
80 if ( $title->getNamespace() == NS_MEDIA
) {
81 $title = Title
::makeTitle( NS_FILE
, $title->getText() );
84 if ( $title->isSpecialPage() ||
$title->isExternal() ||
$title->exists() ) {
88 # See if it still otherwise has content is some sane sense
89 $page = WikiPage
::factory( $title );
90 if ( $page->hasViewableContent() ) {
94 if ( !Hooks
::run( 'SearchAfterNoDirectMatch', [ $term, &$title ] ) ) {
98 # Now try all lower case (i.e. first letter capitalized)
99 $title = Title
::newFromText( $lang->lc( $term ) );
100 if ( $title && $title->exists() ) {
104 # Now try capitalized string
105 $title = Title
::newFromText( $lang->ucwords( $term ) );
106 if ( $title && $title->exists() ) {
110 # Now try all upper case
111 $title = Title
::newFromText( $lang->uc( $term ) );
112 if ( $title && $title->exists() ) {
116 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
117 $title = Title
::newFromText( $lang->ucwordbreaks( $term ) );
118 if ( $title && $title->exists() ) {
122 // Give hooks a chance at better match variants
124 if ( !Hooks
::run( 'SearchGetNearMatch', [ $term, &$title ] ) ) {
129 $title = Title
::newFromText( $searchterm );
131 # Entering an IP address goes to the contributions page
132 if ( $this->config
->get( 'EnableSearchContributorsByIP' ) ) {
133 if ( ( $title->getNamespace() == NS_USER
&& User
::isIP( $title->getText() ) )
134 || User
::isIP( trim( $searchterm ) ) ) {
135 return SpecialPage
::getTitleFor( 'Contributions', $title->getDBkey() );
139 # Entering a user goes to the user page whether it's there or not
140 if ( $title->getNamespace() == NS_USER
) {
144 # Go to images that exist even if there's no local page.
145 # There may have been a funny upload, or it may be on a shared
146 # file repository such as Wikimedia Commons.
147 if ( $title->getNamespace() == NS_FILE
) {
148 $image = wfFindFile( $title );
154 # MediaWiki namespace? Page may be "implied" if not customized.
155 # Just return it, with caps forced as the message system likes it.
156 if ( $title->getNamespace() == NS_MEDIAWIKI
) {
157 return Title
::makeTitle( NS_MEDIAWIKI
, $lang->ucfirst( $title->getText() ) );
160 # Quoted term? Try without the quotes...
162 if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
163 return self
::getNearMatch( $matches[1] );