| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools\ThreadItem; |
| 4 |
|
| 5 |
use JsonSerializable; |
| 6 |
use MediaWiki\Extension\DiscussionTools\CommentModifier; |
| 7 |
use MediaWiki\Extension\DiscussionTools\ImmutableRange; |
| 8 |
use MediaWiki\Parser\Sanitizer; |
| 9 |
use Wikimedia\Parsoid\DOM\Element; |
| 10 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 11 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
abstract class ContentThreadItem implements JsonSerializable, ThreadItem { |
| 17 |
use ThreadItemTrait; |
| 18 |
|
| 19 |
protected string $type; |
| 20 |
protected ImmutableRange $range; |
| 21 |
protected Element $rootNode; |
| 22 |
protected int $level; |
| 23 |
protected ?ContentThreadItem $parent = null; |
| 24 |
|
| 25 |
protected array $warnings = []; |
| 26 |
|
| 27 |
protected string $name; |
| 28 |
protected string $id; |
| 29 |
protected ?string $legacyId = null; |
| 30 |
|
| 31 |
protected array $replies = []; |
| 32 |
|
| 33 |
private $transcludedFrom; |
| 34 |
|
| 35 |
|
| 36 |
protected ?array $authors = null; |
| 37 |
protected int $commentCount; |
| 38 |
protected ?ContentCommentItem $oldestReply; |
| 39 |
protected ?ContentCommentItem $latestReply; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function __construct( |
| 49 |
string $type, int $level, ImmutableRange $range, $transcludedFrom |
| 50 |
) { |
| 51 |
$this->type = $type; |
| 52 |
$this->level = $level; |
| 53 |
$this->range = $range; |
| 54 |
$this->transcludedFrom = $transcludedFrom; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private function calculateThreadSummary(): void { |
| 61 |
if ( $this->authors !== null ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
$authors = []; |
| 65 |
$commentCount = 0; |
| 66 |
$oldestReply = null; |
| 67 |
$latestReply = null; |
| 68 |
$threadScan = static function ( ContentThreadItem $comment ) use ( |
| 69 |
&$authors, &$commentCount, &$oldestReply, &$latestReply, &$threadScan |
| 70 |
) { |
| 71 |
if ( $comment instanceof ContentCommentItem ) { |
| 72 |
$author = $comment->getAuthor(); |
| 73 |
if ( !isset( $authors[ $author] ) ) { |
| 74 |
$authors[ $author ] = [ |
| 75 |
'username' => $author, |
| 76 |
'displayNames' => [], |
| 77 |
]; |
| 78 |
} |
| 79 |
$displayName = $comment->getDisplayName(); |
| 80 |
if ( $displayName && !in_array( $displayName, $authors[ $author ][ 'displayNames' ], true ) ) { |
| 81 |
$authors[ $author ][ 'displayNames' ][] = $displayName; |
| 82 |
} |
| 83 |
|
| 84 |
if ( |
| 85 |
!$oldestReply || |
| 86 |
( $comment->getTimestamp() < $oldestReply->getTimestamp() ) |
| 87 |
) { |
| 88 |
$oldestReply = $comment; |
| 89 |
} |
| 90 |
if ( |
| 91 |
!$latestReply || |
| 92 |
( $latestReply->getTimestamp() < $comment->getTimestamp() ) |
| 93 |
) { |
| 94 |
$latestReply = $comment; |
| 95 |
} |
| 96 |
$commentCount++; |
| 97 |
} |
| 98 |
|
| 99 |
$replies = $comment->getReplies(); |
| 100 |
array_walk( $replies, $threadScan ); |
| 101 |
}; |
| 102 |
$replies = $this->getReplies(); |
| 103 |
array_walk( $replies, $threadScan ); |
| 104 |
|
| 105 |
ksort( $authors ); |
| 106 |
|
| 107 |
$this->authors = array_values( $authors ); |
| 108 |
$this->commentCount = $commentCount; |
| 109 |
$this->oldestReply = $oldestReply; |
| 110 |
$this->latestReply = $latestReply; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function getAuthorsBelow(): array { |
| 121 |
$this->calculateThreadSummary(); |
| 122 |
return $this->authors; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function getCommentCount(): int { |
| 129 |
$this->calculateThreadSummary(); |
| 130 |
return $this->commentCount; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function getLatestReply(): ?ContentCommentItem { |
| 137 |
$this->calculateThreadSummary(); |
| 138 |
return $this->latestReply; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function getOldestReply(): ?ContentCommentItem { |
| 145 |
$this->calculateThreadSummary(); |
| 146 |
return $this->oldestReply; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public function getThreadItemsBelow(): array { |
| 155 |
$threadItems = []; |
| 156 |
$getReplies = static function ( ContentThreadItem $threadItem ) use ( &$threadItems, &$getReplies ) { |
| 157 |
$threadItems[] = $threadItem; |
| 158 |
foreach ( $threadItem->getReplies() as $reply ) { |
| 159 |
$getReplies( $reply ); |
| 160 |
} |
| 161 |
}; |
| 162 |
|
| 163 |
foreach ( $this->getReplies() as $reply ) { |
| 164 |
$getReplies( $reply ); |
| 165 |
} |
| 166 |
|
| 167 |
return $threadItems; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function getTranscludedFrom() { |
| 174 |
return $this->transcludedFrom; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function getHTML(): string { |
| 183 |
$fragment = $this->getRange()->cloneContents(); |
| 184 |
CommentModifier::unwrapFragment( $fragment ); |
| 185 |
$editsection = DOMCompat::querySelector( $fragment, 'mw\\:editsection' ); |
| 186 |
if ( $editsection ) { |
| 187 |
$editsection->parentNode->removeChild( $editsection ); |
| 188 |
} |
| 189 |
return DOMUtils::getFragmentInnerHTML( $fragment ); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public function getText(): string { |
| 198 |
$html = $this->getHTML(); |
| 199 |
return Sanitizer::stripAllTags( $html ); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function getType(): string { |
| 206 |
return $this->type; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public function getLevel(): int { |
| 213 |
return $this->level; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public function getParent(): ?ThreadItem { |
| 220 |
return $this->parent; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function getRange(): ImmutableRange { |
| 227 |
return $this->range; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function getRootNode(): Element { |
| 234 |
return $this->rootNode; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public function getName(): string { |
| 241 |
return $this->name; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public function getId(): string { |
| 248 |
return $this->id; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function getLegacyId(): ?string { |
| 255 |
return $this->legacyId; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public function getReplies(): array { |
| 262 |
return $this->replies; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public function getWarnings(): array { |
| 269 |
return $this->warnings; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public function setLevel( int $level ): void { |
| 276 |
$this->level = $level; |
| 277 |
} |
| 278 |
|
| 279 |
public function setParent( ContentThreadItem $parent ): void { |
| 280 |
$this->parent = $parent; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public function setRange( ImmutableRange $range ): void { |
| 287 |
$this->range = $range; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public function setRootNode( Element $rootNode ): void { |
| 294 |
$this->rootNode = $rootNode; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public function setName( string $name ): void { |
| 301 |
$this->name = $name; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public function setId( string $id ): void { |
| 308 |
$this->id = $id; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public function setLegacyId( ?string $legacyId ): void { |
| 315 |
$this->legacyId = $legacyId; |
| 316 |
} |
| 317 |
|
| 318 |
public function addWarning( string $warning ): void { |
| 319 |
$this->warnings[] = $warning; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public function addWarnings( array $warnings ): void { |
| 326 |
$this->warnings = array_merge( $this->warnings, $warnings ); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public function addReply( ContentThreadItem $reply ): void { |
| 333 |
$this->replies[] = $reply; |
| 334 |
} |
| 335 |
} |