2020-05-22 16:26:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
2021-11-08 17:47:03 +00:00
|
|
|
use DateTimeImmutable;
|
2022-02-21 17:29:50 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-06-29 13:30:47 +00:00
|
|
|
use MWException;
|
2022-02-10 20:34:05 +00:00
|
|
|
use Sanitizer;
|
2020-07-22 18:25:34 +00:00
|
|
|
use Title;
|
2021-07-29 02:16:15 +00:00
|
|
|
use Wikimedia\Parsoid\DOM\DocumentFragment;
|
|
|
|
use Wikimedia\Parsoid\DOM\Text;
|
2020-07-22 18:25:34 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
2022-02-08 22:11:24 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMUtils;
|
2020-06-29 13:30:47 +00:00
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
class CommentItem extends ThreadItem {
|
|
|
|
private $signatureRanges;
|
|
|
|
private $timestamp;
|
|
|
|
private $author;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $level
|
|
|
|
* @param ImmutableRange $range
|
2020-06-25 12:23:17 +00:00
|
|
|
* @param ImmutableRange[] $signatureRanges Objects describing the extent of signatures (plus
|
|
|
|
* timestamps) for this comment. There is always at least one signature, but there may be
|
|
|
|
* multiple. The author and timestamp of the comment is determined from the first signature.
|
|
|
|
* The last node in every signature range is a node containing the timestamp.
|
2021-11-08 17:47:03 +00:00
|
|
|
* @param DateTimeImmutable $timestamp
|
2021-01-08 19:44:15 +00:00
|
|
|
* @param string $author Comment author's username
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
int $level, ImmutableRange $range,
|
2021-11-08 17:47:03 +00:00
|
|
|
array $signatureRanges, DateTimeImmutable $timestamp, string $author
|
2020-05-22 16:26:05 +00:00
|
|
|
) {
|
|
|
|
parent::__construct( 'comment', $level, $range );
|
|
|
|
$this->signatureRanges = $signatureRanges;
|
|
|
|
$this->timestamp = $timestamp;
|
|
|
|
$this->author = $author;
|
|
|
|
}
|
|
|
|
|
2020-09-16 12:06:14 +00:00
|
|
|
/**
|
2022-03-21 21:14:47 +00:00
|
|
|
* @inheritDoc
|
2020-09-16 12:06:14 +00:00
|
|
|
*/
|
2022-03-21 21:14:47 +00:00
|
|
|
public function jsonSerialize( bool $deep = false, ?callable $callback = null ): array {
|
|
|
|
return array_merge( parent::jsonSerialize( $deep, $callback ), [
|
2021-11-08 17:47:03 +00:00
|
|
|
'timestamp' => $this->getTimestampString(),
|
2020-09-16 12:06:14 +00:00
|
|
|
'author' => $this->author,
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2022-01-31 15:01:32 +00:00
|
|
|
/**
|
|
|
|
* @return array JSON-serializable array
|
|
|
|
*/
|
|
|
|
public function jsonSerializeForDiff(): array {
|
|
|
|
$data = $this->jsonSerialize();
|
|
|
|
|
|
|
|
$heading = $this->getHeading();
|
|
|
|
$data['headingId'] = $heading->getId();
|
|
|
|
$subscribableHeading = $this->getSubscribableHeading();
|
|
|
|
$data['subscribableHeadingId'] = $subscribableHeading ? $subscribableHeading->getId() : null;
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2020-07-22 18:25:34 +00:00
|
|
|
/**
|
|
|
|
* Get the HTML of this comment's body
|
|
|
|
*
|
2020-11-20 20:09:55 +00:00
|
|
|
* @param bool $stripTrailingSeparator Strip a trailing separator between the body and
|
|
|
|
* the signature which consists of whitespace and hyphens e.g. ' --'
|
2021-07-29 02:16:15 +00:00
|
|
|
* @return DocumentFragment Cloned fragment of the body content
|
2020-07-22 18:25:34 +00:00
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
private function getBodyFragment( bool $stripTrailingSeparator = false ): DocumentFragment {
|
2020-07-22 18:25:34 +00:00
|
|
|
$fragment = $this->getBodyRange()->cloneContents();
|
2020-11-20 00:21:30 +00:00
|
|
|
CommentModifier::unwrapFragment( $fragment );
|
2020-11-20 20:09:55 +00:00
|
|
|
|
|
|
|
if ( $stripTrailingSeparator ) {
|
|
|
|
// Find a trailing text node
|
|
|
|
$lastChild = $fragment->lastChild;
|
|
|
|
while (
|
2022-03-03 16:14:25 +00:00
|
|
|
$lastChild &&
|
|
|
|
!( $lastChild instanceof Text )
|
2020-11-20 20:09:55 +00:00
|
|
|
) {
|
|
|
|
$lastChild = $lastChild->lastChild;
|
|
|
|
}
|
|
|
|
if (
|
2021-07-29 02:16:15 +00:00
|
|
|
$lastChild instanceof Text &&
|
|
|
|
preg_match( '/[\s\-~\x{2010}-\x{2015}\x{2043}\x{2060}]+$/u', $lastChild->nodeValue ?? '', $matches )
|
2020-11-20 20:09:55 +00:00
|
|
|
) {
|
|
|
|
$lastChild->nodeValue =
|
2021-07-29 02:16:15 +00:00
|
|
|
substr( $lastChild->nodeValue ?? '', 0, -strlen( $matches[0] ) );
|
2020-11-20 20:09:55 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-23 22:50:22 +00:00
|
|
|
return $fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the HTML of this comment's body
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param bool $stripTrailingSeparator See getBodyFragment
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function getBodyHTML( bool $stripTrailingSeparator = false ): string {
|
2021-02-23 22:50:22 +00:00
|
|
|
$fragment = $this->getBodyFragment( $stripTrailingSeparator );
|
2022-02-08 22:11:24 +00:00
|
|
|
return DOMUtils::getFragmentInnerHTML( $fragment );
|
2020-07-22 18:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the text of this comment's body
|
|
|
|
*
|
2021-02-23 22:50:22 +00:00
|
|
|
* @param bool $stripTrailingSeparator See getBodyFragment
|
2020-07-22 18:25:34 +00:00
|
|
|
* @return string Text
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function getBodyText( bool $stripTrailingSeparator = false ): string {
|
2022-02-10 20:34:05 +00:00
|
|
|
$html = $this->getBodyHTML( $stripTrailingSeparator );
|
|
|
|
return Sanitizer::stripAllTags( $html );
|
2020-07-22 18:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of all users mentioned
|
|
|
|
*
|
|
|
|
* @return Title[] Title objects for mentioned user pages
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function getMentions(): array {
|
2020-07-22 18:25:34 +00:00
|
|
|
$fragment = $this->getBodyRange()->cloneContents();
|
2021-08-02 13:07:41 +00:00
|
|
|
// Note: DOMCompat::getElementsByTagName() doesn't take a DocumentFragment argument
|
|
|
|
$links = DOMCompat::querySelectorAll( $fragment, 'a' );
|
2020-07-22 18:25:34 +00:00
|
|
|
$users = [];
|
|
|
|
foreach ( $links as $link ) {
|
2021-08-02 13:07:41 +00:00
|
|
|
$href = $link->getAttribute( 'href' );
|
|
|
|
if ( $href ) {
|
2022-02-21 17:29:50 +00:00
|
|
|
$siteConfig = MediaWikiServices::getInstance()->getMainConfig();
|
2022-02-21 22:07:38 +00:00
|
|
|
$title = Title::newFromText( CommentUtils::getTitleFromUrl( $href, $siteConfig ) );
|
2021-08-02 13:07:41 +00:00
|
|
|
if ( $title && $title->getNamespace() === NS_USER ) {
|
|
|
|
// TODO: Consider returning User objects
|
|
|
|
$users[] = $title;
|
|
|
|
}
|
2020-07-22 18:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return array_unique( $users );
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
|
|
|
* @return ImmutableRange[] Comment signature ranges
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function getSignatureRanges(): array {
|
2020-05-22 16:26:05 +00:00
|
|
|
return $this->signatureRanges;
|
|
|
|
}
|
|
|
|
|
2020-07-22 18:25:34 +00:00
|
|
|
/**
|
|
|
|
* @return ImmutableRange Range of the thread item's "body"
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function getBodyRange(): ImmutableRange {
|
2020-07-22 18:25:34 +00:00
|
|
|
// Exclude last signature from body
|
|
|
|
$signatureRanges = $this->getSignatureRanges();
|
|
|
|
$lastSignature = end( $signatureRanges );
|
|
|
|
return $this->getRange()->setEnd( $lastSignature->startContainer, $lastSignature->startOffset );
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
2021-11-08 17:47:03 +00:00
|
|
|
* @return DateTimeImmutable Comment timestamp
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
2021-11-08 17:47:03 +00:00
|
|
|
public function getTimestamp(): DateTimeImmutable {
|
2020-05-22 16:26:05 +00:00
|
|
|
return $this->timestamp;
|
|
|
|
}
|
|
|
|
|
2021-11-08 17:47:03 +00:00
|
|
|
/**
|
|
|
|
* Get the comment timestamp in a standard format
|
|
|
|
*
|
|
|
|
* Uses ISO 8601 date. Almost DateTimeInterface::RFC3339_EXTENDED, but ending with 'Z' instead
|
|
|
|
* of '+00:00', like Date#toISOString in JavaScript.
|
|
|
|
*
|
|
|
|
* @return string Comment timestamp in standard format
|
|
|
|
*/
|
|
|
|
public function getTimestampString(): string {
|
|
|
|
return $this->timestamp->format( 'Y-m-d\TH:i:s.v\Z' );
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
2021-02-22 20:48:01 +00:00
|
|
|
* @return string Comment author
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function getAuthor(): string {
|
2020-05-22 16:26:05 +00:00
|
|
|
return $this->author;
|
|
|
|
}
|
|
|
|
|
2020-06-29 13:30:47 +00:00
|
|
|
/**
|
|
|
|
* @return HeadingItem Closest ancestor which is a HeadingItem
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function getHeading(): HeadingItem {
|
2020-06-29 13:30:47 +00:00
|
|
|
$parent = $this;
|
|
|
|
while ( $parent instanceof CommentItem ) {
|
|
|
|
$parent = $parent->getParent();
|
|
|
|
}
|
|
|
|
if ( !( $parent instanceof HeadingItem ) ) {
|
|
|
|
throw new MWException( 'heading parent not found' );
|
|
|
|
}
|
|
|
|
return $parent;
|
|
|
|
}
|
|
|
|
|
2021-09-01 22:05:00 +00:00
|
|
|
/**
|
|
|
|
* @return HeadingItem|null Closest heading that can be used for topic subscriptions
|
|
|
|
*/
|
|
|
|
public function getSubscribableHeading(): ?HeadingItem {
|
|
|
|
$heading = $this->getHeading();
|
|
|
|
while ( $heading instanceof HeadingItem && !$heading->isSubscribable() ) {
|
|
|
|
$heading = $heading->getParent();
|
|
|
|
}
|
|
|
|
return $heading instanceof HeadingItem ? $heading : null;
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
|
|
|
* @param ImmutableRange $signatureRange Comment signature range to add
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function addSignatureRange( ImmutableRange $signatureRange ): void {
|
2020-05-22 16:26:05 +00:00
|
|
|
$this->signatureRanges[] = $signatureRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ImmutableRange[] $signatureRanges Comment signature ranges
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function setSignatureRanges( array $signatureRanges ): void {
|
2020-05-22 16:26:05 +00:00
|
|
|
$this->signatureRanges = $signatureRanges;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-08 17:47:03 +00:00
|
|
|
* @param DateTimeImmutable $timestamp Comment timestamp
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
2021-11-08 17:47:03 +00:00
|
|
|
public function setTimestamp( DateTimeImmutable $timestamp ): void {
|
2020-05-22 16:26:05 +00:00
|
|
|
$this->timestamp = $timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-22 20:48:01 +00:00
|
|
|
* @param string $author Comment author
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function setAuthor( string $author ): void {
|
2020-05-22 16:26:05 +00:00
|
|
|
$this->author = $author;
|
|
|
|
}
|
|
|
|
}
|