4 * Implementation of near match title search.
5 * TODO: split into service/implementation.
7 class SearchNearMatcher
{
9 * Configuration object.
10 * @param Config $config
14 public function __construct( Config
$config ) {
16 $this->config
= $config;
20 * If an exact title match can be found, or a very slightly close match,
21 * return the title. If no match, returns NULL.
23 * @param string $searchterm
26 public function getNearMatch( $searchterm ) {
27 $title = $this->getNearMatchInternal( $searchterm );
29 Hooks
::run( 'SearchGetNearMatchComplete', [ $searchterm, &$title ] );
34 * Do a near match (see SearchEngine::getNearMatch) and wrap it into a
37 * @param string $searchterm
38 * @return SearchResultSet
40 public function getNearMatchResultSet( $searchterm ) {
41 return new SearchNearMatchResultSet( $this->getNearMatch( $searchterm ) );
45 * Really find the title match.
46 * @param string $searchterm
49 protected function getNearMatchInternal( $searchterm ) {
50 $lang = $this->config
->get( 'ContLang' );
52 $allSearchTerms = [ $searchterm ];
54 if ( $lang->hasVariants() ) {
55 $allSearchTerms = array_unique( array_merge(
57 $lang->autoConvertToAllVariants( $searchterm )
62 if ( !Hooks
::run( 'SearchGetNearMatchBefore', [ $allSearchTerms, &$titleResult ] ) ) {
66 foreach ( $allSearchTerms as $term ) {
68 # Exact match? No need to look further.
69 $title = Title
::newFromText( $term );
70 if ( is_null( $title ) ) {
74 # Try files if searching in the Media: namespace
75 if ( $title->getNamespace() == NS_MEDIA
) {
76 $title = Title
::makeTitle( NS_FILE
, $title->getText() );
79 if ( $title->isSpecialPage() ||
$title->isExternal() ||
$title->exists() ) {
83 # See if it still otherwise has content is some sane sense
84 $page = WikiPage
::factory( $title );
85 if ( $page->hasViewableContent() ) {
89 if ( !Hooks
::run( 'SearchAfterNoDirectMatch', [ $term, &$title ] ) ) {
93 # Now try all lower case (i.e. first letter capitalized)
94 $title = Title
::newFromText( $lang->lc( $term ) );
95 if ( $title && $title->exists() ) {
99 # Now try capitalized string
100 $title = Title
::newFromText( $lang->ucwords( $term ) );
101 if ( $title && $title->exists() ) {
105 # Now try all upper case
106 $title = Title
::newFromText( $lang->uc( $term ) );
107 if ( $title && $title->exists() ) {
111 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
112 $title = Title
::newFromText( $lang->ucwordbreaks( $term ) );
113 if ( $title && $title->exists() ) {
117 // Give hooks a chance at better match variants
119 if ( !Hooks
::run( 'SearchGetNearMatch', [ $term, &$title ] ) ) {
124 $title = Title
::newFromText( $searchterm );
126 # Entering an IP address goes to the contributions page
127 if ( $this->config
->get( 'EnableSearchContributorsByIP' ) ) {
128 if ( ( $title->getNamespace() == NS_USER
&& User
::isIP( $title->getText() ) )
129 || User
::isIP( trim( $searchterm ) ) ) {
130 return SpecialPage
::getTitleFor( 'Contributions', $title->getDBkey() );
134 # Entering a user goes to the user page whether it's there or not
135 if ( $title->getNamespace() == NS_USER
) {
139 # Go to images that exist even if there's no local page.
140 # There may have been a funny upload, or it may be on a shared
141 # file repository such as Wikimedia Commons.
142 if ( $title->getNamespace() == NS_FILE
) {
143 $image = wfFindFile( $title );
149 # MediaWiki namespace? Page may be "implied" if not customized.
150 # Just return it, with caps forced as the message system likes it.
151 if ( $title->getNamespace() == NS_MEDIAWIKI
) {
152 return Title
::makeTitle( NS_MEDIAWIKI
, $lang->ucfirst( $title->getText() ) );
155 # Quoted term? Try without the quotes...
157 if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
158 return self
::getNearMatch( $matches[1] );