2020-05-11 20:07:14 +00:00
|
|
|
<?php
|
|
|
|
|
2020-05-14 22:44:49 +00:00
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
2021-11-18 22:30:26 +00:00
|
|
|
use LogicException;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2022-03-18 03:28:06 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem;
|
2023-05-19 07:36:50 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2023-08-19 18:16:15 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2021-08-02 13:07:41 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
2021-07-29 02:16:15 +00:00
|
|
|
use Wikimedia\Parsoid\DOM\Comment;
|
|
|
|
use Wikimedia\Parsoid\DOM\Element;
|
|
|
|
use Wikimedia\Parsoid\DOM\Node;
|
2022-02-21 18:42:36 +00:00
|
|
|
use Wikimedia\Parsoid\DOM\Text;
|
2020-12-09 11:58:58 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
2020-05-14 22:44:49 +00:00
|
|
|
|
|
|
|
class CommentUtils {
|
2022-10-21 19:34:18 +00:00
|
|
|
|
2020-05-11 20:07:14 +00:00
|
|
|
private function __construct() {
|
|
|
|
}
|
|
|
|
|
2023-10-30 09:43:11 +00:00
|
|
|
private const BLOCK_ELEMENT_TYPES = [
|
2020-08-11 04:22:55 +00:00
|
|
|
'div', 'p',
|
|
|
|
// Tables
|
|
|
|
'table', 'tbody', 'thead', 'tfoot', 'caption', 'th', 'tr', 'td',
|
|
|
|
// Lists
|
|
|
|
'ul', 'ol', 'li', 'dl', 'dt', 'dd',
|
|
|
|
// HTML5 heading content
|
|
|
|
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hgroup',
|
|
|
|
// HTML5 sectioning content
|
|
|
|
'article', 'aside', 'body', 'nav', 'section', 'footer', 'header', 'figure',
|
|
|
|
'figcaption', 'fieldset', 'details', 'blockquote',
|
|
|
|
// Other
|
|
|
|
'hr', 'button', 'canvas', 'center', 'col', 'colgroup', 'embed',
|
|
|
|
'map', 'object', 'pre', 'progress', 'video'
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Node $node
|
2020-08-11 04:22:55 +00:00
|
|
|
* @return bool Node is a block element
|
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
public static function isBlockElement( Node $node ): bool {
|
|
|
|
return $node instanceof Element &&
|
2023-10-30 09:43:11 +00:00
|
|
|
in_array( strtolower( $node->tagName ), static::BLOCK_ELEMENT_TYPES, true );
|
2020-08-11 04:22:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 03:36:27 +00:00
|
|
|
private const SOL_TRANSPARENT_LINK_REGEX =
|
|
|
|
'/(?:^|\s)mw:PageProp\/(?:Category|redirect|Language)(?=$|\s)/D';
|
|
|
|
|
2020-12-05 21:49:23 +00:00
|
|
|
/**
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Node $node
|
2020-12-05 21:49:23 +00:00
|
|
|
* @return bool Node is considered a rendering-transparent node in Parsoid
|
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
public static function isRenderingTransparentNode( Node $node ): bool {
|
2021-11-12 20:32:51 +00:00
|
|
|
$nextSibling = $node->nextSibling;
|
2020-12-05 21:49:23 +00:00
|
|
|
return (
|
2021-07-29 02:16:15 +00:00
|
|
|
$node instanceof Comment ||
|
2024-03-22 23:29:25 +00:00
|
|
|
( $node instanceof Element && (
|
2020-12-05 21:49:23 +00:00
|
|
|
strtolower( $node->tagName ) === 'meta' ||
|
2021-01-26 03:36:27 +00:00
|
|
|
(
|
|
|
|
strtolower( $node->tagName ) === 'link' &&
|
2022-06-09 13:51:33 +00:00
|
|
|
preg_match( static::SOL_TRANSPARENT_LINK_REGEX, $node->getAttribute( 'rel' ) ?? '' )
|
2021-01-26 03:36:27 +00:00
|
|
|
) ||
|
2021-11-12 20:32:51 +00:00
|
|
|
// Empty inline templates, e.g. tracking templates. (T269036)
|
|
|
|
// But not empty nodes that are just the start of a non-empty template about-group. (T290940)
|
2020-12-09 11:58:58 +00:00
|
|
|
(
|
|
|
|
strtolower( $node->tagName ) === 'span' &&
|
2023-06-06 12:08:00 +00:00
|
|
|
in_array( 'mw:Transclusion', explode( ' ', $node->getAttribute( 'typeof' ) ?? '' ), true ) &&
|
2022-06-09 13:51:33 +00:00
|
|
|
!static::htmlTrim( DOMCompat::getInnerHTML( $node ) ) &&
|
2021-11-12 20:32:51 +00:00
|
|
|
(
|
|
|
|
!$nextSibling || !( $nextSibling instanceof Element ) ||
|
|
|
|
// Maybe we should be checking all of the about-grouped nodes to see if they're empty,
|
|
|
|
// but that's prooobably not needed in practice, and it leads to a quadratic worst case.
|
|
|
|
$nextSibling->getAttribute( 'about' ) !== $node->getAttribute( 'about' )
|
|
|
|
)
|
2020-12-09 11:58:58 +00:00
|
|
|
)
|
2024-03-22 23:29:25 +00:00
|
|
|
) )
|
2020-12-05 21:49:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-16 11:01:41 +00:00
|
|
|
/**
|
|
|
|
* @param Node $node
|
|
|
|
* @return bool Node was added to the page by DiscussionTools
|
|
|
|
*/
|
|
|
|
public static function isOurGeneratedNode( Node $node ): bool {
|
|
|
|
return $node instanceof Element && (
|
2022-01-24 16:16:20 +00:00
|
|
|
DOMCompat::getClassList( $node )->contains( 'ext-discussiontools-init-replylink-buttons' ) ||
|
2021-12-16 11:01:41 +00:00
|
|
|
$node->hasAttribute( 'data-mw-comment-start' ) ||
|
|
|
|
$node->hasAttribute( 'data-mw-comment-end' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-14 16:57:51 +00:00
|
|
|
/**
|
|
|
|
* Elements which can't have element children (but some may have text content).
|
|
|
|
*/
|
2023-10-30 09:43:11 +00:00
|
|
|
private const NO_ELEMENT_CHILDREN_ELEMENT_TYPES = [
|
2022-02-14 19:21:35 +00:00
|
|
|
// https://html.spec.whatwg.org/multipage/syntax.html#elements-2
|
2020-12-14 16:57:51 +00:00
|
|
|
// Void elements
|
|
|
|
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
|
|
|
|
'link', 'meta', 'param', 'source', 'track', 'wbr',
|
|
|
|
// Raw text elements
|
|
|
|
'script', 'style',
|
|
|
|
// Escapable raw text elements
|
|
|
|
'textarea', 'title',
|
2022-02-14 19:21:35 +00:00
|
|
|
// Foreign elements
|
|
|
|
'math', 'svg',
|
2021-03-04 19:26:20 +00:00
|
|
|
// Treated like text when scripting is enabled in the parser
|
|
|
|
// https://html.spec.whatwg.org/#the-noscript-element
|
|
|
|
'noscript',
|
2022-02-14 19:21:35 +00:00
|
|
|
// Replaced elements (that aren't already included above)
|
|
|
|
// https://html.spec.whatwg.org/multipage/rendering.html#replaced-elements
|
|
|
|
// They might allow element children, but they aren't rendered on the page.
|
|
|
|
'audio', 'canvas', 'iframe', 'object', 'video',
|
2020-12-14 16:57:51 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Node $node
|
2020-12-14 16:57:51 +00:00
|
|
|
* @return bool If true, node can't have element children. If false, it's complicated.
|
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
public static function cantHaveElementChildren( Node $node ): bool {
|
2020-12-14 16:57:51 +00:00
|
|
|
return (
|
2021-07-29 02:16:15 +00:00
|
|
|
$node instanceof Comment ||
|
2024-03-22 23:29:25 +00:00
|
|
|
( $node instanceof Element && (
|
2023-10-30 09:43:11 +00:00
|
|
|
in_array( strtolower( $node->tagName ), static::NO_ELEMENT_CHILDREN_ELEMENT_TYPES, true ) ||
|
2022-02-14 19:21:35 +00:00
|
|
|
// Thumbnail wrappers generated by MediaTransformOutput::linkWrap (T301427),
|
|
|
|
// for compatibility with TimedMediaHandler.
|
|
|
|
// There is no better way to detect them, and we can't insert markers here,
|
|
|
|
// because the media DOM CSS depends on specific tag names and their order :(
|
2022-10-08 20:23:31 +00:00
|
|
|
// TODO See if we can remove this condition when wgParserEnableLegacyMediaDOM=false
|
|
|
|
// is enabled everywhere.
|
2022-02-14 19:21:35 +00:00
|
|
|
(
|
2023-06-06 12:08:00 +00:00
|
|
|
in_array( strtolower( $node->tagName ), [ 'a', 'span' ], true ) &&
|
2022-02-14 19:21:35 +00:00
|
|
|
$node->firstChild &&
|
|
|
|
// We always step inside a child node so this can't be infinite, silly Phan
|
|
|
|
// @phan-suppress-next-line PhanInfiniteRecursion
|
2022-06-09 13:51:33 +00:00
|
|
|
static::cantHaveElementChildren( $node->firstChild )
|
2022-10-08 20:23:31 +00:00
|
|
|
) ||
|
|
|
|
// Do not insert anything inside figures when using wgParserEnableLegacyMediaDOM=false,
|
|
|
|
// because their CSS can't handle it (T320285).
|
|
|
|
strtolower( $node->tagName ) === 'figure'
|
2024-03-22 23:29:25 +00:00
|
|
|
) )
|
2020-12-14 16:57:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-04 02:06:57 +00:00
|
|
|
/**
|
|
|
|
* Check whether the node is a comment separator (instead of a part of the comment).
|
|
|
|
*/
|
|
|
|
public static function isCommentSeparator( Node $node ): bool {
|
2023-10-30 09:43:11 +00:00
|
|
|
if ( !( $node instanceof Element ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tagName = strtolower( $node->tagName );
|
|
|
|
if ( $tagName === 'br' || $tagName === 'hr' ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TemplateStyles followed by any of the others
|
|
|
|
if ( $node->nextSibling &&
|
|
|
|
( $tagName === 'link' || $tagName === 'style' ) &&
|
|
|
|
self::isCommentSeparator( $node->nextSibling )
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$classList = DOMCompat::getClassList( $node );
|
|
|
|
if (
|
2023-10-06 23:55:47 +00:00
|
|
|
// Anything marked as not containing comments
|
2023-10-30 09:43:11 +00:00
|
|
|
$classList->contains( 'mw-notalk' ) ||
|
2021-12-04 02:06:57 +00:00
|
|
|
// {{outdent}} templates
|
2023-10-30 09:43:11 +00:00
|
|
|
$classList->contains( 'outdent-template' ) ||
|
2022-07-31 01:36:54 +00:00
|
|
|
// {{tracked}} templates (T313097)
|
2023-10-30 09:43:11 +00:00
|
|
|
$classList->contains( 'mw-trackedTemplate' )
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wikitext definition list term markup (`;`) when used as a fake heading (T265964)
|
|
|
|
if ( $tagName === 'dl' &&
|
|
|
|
count( $node->childNodes ) === 1 &&
|
|
|
|
$node->firstChild instanceof Element &&
|
|
|
|
strtolower( $node->firstChild->nodeName ) === 'dt'
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2021-12-04 02:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the node is a comment content. It's a little vague what this means…
|
|
|
|
*
|
|
|
|
* @param Node $node Node, should be a leaf node (a node with no children)
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-06-28 17:34:17 +00:00
|
|
|
public static function isCommentContent( Node $node ): bool {
|
2021-12-04 02:06:57 +00:00
|
|
|
return (
|
2022-02-21 18:42:36 +00:00
|
|
|
$node instanceof Text &&
|
2022-06-09 13:51:33 +00:00
|
|
|
static::htmlTrim( $node->nodeValue ?? '' ) !== ''
|
2021-12-04 02:06:57 +00:00
|
|
|
) ||
|
|
|
|
(
|
2022-06-09 13:51:33 +00:00
|
|
|
static::cantHaveElementChildren( $node )
|
2021-12-04 02:06:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:07:14 +00:00
|
|
|
/**
|
|
|
|
* Get the index of $child in its parent
|
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
public static function childIndexOf( Node $child ): int {
|
2020-05-11 20:07:14 +00:00
|
|
|
$i = 0;
|
|
|
|
while ( ( $child = $child->previousSibling ) ) {
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-29 02:16:15 +00:00
|
|
|
* Check whether a Node contains (is an ancestor of) another Node (or is the same node)
|
2020-05-11 20:07:14 +00:00
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
public static function contains( Node $ancestor, Node $descendant ): bool {
|
|
|
|
// TODO can we use Node->compareDocumentPosition() here maybe?
|
2020-05-11 20:07:14 +00:00
|
|
|
$node = $descendant;
|
|
|
|
while ( $node && $node !== $ancestor ) {
|
|
|
|
$node = $node->parentNode;
|
|
|
|
}
|
|
|
|
return $node === $ancestor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find closest ancestor element using one of the given tag names.
|
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Node $node
|
2020-05-11 20:07:14 +00:00
|
|
|
* @param string[] $tagNames
|
2021-07-29 02:16:15 +00:00
|
|
|
* @return Element|null
|
2020-05-11 20:07:14 +00:00
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
public static function closestElement( Node $node, array $tagNames ): ?Element {
|
2020-05-11 20:07:14 +00:00
|
|
|
do {
|
|
|
|
if (
|
2022-02-21 18:42:36 +00:00
|
|
|
$node instanceof Element &&
|
2023-06-06 12:08:00 +00:00
|
|
|
in_array( strtolower( $node->tagName ), $tagNames, true )
|
2020-05-11 20:07:14 +00:00
|
|
|
) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
$node = $node->parentNode;
|
|
|
|
} while ( $node );
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-07-27 16:05:06 +00:00
|
|
|
/**
|
|
|
|
* Find closest ancestor element that has sibling nodes
|
|
|
|
*
|
|
|
|
* @param Node $node
|
|
|
|
* @param string $direction Can be 'next', 'previous', or 'either'
|
|
|
|
* @return Element|null
|
|
|
|
*/
|
|
|
|
public static function closestElementWithSibling( Node $node, string $direction ): ?Element {
|
|
|
|
do {
|
|
|
|
if (
|
|
|
|
$node instanceof Element && (
|
2023-07-26 13:29:40 +00:00
|
|
|
( $node->nextSibling && ( $direction === 'next' || $direction === 'either' ) ) ||
|
|
|
|
( $node->previousSibling && ( $direction === 'previous' || $direction === 'either' ) )
|
2022-07-27 16:05:06 +00:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
$node = $node->parentNode;
|
|
|
|
} while ( $node );
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-31 15:17:59 +00:00
|
|
|
/**
|
|
|
|
* Find the transclusion node which rendered the current node, if it exists.
|
|
|
|
*
|
|
|
|
* 1. Find the closest ancestor with an 'about' attribute
|
|
|
|
* 2. Find the main node of the about-group (first sibling with the same 'about' attribute)
|
|
|
|
* 3. If this is an mw:Transclusion node, return it; otherwise, go to step 1
|
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Node $node
|
2022-02-02 18:24:37 +00:00
|
|
|
* @return Element|null Transclusion node, null if not found
|
2020-05-31 15:17:59 +00:00
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
public static function getTranscludedFromElement( Node $node ): ?Element {
|
2020-05-31 15:17:59 +00:00
|
|
|
while ( $node ) {
|
|
|
|
// 1.
|
|
|
|
if (
|
2021-07-29 02:16:15 +00:00
|
|
|
$node instanceof Element &&
|
2020-05-31 15:17:59 +00:00
|
|
|
$node->getAttribute( 'about' ) &&
|
2021-07-29 02:16:15 +00:00
|
|
|
preg_match( '/^#mwt\d+$/', $node->getAttribute( 'about' ) ?? '' )
|
2020-05-31 15:17:59 +00:00
|
|
|
) {
|
|
|
|
$about = $node->getAttribute( 'about' );
|
|
|
|
|
|
|
|
// 2.
|
|
|
|
while (
|
2020-06-04 18:48:58 +00:00
|
|
|
( $previousSibling = $node->previousSibling ) &&
|
2021-07-29 02:16:15 +00:00
|
|
|
$previousSibling instanceof Element &&
|
2020-06-04 18:48:58 +00:00
|
|
|
$previousSibling->getAttribute( 'about' ) === $about
|
2020-05-31 15:17:59 +00:00
|
|
|
) {
|
2020-06-04 18:48:58 +00:00
|
|
|
$node = $previousSibling;
|
2020-05-31 15:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3.
|
|
|
|
if (
|
|
|
|
$node->getAttribute( 'typeof' ) &&
|
2023-06-06 12:08:00 +00:00
|
|
|
in_array( 'mw:Transclusion', explode( ' ', $node->getAttribute( 'typeof' ) ?? '' ), true )
|
2020-05-31 15:17:59 +00:00
|
|
|
) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$node = $node->parentNode;
|
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:36:53 +00:00
|
|
|
/**
|
|
|
|
* Given a heading node, return the node on which the ID attribute is set.
|
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Element $heading Heading node (`<h1>`-`<h6>`)
|
2024-04-20 00:08:35 +00:00
|
|
|
* @return Element Headline node, normally also a `<h1>`-`<h6>` element.
|
|
|
|
* In integration tests and in JS, it can be a `<span class="mw-headline">` (see T363031).
|
2020-11-04 21:36:53 +00:00
|
|
|
*/
|
2024-04-19 22:34:32 +00:00
|
|
|
public static function getHeadlineNode( Element $heading ): Element {
|
2020-11-04 21:36:53 +00:00
|
|
|
// This code assumes that $wgFragmentMode is [ 'html5', 'legacy' ] or [ 'html5' ]
|
|
|
|
$headline = $heading;
|
|
|
|
|
2021-03-10 17:25:52 +00:00
|
|
|
if ( $headline->hasAttribute( 'data-mw-comment-start' ) ) {
|
2024-04-20 00:08:35 +00:00
|
|
|
// HACK: For contaminated integration tests only (see T363031)
|
2020-11-04 21:36:53 +00:00
|
|
|
$headline = $headline->parentNode;
|
2022-10-13 21:52:48 +00:00
|
|
|
Assert::precondition( $headline !== null, 'data-mw-comment-start was attached to a heading' );
|
2020-11-04 21:36:53 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 16:53:51 +00:00
|
|
|
if ( !$headline->getAttribute( 'id' ) && !$headline->getAttribute( 'data-mw-anchor' ) ) {
|
2024-04-20 00:08:35 +00:00
|
|
|
// HACK: For outdated integration tests only (see T363031)
|
2022-10-13 21:52:48 +00:00
|
|
|
$headline = DOMCompat::querySelector( $headline, '.mw-headline' );
|
|
|
|
if ( !$headline ) {
|
2020-11-04 21:36:53 +00:00
|
|
|
$headline = $heading;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-19 22:34:32 +00:00
|
|
|
return $headline;
|
2020-11-04 21:36:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 20:07:14 +00:00
|
|
|
/**
|
|
|
|
* Trim ASCII whitespace, as defined in the HTML spec.
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public static function htmlTrim( string $str ): string {
|
2020-05-11 20:07:14 +00:00
|
|
|
// https://infra.spec.whatwg.org/#ascii-whitespace
|
|
|
|
return trim( $str, "\t\n\f\r " );
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:57:51 +00:00
|
|
|
/**
|
|
|
|
* Get the indent level of $node, relative to $rootNode.
|
|
|
|
*
|
|
|
|
* The indent level is the number of lists inside of which it is nested.
|
|
|
|
*/
|
2022-02-21 18:56:15 +00:00
|
|
|
public static function getIndentLevel( Node $node, Element $rootNode ): int {
|
2020-07-29 23:57:51 +00:00
|
|
|
$indent = 0;
|
|
|
|
while ( $node ) {
|
|
|
|
if ( $node === $rootNode ) {
|
|
|
|
break;
|
|
|
|
}
|
2022-02-21 18:56:15 +00:00
|
|
|
$tagName = $node instanceof Element ? strtolower( $node->tagName ) : null;
|
|
|
|
if ( $tagName === 'li' || $tagName === 'dd' ) {
|
2020-07-29 23:57:51 +00:00
|
|
|
$indent++;
|
|
|
|
}
|
|
|
|
$node = $node->parentNode;
|
|
|
|
}
|
|
|
|
return $indent;
|
|
|
|
}
|
|
|
|
|
2020-05-27 17:31:31 +00:00
|
|
|
/**
|
2021-01-26 18:58:58 +00:00
|
|
|
* Get an array of sibling nodes that contain parts of the given range.
|
2020-05-27 17:31:31 +00:00
|
|
|
*
|
2021-01-26 18:58:58 +00:00
|
|
|
* @param ImmutableRange $range
|
2022-02-19 03:25:14 +00:00
|
|
|
* @return Node[]
|
2020-06-16 14:08:01 +00:00
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public static function getCoveredSiblings( ImmutableRange $range ): array {
|
2020-06-16 14:08:01 +00:00
|
|
|
$ancestor = $range->commonAncestorContainer;
|
|
|
|
|
2021-07-29 02:16:15 +00:00
|
|
|
// Convert to array early because apparently NodeList acts like a linked list
|
2020-06-16 14:08:01 +00:00
|
|
|
// and accessing items by index is slow
|
|
|
|
$siblings = iterator_to_array( $ancestor->childNodes );
|
|
|
|
$start = 0;
|
|
|
|
$end = count( $siblings ) - 1;
|
|
|
|
|
|
|
|
// Find first of the siblings that contains the item
|
2021-02-02 23:08:54 +00:00
|
|
|
if ( $ancestor === $range->startContainer ) {
|
|
|
|
$start = $range->startOffset;
|
|
|
|
} else {
|
2022-06-09 13:51:33 +00:00
|
|
|
while ( !static::contains( $siblings[ $start ], $range->startContainer ) ) {
|
2021-02-02 23:08:54 +00:00
|
|
|
$start++;
|
|
|
|
}
|
2020-06-16 14:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find last of the siblings that contains the item
|
2021-02-02 23:08:54 +00:00
|
|
|
if ( $ancestor === $range->endContainer ) {
|
|
|
|
$end = $range->endOffset - 1;
|
|
|
|
} else {
|
2022-06-09 13:51:33 +00:00
|
|
|
while ( !static::contains( $siblings[ $end ], $range->endContainer ) ) {
|
2021-02-02 23:08:54 +00:00
|
|
|
$end--;
|
|
|
|
}
|
2020-06-16 14:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return array_slice( $siblings, $start, $end - $start + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the nodes (if any) that contain the given thread item, and nothing else.
|
|
|
|
*
|
2022-03-18 03:28:06 +00:00
|
|
|
* @param ContentThreadItem $item
|
2022-03-09 19:05:03 +00:00
|
|
|
* @param ?Node $excludedAncestorNode Node that shouldn't be included in the result, even if it
|
|
|
|
* contains the item and nothing else. This is intended to avoid traversing outside of a node
|
|
|
|
* which is a container for all the thread items.
|
2022-02-19 03:25:14 +00:00
|
|
|
* @return Node[]|null
|
2020-05-27 17:31:31 +00:00
|
|
|
*/
|
2022-03-18 03:28:06 +00:00
|
|
|
public static function getFullyCoveredSiblings(
|
|
|
|
ContentThreadItem $item, ?Node $excludedAncestorNode = null
|
|
|
|
): ?array {
|
2022-06-09 13:51:33 +00:00
|
|
|
$siblings = static::getCoveredSiblings( $item->getRange() );
|
2020-05-27 17:31:31 +00:00
|
|
|
|
2021-12-16 11:01:41 +00:00
|
|
|
$makeRange = static function ( $siblings ) {
|
|
|
|
return new ImmutableRange(
|
|
|
|
$siblings[0]->parentNode,
|
|
|
|
CommentUtils::childIndexOf( $siblings[0] ),
|
|
|
|
end( $siblings )->parentNode,
|
|
|
|
CommentUtils::childIndexOf( end( $siblings ) ) + 1
|
|
|
|
);
|
2020-05-27 17:31:31 +00:00
|
|
|
};
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
$matches = static::compareRanges( $makeRange( $siblings ), $item->getRange() ) === 'equal';
|
2020-05-27 17:31:31 +00:00
|
|
|
|
2021-12-16 11:01:41 +00:00
|
|
|
if ( $matches ) {
|
2020-06-16 14:08:01 +00:00
|
|
|
// If these are all of the children (or the only child), go up one more level
|
2020-05-27 17:31:31 +00:00
|
|
|
while (
|
2020-06-16 14:08:01 +00:00
|
|
|
( $parent = $siblings[ 0 ]->parentNode ) &&
|
2022-03-09 19:05:03 +00:00
|
|
|
$parent !== $excludedAncestorNode &&
|
2022-06-09 13:51:33 +00:00
|
|
|
static::compareRanges( $makeRange( [ $parent ] ), $item->getRange() ) === 'equal'
|
2020-05-27 17:31:31 +00:00
|
|
|
) {
|
2020-06-16 14:08:01 +00:00
|
|
|
$siblings = [ $parent ];
|
2020-05-27 17:31:31 +00:00
|
|
|
}
|
2020-06-16 14:08:01 +00:00
|
|
|
return $siblings;
|
2020-05-27 17:31:31 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-15 00:51:36 +00:00
|
|
|
/**
|
|
|
|
* Unwrap Parsoid sections
|
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Element $element Parent element, e.g. document body
|
2020-05-15 00:51:36 +00:00
|
|
|
*/
|
2023-01-31 00:32:02 +00:00
|
|
|
public static function unwrapParsoidSections( Element $element ): void {
|
2021-08-02 13:07:41 +00:00
|
|
|
$sections = DOMCompat::querySelectorAll( $element, 'section[data-mw-section-id]' );
|
2020-05-15 00:51:36 +00:00
|
|
|
foreach ( $sections as $section ) {
|
|
|
|
$parent = $section->parentNode;
|
|
|
|
while ( $section->firstChild ) {
|
|
|
|
$parent->insertBefore( $section->firstChild, $section );
|
|
|
|
}
|
|
|
|
$parent->removeChild( $section );
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 14:48:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a MediaWiki page title from a URL
|
|
|
|
*
|
2024-02-23 23:49:36 +00:00
|
|
|
* @param string $url Relative URL (from a `href` attribute)
|
|
|
|
* @param Config $config Config settings needed to resolve the relative URL
|
2022-02-21 22:07:38 +00:00
|
|
|
* @return string|null
|
2020-07-20 14:48:41 +00:00
|
|
|
*/
|
2022-02-21 22:07:38 +00:00
|
|
|
public static function getTitleFromUrl( string $url, Config $config ): ?string {
|
2024-02-23 23:49:36 +00:00
|
|
|
// Protocol-relative URLs are handled really badly by parse_url()
|
|
|
|
if ( str_starts_with( $url, '//' ) ) {
|
|
|
|
$url = "http:$url";
|
|
|
|
}
|
|
|
|
|
2020-09-01 23:00:08 +00:00
|
|
|
$bits = parse_url( $url );
|
|
|
|
$query = wfCgiToArray( $bits['query'] ?? '' );
|
|
|
|
if ( isset( $query['title'] ) ) {
|
2022-02-21 22:07:38 +00:00
|
|
|
return $query['title'];
|
2020-09-01 23:00:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 14:48:41 +00:00
|
|
|
// TODO: Set the correct base in the document?
|
2023-10-30 09:43:11 +00:00
|
|
|
$articlePath = $config->get( MainConfigNames::ArticlePath );
|
2023-05-19 07:36:50 +00:00
|
|
|
if ( str_starts_with( $url, './' ) ) {
|
2024-02-23 23:49:36 +00:00
|
|
|
// Assume this is URL in the format used by Parsoid documents
|
2023-05-19 07:36:50 +00:00
|
|
|
$url = substr( $url, 2 );
|
2024-02-23 23:49:36 +00:00
|
|
|
$path = str_replace( '$1', $url, $articlePath );
|
2023-05-19 07:36:50 +00:00
|
|
|
} elseif ( !str_contains( $url, '://' ) ) {
|
2024-02-23 23:49:36 +00:00
|
|
|
// Assume this is URL in the format used by legacy parser documents
|
|
|
|
$path = $url;
|
|
|
|
} else {
|
|
|
|
// External link
|
|
|
|
$path = $bits['path'] ?? '';
|
2020-07-20 14:48:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 23:49:36 +00:00
|
|
|
$articlePathRegexp = '/^' . str_replace(
|
2023-10-30 09:43:11 +00:00
|
|
|
'\\$1',
|
2022-11-29 17:15:16 +00:00
|
|
|
'([^?]*)',
|
2023-10-30 09:43:11 +00:00
|
|
|
preg_quote( $articlePath, '/' )
|
2020-07-20 14:48:41 +00:00
|
|
|
) . '/';
|
|
|
|
$matches = null;
|
2024-02-23 23:49:36 +00:00
|
|
|
if ( preg_match( $articlePathRegexp, $path, $matches ) ) {
|
2024-06-19 15:03:48 +00:00
|
|
|
return rawurldecode( $matches[1] );
|
2020-07-20 14:48:41 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:20:33 +00:00
|
|
|
/**
|
|
|
|
* Traverse the document in depth-first order, calling the callback whenever entering and leaving
|
|
|
|
* a node. The walk starts before the given node and ends when callback returns a truthy value, or
|
|
|
|
* after reaching the end of the document.
|
|
|
|
*
|
|
|
|
* You might also think about this as processing XML token stream linearly (rather than XML
|
|
|
|
* nodes), as if we were parsing the document.
|
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
* @param Node $node Node to start at
|
2021-01-08 19:20:33 +00:00
|
|
|
* @param callable $callback Function accepting two arguments: $event ('enter' or 'leave') and
|
2021-07-29 02:16:15 +00:00
|
|
|
* $node (Node)
|
2021-01-08 19:20:33 +00:00
|
|
|
* @return mixed Final return value of the callback
|
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
public static function linearWalk( Node $node, callable $callback ) {
|
2021-01-08 19:20:33 +00:00
|
|
|
$result = null;
|
|
|
|
[ $withinNode, $beforeNode ] = [ $node->parentNode, $node ];
|
|
|
|
|
|
|
|
while ( $beforeNode || $withinNode ) {
|
|
|
|
if ( $beforeNode ) {
|
|
|
|
$result = $callback( 'enter', $beforeNode );
|
|
|
|
[ $withinNode, $beforeNode ] = [ $beforeNode, $beforeNode->firstChild ];
|
|
|
|
} else {
|
|
|
|
$result = $callback( 'leave', $withinNode );
|
|
|
|
[ $withinNode, $beforeNode ] = [ $withinNode->parentNode, $withinNode->nextSibling ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $result ) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2021-01-26 18:58:58 +00:00
|
|
|
/**
|
|
|
|
* Like #linearWalk, but it goes backwards.
|
|
|
|
*
|
|
|
|
* @inheritDoc ::linearWalk()
|
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
public static function linearWalkBackwards( Node $node, callable $callback ) {
|
2021-01-26 18:58:58 +00:00
|
|
|
$result = null;
|
|
|
|
[ $withinNode, $beforeNode ] = [ $node->parentNode, $node ];
|
|
|
|
|
|
|
|
while ( $beforeNode || $withinNode ) {
|
|
|
|
if ( $beforeNode ) {
|
|
|
|
$result = $callback( 'enter', $beforeNode );
|
|
|
|
[ $withinNode, $beforeNode ] = [ $beforeNode, $beforeNode->lastChild ];
|
|
|
|
} else {
|
|
|
|
$result = $callback( 'leave', $withinNode );
|
|
|
|
[ $withinNode, $beforeNode ] = [ $withinNode->parentNode, $withinNode->previousSibling ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $result ) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2021-11-18 22:30:26 +00:00
|
|
|
/**
|
2022-01-11 21:58:57 +00:00
|
|
|
* @param ImmutableRange $range (must not be collapsed)
|
2021-11-18 22:30:26 +00:00
|
|
|
* @return Node
|
|
|
|
*/
|
|
|
|
public static function getRangeFirstNode( ImmutableRange $range ): Node {
|
2022-01-11 21:58:57 +00:00
|
|
|
Assert::precondition( !$range->collapsed, 'Range is not collapsed' );
|
2021-11-18 22:30:26 +00:00
|
|
|
// PHP bug: childNodes can be null
|
|
|
|
return $range->startContainer->childNodes && $range->startContainer->childNodes->length ?
|
|
|
|
$range->startContainer->childNodes[ $range->startOffset ] :
|
|
|
|
$range->startContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-11 21:58:57 +00:00
|
|
|
* @param ImmutableRange $range (must not be collapsed)
|
2021-11-18 22:30:26 +00:00
|
|
|
* @return Node
|
|
|
|
*/
|
|
|
|
public static function getRangeLastNode( ImmutableRange $range ): Node {
|
2022-01-11 21:58:57 +00:00
|
|
|
Assert::precondition( !$range->collapsed, 'Range is not collapsed' );
|
2021-11-18 22:30:26 +00:00
|
|
|
// PHP bug: childNodes can be null
|
|
|
|
return $range->endContainer->childNodes && $range->endContainer->childNodes->length ?
|
|
|
|
$range->endContainer->childNodes[ $range->endOffset - 1 ] :
|
|
|
|
$range->endContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether two ranges overlap, and how.
|
|
|
|
*
|
|
|
|
* Includes a hack to check for "almost equal" ranges (whose start/end boundaries only differ by
|
|
|
|
* "uninteresting" nodes that we ignore when detecting comments), and treat them as equal.
|
|
|
|
*
|
2022-01-11 13:58:19 +00:00
|
|
|
* Illustration of return values:
|
|
|
|
* [ equal ]
|
|
|
|
* |[ contained ]|
|
|
|
|
* [ | contains | ]
|
|
|
|
* [overlap|start] |
|
|
|
|
* | [overlap|end]
|
|
|
|
* [before] | |
|
|
|
|
* | | [after]
|
|
|
|
*
|
2021-11-18 22:30:26 +00:00
|
|
|
* @param ImmutableRange $a
|
|
|
|
* @param ImmutableRange $b
|
|
|
|
* @return string One of:
|
|
|
|
* - 'equal': Ranges A and B are equal
|
|
|
|
* - 'contains': Range A contains range B
|
|
|
|
* - 'contained': Range A is contained within range B
|
|
|
|
* - 'after': Range A is before range B
|
|
|
|
* - 'before': Range A is after range B
|
|
|
|
* - 'overlapstart': Start of range A overlaps range B
|
|
|
|
* - 'overlapend': End of range A overlaps range B
|
|
|
|
*/
|
|
|
|
public static function compareRanges( ImmutableRange $a, ImmutableRange $b ): string {
|
|
|
|
// Compare the positions of: start of A to start of B, start of A to end of B, and so on.
|
|
|
|
// Watch out, the constant names are the opposite of what they should be.
|
|
|
|
$startToStart = $a->compareBoundaryPoints( ImmutableRange::START_TO_START, $b );
|
|
|
|
$startToEnd = $a->compareBoundaryPoints( ImmutableRange::END_TO_START, $b );
|
|
|
|
$endToStart = $a->compareBoundaryPoints( ImmutableRange::START_TO_END, $b );
|
|
|
|
$endToEnd = $a->compareBoundaryPoints( ImmutableRange::END_TO_END, $b );
|
|
|
|
|
|
|
|
// Handle almost equal ranges: When start or end boundary points of the two ranges are different,
|
|
|
|
// but only differ by "uninteresting" nodes, treat them as equal instead.
|
|
|
|
if (
|
2022-06-09 13:51:33 +00:00
|
|
|
( $startToStart < 0 && static::compareRangesAlmostEqualBoundaries( $a, $b, 'start' ) ) ||
|
|
|
|
( $startToStart > 0 && static::compareRangesAlmostEqualBoundaries( $b, $a, 'start' ) )
|
2021-11-18 22:30:26 +00:00
|
|
|
) {
|
|
|
|
$startToStart = 0;
|
|
|
|
}
|
|
|
|
if (
|
2022-06-09 13:51:33 +00:00
|
|
|
( $endToEnd < 0 && static::compareRangesAlmostEqualBoundaries( $a, $b, 'end' ) ) ||
|
|
|
|
( $endToEnd > 0 && static::compareRangesAlmostEqualBoundaries( $b, $a, 'end' ) )
|
2021-11-18 22:30:26 +00:00
|
|
|
) {
|
|
|
|
$endToEnd = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $startToStart === 0 && $endToEnd === 0 ) {
|
|
|
|
return 'equal';
|
|
|
|
}
|
|
|
|
if ( $startToStart <= 0 && $endToEnd >= 0 ) {
|
|
|
|
return 'contains';
|
|
|
|
}
|
|
|
|
if ( $startToStart >= 0 && $endToEnd <= 0 ) {
|
|
|
|
return 'contained';
|
|
|
|
}
|
|
|
|
if ( $startToEnd >= 0 ) {
|
|
|
|
return 'after';
|
|
|
|
}
|
|
|
|
if ( $endToStart <= 0 ) {
|
|
|
|
return 'before';
|
|
|
|
}
|
|
|
|
if ( $startToStart > 0 && $startToEnd < 0 && $endToEnd >= 0 ) {
|
|
|
|
return 'overlapstart';
|
|
|
|
}
|
|
|
|
if ( $endToEnd < 0 && $endToStart > 0 && $startToStart <= 0 ) {
|
|
|
|
return 'overlapend';
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new LogicException( 'Unreachable' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given boundary points of ranges A and B are almost equal (only differing by
|
|
|
|
* uninteresting nodes).
|
|
|
|
*
|
|
|
|
* Boundary of A must be before the boundary of B in the tree.
|
|
|
|
*
|
|
|
|
* @param ImmutableRange $a
|
|
|
|
* @param ImmutableRange $b
|
|
|
|
* @param string $boundary 'start' or 'end'
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function compareRangesAlmostEqualBoundaries(
|
|
|
|
ImmutableRange $a, ImmutableRange $b, string $boundary
|
|
|
|
): bool {
|
|
|
|
// This code is awful, but several attempts to rewrite it made it even worse.
|
|
|
|
// You're welcome to give it a try.
|
|
|
|
|
2022-06-09 13:51:33 +00:00
|
|
|
$from = $boundary === 'end' ? static::getRangeLastNode( $a ) : static::getRangeFirstNode( $a );
|
|
|
|
$to = $boundary === 'end' ? static::getRangeLastNode( $b ) : static::getRangeFirstNode( $b );
|
2021-11-18 22:30:26 +00:00
|
|
|
|
2022-01-24 15:29:34 +00:00
|
|
|
$skipNode = null;
|
|
|
|
if ( $boundary === 'end' ) {
|
|
|
|
$skipNode = $from;
|
|
|
|
}
|
|
|
|
|
2021-11-18 22:30:26 +00:00
|
|
|
$foundContent = false;
|
2022-06-09 13:51:33 +00:00
|
|
|
static::linearWalk(
|
2021-11-18 22:30:26 +00:00
|
|
|
$from,
|
|
|
|
static function ( string $event, Node $n ) use (
|
2022-01-24 15:29:34 +00:00
|
|
|
$from, $to, $boundary, &$skipNode, &$foundContent
|
2021-11-18 22:30:26 +00:00
|
|
|
) {
|
|
|
|
if ( $n === $to && $event === ( $boundary === 'end' ? 'leave' : 'enter' ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-01-24 15:29:34 +00:00
|
|
|
if ( $skipNode ) {
|
|
|
|
if ( $n === $skipNode && $event === 'leave' ) {
|
|
|
|
$skipNode = null;
|
|
|
|
}
|
2021-11-18 22:30:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-24 15:29:34 +00:00
|
|
|
if ( $event === 'enter' ) {
|
|
|
|
if (
|
|
|
|
CommentUtils::isCommentSeparator( $n ) ||
|
|
|
|
CommentUtils::isRenderingTransparentNode( $n ) ||
|
|
|
|
CommentUtils::isOurGeneratedNode( $n )
|
|
|
|
) {
|
|
|
|
$skipNode = $n;
|
|
|
|
|
|
|
|
} elseif (
|
2021-11-18 22:30:26 +00:00
|
|
|
CommentUtils::isCommentContent( $n )
|
2022-01-24 15:29:34 +00:00
|
|
|
) {
|
|
|
|
$foundContent = true;
|
|
|
|
return true;
|
|
|
|
}
|
2021-11-18 22:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return !$foundContent;
|
|
|
|
}
|
2022-02-04 21:58:20 +00:00
|
|
|
|
|
|
|
/**
|
2024-04-25 21:11:22 +00:00
|
|
|
* Check whether the last item in the thread item set is a properly signed comment by
|
2022-02-04 21:58:20 +00:00
|
|
|
* the expected author (that is: there is a signature, and either there's nothing following the
|
|
|
|
* signature, or there's some text within the same paragraph that was detected as part of the same
|
|
|
|
* comment).
|
|
|
|
*/
|
|
|
|
public static function isSingleCommentSignedBy(
|
2022-03-18 03:28:06 +00:00
|
|
|
ContentThreadItemSet $itemSet,
|
2022-02-04 21:58:20 +00:00
|
|
|
string $author,
|
|
|
|
Element $rootNode
|
|
|
|
): bool {
|
|
|
|
$items = $itemSet->getThreadItems();
|
|
|
|
|
|
|
|
if ( $items ) {
|
2022-03-21 22:47:52 +00:00
|
|
|
$lastItem = end( $items );
|
|
|
|
// Check that we've detected a comment first, not just headings (T304377)
|
2022-03-18 03:28:06 +00:00
|
|
|
if ( !( $lastItem instanceof ContentCommentItem && $lastItem->getAuthor() === $author ) ) {
|
2022-03-21 22:47:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-04-25 21:11:22 +00:00
|
|
|
$commentRange = $lastItem->getRange();
|
2022-02-04 21:58:20 +00:00
|
|
|
$bodyRange = new ImmutableRange(
|
|
|
|
$rootNode, 0, $rootNode, count( $rootNode->childNodes )
|
|
|
|
);
|
|
|
|
|
2024-04-25 21:11:22 +00:00
|
|
|
// Only check that the end of the comment range is at the end of the body range.
|
|
|
|
// We don't care about preceding headings, comments, or other content (T363285).
|
|
|
|
// This is a simplified fragment of static::compareRanges().
|
|
|
|
$cmp = $commentRange->compareBoundaryPoints( ImmutableRange::END_TO_END, $bodyRange );
|
|
|
|
if (
|
|
|
|
$cmp === 0 ||
|
|
|
|
( $cmp < 0 && static::compareRangesAlmostEqualBoundaries( $commentRange, $bodyRange, 'end' ) ) ||
|
|
|
|
( $cmp > 0 && static::compareRangesAlmostEqualBoundaries( $bodyRange, $commentRange, 'end' ) )
|
|
|
|
) {
|
2022-02-04 21:58:20 +00:00
|
|
|
// New comment includes a signature in the proper place
|
2022-03-21 22:47:52 +00:00
|
|
|
return true;
|
2022-02-04 21:58:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2023-01-31 15:50:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID for a new topics subscription from a page title
|
|
|
|
*
|
|
|
|
* @param Title $title Page title
|
|
|
|
* @return string ID for a new topics subscription
|
|
|
|
*/
|
|
|
|
public static function getNewTopicsSubscriptionId( Title $title ) {
|
|
|
|
return "p-topics-{$title->getNamespace()}:{$title->getDBkey()}";
|
|
|
|
}
|
2020-05-11 20:07:14 +00:00
|
|
|
}
|