| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Preferences; |
| 22 |
|
| 23 |
use MediaWiki\Config\ServiceOptions; |
| 24 |
use MediaWiki\Html\Html; |
| 25 |
use MediaWiki\MainConfigNames; |
| 26 |
use MediaWiki\Parser\ParserFactory; |
| 27 |
use MediaWiki\Parser\ParserOptions; |
| 28 |
use MediaWiki\Parser\ParserOutputFlags; |
| 29 |
use MediaWiki\Parser\Parsoid\LintErrorChecker; |
| 30 |
use MediaWiki\SpecialPage\SpecialPage; |
| 31 |
use MediaWiki\SpecialPage\SpecialPageFactory; |
| 32 |
use MediaWiki\Title\TitleFactory; |
| 33 |
use MediaWiki\User\UserIdentity; |
| 34 |
use MessageLocalizer; |
| 35 |
use OOUI\ButtonWidget; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class SignatureValidator { |
| 41 |
|
| 42 |
|
| 43 |
public const CONSTRUCTOR_OPTIONS = [ |
| 44 |
MainConfigNames::SignatureAllowedLintErrors, |
| 45 |
MainConfigNames::VirtualRestConfig, |
| 46 |
]; |
| 47 |
|
| 48 |
|
| 49 |
private $user; |
| 50 |
|
| 51 |
private $localizer; |
| 52 |
|
| 53 |
private $popts; |
| 54 |
|
| 55 |
private $parserFactory; |
| 56 |
private LintErrorChecker $lintErrorChecker; |
| 57 |
|
| 58 |
private $serviceOptions; |
| 59 |
|
| 60 |
private $specialPageFactory; |
| 61 |
|
| 62 |
private $titleFactory; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function __construct( |
| 75 |
ServiceOptions $options, |
| 76 |
UserIdentity $user, |
| 77 |
?MessageLocalizer $localizer, |
| 78 |
ParserOptions $popts, |
| 79 |
ParserFactory $parserFactory, |
| 80 |
LintErrorChecker $lintErrorChecker, |
| 81 |
SpecialPageFactory $specialPageFactory, |
| 82 |
TitleFactory $titleFactory |
| 83 |
) { |
| 84 |
$this->user = $user; |
| 85 |
$this->localizer = $localizer; |
| 86 |
$this->popts = $popts; |
| 87 |
$this->parserFactory = $parserFactory; |
| 88 |
$this->lintErrorChecker = $lintErrorChecker; |
| 89 |
|
| 90 |
$this->serviceOptions = $options; |
| 91 |
$this->serviceOptions->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 92 |
|
| 93 |
$this->specialPageFactory = $specialPageFactory; |
| 94 |
$this->titleFactory = $titleFactory; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function validateSignature( string $signature ) { |
| 103 |
$pstSignature = $this->applyPreSaveTransform( $signature ); |
| 104 |
if ( $pstSignature === false ) { |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
if ( $this->localizer ) { |
| 109 |
return [ $this->localizer->msg( 'badsigsubst' )->parse() ]; |
| 110 |
} |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
$pstWasApplied = false; |
| 115 |
if ( $pstSignature !== $signature ) { |
| 116 |
$pstWasApplied = true; |
| 117 |
$signature = $pstSignature; |
| 118 |
} |
| 119 |
|
| 120 |
$errors = $this->localizer ? [] : false; |
| 121 |
|
| 122 |
$lintErrors = $this->checkLintErrors( $signature ); |
| 123 |
if ( $lintErrors ) { |
| 124 |
$messages = ''; |
| 125 |
|
| 126 |
foreach ( $lintErrors as $error ) { |
| 127 |
if ( !$this->localizer ) { |
| 128 |
$errors = true; |
| 129 |
break; |
| 130 |
} |
| 131 |
|
| 132 |
$details = $this->getLintErrorDetails( $error ); |
| 133 |
$location = $this->getLintErrorLocation( $error ); |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
$label = $this->localizer->msg( "linterror-{$error['type']}" )->parse(); |
| 165 |
$docsLink = new ButtonWidget( [ |
| 166 |
'href' => |
| 167 |
"https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Lint_errors/{$error['type']}", |
| 168 |
'target' => '_blank', |
| 169 |
'label' => $this->localizer->msg( 'prefs-signature-error-details' )->text(), |
| 170 |
] ); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
$encLocation = $pstWasApplied ? null : json_encode( $location ); |
| 176 |
|
| 177 |
$messages .= Html::rawElement( |
| 178 |
'li', |
| 179 |
[ 'data-mw-lint-error-location' => $encLocation ], |
| 180 |
$label . $this->localizer->msg( 'colon-separator' )->escaped() . |
| 181 |
$details . ' ' . $docsLink |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
if ( $messages && $this->localizer ) { |
| 186 |
$errors[] = $this->localizer->msg( 'badsightml' )->parse() . |
| 187 |
Html::rawElement( 'ol', [], $messages ); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
if ( !$this->checkUserLinks( $signature ) ) { |
| 192 |
if ( $this->localizer ) { |
| 193 |
$userText = wfEscapeWikiText( $this->user->getName() ); |
| 194 |
$linkWikitext = $this->localizer->msg( 'signature', $userText, $userText )->inContentLanguage()->text(); |
| 195 |
$errors[] = $this->localizer->msg( 'badsiglinks', wfEscapeWikiText( $linkWikitext ) )->parse(); |
| 196 |
} else { |
| 197 |
$errors = true; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if ( !$this->checkLineBreaks( $signature ) ) { |
| 202 |
if ( $this->localizer ) { |
| 203 |
$errors[] = $this->localizer->msg( 'badsiglinebreak' )->parse(); |
| 204 |
} else { |
| 205 |
$errors = true; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return $errors; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
protected function applyPreSaveTransform( string $signature ) { |
| 218 |
|
| 219 |
$parser = $this->parserFactory->getInstance(); |
| 220 |
|
| 221 |
$pstSignature = $parser->preSaveTransform( |
| 222 |
$signature, |
| 223 |
SpecialPage::getTitleFor( 'Preferences' ), |
| 224 |
$this->user, |
| 225 |
$this->popts |
| 226 |
); |
| 227 |
|
| 228 |
|
| 229 |
if ( $parser->getOutput()->getOutputFlag( ParserOutputFlags::USER_SIGNATURE ) ) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
$pstPstSignature = $parser->preSaveTransform( |
| 235 |
$pstSignature, |
| 236 |
SpecialPage::getTitleFor( 'Preferences' ), |
| 237 |
$this->user, |
| 238 |
$this->popts |
| 239 |
); |
| 240 |
if ( $pstPstSignature !== $pstSignature ) { |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
return $pstSignature; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
protected function checkLintErrors( string $signature ): array { |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
$disabled = array_merge( |
| 257 |
[ |
| 258 |
|
| 259 |
|
| 260 |
'multiple-unclosed-formatting-tags', |
| 261 |
], |
| 262 |
$this->serviceOptions->get( |
| 263 |
MainConfigNames::SignatureAllowedLintErrors |
| 264 |
) |
| 265 |
); |
| 266 |
return $this->lintErrorChecker->checkSome( $signature, $disabled ); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
protected function checkUserLinks( string $signature ): bool { |
| 274 |
|
| 275 |
$parser = $this->parserFactory->getInstance(); |
| 276 |
|
| 277 |
|
| 278 |
$pout = $parser->parse( |
| 279 |
$signature, |
| 280 |
SpecialPage::getTitleFor( 'Preferences' ), |
| 281 |
$this->popts |
| 282 |
); |
| 283 |
|
| 284 |
|
| 285 |
$links = $pout->getLinks(); |
| 286 |
$username = $this->user->getName(); |
| 287 |
if ( |
| 288 |
isset( $links[ NS_USER ][ strtr( $username, ' ', '_' ) ] ) || |
| 289 |
isset( $links[ NS_USER_TALK ][ strtr( $username, ' ', '_' ) ] ) |
| 290 |
) { |
| 291 |
return true; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
$splinks = $pout->getLinksSpecial(); |
| 297 |
foreach ( $splinks as $dbkey => $unused ) { |
| 298 |
[ $name, $subpage ] = $this->specialPageFactory->resolveAlias( $dbkey ); |
| 299 |
if ( $name === 'Contributions' && $subpage ) { |
| 300 |
$userTitle = $this->titleFactory->makeTitleSafe( NS_USER, $subpage ); |
| 301 |
if ( $userTitle && $userTitle->getText() === $username ) { |
| 302 |
return true; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
protected function checkLineBreaks( string $signature ): bool { |
| 315 |
return !preg_match( "/[\r\n]/", $signature ); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
private function getLintErrorLocation( array $lintError ): array { |
| 320 |
return array_slice( $lintError['dsr'], 0, 2 ); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
private function getLintErrorDetails( array $lintError ): string { |
| 325 |
[ 'type' => $type, 'params' => $params ] = $lintError; |
| 326 |
|
| 327 |
if ( $type === 'bogus-image-options' && isset( $params['items'] ) ) { |
| 328 |
$list = array_map( static function ( $in ) { |
| 329 |
return Html::element( 'code', [], $in ); |
| 330 |
}, $params['items'] ); |
| 331 |
return implode( |
| 332 |
$this->localizer->msg( 'comma-separator' )->escaped(), |
| 333 |
$list |
| 334 |
); |
| 335 |
} elseif ( $type === 'pwrap-bug-workaround' && |
| 336 |
isset( $params['root'] ) && |
| 337 |
isset( $params['child'] ) ) { |
| 338 |
return Html::element( 'code', [], |
| 339 |
$params['root'] . " > " . $params['child'] ); |
| 340 |
} elseif ( $type === 'tidy-whitespace-bug' && |
| 341 |
isset( $params['node'] ) && |
| 342 |
isset( $params['sibling'] ) ) { |
| 343 |
return Html::element( 'code', [], |
| 344 |
$params['node'] . " + " . $params['sibling'] ); |
| 345 |
} elseif ( $type === 'multi-colon-escape' && |
| 346 |
isset( $params['href'] ) ) { |
| 347 |
return Html::element( 'code', [], $params['href'] ); |
| 348 |
} elseif ( $type === 'multiline-html-table-in-list' ) { |
| 349 |
|
| 350 |
return Html::element( 'code', [], |
| 351 |
$params['ancestorName'] . " > " . $params['name'] ); |
| 352 |
} elseif ( $type === 'misc-tidy-replacement-issues' ) { |
| 353 |
|
| 354 |
return Html::element( 'code', [], $params['subtype'] ); |
| 355 |
} elseif ( $type === 'missing-end-tag' ) { |
| 356 |
return Html::element( 'code', [], '</' . $params['name'] . '>' ); |
| 357 |
} elseif ( isset( $params['name'] ) ) { |
| 358 |
return Html::element( 'code', [], $params['name'] ); |
| 359 |
} |
| 360 |
|
| 361 |
return ''; |
| 362 |
} |
| 363 |
|
| 364 |
} |