| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools; |
| 4 |
|
| 5 |
use LogicException; |
| 6 |
use MediaWiki\Config\Config; |
| 7 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem; |
| 8 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
use Wikimedia\Assert\Assert; |
| 12 |
use Wikimedia\Parsoid\DOM\Comment; |
| 13 |
use Wikimedia\Parsoid\DOM\Element; |
| 14 |
use Wikimedia\Parsoid\DOM\Node; |
| 15 |
use Wikimedia\Parsoid\DOM\Text; |
| 16 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 17 |
|
| 18 |
class CommentUtils { |
| 19 |
|
| 20 |
private function __construct() { |
| 21 |
} |
| 22 |
|
| 23 |
private const BLOCK_ELEMENT_TYPES = [ |
| 24 |
'div', 'p', |
| 25 |
|
| 26 |
'table', 'tbody', 'thead', 'tfoot', 'caption', 'th', 'tr', 'td', |
| 27 |
|
| 28 |
'ul', 'ol', 'li', 'dl', 'dt', 'dd', |
| 29 |
|
| 30 |
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hgroup', |
| 31 |
|
| 32 |
'article', 'aside', 'body', 'nav', 'section', 'footer', 'header', 'figure', |
| 33 |
'figcaption', 'fieldset', 'details', 'blockquote', |
| 34 |
|
| 35 |
'hr', 'button', 'canvas', 'center', 'col', 'colgroup', 'embed', |
| 36 |
'map', 'object', 'pre', 'progress', 'video' |
| 37 |
]; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public static function isBlockElement( Node $node ): bool { |
| 44 |
return $node instanceof Element && |
| 45 |
in_array( strtolower( $node->tagName ), static::BLOCK_ELEMENT_TYPES, true ); |
| 46 |
} |
| 47 |
|
| 48 |
private const SOL_TRANSPARENT_LINK_REGEX = |
| 49 |
'/(?:^|\s)mw:PageProp\/(?:Category|redirect|Language)(?=$|\s)/D'; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static function isRenderingTransparentNode( Node $node ): bool { |
| 56 |
$nextSibling = $node->nextSibling; |
| 57 |
return ( |
| 58 |
$node instanceof Comment || |
| 59 |
( $node instanceof Element && ( |
| 60 |
strtolower( $node->tagName ) === 'meta' || |
| 61 |
( |
| 62 |
strtolower( $node->tagName ) === 'link' && |
| 63 |
preg_match( static::SOL_TRANSPARENT_LINK_REGEX, $node->getAttribute( 'rel' ) ?? '' ) |
| 64 |
) || |
| 65 |
|
| 66 |
|
| 67 |
( |
| 68 |
strtolower( $node->tagName ) === 'span' && |
| 69 |
in_array( 'mw:Transclusion', explode( ' ', $node->getAttribute( 'typeof' ) ?? '' ), true ) && |
| 70 |
!static::htmlTrim( DOMCompat::getInnerHTML( $node ) ) && |
| 71 |
( |
| 72 |
!$nextSibling || !( $nextSibling instanceof Element ) || |
| 73 |
|
| 74 |
|
| 75 |
$nextSibling->getAttribute( 'about' ) !== $node->getAttribute( 'about' ) |
| 76 |
) |
| 77 |
) |
| 78 |
) ) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public static function isOurGeneratedNode( Node $node ): bool { |
| 87 |
return $node instanceof Element && ( |
| 88 |
DOMCompat::getClassList( $node )->contains( 'ext-discussiontools-init-replylink-buttons' ) || |
| 89 |
$node->hasAttribute( 'data-mw-comment-start' ) || |
| 90 |
$node->hasAttribute( 'data-mw-comment-end' ) |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
private const NO_ELEMENT_CHILDREN_ELEMENT_TYPES = [ |
| 98 |
|
| 99 |
|
| 100 |
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', |
| 101 |
'link', 'meta', 'param', 'source', 'track', 'wbr', |
| 102 |
|
| 103 |
'script', 'style', |
| 104 |
|
| 105 |
'textarea', 'title', |
| 106 |
|
| 107 |
'math', 'svg', |
| 108 |
|
| 109 |
|
| 110 |
'noscript', |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
'audio', 'canvas', 'iframe', 'object', 'video', |
| 115 |
]; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public static function cantHaveElementChildren( Node $node ): bool { |
| 122 |
return ( |
| 123 |
$node instanceof Comment || |
| 124 |
( $node instanceof Element && ( |
| 125 |
in_array( strtolower( $node->tagName ), static::NO_ELEMENT_CHILDREN_ELEMENT_TYPES, true ) || |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
( |
| 133 |
in_array( strtolower( $node->tagName ), [ 'a', 'span' ], true ) && |
| 134 |
$node->firstChild && |
| 135 |
|
| 136 |
|
| 137 |
static::cantHaveElementChildren( $node->firstChild ) |
| 138 |
) || |
| 139 |
|
| 140 |
|
| 141 |
strtolower( $node->tagName ) === 'figure' |
| 142 |
) ) |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public static function isCommentSeparator( Node $node ): bool { |
| 150 |
if ( !( $node instanceof Element ) ) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
$tagName = strtolower( $node->tagName ); |
| 155 |
if ( $tagName === 'br' || $tagName === 'hr' ) { |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
if ( $node->nextSibling && |
| 161 |
( $tagName === 'link' || $tagName === 'style' ) && |
| 162 |
self::isCommentSeparator( $node->nextSibling ) |
| 163 |
) { |
| 164 |
return true; |
| 165 |
} |
| 166 |
|
| 167 |
$classList = DOMCompat::getClassList( $node ); |
| 168 |
if ( |
| 169 |
|
| 170 |
$classList->contains( 'mw-notalk' ) || |
| 171 |
|
| 172 |
$classList->contains( 'outdent-template' ) || |
| 173 |
|
| 174 |
$classList->contains( 'mw-trackedTemplate' ) |
| 175 |
) { |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
if ( $tagName === 'dl' && |
| 181 |
count( $node->childNodes ) === 1 && |
| 182 |
$node->firstChild instanceof Element && |
| 183 |
strtolower( $node->firstChild->nodeName ) === 'dt' |
| 184 |
) { |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
… |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public static function isCommentContent( Node $node ): bool { |
| 198 |
return ( |
| 199 |
$node instanceof Text && |
| 200 |
static::htmlTrim( $node->nodeValue ?? '' ) !== '' |
| 201 |
) || |
| 202 |
( |
| 203 |
static::cantHaveElementChildren( $node ) |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public static function childIndexOf( Node $child ): int { |
| 211 |
$i = 0; |
| 212 |
while ( ( $child = $child->previousSibling ) ) { |
| 213 |
$i++; |
| 214 |
} |
| 215 |
return $i; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public static function contains( Node $ancestor, Node $descendant ): bool { |
| 222 |
|
| 223 |
$node = $descendant; |
| 224 |
while ( $node && $node !== $ancestor ) { |
| 225 |
$node = $node->parentNode; |
| 226 |
} |
| 227 |
return $node === $ancestor; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public static function closestElement( Node $node, array $tagNames ): ?Element { |
| 238 |
do { |
| 239 |
if ( |
| 240 |
$node instanceof Element && |
| 241 |
in_array( strtolower( $node->tagName ), $tagNames, true ) |
| 242 |
) { |
| 243 |
return $node; |
| 244 |
} |
| 245 |
$node = $node->parentNode; |
| 246 |
} while ( $node ); |
| 247 |
return null; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public static function closestElementWithSibling( Node $node, string $direction ): ?Element { |
| 258 |
do { |
| 259 |
if ( |
| 260 |
$node instanceof Element && ( |
| 261 |
( $node->nextSibling && ( $direction === 'next' || $direction === 'either' ) ) || |
| 262 |
( $node->previousSibling && ( $direction === 'previous' || $direction === 'either' ) ) |
| 263 |
) |
| 264 |
) { |
| 265 |
return $node; |
| 266 |
} |
| 267 |
$node = $node->parentNode; |
| 268 |
} while ( $node ); |
| 269 |
return null; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public static function getTranscludedFromElement( Node $node ): ?Element { |
| 283 |
while ( $node ) { |
| 284 |
|
| 285 |
if ( |
| 286 |
$node instanceof Element && |
| 287 |
$node->getAttribute( 'about' ) && |
| 288 |
preg_match( '/^#mwt\d+$/', $node->getAttribute( 'about' ) ?? '' ) |
| 289 |
) { |
| 290 |
$about = $node->getAttribute( 'about' ); |
| 291 |
|
| 292 |
|
| 293 |
while ( |
| 294 |
( $previousSibling = $node->previousSibling ) && |
| 295 |
$previousSibling instanceof Element && |
| 296 |
$previousSibling->getAttribute( 'about' ) === $about |
| 297 |
) { |
| 298 |
$node = $previousSibling; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
if ( |
| 303 |
$node->getAttribute( 'typeof' ) && |
| 304 |
in_array( 'mw:Transclusion', explode( ' ', $node->getAttribute( 'typeof' ) ?? '' ), true ) |
| 305 |
) { |
| 306 |
break; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
$node = $node->parentNode; |
| 311 |
} |
| 312 |
return $node; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public static function getHeadlineNode( Element $heading ): Element { |
| 323 |
|
| 324 |
$headline = $heading; |
| 325 |
|
| 326 |
if ( $headline->hasAttribute( 'data-mw-comment-start' ) ) { |
| 327 |
|
| 328 |
$headline = $headline->parentNode; |
| 329 |
Assert::precondition( $headline !== null, 'data-mw-comment-start was attached to a heading' ); |
| 330 |
} |
| 331 |
|
| 332 |
if ( !$headline->getAttribute( 'id' ) && !$headline->getAttribute( 'data-mw-anchor' ) ) { |
| 333 |
|
| 334 |
$headline = DOMCompat::querySelector( $headline, '.mw-headline' ); |
| 335 |
if ( !$headline ) { |
| 336 |
$headline = $heading; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
return $headline; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public static function htmlTrim( string $str ): string { |
| 347 |
|
| 348 |
return trim( $str, "\t\n\f\r " ); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
public static function getIndentLevel( Node $node, Element $rootNode ): int { |
| 357 |
$indent = 0; |
| 358 |
while ( $node ) { |
| 359 |
if ( $node === $rootNode ) { |
| 360 |
break; |
| 361 |
} |
| 362 |
$tagName = $node instanceof Element ? strtolower( $node->tagName ) : null; |
| 363 |
if ( $tagName === 'li' || $tagName === 'dd' ) { |
| 364 |
$indent++; |
| 365 |
} |
| 366 |
$node = $node->parentNode; |
| 367 |
} |
| 368 |
return $indent; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public static function getCoveredSiblings( ImmutableRange $range ): array { |
| 378 |
$ancestor = $range->commonAncestorContainer; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
$siblings = iterator_to_array( $ancestor->childNodes ); |
| 383 |
$start = 0; |
| 384 |
$end = count( $siblings ) - 1; |
| 385 |
|
| 386 |
|
| 387 |
if ( $ancestor === $range->startContainer ) { |
| 388 |
$start = $range->startOffset; |
| 389 |
} else { |
| 390 |
while ( !static::contains( $siblings[ $start ], $range->startContainer ) ) { |
| 391 |
$start++; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
if ( $ancestor === $range->endContainer ) { |
| 397 |
$end = $range->endOffset - 1; |
| 398 |
} else { |
| 399 |
while ( !static::contains( $siblings[ $end ], $range->endContainer ) ) { |
| 400 |
$end--; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
return array_slice( $siblings, $start, $end - $start + 1 ); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public static function getFullyCoveredSiblings( |
| 417 |
ContentThreadItem $item, ?Node $excludedAncestorNode = null |
| 418 |
): ?array { |
| 419 |
$siblings = static::getCoveredSiblings( $item->getRange() ); |
| 420 |
|
| 421 |
$makeRange = static function ( $siblings ) { |
| 422 |
return new ImmutableRange( |
| 423 |
$siblings[0]->parentNode, |
| 424 |
CommentUtils::childIndexOf( $siblings[0] ), |
| 425 |
end( $siblings )->parentNode, |
| 426 |
CommentUtils::childIndexOf( end( $siblings ) ) + 1 |
| 427 |
); |
| 428 |
}; |
| 429 |
|
| 430 |
$matches = static::compareRanges( $makeRange( $siblings ), $item->getRange() ) === 'equal'; |
| 431 |
|
| 432 |
if ( $matches ) { |
| 433 |
|
| 434 |
while ( |
| 435 |
( $parent = $siblings[ 0 ]->parentNode ) && |
| 436 |
$parent !== $excludedAncestorNode && |
| 437 |
static::compareRanges( $makeRange( [ $parent ] ), $item->getRange() ) === 'equal' |
| 438 |
) { |
| 439 |
$siblings = [ $parent ]; |
| 440 |
} |
| 441 |
return $siblings; |
| 442 |
} |
| 443 |
return null; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public static function unwrapParsoidSections( Element $element ): void { |
| 452 |
$sections = DOMCompat::querySelectorAll( $element, 'section[data-mw-section-id]' ); |
| 453 |
foreach ( $sections as $section ) { |
| 454 |
$parent = $section->parentNode; |
| 455 |
while ( $section->firstChild ) { |
| 456 |
$parent->insertBefore( $section->firstChild, $section ); |
| 457 |
} |
| 458 |
$parent->removeChild( $section ); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
public static function getTitleFromUrl( string $url, Config $config ): ?string { |
| 470 |
|
| 471 |
if ( str_starts_with( $url, '//' ) ) { |
| 472 |
$url = "http:$url"; |
| 473 |
} |
| 474 |
|
| 475 |
$bits = parse_url( $url ); |
| 476 |
$query = wfCgiToArray( $bits['query'] ?? '' ); |
| 477 |
if ( isset( $query['title'] ) ) { |
| 478 |
return $query['title']; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
$articlePath = $config->get( MainConfigNames::ArticlePath ); |
| 483 |
if ( str_starts_with( $url, './' ) ) { |
| 484 |
|
| 485 |
$url = substr( $url, 2 ); |
| 486 |
$path = str_replace( '$1', $url, $articlePath ); |
| 487 |
} elseif ( !str_contains( $url, '://' ) ) { |
| 488 |
|
| 489 |
$path = $url; |
| 490 |
} else { |
| 491 |
|
| 492 |
$path = $bits['path'] ?? ''; |
| 493 |
} |
| 494 |
|
| 495 |
$articlePathRegexp = '/^' . str_replace( |
| 496 |
'\\$1', |
| 497 |
'([^?]*)', |
| 498 |
preg_quote( $articlePath, '/' ) |
| 499 |
) . '/'; |
| 500 |
$matches = null; |
| 501 |
if ( preg_match( $articlePathRegexp, $path, $matches ) ) { |
| 502 |
return rawurldecode( $matches[1] ); |
| 503 |
} |
| 504 |
return null; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
public static function linearWalk( Node $node, callable $callback ) { |
| 521 |
$result = null; |
| 522 |
[ $withinNode, $beforeNode ] = [ $node->parentNode, $node ]; |
| 523 |
|
| 524 |
while ( $beforeNode || $withinNode ) { |
| 525 |
if ( $beforeNode ) { |
| 526 |
$result = $callback( 'enter', $beforeNode ); |
| 527 |
[ $withinNode, $beforeNode ] = [ $beforeNode, $beforeNode->firstChild ]; |
| 528 |
} else { |
| 529 |
$result = $callback( 'leave', $withinNode ); |
| 530 |
[ $withinNode, $beforeNode ] = [ $withinNode->parentNode, $withinNode->nextSibling ]; |
| 531 |
} |
| 532 |
|
| 533 |
if ( $result ) { |
| 534 |
return $result; |
| 535 |
} |
| 536 |
} |
| 537 |
return $result; |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
public static function linearWalkBackwards( Node $node, callable $callback ) { |
| 546 |
$result = null; |
| 547 |
[ $withinNode, $beforeNode ] = [ $node->parentNode, $node ]; |
| 548 |
|
| 549 |
while ( $beforeNode || $withinNode ) { |
| 550 |
if ( $beforeNode ) { |
| 551 |
$result = $callback( 'enter', $beforeNode ); |
| 552 |
[ $withinNode, $beforeNode ] = [ $beforeNode, $beforeNode->lastChild ]; |
| 553 |
} else { |
| 554 |
$result = $callback( 'leave', $withinNode ); |
| 555 |
[ $withinNode, $beforeNode ] = [ $withinNode->parentNode, $withinNode->previousSibling ]; |
| 556 |
} |
| 557 |
|
| 558 |
if ( $result ) { |
| 559 |
return $result; |
| 560 |
} |
| 561 |
} |
| 562 |
return $result; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
public static function getRangeFirstNode( ImmutableRange $range ): Node { |
| 570 |
Assert::precondition( !$range->collapsed, 'Range is not collapsed' ); |
| 571 |
|
| 572 |
return $range->startContainer->childNodes && $range->startContainer->childNodes->length ? |
| 573 |
$range->startContainer->childNodes[ $range->startOffset ] : |
| 574 |
$range->startContainer; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
public static function getRangeLastNode( ImmutableRange $range ): Node { |
| 582 |
Assert::precondition( !$range->collapsed, 'Range is not collapsed' ); |
| 583 |
|
| 584 |
return $range->endContainer->childNodes && $range->endContainer->childNodes->length ? |
| 585 |
$range->endContainer->childNodes[ $range->endOffset - 1 ] : |
| 586 |
$range->endContainer; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
public static function compareRanges( ImmutableRange $a, ImmutableRange $b ): string { |
| 616 |
|
| 617 |
|
| 618 |
$startToStart = $a->compareBoundaryPoints( ImmutableRange::START_TO_START, $b ); |
| 619 |
$startToEnd = $a->compareBoundaryPoints( ImmutableRange::END_TO_START, $b ); |
| 620 |
$endToStart = $a->compareBoundaryPoints( ImmutableRange::START_TO_END, $b ); |
| 621 |
$endToEnd = $a->compareBoundaryPoints( ImmutableRange::END_TO_END, $b ); |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
if ( |
| 626 |
( $startToStart < 0 && static::compareRangesAlmostEqualBoundaries( $a, $b, 'start' ) ) || |
| 627 |
( $startToStart > 0 && static::compareRangesAlmostEqualBoundaries( $b, $a, 'start' ) ) |
| 628 |
) { |
| 629 |
$startToStart = 0; |
| 630 |
} |
| 631 |
if ( |
| 632 |
( $endToEnd < 0 && static::compareRangesAlmostEqualBoundaries( $a, $b, 'end' ) ) || |
| 633 |
( $endToEnd > 0 && static::compareRangesAlmostEqualBoundaries( $b, $a, 'end' ) ) |
| 634 |
) { |
| 635 |
$endToEnd = 0; |
| 636 |
} |
| 637 |
|
| 638 |
if ( $startToStart === 0 && $endToEnd === 0 ) { |
| 639 |
return 'equal'; |
| 640 |
} |
| 641 |
if ( $startToStart <= 0 && $endToEnd >= 0 ) { |
| 642 |
return 'contains'; |
| 643 |
} |
| 644 |
if ( $startToStart >= 0 && $endToEnd <= 0 ) { |
| 645 |
return 'contained'; |
| 646 |
} |
| 647 |
if ( $startToEnd >= 0 ) { |
| 648 |
return 'after'; |
| 649 |
} |
| 650 |
if ( $endToStart <= 0 ) { |
| 651 |
return 'before'; |
| 652 |
} |
| 653 |
if ( $startToStart > 0 && $startToEnd < 0 && $endToEnd >= 0 ) { |
| 654 |
return 'overlapstart'; |
| 655 |
} |
| 656 |
if ( $endToEnd < 0 && $endToStart > 0 && $startToStart <= 0 ) { |
| 657 |
return 'overlapend'; |
| 658 |
} |
| 659 |
|
| 660 |
throw new LogicException( 'Unreachable' ); |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
private static function compareRangesAlmostEqualBoundaries( |
| 675 |
ImmutableRange $a, ImmutableRange $b, string $boundary |
| 676 |
): bool { |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
$from = $boundary === 'end' ? static::getRangeLastNode( $a ) : static::getRangeFirstNode( $a ); |
| 681 |
$to = $boundary === 'end' ? static::getRangeLastNode( $b ) : static::getRangeFirstNode( $b ); |
| 682 |
|
| 683 |
$skipNode = null; |
| 684 |
if ( $boundary === 'end' ) { |
| 685 |
$skipNode = $from; |
| 686 |
} |
| 687 |
|
| 688 |
$foundContent = false; |
| 689 |
static::linearWalk( |
| 690 |
$from, |
| 691 |
static function ( string $event, Node $n ) use ( |
| 692 |
$from, $to, $boundary, &$skipNode, &$foundContent |
| 693 |
) { |
| 694 |
if ( $n === $to && $event === ( $boundary === 'end' ? 'leave' : 'enter' ) ) { |
| 695 |
return true; |
| 696 |
} |
| 697 |
if ( $skipNode ) { |
| 698 |
if ( $n === $skipNode && $event === 'leave' ) { |
| 699 |
$skipNode = null; |
| 700 |
} |
| 701 |
return; |
| 702 |
} |
| 703 |
|
| 704 |
if ( $event === 'enter' ) { |
| 705 |
if ( |
| 706 |
CommentUtils::isCommentSeparator( $n ) || |
| 707 |
CommentUtils::isRenderingTransparentNode( $n ) || |
| 708 |
CommentUtils::isOurGeneratedNode( $n ) |
| 709 |
) { |
| 710 |
$skipNode = $n; |
| 711 |
|
| 712 |
} elseif ( |
| 713 |
CommentUtils::isCommentContent( $n ) |
| 714 |
) { |
| 715 |
$foundContent = true; |
| 716 |
return true; |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
); |
| 721 |
|
| 722 |
return !$foundContent; |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
public static function isSingleCommentSignedBy( |
| 732 |
ContentThreadItemSet $itemSet, |
| 733 |
string $author, |
| 734 |
Element $rootNode |
| 735 |
): bool { |
| 736 |
$items = $itemSet->getThreadItems(); |
| 737 |
|
| 738 |
if ( $items ) { |
| 739 |
$lastItem = end( $items ); |
| 740 |
|
| 741 |
if ( !( $lastItem instanceof ContentCommentItem && $lastItem->getAuthor() === $author ) ) { |
| 742 |
return false; |
| 743 |
} |
| 744 |
|
| 745 |
$commentRange = $lastItem->getRange(); |
| 746 |
$bodyRange = new ImmutableRange( |
| 747 |
$rootNode, 0, $rootNode, count( $rootNode->childNodes ) |
| 748 |
); |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
$cmp = $commentRange->compareBoundaryPoints( ImmutableRange::END_TO_END, $bodyRange ); |
| 754 |
if ( |
| 755 |
$cmp === 0 || |
| 756 |
( $cmp < 0 && static::compareRangesAlmostEqualBoundaries( $commentRange, $bodyRange, 'end' ) ) || |
| 757 |
( $cmp > 0 && static::compareRangesAlmostEqualBoundaries( $bodyRange, $commentRange, 'end' ) ) |
| 758 |
) { |
| 759 |
|
| 760 |
return true; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
return false; |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
public static function getNewTopicsSubscriptionId( Title $title ) { |
| 774 |
return "p-topics-{$title->getNamespace()}:{$title->getDBkey()}"; |
| 775 |
} |
| 776 |
} |