Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
31.03% |
9 / 29 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| CommentItemTrait | |
31.03% |
9 / 29 |
|
20.00% |
1 / 5 |
59.23 | |
0.00% |
0 / 1 |
| getParent | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getAuthor | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getTimestamp | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| jsonSerialize | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| jsonSerializeForDiff | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getTimestampString | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| getHeading | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getSubscribableHeading | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
| 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 | // phpcs:disable Squiz.WhiteSpace, MediaWiki.Commenting |
| 13 | // Required ThreadItem methods (listed for Phan) |
| 14 | abstract public function getParent(): ?ThreadItem; |
| 15 | // Required CommentItem methods (listed for Phan) |
| 16 | abstract public function getAuthor(): string; |
| 17 | abstract public function getTimestamp(): DateTimeImmutable; |
| 18 | // phpcs:enable |
| 19 | |
| 20 | /** |
| 21 | * @inheritDoc |
| 22 | * @suppress PhanTraitParentReference |
| 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 | * @return array JSON-serializable array |
| 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 | * Get the comment timestamp in the format used in IDs and names. |
| 47 | * |
| 48 | * Depending on the date of the comment, this may use one of two formats: |
| 49 | * |
| 50 | * - For dates prior to 'DiscussionToolsTimestampFormatSwitchTime' (by default 2022-07-12): |
| 51 | * Uses ISO 8601 date. Almost DateTimeInterface::RFC3339_EXTENDED, but ending with 'Z' instead |
| 52 | * of '+00:00', like Date#toISOString in JavaScript. |
| 53 | * |
| 54 | * - For dates on or after 'DiscussionToolsTimestampFormatSwitchTime' (by default 2022-07-12): |
| 55 | * Uses MediaWiki timestamp (TS_MW in MediaWiki PHP code). |
| 56 | * |
| 57 | * @return string Comment timestamp in standard format |
| 58 | */ |
| 59 | public function getTimestampString(): string { |
| 60 | $config = MediaWikiServices::getInstance()->getMainConfig(); |
| 61 | $switchTime = new DateTimeImmutable( |
| 62 | $config->get( 'DiscussionToolsTimestampFormatSwitchTime' ), |
| 63 | // Oops, this probably should have been in UTC, but it's too late to change it now... |
| 64 | new DateTimeZone( $config->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 | * @return ContentHeadingItem Closest ancestor which is a HeadingItem |
| 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 | * @return ContentHeadingItem|null Closest heading that can be used for topic subscriptions |
| 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 | } |