| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools; |
| 4 |
|
| 5 |
use DateInterval; |
| 6 |
use DateTime; |
| 7 |
use DateTimeImmutable; |
| 8 |
use DateTimeZone; |
| 9 |
use InvalidArgumentException; |
| 10 |
use LogicException; |
| 11 |
use MediaWiki\Config\Config; |
| 12 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem; |
| 13 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem; |
| 14 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem; |
| 15 |
use MediaWiki\Language\Language; |
| 16 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 17 |
use MediaWiki\Title\MalformedTitleException; |
| 18 |
use MediaWiki\Title\TitleParser; |
| 19 |
use MediaWiki\Title\TitleValue; |
| 20 |
use MediaWiki\Utils\MWTimestamp; |
| 21 |
use RuntimeException; |
| 22 |
use Wikimedia\Assert\Assert; |
| 23 |
use Wikimedia\IPUtils; |
| 24 |
use Wikimedia\Parsoid\DOM\Element; |
| 25 |
use Wikimedia\Parsoid\DOM\Node; |
| 26 |
use Wikimedia\Parsoid\DOM\Text; |
| 27 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 28 |
use Wikimedia\Timestamp\TimestampException; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class CommentParser { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private const SIGNATURE_SCAN_LIMIT = 100; |
| 39 |
|
| 40 |
private Config $config; |
| 41 |
private Language $language; |
| 42 |
private LanguageConverterFactory $languageConverterFactory; |
| 43 |
private TitleParser $titleParser; |
| 44 |
|
| 45 |
|
| 46 |
private array $dateFormat; |
| 47 |
|
| 48 |
private array $digits; |
| 49 |
|
| 50 |
private $contLangMessages; |
| 51 |
private string $localTimezone; |
| 52 |
|
| 53 |
private array $timezones; |
| 54 |
private string $specialContributionsName; |
| 55 |
|
| 56 |
private Element $rootNode; |
| 57 |
private TitleValue $title; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function __construct( |
| 67 |
Config $config, |
| 68 |
Language $language, |
| 69 |
LanguageConverterFactory $languageConverterFactory, |
| 70 |
LanguageData $languageData, |
| 71 |
TitleParser $titleParser |
| 72 |
) { |
| 73 |
$this->config = $config; |
| 74 |
$this->language = $language; |
| 75 |
$this->languageConverterFactory = $languageConverterFactory; |
| 76 |
$this->titleParser = $titleParser; |
| 77 |
|
| 78 |
$data = $languageData->getLocalData(); |
| 79 |
$this->dateFormat = $data['dateFormat']; |
| 80 |
$this->digits = $data['digits']; |
| 81 |
$this->contLangMessages = $data['contLangMessages']; |
| 82 |
$this->localTimezone = $data['localTimezone']; |
| 83 |
$this->timezones = $data['timezones']; |
| 84 |
$this->specialContributionsName = $data['specialContributionsName']; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function parse( Element $rootNode, TitleValue $title ): ContentThreadItemSet { |
| 95 |
$this->rootNode = $rootNode; |
| 96 |
$this->title = $title; |
| 97 |
|
| 98 |
$result = $this->buildThreadItems(); |
| 99 |
$this->buildThreads( $result ); |
| 100 |
$this->computeIdsAndNames( $result ); |
| 101 |
|
| 102 |
return $result; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
private function nextInterestingLeafNode( ?Node $node ): Node { |
| 118 |
$rootNode = $this->rootNode; |
| 119 |
$treeWalker = new TreeWalker( |
| 120 |
$rootNode, |
| 121 |
NodeFilter::SHOW_ELEMENT | NodeFilter::SHOW_TEXT, |
| 122 |
static function ( $n ) use ( $node, $rootNode ) { |
| 123 |
|
| 124 |
if ( $n === $node || $n->parentNode === $node ) { |
| 125 |
return NodeFilter::FILTER_REJECT; |
| 126 |
} |
| 127 |
|
| 128 |
if ( CommentUtils::isCommentSeparator( $n ) ) { |
| 129 |
return NodeFilter::FILTER_REJECT; |
| 130 |
} |
| 131 |
|
| 132 |
if ( CommentUtils::isRenderingTransparentNode( $n ) ) { |
| 133 |
return NodeFilter::FILTER_REJECT; |
| 134 |
} |
| 135 |
if ( CommentUtils::isCommentContent( $n ) ) { |
| 136 |
return NodeFilter::FILTER_ACCEPT; |
| 137 |
} |
| 138 |
return NodeFilter::FILTER_SKIP; |
| 139 |
} |
| 140 |
); |
| 141 |
if ( $node ) { |
| 142 |
$treeWalker->currentNode = $node; |
| 143 |
} |
| 144 |
$treeWalker->nextNode(); |
| 145 |
if ( !$treeWalker->currentNode ) { |
| 146 |
throw new RuntimeException( 'nextInterestingLeafNode not found' ); |
| 147 |
} |
| 148 |
return $treeWalker->currentNode; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
private static function regexpAlternateGroup( array $values ): string { |
| 156 |
return '(' . implode( '|', array_map( static function ( string $x ) { |
| 157 |
return preg_quote( $x, '/' ); |
| 158 |
}, $values ) ) . ')'; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private function getMessages( string $contLangVariant, array $messages ): array { |
| 169 |
return array_map( function ( string $key ) use ( $contLangVariant ) { |
| 170 |
return $this->contLangMessages[$contLangVariant][$key]; |
| 171 |
}, $messages ); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
private function getTimestampRegexp( |
| 190 |
string $contLangVariant, string $format, string $digitsRegexp, array $tzAbbrs |
| 191 |
): string { |
| 192 |
$formatLength = strlen( $format ); |
| 193 |
$s = ''; |
| 194 |
$raw = false; |
| 195 |
|
| 196 |
for ( $p = 0; $p < $formatLength; $p++ ) { |
| 197 |
$num = false; |
| 198 |
$code = $format[ $p ]; |
| 199 |
if ( $code === 'x' && $p < $formatLength - 1 ) { |
| 200 |
$code .= $format[++$p]; |
| 201 |
} |
| 202 |
if ( $code === 'xk' && $p < $formatLength - 1 ) { |
| 203 |
$code .= $format[++$p]; |
| 204 |
} |
| 205 |
|
| 206 |
switch ( $code ) { |
| 207 |
case 'xx': |
| 208 |
$s .= 'x'; |
| 209 |
break; |
| 210 |
case 'xg': |
| 211 |
$s .= static::regexpAlternateGroup( |
| 212 |
$this->getMessages( $contLangVariant, Language::MONTH_GENITIVE_MESSAGES ) |
| 213 |
); |
| 214 |
break; |
| 215 |
case 'xn': |
| 216 |
$raw = true; |
| 217 |
break; |
| 218 |
case 'd': |
| 219 |
$num = '2'; |
| 220 |
break; |
| 221 |
case 'D': |
| 222 |
$s .= static::regexpAlternateGroup( |
| 223 |
$this->getMessages( $contLangVariant, Language::WEEKDAY_ABBREVIATED_MESSAGES ) |
| 224 |
); |
| 225 |
break; |
| 226 |
case 'j': |
| 227 |
$num = '1,2'; |
| 228 |
break; |
| 229 |
case 'l': |
| 230 |
$s .= static::regexpAlternateGroup( |
| 231 |
$this->getMessages( $contLangVariant, Language::WEEKDAY_MESSAGES ) |
| 232 |
); |
| 233 |
break; |
| 234 |
case 'F': |
| 235 |
$s .= static::regexpAlternateGroup( |
| 236 |
$this->getMessages( $contLangVariant, Language::MONTH_MESSAGES ) |
| 237 |
); |
| 238 |
break; |
| 239 |
case 'M': |
| 240 |
$s .= static::regexpAlternateGroup( |
| 241 |
$this->getMessages( $contLangVariant, Language::MONTH_ABBREVIATED_MESSAGES ) |
| 242 |
); |
| 243 |
break; |
| 244 |
case 'm': |
| 245 |
$num = '2'; |
| 246 |
break; |
| 247 |
case 'n': |
| 248 |
$num = '1,2'; |
| 249 |
break; |
| 250 |
case 'Y': |
| 251 |
$num = '4'; |
| 252 |
break; |
| 253 |
case 'xkY': |
| 254 |
$num = '4'; |
| 255 |
break; |
| 256 |
case 'G': |
| 257 |
$num = '1,2'; |
| 258 |
break; |
| 259 |
case 'H': |
| 260 |
$num = '2'; |
| 261 |
break; |
| 262 |
case 'i': |
| 263 |
$num = '2'; |
| 264 |
break; |
| 265 |
case 's': |
| 266 |
$num = '2'; |
| 267 |
break; |
| 268 |
case '\\': |
| 269 |
|
| 270 |
if ( $p < $formatLength - 1 ) { |
| 271 |
$s .= preg_quote( $format[++$p], '/' ); |
| 272 |
} else { |
| 273 |
$s .= preg_quote( '\\', '/' ); |
| 274 |
} |
| 275 |
break; |
| 276 |
case '"': |
| 277 |
|
| 278 |
if ( $p < $formatLength - 1 ) { |
| 279 |
$endQuote = strpos( $format, '"', $p + 1 ); |
| 280 |
if ( $endQuote === false ) { |
| 281 |
|
| 282 |
$s .= '"'; |
| 283 |
} else { |
| 284 |
$s .= preg_quote( substr( $format, $p + 1, $endQuote - $p - 1 ), '/' ); |
| 285 |
$p = $endQuote; |
| 286 |
} |
| 287 |
} else { |
| 288 |
|
| 289 |
$s .= '"'; |
| 290 |
} |
| 291 |
break; |
| 292 |
default: |
| 293 |
|
| 294 |
$char = mb_substr( mb_strcut( $format, $p, 4 ), 0, 1 ); |
| 295 |
$s .= preg_quote( $char, '/' ); |
| 296 |
$p += strlen( $char ) - 1; |
| 297 |
} |
| 298 |
if ( $num !== false ) { |
| 299 |
if ( $raw ) { |
| 300 |
$s .= '([0-9]{' . $num . '})'; |
| 301 |
$raw = false; |
| 302 |
} else { |
| 303 |
$s .= '(' . $digitsRegexp . '{' . $num . '})'; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
$s .= '[\\x{200E}\\x{200F}]?'; |
| 308 |
} |
| 309 |
|
| 310 |
$tzRegexp = static::regexpAlternateGroup( array_keys( $tzAbbrs ) ); |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
return '/' . $s . ' [\\x{200E}\\x{200F}]?\\(' . $tzRegexp . '\\)/u'; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
private function getTimestampParser( |
| 331 |
string $contLangVariant, string $format, ?array $digits, string $localTimezone, array $tzAbbrs |
| 332 |
): callable { |
| 333 |
$untransformDigits = static function ( string $text ) use ( $digits ): int { |
| 334 |
return (int)( $digits ? strtr( $text, array_flip( $digits ) ) : $text ); |
| 335 |
}; |
| 336 |
|
| 337 |
$formatLength = strlen( $format ); |
| 338 |
$matchingGroups = []; |
| 339 |
for ( $p = 0; $p < $formatLength; $p++ ) { |
| 340 |
$code = $format[$p]; |
| 341 |
if ( $code === 'x' && $p < $formatLength - 1 ) { |
| 342 |
$code .= $format[++$p]; |
| 343 |
} |
| 344 |
if ( $code === 'xk' && $p < $formatLength - 1 ) { |
| 345 |
$code .= $format[++$p]; |
| 346 |
} |
| 347 |
|
| 348 |
switch ( $code ) { |
| 349 |
case 'xx': |
| 350 |
case 'xn': |
| 351 |
break; |
| 352 |
case 'xg': |
| 353 |
case 'd': |
| 354 |
case 'j': |
| 355 |
case 'D': |
| 356 |
case 'l': |
| 357 |
case 'F': |
| 358 |
case 'M': |
| 359 |
case 'm': |
| 360 |
case 'n': |
| 361 |
case 'Y': |
| 362 |
case 'xkY': |
| 363 |
case 'G': |
| 364 |
case 'H': |
| 365 |
case 'i': |
| 366 |
case 's': |
| 367 |
$matchingGroups[] = $code; |
| 368 |
break; |
| 369 |
case '\\': |
| 370 |
|
| 371 |
if ( $p < $formatLength - 1 ) { |
| 372 |
$p++; |
| 373 |
} |
| 374 |
break; |
| 375 |
case '"': |
| 376 |
|
| 377 |
if ( $p < $formatLength - 1 ) { |
| 378 |
$endQuote = strpos( $format, '"', $p + 1 ); |
| 379 |
if ( $endQuote !== false ) { |
| 380 |
$p = $endQuote; |
| 381 |
} |
| 382 |
} |
| 383 |
break; |
| 384 |
default: |
| 385 |
break; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
return function ( array $match ) use ( |
| 390 |
$matchingGroups, $untransformDigits, $localTimezone, $tzAbbrs, $contLangVariant |
| 391 |
) { |
| 392 |
if ( is_array( $match[0] ) ) { |
| 393 |
|
| 394 |
unset( $match['offset'] ); |
| 395 |
$match = array_map( static function ( array $tuple ) { |
| 396 |
return $tuple[0]; |
| 397 |
}, $match ); |
| 398 |
} |
| 399 |
$year = 0; |
| 400 |
$monthIdx = 0; |
| 401 |
$day = 0; |
| 402 |
$hour = 0; |
| 403 |
$minute = 0; |
| 404 |
foreach ( $matchingGroups as $i => $code ) { |
| 405 |
$text = $match[$i + 1]; |
| 406 |
switch ( $code ) { |
| 407 |
case 'xg': |
| 408 |
$monthIdx = array_search( |
| 409 |
$text, |
| 410 |
$this->getMessages( $contLangVariant, Language::MONTH_GENITIVE_MESSAGES ), |
| 411 |
true |
| 412 |
); |
| 413 |
break; |
| 414 |
case 'd': |
| 415 |
case 'j': |
| 416 |
$day = $untransformDigits( $text ); |
| 417 |
break; |
| 418 |
case 'D': |
| 419 |
case 'l': |
| 420 |
|
| 421 |
break; |
| 422 |
case 'F': |
| 423 |
$monthIdx = array_search( |
| 424 |
$text, |
| 425 |
$this->getMessages( $contLangVariant, Language::MONTH_MESSAGES ), |
| 426 |
true |
| 427 |
); |
| 428 |
break; |
| 429 |
case 'M': |
| 430 |
$monthIdx = array_search( |
| 431 |
$text, |
| 432 |
$this->getMessages( $contLangVariant, Language::MONTH_ABBREVIATED_MESSAGES ), |
| 433 |
true |
| 434 |
); |
| 435 |
break; |
| 436 |
case 'm': |
| 437 |
case 'n': |
| 438 |
$monthIdx = $untransformDigits( $text ) - 1; |
| 439 |
break; |
| 440 |
case 'Y': |
| 441 |
$year = $untransformDigits( $text ); |
| 442 |
break; |
| 443 |
case 'xkY': |
| 444 |
|
| 445 |
$year = $untransformDigits( $text ) - 543; |
| 446 |
break; |
| 447 |
case 'G': |
| 448 |
case 'H': |
| 449 |
$hour = $untransformDigits( $text ); |
| 450 |
break; |
| 451 |
case 'i': |
| 452 |
$minute = $untransformDigits( $text ); |
| 453 |
break; |
| 454 |
case 's': |
| 455 |
|
| 456 |
break; |
| 457 |
default: |
| 458 |
throw new LogicException( 'Not implemented' ); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
$tzAbbr = $tzAbbrs[ end( $match ) ]; |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
$date = new DateTime(); |
| 468 |
|
| 469 |
$date->setTimezone( new DateTimeZone( $localTimezone ) ); |
| 470 |
$date->setDate( $year, $monthIdx + 1, $day ); |
| 471 |
$date->setTime( $hour, $minute, 0 ); |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
$dateWarning = null; |
| 478 |
if ( $date->format( 'T' ) !== $tzAbbr ) { |
| 479 |
$altDate = clone $date; |
| 480 |
if ( $date->format( 'I' ) ) { |
| 481 |
|
| 482 |
$altDate->add( new DateInterval( 'PT1H' ) ); |
| 483 |
} else { |
| 484 |
|
| 485 |
$altDate->sub( new DateInterval( 'PT1H' ) ); |
| 486 |
} |
| 487 |
if ( $altDate->format( 'T' ) === $tzAbbr ) { |
| 488 |
$date = $altDate; |
| 489 |
$dateWarning = 'Timestamp has timezone abbreviation for the wrong time'; |
| 490 |
} else { |
| 491 |
$dateWarning = 'Ambiguous time at DST switchover was parsed'; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
$date->setTimezone( new DateTimeZone( 'UTC' ) ); |
| 497 |
$date = DateTimeImmutable::createFromMutable( $date ); |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
try { |
| 503 |
|
| 504 |
new MWTimestamp( $date->format( 'c' ) ); |
| 505 |
} catch ( TimestampException $ex ) { |
| 506 |
return null; |
| 507 |
} |
| 508 |
|
| 509 |
return [ |
| 510 |
'date' => $date, |
| 511 |
'warning' => $dateWarning, |
| 512 |
]; |
| 513 |
}; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
public function getLocalTimestampRegexps(): array { |
| 524 |
$langConv = $this->languageConverterFactory->getLanguageConverter( $this->language ); |
| 525 |
return array_map( function ( $contLangVariant ) { |
| 526 |
return $this->getTimestampRegexp( |
| 527 |
$contLangVariant, |
| 528 |
$this->dateFormat[$contLangVariant], |
| 529 |
'[' . implode( '', $this->digits[$contLangVariant] ) . ']', |
| 530 |
$this->timezones[$contLangVariant] |
| 531 |
); |
| 532 |
}, $langConv->getVariants() ); |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
private function getLocalTimestampParsers(): array { |
| 544 |
$langConv = $this->languageConverterFactory->getLanguageConverter( $this->language ); |
| 545 |
return array_map( function ( $contLangVariant ) { |
| 546 |
return $this->getTimestampParser( |
| 547 |
$contLangVariant, |
| 548 |
$this->dateFormat[$contLangVariant], |
| 549 |
$this->digits[$contLangVariant], |
| 550 |
$this->localTimezone, |
| 551 |
$this->timezones[$contLangVariant] |
| 552 |
); |
| 553 |
}, $langConv->getVariants() ); |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
private function getUsernameFromLink( Element $link ): ?array { |
| 565 |
|
| 566 |
if ( DOMCompat::getClassList( $link )->contains( 'mw-selflink' ) ) { |
| 567 |
$title = $this->title; |
| 568 |
} else { |
| 569 |
$titleString = CommentUtils::getTitleFromUrl( $link->getAttribute( 'href' ) ?? '', $this->config ) ?? ''; |
| 570 |
|
| 571 |
if ( $titleString === '' || !str_contains( $titleString, ':' ) ) { |
| 572 |
return null; |
| 573 |
} |
| 574 |
$title = $this->parseTitle( $titleString ); |
| 575 |
if ( !$title ) { |
| 576 |
return null; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
$username = null; |
| 581 |
$displayName = null; |
| 582 |
$mainText = $title->getText(); |
| 583 |
|
| 584 |
if ( $title->inNamespace( NS_USER ) || $title->inNamespace( NS_USER_TALK ) ) { |
| 585 |
$username = $mainText; |
| 586 |
if ( str_contains( $username, '/' ) ) { |
| 587 |
return null; |
| 588 |
} |
| 589 |
if ( $title->inNamespace( NS_USER ) ) { |
| 590 |
|
| 591 |
$text = preg_replace( [ '/^[\s]+/u', '/[\s]+$/u' ], '', $link->textContent ?? '' ); |
| 592 |
|
| 593 |
if ( $text && mb_strtolower( $text ) !== mb_strtolower( $username ) ) { |
| 594 |
$displayName = $text; |
| 595 |
} |
| 596 |
} |
| 597 |
} elseif ( $title->inNamespace( NS_SPECIAL ) ) { |
| 598 |
$parts = explode( '/', $mainText ); |
| 599 |
if ( count( $parts ) === 2 && $parts[0] === $this->specialContributionsName ) { |
| 600 |
|
| 601 |
$userpage = $this->titleParser->makeTitleValueSafe( NS_USER, $parts[1] ); |
| 602 |
if ( !$userpage ) { |
| 603 |
return null; |
| 604 |
} |
| 605 |
$username = $userpage->getText(); |
| 606 |
} |
| 607 |
} |
| 608 |
if ( $username === null ) { |
| 609 |
return null; |
| 610 |
} |
| 611 |
if ( IPUtils::isIPv6( $username ) ) { |
| 612 |
|
| 613 |
$username = strtoupper( $username ); |
| 614 |
} |
| 615 |
return [ |
| 616 |
'username' => $username, |
| 617 |
'displayName' => $displayName, |
| 618 |
]; |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
private function findSignature( Text $timestampNode, ?Node $until = null ): array { |
| 637 |
$sigUsername = null; |
| 638 |
$sigDisplayName = null; |
| 639 |
$length = 0; |
| 640 |
$lastLinkNode = $timestampNode; |
| 641 |
|
| 642 |
CommentUtils::linearWalkBackwards( |
| 643 |
$timestampNode, |
| 644 |
function ( string $event, Node $node ) use ( |
| 645 |
&$sigUsername, &$sigDisplayName, &$lastLinkNode, &$length, |
| 646 |
$until, $timestampNode |
| 647 |
) { |
| 648 |
if ( $event === 'enter' && $node === $until ) { |
| 649 |
return true; |
| 650 |
} |
| 651 |
if ( $length >= static::SIGNATURE_SCAN_LIMIT ) { |
| 652 |
return true; |
| 653 |
} |
| 654 |
if ( CommentUtils::isBlockElement( $node ) ) { |
| 655 |
|
| 656 |
return true; |
| 657 |
} |
| 658 |
|
| 659 |
if ( $event === 'leave' && $node !== $timestampNode ) { |
| 660 |
$length += $node instanceof Text ? |
| 661 |
mb_strlen( CommentUtils::htmlTrim( $node->textContent ?? '' ) ) : 0; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
if ( $event === 'leave' && $node instanceof Element && strtolower( $node->tagName ) === 'a' ) { |
| 671 |
$classList = DOMCompat::getClassList( $node ); |
| 672 |
|
| 673 |
|
| 674 |
if ( !$classList->contains( 'ext-discussiontools-init-timestamplink' ) ) { |
| 675 |
$user = $this->getUsernameFromLink( $node ); |
| 676 |
if ( $user ) { |
| 677 |
|
| 678 |
if ( $sigUsername === null ) { |
| 679 |
$sigUsername = $user['username']; |
| 680 |
} |
| 681 |
if ( $user['username'] === $sigUsername ) { |
| 682 |
$lastLinkNode = $node; |
| 683 |
if ( $user['displayName'] ) { |
| 684 |
$sigDisplayName = $user['displayName']; |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
)" |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
); |
| 694 |
|
| 695 |
$range = new ImmutableRange( |
| 696 |
$lastLinkNode->parentNode, |
| 697 |
CommentUtils::childIndexOf( $lastLinkNode ), |
| 698 |
$timestampNode->parentNode, |
| 699 |
CommentUtils::childIndexOf( $timestampNode ) + 1 |
| 700 |
); |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
)" |
| 708 |
»" |
| 709 |
|
| 710 |
|
| 711 |
$sigNodes = array_reverse( CommentUtils::getCoveredSiblings( $range ) ); |
| 712 |
|
| 713 |
return [ |
| 714 |
'nodes' => $sigNodes, |
| 715 |
'username' => $sigUsername, |
| 716 |
'displayName' => $sigDisplayName, |
| 717 |
]; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
public static function acceptOnlyNodesAllowingComments( Node $node ): int { |
| 728 |
if ( $node instanceof Element ) { |
| 729 |
$tagName = strtolower( $node->tagName ); |
| 730 |
|
| 731 |
if ( $node->getAttribute( 'id' ) === 'toc' ) { |
| 732 |
return NodeFilter::FILTER_REJECT; |
| 733 |
} |
| 734 |
|
| 735 |
if ( |
| 736 |
$tagName === 'blockquote' || |
| 737 |
$tagName === 'cite' || |
| 738 |
$tagName === 'q' |
| 739 |
) { |
| 740 |
return NodeFilter::FILTER_REJECT; |
| 741 |
} |
| 742 |
$classList = DOMCompat::getClassList( $node ); |
| 743 |
|
| 744 |
if ( $classList->contains( 'mw-notalk' ) ) { |
| 745 |
return NodeFilter::FILTER_REJECT; |
| 746 |
} |
| 747 |
|
| 748 |
… |
| 749 |
if ( |
| 750 |
|
| 751 |
$tagName === 'ol' && |
| 752 |
DOMCompat::getClassList( $node )->contains( 'references' ) |
| 753 |
) { |
| 754 |
return NodeFilter::FILTER_REJECT; |
| 755 |
} |
| 756 |
} |
| 757 |
$parentNode = $node->parentNode; |
| 758 |
|
| 759 |
if ( $parentNode instanceof Element && preg_match( '/^h([1-6])$/i', $parentNode->tagName ) ) { |
| 760 |
return NodeFilter::FILTER_REJECT; |
| 761 |
} |
| 762 |
return NodeFilter::FILTER_ACCEPT; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
private static function getCodepointOffset( Text $node, int $byteOffset ): int { |
| 773 |
return mb_strlen( substr( $node->nodeValue ?? '', 0, $byteOffset ) ); |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
public function findTimestamp( Text $node, array $timestampRegexps ): ?array { |
| 789 |
$nodeText = ''; |
| 790 |
$offset = 0; |
| 791 |
|
| 792 |
$nodes = []; |
| 793 |
|
| 794 |
while ( $node ) { |
| 795 |
$nodeText = $node->nodeValue . $nodeText; |
| 796 |
$nodes[] = $node; |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
if ( |
| 803 |
( $previousSibling = $node->previousSibling ) && |
| 804 |
$previousSibling instanceof Element && |
| 805 |
$previousSibling->getAttribute( 'typeof' ) === 'mw:Entity' |
| 806 |
) { |
| 807 |
$nodeText = $previousSibling->firstChild->nodeValue . $nodeText; |
| 808 |
$offset += strlen( $previousSibling->firstChild->nodeValue ?? '' ); |
| 809 |
$nodes[] = $previousSibling->firstChild; |
| 810 |
|
| 811 |
|
| 812 |
if ( |
| 813 |
$previousSibling->previousSibling && |
| 814 |
$previousSibling->previousSibling instanceof Text |
| 815 |
) { |
| 816 |
$offset += strlen( $previousSibling->previousSibling->nodeValue ?? '' ); |
| 817 |
$node = $previousSibling->previousSibling; |
| 818 |
} else { |
| 819 |
$node = null; |
| 820 |
} |
| 821 |
} else { |
| 822 |
$node = null; |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
foreach ( $timestampRegexps as $i => $timestampRegexp ) { |
| 827 |
$matchData = null; |
| 828 |
|
| 829 |
if ( preg_match( $timestampRegexp, $nodeText, $matchData, PREG_OFFSET_CAPTURE ) ) { |
| 830 |
$timestampLength = strlen( $matchData[0][0] ); |
| 831 |
|
| 832 |
$tailLength = strlen( $nodeText ) - $timestampLength - $matchData[0][1]; |
| 833 |
|
| 834 |
|
| 835 |
$count = -$tailLength; |
| 836 |
$endNode = $nodes[0]; |
| 837 |
$endOffset = strlen( $endNode->nodeValue ?? '' ) - $tailLength; |
| 838 |
|
| 839 |
foreach ( $nodes as $n ) { |
| 840 |
$count += strlen( $n->nodeValue ?? '' ); |
| 841 |
|
| 842 |
|
| 843 |
if ( $count >= $timestampLength ) { |
| 844 |
$startNode = $n; |
| 845 |
|
| 846 |
$startOffset = $count - $timestampLength; |
| 847 |
break; |
| 848 |
} |
| 849 |
} |
| 850 |
Assert::precondition( $endNode instanceof Node, 'endNode of timestamp is a Node' ); |
| 851 |
Assert::precondition( $startNode instanceof Node, 'startNode of timestamp range found' ); |
| 852 |
Assert::precondition( is_int( $startOffset ), 'startOffset of timestamp range found' ); |
| 853 |
|
| 854 |
$startOffset = static::getCodepointOffset( $startNode, $startOffset ); |
| 855 |
$endOffset = static::getCodepointOffset( $endNode, $endOffset ); |
| 856 |
|
| 857 |
$range = new ImmutableRange( $startNode, $startOffset, $endNode, $endOffset ); |
| 858 |
|
| 859 |
return [ |
| 860 |
'matchData' => $matchData, |
| 861 |
|
| 862 |
|
| 863 |
'offset' => $offset, |
| 864 |
'range' => $range, |
| 865 |
'parserIndex' => $i, |
| 866 |
]; |
| 867 |
} |
| 868 |
} |
| 869 |
return null; |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
private function adjustSigRange( array $sigNodes, array $match, Text $node ): ImmutableRange { |
| 879 |
$firstSigNode = end( $sigNodes ); |
| 880 |
$lastSigNode = $sigNodes[0]; |
| 881 |
|
| 882 |
|
| 883 |
$lastSigNodeOffsetByteOffset = |
| 884 |
$match['matchData'][0][1] + strlen( $match['matchData'][0][0] ) - $match['offset']; |
| 885 |
$lastSigNodeOffset = $lastSigNode === $node ? |
| 886 |
static::getCodepointOffset( $node, $lastSigNodeOffsetByteOffset ) : |
| 887 |
CommentUtils::childIndexOf( $lastSigNode ) + 1; |
| 888 |
$sigRange = new ImmutableRange( |
| 889 |
$firstSigNode->parentNode, |
| 890 |
CommentUtils::childIndexOf( $firstSigNode ), |
| 891 |
$lastSigNode === $node ? $node : $lastSigNode->parentNode, |
| 892 |
$lastSigNodeOffset |
| 893 |
); |
| 894 |
|
| 895 |
return $sigRange; |
| 896 |
} |
| 897 |
|
| 898 |
private function buildThreadItems(): ContentThreadItemSet { |
| 899 |
$result = new ContentThreadItemSet(); |
| 900 |
|
| 901 |
$timestampRegexps = $this->getLocalTimestampRegexps(); |
| 902 |
$dfParsers = $this->getLocalTimestampParsers(); |
| 903 |
|
| 904 |
$curCommentEnd = null; |
| 905 |
|
| 906 |
$treeWalker = new TreeWalker( |
| 907 |
$this->rootNode, |
| 908 |
NodeFilter::SHOW_ELEMENT | NodeFilter::SHOW_TEXT, |
| 909 |
[ static::class, 'acceptOnlyNodesAllowingComments' ] |
| 910 |
); |
| 911 |
while ( $node = $treeWalker->nextNode() ) { |
| 912 |
if ( $node instanceof Element && preg_match( '/^h([1-6])$/i', $node->tagName, $match ) ) { |
| 913 |
$headingNode = CommentUtils::getHeadlineNode( $node ); |
| 914 |
$range = new ImmutableRange( |
| 915 |
$headingNode, 0, $headingNode, $headingNode->childNodes->length |
| 916 |
); |
| 917 |
$transcludedFrom = $this->computeTranscludedFrom( $range ); |
| 918 |
$curComment = new ContentHeadingItem( $range, $transcludedFrom, (int)( $match[ 1 ] ) ); |
| 919 |
$curComment->setRootNode( $this->rootNode ); |
| 920 |
$result->addThreadItem( $curComment ); |
| 921 |
$curCommentEnd = $node; |
| 922 |
} elseif ( $node instanceof Text && ( $match = $this->findTimestamp( $node, $timestampRegexps ) ) ) { |
| 923 |
$warnings = []; |
| 924 |
$foundSignature = $this->findSignature( $node, $curCommentEnd ); |
| 925 |
$author = $foundSignature['username']; |
| 926 |
|
| 927 |
if ( $author === null ) { |
| 928 |
|
| 929 |
|
| 930 |
continue; |
| 931 |
} |
| 932 |
|
| 933 |
$sigRanges = []; |
| 934 |
$timestampRanges = []; |
| 935 |
|
| 936 |
$sigRanges[] = $this->adjustSigRange( $foundSignature['nodes'], $match, $node ); |
| 937 |
$timestampRanges[] = $match['range']; |
| 938 |
|
| 939 |
|
| 940 |
$startNode = $this->nextInterestingLeafNode( $curCommentEnd ); |
| 941 |
$endNode = $foundSignature['nodes'][0]; |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
CommentUtils::linearWalk( |
| 954 |
$endNode, |
| 955 |
function ( string $event, Node $n ) use ( |
| 956 |
&$endNode, &$sigRanges, &$timestampRanges, |
| 957 |
$treeWalker, $timestampRegexps, $node |
| 958 |
) { |
| 959 |
if ( CommentUtils::isBlockElement( $n ) || CommentUtils::isCommentSeparator( $n ) ) { |
| 960 |
|
| 961 |
return true; |
| 962 |
} |
| 963 |
if ( |
| 964 |
$event === 'leave' && |
| 965 |
$n instanceof Text && $n !== $node && |
| 966 |
( $match2 = $this->findTimestamp( $n, $timestampRegexps ) ) |
| 967 |
) { |
| 968 |
|
| 969 |
$treeWalker->currentNode = $n; |
| 970 |
p) |
| 971 |
$foundSignature2 = $this->findSignature( $n, $node ); |
| 972 |
if ( $foundSignature2['username'] !== null ) { |
| 973 |
$sigRanges[] = $this->adjustSigRange( $foundSignature2['nodes'], $match2, $n ); |
| 974 |
$timestampRanges[] = $match2['range']; |
| 975 |
} |
| 976 |
} |
| 977 |
if ( $event === 'leave' ) { |
| 978 |
|
| 979 |
$endNode = $n; |
| 980 |
} |
| 981 |
} |
| 982 |
); |
| 983 |
|
| 984 |
$length = ( $endNode instanceof Text ) ? |
| 985 |
mb_strlen( rtrim( $endNode->nodeValue ?? '', "\t\n\f\r " ) ) : |
| 986 |
|
| 987 |
|
| 988 |
( $endNode->childNodes ? $endNode->childNodes->length : 0 ); |
| 989 |
$range = new ImmutableRange( |
| 990 |
$startNode->parentNode, |
| 991 |
CommentUtils::childIndexOf( $startNode ), |
| 992 |
$endNode, |
| 993 |
$length |
| 994 |
); |
| 995 |
$transcludedFrom = $this->computeTranscludedFrom( $range ); |
| 996 |
|
| 997 |
$startLevel = CommentUtils::getIndentLevel( $startNode, $this->rootNode ) + 1; |
| 998 |
$endLevel = CommentUtils::getIndentLevel( $node, $this->rootNode ) + 1; |
| 999 |
if ( $startLevel !== $endLevel ) { |
| 1000 |
$warnings[] = 'Comment starts and ends with different indentation'; |
| 1001 |
} |
| 1002 |
|
| 1003 |
$level = min( $startLevel, $endLevel ); |
| 1004 |
|
| 1005 |
$parserResult = $dfParsers[ $match['parserIndex'] ]( $match['matchData'] ); |
| 1006 |
if ( !$parserResult ) { |
| 1007 |
continue; |
| 1008 |
} |
| 1009 |
[ 'date' => $dateTime, 'warning' => $dateWarning ] = $parserResult; |
| 1010 |
|
| 1011 |
if ( $dateWarning ) { |
| 1012 |
$warnings[] = $dateWarning; |
| 1013 |
} |
| 1014 |
|
| 1015 |
$curComment = new ContentCommentItem( |
| 1016 |
$level, |
| 1017 |
$range, |
| 1018 |
$transcludedFrom, |
| 1019 |
$sigRanges, |
| 1020 |
$timestampRanges, |
| 1021 |
$dateTime, |
| 1022 |
$author, |
| 1023 |
$foundSignature['displayName'] |
| 1024 |
); |
| 1025 |
$curComment->setRootNode( $this->rootNode ); |
| 1026 |
if ( $warnings ) { |
| 1027 |
$curComment->addWarnings( $warnings ); |
| 1028 |
} |
| 1029 |
if ( $result->isEmpty() ) { |
| 1030 |
|
| 1031 |
|
| 1032 |
$range = new ImmutableRange( $this->rootNode, 0, $this->rootNode, 0 ); |
| 1033 |
$fakeHeading = new ContentHeadingItem( $range, false, null ); |
| 1034 |
$fakeHeading->setRootNode( $this->rootNode ); |
| 1035 |
$result->addThreadItem( $fakeHeading ); |
| 1036 |
} |
| 1037 |
$result->addThreadItem( $curComment ); |
| 1038 |
$curCommentEnd = $curComment->getRange()->endContainer; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
return $result; |
| 1043 |
} |
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
nt |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
public function computeTranscludedFrom( ImmutableRange $commentRange ) { |
| 1063 |
|
| 1064 |
|
| 1065 |
if ( $commentRange->collapsed ) { |
| 1066 |
return false; |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
d. |
| 1076 |
|
| 1077 |
). |
| 1078 |
|
| 1079 |
). |
| 1080 |
|
| 1081 |
e. |
| 1082 |
|
| 1083 |
), |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
$startTransclNode = CommentUtils::getTranscludedFromElement( |
| 1096 |
CommentUtils::getRangeFirstNode( $commentRange ) |
| 1097 |
); |
| 1098 |
$endTransclNode = CommentUtils::getTranscludedFromElement( |
| 1099 |
CommentUtils::getRangeLastNode( $commentRange ) |
| 1100 |
); |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
$transclNodes = []; |
| 1105 |
if ( $startTransclNode ) { |
| 1106 |
$transclNodes[] = $startTransclNode; |
| 1107 |
} |
| 1108 |
if ( $endTransclNode && $endTransclNode !== $startTransclNode ) { |
| 1109 |
$transclNodes[] = $endTransclNode; |
| 1110 |
} |
| 1111 |
|
| 1112 |
foreach ( $transclNodes as $transclNode ) { |
| 1113 |
$transclRange = static::getTransclusionRange( $transclNode ); |
| 1114 |
$compared = CommentUtils::compareRanges( $commentRange, $transclRange ); |
| 1115 |
$transclTitles = $this->getTransclusionTitles( $transclNode ); |
| 1116 |
$simpleTransclTitle = count( $transclTitles ) === 1 && $transclTitles[0] !== null ? |
| 1117 |
$this->parseTitle( $transclTitles[0] ) : null; |
| 1118 |
|
| 1119 |
switch ( $compared ) { |
| 1120 |
case 'equal': |
| 1121 |
|
| 1122 |
if ( $simpleTransclTitle === null ) { |
| 1123 |
|
| 1124 |
|
| 1125 |
if ( count( $transclTitles ) > 1 ) { |
| 1126 |
foreach ( $transclTitles as $transclTitleString ) { |
| 1127 |
if ( $transclTitleString !== null ) { |
| 1128 |
$transclTitle = $this->parseTitle( $transclTitleString ); |
| 1129 |
if ( $transclTitle && !$transclTitle->inNamespace( NS_TEMPLATE ) ) { |
| 1130 |
return true; |
| 1131 |
} |
| 1132 |
} |
| 1133 |
} |
| 1134 |
|
| 1135 |
break; |
| 1136 |
} |
| 1137 |
|
| 1138 |
|
| 1139 |
return true; |
| 1140 |
|
| 1141 |
} elseif ( $simpleTransclTitle->inNamespace( NS_TEMPLATE ) ) { |
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
break; |
| 1147 |
} elseif ( !$this->titleCanExist( $simpleTransclTitle ) ) { |
| 1148 |
|
| 1149 |
|
| 1150 |
return true; |
| 1151 |
} else { |
| 1152 |
Assert::precondition( $transclTitles[0] !== null, "Simple transclusion found" ); |
| 1153 |
return strtr( $transclTitles[0], '_', ' ' ); |
| 1154 |
} |
| 1155 |
|
| 1156 |
case 'contains': |
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
break; |
| 1165 |
|
| 1166 |
case 'contained': |
| 1167 |
|
| 1168 |
if ( $simpleTransclTitle === null ) { |
| 1169 |
return true; |
| 1170 |
} elseif ( !$this->titleCanExist( $simpleTransclTitle ) ) { |
| 1171 |
|
| 1172 |
|
| 1173 |
return true; |
| 1174 |
} else { |
| 1175 |
Assert::precondition( $transclTitles[0] !== null, "Simple transclusion found" ); |
| 1176 |
return strtr( $transclTitles[0], '_', ' ' ); |
| 1177 |
} |
| 1178 |
|
| 1179 |
case 'after': |
| 1180 |
case 'before': |
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
throw new LogicException( 'Unexpected transclusion or comment range' ); |
| 1185 |
|
| 1186 |
case 'overlapstart': |
| 1187 |
case 'overlapend': |
| 1188 |
|
| 1189 |
return true; |
| 1190 |
|
| 1191 |
default: |
| 1192 |
throw new LogicException( 'Unexpected return value from compareRanges()' ); |
| 1193 |
} |
| 1194 |
} |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
return false; |
| 1199 |
} |
| 1200 |
|
| 1201 |
private function titleCanExist( TitleValue $title ): bool { |
| 1202 |
return $title->getNamespace() >= NS_MAIN && |
| 1203 |
!$title->isExternal() && |
| 1204 |
$title->getText() !== ''; |
| 1205 |
} |
| 1206 |
|
| 1207 |
private function parseTitle( string $titleString ): ?TitleValue { |
| 1208 |
try { |
| 1209 |
return $this->titleParser->parseTitle( $titleString ); |
| 1210 |
} catch ( MalformedTitleException $err ) { |
| 1211 |
return null; |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
private function getTransclusionTitles( Element $node ): array { |
| 1226 |
$dataMw = json_decode( $node->getAttribute( 'data-mw' ) ?? '', true ); |
| 1227 |
$out = []; |
| 1228 |
|
| 1229 |
foreach ( $dataMw['parts'] ?? [] as $part ) { |
| 1230 |
if ( |
| 1231 |
!is_string( $part ) && |
| 1232 |
|
| 1233 |
isset( $part['template']['target']['href'] ) |
| 1234 |
) { |
| 1235 |
$parsoidHref = $part['template']['target']['href']; |
| 1236 |
Assert::precondition( substr( $parsoidHref, 0, 2 ) === './', "href has valid format" ); |
| 1237 |
$out[] = rawurldecode( substr( $parsoidHref, 2 ) ); |
| 1238 |
} else { |
| 1239 |
$out[] = null; |
| 1240 |
} |
| 1241 |
} |
| 1242 |
|
| 1243 |
return $out; |
| 1244 |
} |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
private function getTransclusionRange( Element $startNode ): ImmutableRange { |
| 1254 |
$endNode = $startNode; |
| 1255 |
while ( |
| 1256 |
|
| 1257 |
|
| 1258 |
$endNode && |
| 1259 |
( $nextSibling = $endNode->nextSibling ) && |
| 1260 |
$nextSibling instanceof Element && |
| 1261 |
$nextSibling->getAttribute( 'about' ) === $endNode->getAttribute( 'about' ) |
| 1262 |
) { |
| 1263 |
$endNode = $nextSibling; |
| 1264 |
} |
| 1265 |
|
| 1266 |
$range = new ImmutableRange( |
| 1267 |
$startNode->parentNode, |
| 1268 |
CommentUtils::childIndexOf( $startNode ), |
| 1269 |
$endNode->parentNode, |
| 1270 |
CommentUtils::childIndexOf( $endNode ) + 1 |
| 1271 |
); |
| 1272 |
|
| 1273 |
return $range; |
| 1274 |
} |
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
private function truncateForId( string $text, bool $legacy = false ): string { |
| 1286 |
$truncated = $this->language->truncateForDatabase( $text, 80, '' ); |
| 1287 |
if ( !$legacy ) { |
| 1288 |
$truncated = trim( $truncated, '_' ); |
| 1289 |
} |
| 1290 |
return $truncated; |
| 1291 |
} |
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
private function computeId( |
| 1302 |
ContentThreadItem $threadItem, ContentThreadItemSet $previousItems, bool $legacy = false |
| 1303 |
): string { |
| 1304 |
$id = null; |
| 1305 |
|
| 1306 |
if ( $threadItem instanceof ContentHeadingItem && $threadItem->isPlaceholderHeading() ) { |
| 1307 |
|
| 1308 |
$id = 'h-'; |
| 1309 |
} elseif ( $threadItem instanceof ContentHeadingItem ) { |
| 1310 |
$id = 'h-' . $this->truncateForId( $threadItem->getLinkableId(), $legacy ); |
| 1311 |
} elseif ( $threadItem instanceof ContentCommentItem ) { |
| 1312 |
$id = 'c-' . $this->truncateForId( str_replace( ' ', '_', $threadItem->getAuthor() ), $legacy ) . |
| 1313 |
'-' . $threadItem->getTimestampString(); |
| 1314 |
} else { |
| 1315 |
throw new InvalidArgumentException( 'Unknown ThreadItem type' ); |
| 1316 |
} |
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
$threadItemParent = $threadItem->getParent(); |
| 1321 |
if ( $threadItemParent instanceof ContentHeadingItem && !$threadItemParent->isPlaceholderHeading() ) { |
| 1322 |
$id .= '-' . $this->truncateForId( $threadItemParent->getLinkableId(), $legacy ); |
| 1323 |
} elseif ( $threadItemParent instanceof ContentCommentItem ) { |
| 1324 |
$id .= '-' . $this->truncateForId( str_replace( ' ', '_', $threadItemParent->getAuthor() ), $legacy ) . |
| 1325 |
'-' . $threadItemParent->getTimestampString(); |
| 1326 |
} |
| 1327 |
|
| 1328 |
if ( $threadItem instanceof ContentHeadingItem ) { |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
$oldestComment = $threadItem->getOldestReply(); |
| 1334 |
if ( $oldestComment ) { |
| 1335 |
$id .= '-' . $oldestComment->getTimestampString(); |
| 1336 |
} |
| 1337 |
} |
| 1338 |
|
| 1339 |
if ( $previousItems->findCommentById( $id ) ) { |
| 1340 |
|
| 1341 |
if ( !$legacy ) { |
| 1342 |
$threadItem->addWarning( 'Duplicate comment ID' ); |
| 1343 |
} |
| 1344 |
|
| 1345 |
$number = 1; |
| 1346 |
while ( $previousItems->findCommentById( "$id-$number" ) ) { |
| 1347 |
$number++; |
| 1348 |
} |
| 1349 |
$id = "$id-$number"; |
| 1350 |
} |
| 1351 |
|
| 1352 |
return $id; |
| 1353 |
} |
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
private function computeName( ContentThreadItem $threadItem ): string { |
| 1362 |
$name = null; |
| 1363 |
|
| 1364 |
if ( $threadItem instanceof ContentHeadingItem ) { |
| 1365 |
$name = 'h-'; |
| 1366 |
$mainComment = $threadItem->getOldestReply(); |
| 1367 |
} elseif ( $threadItem instanceof ContentCommentItem ) { |
| 1368 |
$name = 'c-'; |
| 1369 |
$mainComment = $threadItem; |
| 1370 |
} else { |
| 1371 |
throw new InvalidArgumentException( 'Unknown ThreadItem type' ); |
| 1372 |
} |
| 1373 |
|
| 1374 |
if ( $mainComment ) { |
| 1375 |
$name .= $this->truncateForId( str_replace( ' ', '_', $mainComment->getAuthor() ) ) . |
| 1376 |
'-' . $mainComment->getTimestampString(); |
| 1377 |
} |
| 1378 |
|
| 1379 |
return $name; |
| 1380 |
} |
| 1381 |
|
| 1382 |
private function buildThreads( ContentThreadItemSet $result ): void { |
| 1383 |
$lastHeading = null; |
| 1384 |
$replies = []; |
| 1385 |
|
| 1386 |
foreach ( $result->getThreadItems() as $threadItem ) { |
| 1387 |
if ( count( $replies ) < $threadItem->getLevel() ) { |
| 1388 |
|
| 1389 |
|
| 1390 |
$threadItem->addWarning( 'Comment skips indentation level' ); |
| 1391 |
while ( count( $replies ) < $threadItem->getLevel() ) { |
| 1392 |
$replies[] = end( $replies ); |
| 1393 |
} |
| 1394 |
} |
| 1395 |
|
| 1396 |
if ( $threadItem instanceof ContentHeadingItem ) { |
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
$maybeParent = $lastHeading; |
| 1401 |
while ( $maybeParent && $maybeParent->getHeadingLevel() >= $threadItem->getHeadingLevel() ) { |
| 1402 |
$maybeParent = $maybeParent->getParent(); |
| 1403 |
} |
| 1404 |
if ( $maybeParent ) { |
| 1405 |
$threadItem->setParent( $maybeParent ); |
| 1406 |
$maybeParent->addReply( $threadItem ); |
| 1407 |
} |
| 1408 |
$lastHeading = $threadItem; |
| 1409 |
} elseif ( isset( $replies[ $threadItem->getLevel() - 1 ] ) ) { |
| 1410 |
|
| 1411 |
$threadItem->setParent( $replies[ $threadItem->getLevel() - 1 ] ); |
| 1412 |
$threadItem->getParent()->addReply( $threadItem ); |
| 1413 |
} else { |
| 1414 |
$threadItem->addWarning( 'Comment could not be connected to a thread' ); |
| 1415 |
} |
| 1416 |
|
| 1417 |
$replies[ $threadItem->getLevel() ] = $threadItem; |
| 1418 |
|
| 1419 |
array_splice( $replies, $threadItem->getLevel() + 1 ); |
| 1420 |
} |
| 1421 |
} |
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
private function computeIdsAndNames( ContentThreadItemSet $result ): void { |
| 1429 |
foreach ( $result->getThreadItems() as $threadItem ) { |
| 1430 |
$name = $this->computeName( $threadItem ); |
| 1431 |
$threadItem->setName( $name ); |
| 1432 |
|
| 1433 |
$id = $this->computeId( $threadItem, $result ); |
| 1434 |
$threadItem->setId( $id ); |
| 1435 |
$legacyId = $this->computeId( $threadItem, $result, true ); |
| 1436 |
if ( $legacyId !== $id ) { |
| 1437 |
$threadItem->setLegacyId( $legacyId ); |
| 1438 |
} |
| 1439 |
|
| 1440 |
$result->updateIdAndNameMaps( $threadItem ); |
| 1441 |
} |
| 1442 |
} |
| 1443 |
} |