Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
DatabaseThreadItemSet
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 9
132
0.00% covered (danger)
0.00%
0 / 1
 addThreadItem
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 isEmpty
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 updateIdAndNameMaps
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getThreadItems
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCommentItems
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findCommentsByName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findCommentById
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getThreads
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getThreadsStructured
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\DiscussionTools;
4
5use MediaWiki\Extension\DiscussionTools\ThreadItem\CommentItem;
6use MediaWiki\Extension\DiscussionTools\ThreadItem\DatabaseCommentItem;
7use MediaWiki\Extension\DiscussionTools\ThreadItem\DatabaseHeadingItem;
8use MediaWiki\Extension\DiscussionTools\ThreadItem\DatabaseThreadItem;
9use MediaWiki\Extension\DiscussionTools\ThreadItem\HeadingItem;
10use MediaWiki\Extension\DiscussionTools\ThreadItem\ThreadItem;
11use Wikimedia\Assert\Assert;
12
13/**
14 * Groups thread items (headings and comments) generated from database.
15 */
16class DatabaseThreadItemSet implements ThreadItemSet {
17
18    /** @var DatabaseThreadItem[] */
19    private array $threadItems = [];
20    /** @var DatabaseCommentItem[] */
21    private array $commentItems = [];
22    /** @var DatabaseThreadItem[][] */
23    private array $threadItemsByName = [];
24    /** @var DatabaseThreadItem[] */
25    private array $threadItemsById = [];
26    /** @var DatabaseHeadingItem[] */
27    private array $threads = [];
28
29    /**
30     * @inheritDoc
31     * @param ThreadItem $item
32     */
33    public function addThreadItem( ThreadItem $item ) {
34        Assert::precondition( $item instanceof DatabaseThreadItem, 'Must be DatabaseThreadItem' );
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 DatabaseThreadItem, 'Must be DatabaseThreadItem' );
58
59        $this->threadItemsByName[ $item->getName() ][] = $item;
60
61        $this->threadItemsById[ $item->getId() ] = $item;
62    }
63
64    /**
65     * @inheritDoc
66     * @return DatabaseThreadItem[] Thread items
67     */
68    public function getThreadItems(): array {
69        return $this->threadItems;
70    }
71
72    /**
73     * @inheritDoc
74     * @return DatabaseCommentItem[] Comment items
75     */
76    public function getCommentItems(): array {
77        return $this->commentItems;
78    }
79
80    /**
81     * @inheritDoc
82     * @return DatabaseThreadItem[] 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 DatabaseThreadItem|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 DatabaseHeadingItem[] 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 DatabaseHeadingItem[] 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 ( DatabaseThreadItem $item ) {
110            return $item->getParent() === null;
111        } ) );
112    }
113}