| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\CommentFormatter; |
| 4 |
|
| 5 |
use File; |
| 6 |
use HtmlArmor; |
| 7 |
use MediaWiki\Cache\LinkBatch; |
| 8 |
use MediaWiki\Cache\LinkBatchFactory; |
| 9 |
use MediaWiki\Cache\LinkCache; |
| 10 |
use MediaWiki\HookContainer\HookContainer; |
| 11 |
use MediaWiki\HookContainer\HookRunner; |
| 12 |
use MediaWiki\Html\Html; |
| 13 |
use MediaWiki\Language\Language; |
| 14 |
use MediaWiki\Linker\Linker; |
| 15 |
use MediaWiki\Linker\LinkRenderer; |
| 16 |
use MediaWiki\Linker\LinkTarget; |
| 17 |
use MediaWiki\Parser\Parser; |
| 18 |
use MediaWiki\Parser\Sanitizer; |
| 19 |
use MediaWiki\SpecialPage\SpecialPage; |
| 20 |
use MediaWiki\Title\MalformedTitleException; |
| 21 |
use MediaWiki\Title\NamespaceInfo; |
| 22 |
use MediaWiki\Title\Title; |
| 23 |
use MediaWiki\Title\TitleParser; |
| 24 |
use MediaWiki\Title\TitleValue; |
| 25 |
use MediaWiki\WikiMap\WikiMap; |
| 26 |
use RepoGroup; |
| 27 |
use StringUtils; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class CommentParser { |
| 38 |
|
| 39 |
private $linkRenderer; |
| 40 |
|
| 41 |
private $linkBatchFactory; |
| 42 |
|
| 43 |
private $repoGroup; |
| 44 |
|
| 45 |
private $userLang; |
| 46 |
|
| 47 |
private $contLang; |
| 48 |
|
| 49 |
private $titleParser; |
| 50 |
|
| 51 |
private $namespaceInfo; |
| 52 |
|
| 53 |
private $hookRunner; |
| 54 |
|
| 55 |
private $linkCache; |
| 56 |
|
| 57 |
|
| 58 |
private $links = []; |
| 59 |
|
| 60 |
private $linkBatch; |
| 61 |
|
| 62 |
|
| 63 |
private $fileBatch; |
| 64 |
|
| 65 |
private $files = []; |
| 66 |
|
| 67 |
|
| 68 |
private const MAX_ID_SIZE = 7; |
| 69 |
|
| 70 |
private const MARKER_PREFIX = "\x1B\"'"; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function __construct( |
| 84 |
LinkRenderer $linkRenderer, |
| 85 |
LinkBatchFactory $linkBatchFactory, |
| 86 |
LinkCache $linkCache, |
| 87 |
RepoGroup $repoGroup, |
| 88 |
Language $userLang, |
| 89 |
Language $contLang, |
| 90 |
TitleParser $titleParser, |
| 91 |
NamespaceInfo $namespaceInfo, |
| 92 |
HookContainer $hookContainer |
| 93 |
) { |
| 94 |
$this->linkRenderer = $linkRenderer; |
| 95 |
$this->linkBatchFactory = $linkBatchFactory; |
| 96 |
$this->linkCache = $linkCache; |
| 97 |
$this->repoGroup = $repoGroup; |
| 98 |
$this->userLang = $userLang; |
| 99 |
$this->contLang = $contLang; |
| 100 |
$this->titleParser = $titleParser; |
| 101 |
$this->namespaceInfo = $namespaceInfo; |
| 102 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function preprocess( string $comment, ?LinkTarget $selfLinkTarget = null, |
| 117 |
$samePage = false, $wikiId = false, $enableSectionLinks = true |
| 118 |
) { |
| 119 |
return $this->preprocessInternal( $comment, false, $selfLinkTarget, |
| 120 |
$samePage, $wikiId, $enableSectionLinks ); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function preprocessUnsafe( $comment, ?LinkTarget $selfLinkTarget = null, |
| 134 |
$samePage = false, $wikiId = false, $enableSectionLinks = true |
| 135 |
) { |
| 136 |
return $this->preprocessInternal( $comment, true, $selfLinkTarget, |
| 137 |
$samePage, $wikiId, $enableSectionLinks ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function finalize( $comments ) { |
| 148 |
$this->flushLinkBatches(); |
| 149 |
return preg_replace_callback( |
| 150 |
'/' . self::MARKER_PREFIX . '([0-9]{' . self::MAX_ID_SIZE . '})/', |
| 151 |
function ( $m ) { |
| 152 |
$callback = $this->links[(int)$m[1]] ?? null; |
| 153 |
if ( $callback ) { |
| 154 |
return $callback(); |
| 155 |
} else { |
| 156 |
return '<!-- MISSING -->'; |
| 157 |
} |
| 158 |
}, |
| 159 |
$comments |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
private function preprocessInternal( $comment, $unsafe, $selfLinkTarget, $samePage, $wikiId, |
| 173 |
$enableSectionLinks |
| 174 |
) { |
| 175 |
|
| 176 |
|
| 177 |
$comment = strtr( $comment, "\n\x1b", " " ); |
| 178 |
|
| 179 |
if ( !$unsafe ) { |
| 180 |
$comment = Sanitizer::escapeHtmlAllowEntities( $comment ); |
| 181 |
} |
| 182 |
if ( $enableSectionLinks ) { |
| 183 |
$comment = $this->doSectionLinks( $comment, $selfLinkTarget, $samePage, $wikiId ); |
| 184 |
} |
| 185 |
return $this->doWikiLinks( $comment, $selfLinkTarget, $samePage, $wikiId ); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
private function doSectionLinks( |
| 205 |
$comment, |
| 206 |
$selfLinkTarget = null, |
| 207 |
$samePage = false, |
| 208 |
$wikiId = false |
| 209 |
) { |
| 210 |
$comment = preg_replace_callback( |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
'!(?:(?<=(.)))?/\*\s*(.*?)\s*\*/(?:(?=(.)))?!', |
| 217 |
function ( $match ) use ( $selfLinkTarget, $samePage, $wikiId ) { |
| 218 |
|
| 219 |
$match += [ '', '', '', '' ]; |
| 220 |
|
| 221 |
$pre = $match[1] !== ''; |
| 222 |
$auto = $match[2]; |
| 223 |
$post = $match[3] !== ''; |
| 224 |
$comment = null; |
| 225 |
|
| 226 |
$this->hookRunner->onFormatAutocomments( |
| 227 |
$comment, $pre, $auto, $post, |
| 228 |
Title::castFromLinkTarget( $selfLinkTarget ), |
| 229 |
$samePage, |
| 230 |
$wikiId ); |
| 231 |
if ( $comment !== null ) { |
| 232 |
return $comment; |
| 233 |
} |
| 234 |
|
| 235 |
if ( $selfLinkTarget ) { |
| 236 |
$section = $auto; |
| 237 |
|
| 238 |
|
| 239 |
$section = str_replace( [ |
| 240 |
'[[:', |
| 241 |
'[[', |
| 242 |
']]' |
| 243 |
], '', $section ); |
| 244 |
|
| 245 |
$section = substr( Parser::guessSectionNameFromStrippedText( $section ), 1 ); |
| 246 |
if ( $section !== '' ) { |
| 247 |
if ( $samePage ) { |
| 248 |
$sectionTitle = new TitleValue( NS_MAIN, '', $section ); |
| 249 |
} else { |
| 250 |
$sectionTitle = $selfLinkTarget->createFragmentTarget( $section ); |
| 251 |
} |
| 252 |
$auto = $this->makeSectionLink( |
| 253 |
$sectionTitle, |
| 254 |
$this->userLang->getArrow() . |
| 255 |
Html::rawElement( 'bdi', [ 'dir' => $this->userLang->getDir() ], $auto ), |
| 256 |
$wikiId, |
| 257 |
$selfLinkTarget |
| 258 |
); |
| 259 |
} |
| 260 |
} |
| 261 |
if ( $pre ) { |
| 262 |
|
| 263 |
$pre = wfMessage( 'autocomment-prefix' )->inContentLanguage()->escaped(); |
| 264 |
} |
| 265 |
if ( $post ) { |
| 266 |
|
| 267 |
$auto .= wfMessage( 'colon-separator' )->inContentLanguage()->escaped(); |
| 268 |
} |
| 269 |
if ( $auto ) { |
| 270 |
$auto = Html::rawElement( 'span', [ 'class' => 'autocomment' ], $auto ); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
return str_replace( [ '[', ']' ], [ '[', ']' ], $pre . $auto ); |
| 276 |
}, |
| 277 |
$comment |
| 278 |
); |
| 279 |
return $comment; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
private function makeSectionLink( |
| 295 |
LinkTarget $target, $text, $wikiId, LinkTarget $contextTitle |
| 296 |
) { |
| 297 |
if ( $wikiId !== null && $wikiId !== false && !$target->isExternal() ) { |
| 298 |
return $this->linkRenderer->makeExternalLink( |
| 299 |
WikiMap::getForeignURL( |
| 300 |
$wikiId, |
| 301 |
$target->getNamespace() === 0 |
| 302 |
? $target->getDBkey() |
| 303 |
: $this->namespaceInfo->getCanonicalName( $target->getNamespace() ) . |
| 304 |
':' . $target->getDBkey(), |
| 305 |
$target->getFragment() |
| 306 |
), |
| 307 |
new HtmlArmor( $text ), |
| 308 |
$contextTitle |
| 309 |
); |
| 310 |
} |
| 311 |
return $this->linkRenderer->makePreloadedLink( $target, new HtmlArmor( $text ), '' ); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
private function doWikiLinks( $comment, $selfLinkTarget = null, $samePage = false, $wikiId = false ) { |
| 333 |
return preg_replace_callback( |
| 334 |
'/ |
| 335 |
\[\[ |
| 336 |
\s*+ # ignore leading whitespace, the *+ quantifier disallows backtracking |
| 337 |
:? # ignore optional leading colon |
| 338 |
([^[\]|]+) # 1. link target; page names cannot include [, ] or | |
| 339 |
(?:\| |
| 340 |
# 2. link text |
| 341 |
# Stop matching at ]] without relying on backtracking. |
| 342 |
((?:]?[^\]])*+) |
| 343 |
)? |
| 344 |
\]\] |
| 345 |
([^[]*) # 3. link trail (the text up until the next link) |
| 346 |
/x', |
| 347 |
function ( $match ) use ( $selfLinkTarget, $samePage, $wikiId ) { |
| 348 |
$medians = '(?:'; |
| 349 |
$medians .= preg_quote( |
| 350 |
$this->namespaceInfo->getCanonicalName( NS_MEDIA ), '/' ); |
| 351 |
$medians .= '|'; |
| 352 |
$medians .= preg_quote( |
| 353 |
$this->contLang->getNsText( NS_MEDIA ), |
| 354 |
'/' |
| 355 |
) . '):'; |
| 356 |
|
| 357 |
$comment = $match[0]; |
| 358 |
|
| 359 |
|
| 360 |
if ( strpos( $match[1], '%' ) !== false ) { |
| 361 |
$match[1] = strtr( |
| 362 |
rawurldecode( $match[1] ), |
| 363 |
[ '<' => '<', '>' => '>' ] |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
if ( $match[2] != "" ) { |
| 369 |
$text = $match[2]; |
| 370 |
} else { |
| 371 |
$text = $match[1]; |
| 372 |
} |
| 373 |
$submatch = []; |
| 374 |
$linkMarker = null; |
| 375 |
if ( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) { |
| 376 |
|
| 377 |
$linkRegexp = '/\[\[(.*?)\]\]/'; |
| 378 |
$linkTarget = $this->titleParser->makeTitleValueSafe( NS_FILE, $submatch[1] ); |
| 379 |
if ( $linkTarget ) { |
| 380 |
$linkMarker = $this->addFileLink( $linkTarget, $text ); |
| 381 |
} |
| 382 |
} else { |
| 383 |
|
| 384 |
|
| 385 |
if ( isset( $match[1][0] ) && $match[1][0] == ':' ) { |
| 386 |
$match[1] = substr( $match[1], 1 ); |
| 387 |
} |
| 388 |
|
| 389 |
if ( $match[1] !== false && $match[1] !== null && $match[1] !== '' ) { |
| 390 |
if ( preg_match( |
| 391 |
$this->contLang->linkTrail(), |
| 392 |
$match[3], |
| 393 |
$submatch |
| 394 |
) ) { |
| 395 |
$trail = $submatch[1]; |
| 396 |
} else { |
| 397 |
$trail = ""; |
| 398 |
} |
| 399 |
$linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/'; |
| 400 |
[ $inside, $trail ] = Linker::splitTrail( $trail ); |
| 401 |
|
| 402 |
$linkText = $text; |
| 403 |
$linkTarget = Linker::normalizeSubpageLink( $selfLinkTarget, $match[1], $linkText ); |
| 404 |
|
| 405 |
try { |
| 406 |
$target = $this->titleParser->parseTitle( $linkTarget ); |
| 407 |
|
| 408 |
if ( $target->getText() == '' && !$target->isExternal() |
| 409 |
&& !$samePage && $selfLinkTarget |
| 410 |
) { |
| 411 |
$target = $selfLinkTarget->createFragmentTarget( $target->getFragment() ); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
global $wgTitle; |
| 419 |
$linkMarker = $this->addPageLink( |
| 420 |
$target, |
| 421 |
$linkText . $inside, |
| 422 |
$wikiId, |
| 423 |
$selfLinkTarget ?? $wgTitle ?? SpecialPage::getTitleFor( 'Badtitle' ) |
| 424 |
); |
| 425 |
$linkMarker .= $trail; |
| 426 |
} catch ( MalformedTitleException $e ) { |
| 427 |
|
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
if ( $linkMarker ) { |
| 432 |
|
| 433 |
$comment = preg_replace( |
| 434 |
|
| 435 |
|
| 436 |
$linkRegexp, |
| 437 |
StringUtils::escapeRegexReplacement( $linkMarker ), |
| 438 |
$comment, |
| 439 |
1 |
| 440 |
); |
| 441 |
} |
| 442 |
|
| 443 |
return $comment; |
| 444 |
}, |
| 445 |
$comment |
| 446 |
); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
private function addLinkMarker( $callback ) { |
| 456 |
$nextId = count( $this->links ); |
| 457 |
if ( strlen( (string)$nextId ) > self::MAX_ID_SIZE ) { |
| 458 |
throw new \RuntimeException( 'Too many links in comment batch' ); |
| 459 |
} |
| 460 |
$this->links[] = $callback; |
| 461 |
return sprintf( self::MARKER_PREFIX . "%0" . self::MAX_ID_SIZE . 'd', $nextId ); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
private function addPageLink( LinkTarget $target, $text, $wikiId, LinkTarget $contextTitle ) { |
| 475 |
if ( $wikiId !== null && $wikiId !== false && !$target->isExternal() ) { |
| 476 |
|
| 477 |
return $this->linkRenderer->makeExternalLink( |
| 478 |
WikiMap::getForeignURL( |
| 479 |
$wikiId, |
| 480 |
$target->getNamespace() === 0 |
| 481 |
? $target->getDBkey() |
| 482 |
: $this->namespaceInfo->getCanonicalName( $target->getNamespace() ) . |
| 483 |
':' . $target->getDBkey(), |
| 484 |
$target->getFragment() |
| 485 |
), |
| 486 |
new HtmlArmor( $text ), |
| 487 |
$contextTitle |
| 488 |
); |
| 489 |
} elseif ( $this->linkCache->getGoodLinkID( $target ) || |
| 490 |
Title::newFromLinkTarget( $target )->isAlwaysKnown() |
| 491 |
) { |
| 492 |
|
| 493 |
return $this->linkRenderer->makeKnownLink( $target, new HtmlArmor( $text ) ); |
| 494 |
} elseif ( $this->linkCache->isBadLink( $target ) ) { |
| 495 |
|
| 496 |
return $this->linkRenderer->makeBrokenLink( $target, new HtmlArmor( $text ) ); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
if ( !$this->linkBatch ) { |
| 501 |
$this->linkBatch = $this->linkBatchFactory->newLinkBatch(); |
| 502 |
$this->linkBatch->setCaller( __METHOD__ ); |
| 503 |
} |
| 504 |
$this->linkBatch->addObj( $target ); |
| 505 |
return $this->addLinkMarker( function () use ( $target, $text ) { |
| 506 |
return $this->linkRenderer->makeLink( $target, new HtmlArmor( $text ) ); |
| 507 |
} ); |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
private function addFileLink( LinkTarget $target, $html ) { |
| 518 |
$this->fileBatch[] = [ |
| 519 |
'title' => $target |
| 520 |
]; |
| 521 |
return $this->addLinkMarker( function () use ( $target, $html ) { |
| 522 |
return Linker::makeMediaLinkFile( |
| 523 |
$target, |
| 524 |
$this->files[$target->getDBkey()] ?? false, |
| 525 |
$html |
| 526 |
); |
| 527 |
} ); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
private function flushLinkBatches() { |
| 534 |
if ( $this->linkBatch ) { |
| 535 |
$this->linkBatch->execute(); |
| 536 |
$this->linkBatch = null; |
| 537 |
} |
| 538 |
if ( $this->fileBatch ) { |
| 539 |
$this->files += $this->repoGroup->findFiles( $this->fileBatch ); |
| 540 |
$this->fileBatch = []; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
} |