| 1 |
<?php |
| 2 |
namespace MediaWiki\Search; |
| 3 |
|
| 4 |
use ISearchResultSet; |
| 5 |
use MediaWiki\Config\ServiceOptions; |
| 6 |
use MediaWiki\HookContainer\HookContainer; |
| 7 |
use MediaWiki\HookContainer\HookRunner; |
| 8 |
use MediaWiki\Language\ILanguageConverter; |
| 9 |
use MediaWiki\Language\Language; |
| 10 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 11 |
use MediaWiki\MainConfigNames; |
| 12 |
use MediaWiki\Page\WikiPageFactory; |
| 13 |
use MediaWiki\SpecialPage\SpecialPage; |
| 14 |
use MediaWiki\Title\Title; |
| 15 |
use MediaWiki\Title\TitleFactory; |
| 16 |
use MediaWiki\User\UserNameUtils; |
| 17 |
use RepoGroup; |
| 18 |
use SearchNearMatchResultSet; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class TitleMatcher { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public const CONSTRUCTOR_OPTIONS = [ |
| 28 |
MainConfigNames::EnableSearchContributorsByIP, |
| 29 |
]; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private $options; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
private $language; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private $languageConverter; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private $hookRunner; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private $wikiPageFactory; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private $userNameUtils; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private $repoGroup; |
| 67 |
|
| 68 |
private TitleFactory $titleFactory; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function __construct( |
| 81 |
ServiceOptions $options, |
| 82 |
Language $contentLanguage, |
| 83 |
LanguageConverterFactory $languageConverterFactory, |
| 84 |
HookContainer $hookContainer, |
| 85 |
WikiPageFactory $wikiPageFactory, |
| 86 |
UserNameUtils $userNameUtils, |
| 87 |
RepoGroup $repoGroup, |
| 88 |
TitleFactory $titleFactory |
| 89 |
) { |
| 90 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 91 |
$this->options = $options; |
| 92 |
|
| 93 |
$this->language = $contentLanguage; |
| 94 |
$this->languageConverter = $languageConverterFactory->getLanguageConverter( $contentLanguage ); |
| 95 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 96 |
$this->wikiPageFactory = $wikiPageFactory; |
| 97 |
$this->userNameUtils = $userNameUtils; |
| 98 |
$this->repoGroup = $repoGroup; |
| 99 |
$this->titleFactory = $titleFactory; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function getNearMatch( $searchterm ) { |
| 110 |
$title = $this->getNearMatchInternal( $searchterm ); |
| 111 |
|
| 112 |
$this->hookRunner->onSearchGetNearMatchComplete( $searchterm, $title ); |
| 113 |
return $title; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function getNearMatchResultSet( $searchterm ) { |
| 124 |
return new SearchNearMatchResultSet( $this->getNearMatch( $searchterm ) ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
protected function getNearMatchInternal( $searchterm ) { |
| 133 |
$allSearchTerms = [ $searchterm ]; |
| 134 |
|
| 135 |
if ( $this->languageConverter->hasVariants() ) { |
| 136 |
$allSearchTerms = array_unique( array_merge( |
| 137 |
$allSearchTerms, |
| 138 |
$this->languageConverter->autoConvertToAllVariants( $searchterm ) |
| 139 |
) ); |
| 140 |
} |
| 141 |
|
| 142 |
$titleResult = null; |
| 143 |
if ( !$this->hookRunner->onSearchGetNearMatchBefore( $allSearchTerms, $titleResult ) ) { |
| 144 |
return $titleResult; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
if ( $searchterm === '' || $searchterm[0] === '#' ) { |
| 151 |
return null; |
| 152 |
} |
| 153 |
|
| 154 |
foreach ( $allSearchTerms as $term ) { |
| 155 |
|
| 156 |
$title = $this->titleFactory->newFromText( $term ); |
| 157 |
if ( $title === null ) { |
| 158 |
return null; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
if ( $title->getNamespace() === NS_MEDIA ) { |
| 163 |
$title = Title::makeTitle( NS_FILE, $title->getText() ); |
| 164 |
} |
| 165 |
|
| 166 |
if ( $title->isSpecialPage() || $title->isExternal() || $title->exists() ) { |
| 167 |
return $title; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
if ( $title->canExist() ) { |
| 172 |
$page = $this->wikiPageFactory->newFromTitle( $title ); |
| 173 |
if ( $page->hasViewableContent() ) { |
| 174 |
return $title; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
if ( !$this->hookRunner->onSearchAfterNoDirectMatch( $term, $title ) ) { |
| 179 |
return $title; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
$title = $this->titleFactory->newFromText( $this->language->lc( $term ) ); |
| 184 |
if ( $title && $title->exists() ) { |
| 185 |
return $title; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
$title = $this->titleFactory->newFromText( $this->language->ucwords( $term ) ); |
| 190 |
if ( $title && $title->exists() ) { |
| 191 |
return $title; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
$title = $this->titleFactory->newFromText( $this->language->uc( $term ) ); |
| 196 |
if ( $title && $title->exists() ) { |
| 197 |
return $title; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
$title = $this->titleFactory->newFromText( $this->language->ucwordbreaks( $term ) ); |
| 202 |
if ( $title && $title->exists() ) { |
| 203 |
return $title; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
$title = null; |
| 208 |
|
| 209 |
if ( !$this->hookRunner->onSearchGetNearMatch( $term, $title ) ) { |
| 210 |
return $title; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
$title = $this->titleFactory->newFromTextThrow( $searchterm ); |
| 215 |
|
| 216 |
|
| 217 |
if ( $this->options->get( MainConfigNames::EnableSearchContributorsByIP ) ) { |
| 218 |
if ( ( $title->getNamespace() === NS_USER && $this->userNameUtils->isIP( $title->getText() ) ) |
| 219 |
|| $this->userNameUtils->isIP( trim( $searchterm ) ) ) { |
| 220 |
return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() ); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
if ( $title->getNamespace() === NS_USER ) { |
| 226 |
return $title; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
if ( $title->getNamespace() === NS_FILE ) { |
| 233 |
$image = $this->repoGroup->findFile( $title ); |
| 234 |
if ( $image ) { |
| 235 |
return $title; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
if ( $title->getNamespace() === NS_MEDIAWIKI ) { |
| 242 |
return Title::makeTitle( NS_MEDIAWIKI, $this->language->ucfirst( $title->getText() ) ); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
$matches = []; |
| 247 |
if ( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) { |
| 248 |
return $this->getNearMatch( $matches[1] ); |
| 249 |
} |
| 250 |
|
| 251 |
return null; |
| 252 |
} |
| 253 |
} |