Debian Codegraph @

extensions/DiscussionTools/includes/ContentThreadItemSet.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\DiscussionTools;
4
5 use MediaWiki\Extension\DiscussionTools\ThreadItem\CommentItem;
6 use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem;
7 use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem;
8 use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem;
9 use MediaWiki\Extension\DiscussionTools\ThreadItem\HeadingItem;
10 use MediaWiki\Extension\DiscussionTools\ThreadItem\ThreadItem;
11 use Wikimedia\Assert\Assert;
12
13 /**
14 * Groups thread items (headings and comments) generated by parsing a discussion page.
15 */
16 class ContentThreadItemSet implements ThreadItemSet {
17
18 /** @var ContentThreadItem[] */
19 private array $threadItems = [];
20 /** @var ContentCommentItem[] */
21 private array $commentItems = [];
22 /** @var ContentThreadItem[][] */
23 private array $threadItemsByName = [];
24 /** @var ContentThreadItem[] */
25 private array $threadItemsById = [];
26 /** @var ContentHeadingItem[] */
27 private array $threads = [];
28
29 /**
30 * @inheritDoc
31 * @param ThreadItem $item
32 */
33 public function addThreadItem( ThreadItem $item ) {
34 Assert::precondition( $item instanceof ContentThreadItem, 'Must be ContentThreadItem' );
35
36 $this->threadItems[] = $item;
37 if ( $item instanceof CommentItem ) {
38 $this->commentItems[] = $item;
39 }
40 if ( $item instanceof HeadingItem ) {
41 $this->threads[] = $item;
42 }
43 }
44
45 /**
46 * @inheritDoc
47 */
48 public function isEmpty(): bool {
49 return !$this->threadItems;
50 }
51
52 /**
53 * @inheritDoc
54 * @param ThreadItem $item
55 */
56 public function updateIdAndNameMaps( ThreadItem $item ) {
57 Assert::precondition( $item instanceof ContentThreadItem, 'Must be ContentThreadItem' );
58
59 $this->threadItemsByName[ $item->getName() ][] = $item;
60
61 $this->threadItemsById[ $item->getId() ] = $item;
62 }
63
64 /**
65 * @inheritDoc
66 * @return ContentThreadItem[] Thread items
67 */
68 public function getThreadItems(): array {
69 return $this->threadItems;
70 }
71
72 /**
73 * @inheritDoc
74 * @return ContentCommentItem[] Comment items
75 */
76 public function getCommentItems(): array {
77 return $this->commentItems;
78 }
79
80 /**
81 * @inheritDoc
82 * @return ContentThreadItem[] Thread items, empty array if not found
83 */
84 public function findCommentsByName( string $name ): array {
85 return $this->threadItemsByName[$name] ?? [];
86 }
87
88 /**
89 * @inheritDoc
90 * @return ContentThreadItem|null Thread item, null if not found
91 */
92 public function findCommentById( string $id ): ?ThreadItem {
93 return $this->threadItemsById[$id] ?? null;
94 }
95
96 /**
97 * @inheritDoc
98 * @return ContentHeadingItem[] Tree structure of comments, top-level items are the headings.
99 */
100 public function getThreads(): array {
101 return $this->threads;
102 }
103
104 /**
105 * @inheritDoc
106 * @return ContentHeadingItem[] Tree structure of comments, top-level items are the headings.
107 */
108 public function getThreadsStructured(): array {
109 return array_values( array_filter( $this->getThreads(), static function ( ContentThreadItem $item ) {
110 return $item->getParent() === null;
111 } ) );
112 }
113 }