2020-05-22 16:26:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
2020-07-22 18:25:34 +00:00
|
|
|
use DOMXPath;
|
2020-06-29 13:30:47 +00:00
|
|
|
use MWException;
|
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;
|
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-01-08 19:44:15 +00:00
|
|
|
* @param string $timestamp
|
|
|
|
* @param string $author Comment author's username
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
int $level, ImmutableRange $range,
|
2021-01-08 19:44:15 +00:00
|
|
|
array $signatureRanges, string $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
|
|
|
/**
|
|
|
|
* @return array JSON-serializable array
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function jsonSerialize(): array {
|
2020-09-16 12:06:14 +00:00
|
|
|
return array_merge( parent::jsonSerialize(), [
|
|
|
|
'timestamp' => $this->timestamp,
|
|
|
|
'author' => $this->author,
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
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 (
|
2021-07-29 02:16:15 +00:00
|
|
|
!( $lastChild instanceof Text ) &&
|
2020-11-20 20:09:55 +00:00
|
|
|
$lastChild->lastChild
|
|
|
|
) {
|
|
|
|
$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 );
|
2020-07-22 18:25:34 +00:00
|
|
|
$container = $fragment->ownerDocument->createElement( 'div' );
|
|
|
|
$container->appendChild( $fragment );
|
|
|
|
return DOMCompat::getInnerHTML( $container );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2021-02-23 22:50:22 +00:00
|
|
|
$fragment = $this->getBodyFragment( $stripTrailingSeparator );
|
2021-07-29 02:16:15 +00:00
|
|
|
return $fragment->textContent ?? '';
|
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-07-29 02:16:15 +00:00
|
|
|
// XXX use DOMCompat::querySelectorAll('a[href]') perhaps
|
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal Nonstandard DOM
|
2020-07-22 18:25:34 +00:00
|
|
|
$xPath = new DOMXPath( $fragment->ownerDocument );
|
2021-07-29 02:16:15 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal Nonstandard DOM
|
2020-07-22 18:25:34 +00:00
|
|
|
$links = $xPath->query( './/a', $fragment );
|
|
|
|
$users = [];
|
|
|
|
foreach ( $links as $link ) {
|
|
|
|
$title = CommentUtils::getTitleFromUrl( $link->getAttribute( 'href' ) );
|
|
|
|
if ( $title && $title->getNamespace() === NS_USER ) {
|
|
|
|
// TODO: Consider returning User objects
|
|
|
|
$users[] = $title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @return string Comment timestamp
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function getTimestamp(): string {
|
2020-05-22 16:26:05 +00:00
|
|
|
return $this->timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $timestamp Comment timestamp
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function setTimestamp( string $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;
|
|
|
|
}
|
|
|
|
}
|