2020-05-22 16:26:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
2020-07-29 23:57:51 +00:00
|
|
|
use DOMNode;
|
2020-09-16 12:06:14 +00:00
|
|
|
use JsonSerializable;
|
2020-07-22 18:25:34 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
2020-07-29 23:57:51 +00:00
|
|
|
|
2020-06-25 12:23:17 +00:00
|
|
|
/**
|
|
|
|
* A thread item, either a heading or a comment
|
|
|
|
*/
|
2020-09-16 12:06:14 +00:00
|
|
|
abstract class ThreadItem implements JsonSerializable {
|
2020-07-20 21:15:03 +00:00
|
|
|
protected $type;
|
|
|
|
protected $range;
|
2020-07-29 23:57:51 +00:00
|
|
|
protected $rootNode;
|
2020-07-20 21:15:03 +00:00
|
|
|
protected $level;
|
2020-10-01 19:36:11 +00:00
|
|
|
protected $parent;
|
2020-05-22 16:26:05 +00:00
|
|
|
|
2020-07-20 21:15:03 +00:00
|
|
|
protected $id = null;
|
2020-10-21 15:52:04 +00:00
|
|
|
protected $legacyId = null;
|
2020-07-20 21:15:03 +00:00
|
|
|
protected $replies = [];
|
2020-05-22 16:26:05 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-25 12:23:17 +00:00
|
|
|
* @param string $type `heading` or `comment`
|
2020-10-01 19:36:11 +00:00
|
|
|
* @param int $level Indentation level
|
2020-06-25 12:23:17 +00:00
|
|
|
* @param ImmutableRange $range Object describing the extent of the comment, including the
|
|
|
|
* signature and timestamp.
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
string $type, int $level, ImmutableRange $range
|
|
|
|
) {
|
|
|
|
$this->type = $type;
|
|
|
|
$this->level = $level;
|
|
|
|
$this->range = $range;
|
|
|
|
}
|
|
|
|
|
2020-09-16 12:06:14 +00:00
|
|
|
/**
|
|
|
|
* @return array JSON-serializable array
|
|
|
|
*/
|
|
|
|
public function jsonSerialize() : array {
|
2020-10-21 15:52:04 +00:00
|
|
|
// The output of this method can end up in the HTTP cache (Varnish). Avoid changing it;
|
|
|
|
// and when doing so, ensure that frontend code can handle both the old and new outputs.
|
|
|
|
// See ThreadItem.static.newFromJSON in JS.
|
|
|
|
|
2020-09-16 12:06:14 +00:00
|
|
|
return [
|
|
|
|
'type' => $this->type,
|
|
|
|
'level' => $this->level,
|
|
|
|
'id' => $this->id,
|
2020-10-01 19:36:11 +00:00
|
|
|
'replies' => array_map( function ( ThreadItem $comment ) {
|
2020-09-16 12:06:14 +00:00
|
|
|
return $comment->getId();
|
|
|
|
}, $this->replies )
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-07-20 14:13:59 +00:00
|
|
|
/**
|
|
|
|
* Get the list of authors in the comment tree below this thread item.
|
|
|
|
*
|
|
|
|
* Usually called on a HeadingItem to find all authors in a thread.
|
|
|
|
*
|
|
|
|
* @return string[] Author usernames
|
|
|
|
*/
|
|
|
|
public function getAuthorsBelow() : array {
|
|
|
|
$authors = [];
|
2020-10-01 19:36:11 +00:00
|
|
|
$getAuthorSet = function ( ThreadItem $comment ) use ( &$authors, &$getAuthorSet ) {
|
|
|
|
if ( $comment instanceof CommentItem ) {
|
|
|
|
$author = $comment->getAuthor();
|
|
|
|
if ( $author ) {
|
|
|
|
$authors[ $author ] = true;
|
|
|
|
}
|
2020-07-20 14:13:59 +00:00
|
|
|
}
|
|
|
|
// Get the set of authors in the same format from each reply
|
|
|
|
array_map( $getAuthorSet, $comment->getReplies() );
|
|
|
|
};
|
|
|
|
|
|
|
|
array_map( $getAuthorSet, $this->getReplies() );
|
|
|
|
|
|
|
|
ksort( $authors );
|
|
|
|
return array_keys( $authors );
|
|
|
|
}
|
|
|
|
|
2020-07-20 14:48:41 +00:00
|
|
|
/**
|
|
|
|
* Get the name of the page from which this thread item is transcluded (if any).
|
|
|
|
*
|
|
|
|
* @return string|bool `false` if this item is not transcluded. A string if it's transcluded
|
|
|
|
* from a single page (the page title, in text form with spaces). `true` if it's transcluded, but
|
|
|
|
* we can't determine the source.
|
|
|
|
*/
|
|
|
|
public function getTranscludedFrom() {
|
2020-06-16 14:08:01 +00:00
|
|
|
// If some template is used within the comment (e.g. {{ping|…}} or {{tl|…}}, or a
|
|
|
|
// non-substituted signature template), that *does not* mean the comment is transcluded.
|
|
|
|
// We only want to consider comments to be transcluded if all wrapper elements (usually
|
|
|
|
// <li> or <p>) are marked as part of a single transclusion.
|
|
|
|
|
|
|
|
// If we can't find "exact" wrappers, using only the end container works out well
|
|
|
|
// (because the main purpose of this method is to decide on which page we should post
|
|
|
|
// replies to the given comment, and they'll go after the comment).
|
|
|
|
|
|
|
|
$coveredNodes = CommentUtils::getFullyCoveredSiblings( $this ) ?:
|
|
|
|
[ $this->getRange()->endContainer ];
|
|
|
|
|
|
|
|
$node = CommentUtils::getTranscludedFromElement( $coveredNodes[ 0 ] );
|
|
|
|
$length = count( $coveredNodes );
|
|
|
|
for ( $i = 1; $i < $length; $i++ ) {
|
|
|
|
if ( $node !== CommentUtils::getTranscludedFromElement( $coveredNodes[ $i ] ) ) {
|
|
|
|
// Comment is only partially transcluded, that should be fine
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 14:48:41 +00:00
|
|
|
|
|
|
|
if ( !$node ) {
|
|
|
|
// No mw:Transclusion node found, this item is not transcluded
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dataMw = json_decode( $node->getAttribute( 'data-mw' ), true );
|
|
|
|
|
|
|
|
// Only return a page name if this is a simple single-template transclusion.
|
|
|
|
if (
|
|
|
|
is_array( $dataMw ) &&
|
|
|
|
$dataMw['parts'] &&
|
|
|
|
count( $dataMw['parts'] ) === 1 &&
|
|
|
|
$dataMw['parts'][0]['template'] &&
|
2020-08-07 15:41:41 +00:00
|
|
|
// 'href' will be unset if this is a parser function rather than a template
|
|
|
|
isset( $dataMw['parts'][0]['template']['target']['href'] )
|
2020-07-20 14:48:41 +00:00
|
|
|
) {
|
|
|
|
$title = CommentUtils::getTitleFromUrl( $dataMw['parts'][0]['template']['target']['href'] );
|
|
|
|
return $title->getPrefixedText();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multi-template transclusion, or a parser function call, or template-affected wikitext outside
|
|
|
|
// of a template call, or a mix of the above
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-22 18:25:34 +00:00
|
|
|
/**
|
|
|
|
* Get the HTML of this thread item
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
|
|
|
public function getHTML() : string {
|
|
|
|
$fragment = $this->getRange()->cloneContents();
|
|
|
|
$container = $fragment->ownerDocument->createElement( 'div' );
|
|
|
|
$container->appendChild( $fragment );
|
|
|
|
return DOMCompat::getInnerHTML( $container );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the text of this thread item
|
|
|
|
*
|
|
|
|
* @return string Text
|
|
|
|
*/
|
|
|
|
public function getText() : string {
|
|
|
|
$fragment = $this->getRange()->cloneContents();
|
|
|
|
return $fragment->textContent;
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
|
|
|
* @return string Thread item type
|
|
|
|
*/
|
|
|
|
public function getType() : string {
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-01 19:36:11 +00:00
|
|
|
* @return int Indentation level
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
|
|
|
public function getLevel() : int {
|
|
|
|
return $this->level;
|
|
|
|
}
|
|
|
|
|
2020-10-01 19:36:11 +00:00
|
|
|
/**
|
|
|
|
* @return ThreadItem|null Parent thread item
|
|
|
|
*/
|
|
|
|
public function getParent() : ?ThreadItem {
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
2020-07-22 18:25:34 +00:00
|
|
|
* @return ImmutableRange Range of the entire thread item
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
|
|
|
public function getRange() : ImmutableRange {
|
|
|
|
return $this->range;
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:57:51 +00:00
|
|
|
/**
|
|
|
|
* @return DOMNode Root node (level is relative to this node)
|
|
|
|
*/
|
|
|
|
public function getRootNode() : DOMNode {
|
|
|
|
return $this->rootNode;
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
2020-09-22 23:05:25 +00:00
|
|
|
* @return string Thread ID
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
2020-09-22 23:05:25 +00:00
|
|
|
public function getId() : string {
|
2020-05-22 16:26:05 +00:00
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2020-10-21 15:52:04 +00:00
|
|
|
/**
|
|
|
|
* @return string|null Thread ID, according to an older algorithm
|
|
|
|
*/
|
|
|
|
public function getLegacyId() : ?string {
|
|
|
|
return $this->legacyId;
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
2020-10-01 19:36:11 +00:00
|
|
|
* @return ThreadItem[] Replies to this thread item
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
|
|
|
public function getReplies() : array {
|
|
|
|
return $this->replies;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-01 19:36:11 +00:00
|
|
|
* @param int $level Indentation level
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
|
|
|
public function setLevel( int $level ) : void {
|
|
|
|
$this->level = $level;
|
|
|
|
}
|
|
|
|
|
2020-10-01 19:36:11 +00:00
|
|
|
/**
|
|
|
|
* @param ThreadItem $parent Parent thread item
|
|
|
|
*/
|
|
|
|
public function setParent( ThreadItem $parent ) {
|
|
|
|
$this->parent = $parent;
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
|
|
|
* @param ImmutableRange $range Thread item range
|
|
|
|
*/
|
|
|
|
public function setRange( ImmutableRange $range ) : void {
|
|
|
|
$this->range = $range;
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:57:51 +00:00
|
|
|
/**
|
|
|
|
* @param DOMNode $rootNode Root node (level is relative to this node)
|
|
|
|
*/
|
|
|
|
public function setRootNode( DOMNode $rootNode ) : void {
|
|
|
|
$this->rootNode = $rootNode;
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
|
|
|
* @param string|null $id Thread ID
|
|
|
|
*/
|
|
|
|
public function setId( ?string $id ) : void {
|
|
|
|
$this->id = $id;
|
|
|
|
}
|
|
|
|
|
2020-10-21 15:52:04 +00:00
|
|
|
/**
|
|
|
|
* @param string|null $id Thread ID
|
|
|
|
*/
|
|
|
|
public function setLegacyId( ?string $id ) : void {
|
|
|
|
$this->legacyId = $id;
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
2020-10-01 19:36:11 +00:00
|
|
|
* @param ThreadItem $reply Reply comment
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
2020-10-01 19:36:11 +00:00
|
|
|
public function addReply( ThreadItem $reply ) : void {
|
2020-05-22 16:26:05 +00:00
|
|
|
$this->replies[] = $reply;
|
|
|
|
}
|
|
|
|
}
|