| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools\ThreadItem; |
| 4 |
|
| 5 |
use DateTimeImmutable; |
| 6 |
use DateTimeZone; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWiki\MediaWikiServices; |
| 9 |
use RuntimeException; |
| 10 |
|
| 11 |
trait CommentItemTrait { |
| 12 |
|
| 13 |
|
| 14 |
abstract public function getParent(): ?ThreadItem; |
| 15 |
|
| 16 |
abstract public function getAuthor(): string; |
| 17 |
abstract public function getTimestamp(): DateTimeImmutable; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function jsonSerialize( bool $deep = false, ?callable $callback = null ): array { |
| 25 |
return array_merge( [ |
| 26 |
'timestamp' => $this->getTimestampString(), |
| 27 |
'author' => $this->getAuthor(), |
| 28 |
], parent::jsonSerialize( $deep, $callback ) ); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function jsonSerializeForDiff(): array { |
| 35 |
$data = $this->jsonSerialize(); |
| 36 |
|
| 37 |
$heading = $this->getHeading(); |
| 38 |
$data['headingId'] = $heading->getId(); |
| 39 |
$subscribableHeading = $this->getSubscribableHeading(); |
| 40 |
$data['subscribableHeadingId'] = $subscribableHeading ? $subscribableHeading->getId() : null; |
| 41 |
|
| 42 |
return $data; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function getTimestampString(): string { |
| 60 |
$dtConfig = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'discussiontools' ); |
| 61 |
$switchTime = new DateTimeImmutable( |
| 62 |
$dtConfig->get( 'DiscussionToolsTimestampFormatSwitchTime' ), |
| 63 |
|
| 64 |
new DateTimeZone( MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Localtimezone ) ) |
| 65 |
); |
| 66 |
$timestamp = $this->getTimestamp(); |
| 67 |
if ( $timestamp < $switchTime ) { |
| 68 |
return $timestamp->format( 'Y-m-d\TH:i:s.v\Z' ); |
| 69 |
} else { |
| 70 |
return $timestamp->format( 'YmdHis' ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function getHeading(): HeadingItem { |
| 78 |
$parent = $this; |
| 79 |
while ( $parent instanceof CommentItem ) { |
| 80 |
$parent = $parent->getParent(); |
| 81 |
} |
| 82 |
if ( !( $parent instanceof HeadingItem ) ) { |
| 83 |
throw new RuntimeException( 'Heading parent not found' ); |
| 84 |
} |
| 85 |
return $parent; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function getSubscribableHeading(): ?HeadingItem { |
| 92 |
$heading = $this->getHeading(); |
| 93 |
while ( $heading instanceof HeadingItem && !$heading->isSubscribable() ) { |
| 94 |
$heading = $heading->getParent(); |
| 95 |
} |
| 96 |
return $heading instanceof HeadingItem ? $heading : null; |
| 97 |
} |
| 98 |
} |