| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools\ThreadItem; |
| 4 |
|
| 5 |
use DateTimeImmutable; |
| 6 |
use MediaWiki\Extension\DiscussionTools\CommentModifier; |
| 7 |
use MediaWiki\Extension\DiscussionTools\CommentUtils; |
| 8 |
use MediaWiki\Extension\DiscussionTools\ImmutableRange; |
| 9 |
use MediaWiki\MediaWikiServices; |
| 10 |
use MediaWiki\Parser\Sanitizer; |
| 11 |
use MediaWiki\Title\Title; |
| 12 |
use Wikimedia\Parsoid\DOM\DocumentFragment; |
| 13 |
use Wikimedia\Parsoid\DOM\Text; |
| 14 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 15 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 16 |
|
| 17 |
class ContentCommentItem extends ContentThreadItem implements CommentItem { |
| 18 |
use CommentItemTrait { |
| 19 |
getHeading as protected traitGetHeading; |
| 20 |
getSubscribableHeading as protected traitGetSubscribableHeading; |
| 21 |
jsonSerialize as protected traitJsonSerialize; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
private array $signatureRanges; |
| 26 |
|
| 27 |
private array $timestampRanges; |
| 28 |
private DateTimeImmutable $timestamp; |
| 29 |
private string $author; |
| 30 |
private ?string $displayName; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function __construct( |
| 46 |
int $level, ImmutableRange $range, $transcludedFrom, |
| 47 |
array $signatureRanges, array $timestampRanges, |
| 48 |
DateTimeImmutable $timestamp, |
| 49 |
string $author, ?string $displayName = null |
| 50 |
) { |
| 51 |
parent::__construct( 'comment', $level, $range, $transcludedFrom ); |
| 52 |
$this->signatureRanges = $signatureRanges; |
| 53 |
$this->timestampRanges = $timestampRanges; |
| 54 |
$this->timestamp = $timestamp; |
| 55 |
$this->author = $author; |
| 56 |
$this->displayName = $displayName; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function jsonSerialize( bool $deep = false, ?callable $callback = null ): array { |
| 63 |
$data = $this->traitJsonSerialize( $deep, $callback ); |
| 64 |
if ( $this->displayName ) { |
| 65 |
$data['displayName'] = $this->displayName; |
| 66 |
} |
| 67 |
return $data; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
private function getBodyFragment( bool $stripTrailingSeparator = false ): DocumentFragment { |
| 78 |
$fragment = $this->getBodyRange()->cloneContents(); |
| 79 |
CommentModifier::unwrapFragment( $fragment ); |
| 80 |
|
| 81 |
if ( $stripTrailingSeparator ) { |
| 82 |
|
| 83 |
$lastChild = $fragment->lastChild; |
| 84 |
while ( |
| 85 |
$lastChild && |
| 86 |
!( $lastChild instanceof Text ) |
| 87 |
) { |
| 88 |
$lastChild = $lastChild->lastChild; |
| 89 |
} |
| 90 |
if ( |
| 91 |
$lastChild instanceof Text && |
| 92 |
preg_match( '/[\s\-~\x{2010}-\x{2015}\x{2043}\x{2060}]+$/u', $lastChild->nodeValue ?? '', $matches ) |
| 93 |
) { |
| 94 |
$lastChild->nodeValue = |
| 95 |
substr( $lastChild->nodeValue ?? '', 0, -strlen( $matches[0] ) ); |
| 96 |
} |
| 97 |
} |
| 98 |
return $fragment; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function getBodyHTML( bool $stripTrailingSeparator = false ): string { |
| 109 |
$fragment = $this->getBodyFragment( $stripTrailingSeparator ); |
| 110 |
return DOMUtils::getFragmentInnerHTML( $fragment ); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function getBodyText( bool $stripTrailingSeparator = false ): string { |
| 120 |
$html = $this->getBodyHTML( $stripTrailingSeparator ); |
| 121 |
return Sanitizer::stripAllTags( $html ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function getMentions(): array { |
| 130 |
$fragment = $this->getBodyRange()->cloneContents(); |
| 131 |
|
| 132 |
$links = DOMCompat::querySelectorAll( $fragment, 'a' ); |
| 133 |
$users = []; |
| 134 |
foreach ( $links as $link ) { |
| 135 |
$href = $link->getAttribute( 'href' ); |
| 136 |
if ( $href ) { |
| 137 |
$siteConfig = MediaWikiServices::getInstance()->getMainConfig(); |
| 138 |
$title = Title::newFromText( CommentUtils::getTitleFromUrl( $href, $siteConfig ) ); |
| 139 |
if ( $title && $title->inNamespace( NS_USER ) ) { |
| 140 |
|
| 141 |
$users[] = $title; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
return array_unique( $users ); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public function getSignatureRanges(): array { |
| 152 |
return $this->signatureRanges; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function getTimestampRanges(): array { |
| 159 |
return $this->timestampRanges; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function getBodyRange(): ImmutableRange { |
| 166 |
|
| 167 |
$signatureRanges = $this->getSignatureRanges(); |
| 168 |
$lastSignature = end( $signatureRanges ); |
| 169 |
return $this->getRange()->setEnd( $lastSignature->startContainer, $lastSignature->startOffset ); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function getTimestamp(): DateTimeImmutable { |
| 176 |
return $this->timestamp; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function getAuthor(): string { |
| 183 |
return $this->author; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function getDisplayName(): ?string { |
| 190 |
return $this->displayName; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public function getHeading(): ContentHeadingItem { |
| 198 |
return $this->traitGetHeading(); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public function getSubscribableHeading(): ?ContentHeadingItem { |
| 205 |
return $this->traitGetSubscribableHeading(); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public function addSignatureRange( ImmutableRange $signatureRange ): void { |
| 212 |
$this->signatureRanges[] = $signatureRange; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public function setSignatureRanges( array $signatureRanges ): void { |
| 219 |
$this->signatureRanges = $signatureRanges; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function setTimestamp( DateTimeImmutable $timestamp ): void { |
| 226 |
$this->timestamp = $timestamp; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public function setAuthor( string $author ): void { |
| 233 |
$this->author = $author; |
| 234 |
} |
| 235 |
} |