Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
TopicHistoryQuery
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
210
0.00% covered (danger)
0.00%
0 / 1
 getResults
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
156
 getTopicPostResults
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getTopicSummaryResults
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Flow\Formatter;
4
5use Flow\Data\Utils\SortRevisionsByRevisionId;
6use Flow\Exception\FlowException;
7use Flow\Model\PostRevision;
8use Flow\Model\UUID;
9use MediaWiki\Exception\MWExceptionHandler;
10
11class TopicHistoryQuery extends HistoryQuery {
12    /**
13     * @param UUID $topicRootId
14     * @param int $limit
15     * @param UUID|null $offset
16     * @param string $direction 'rev' or 'fwd'
17     * @return FormatterRow[]
18     */
19    public function getResults( UUID $topicRootId, $limit = 50, ?UUID $offset = null, $direction = 'fwd' ) {
20        $options = $this->getOptions( $direction, $limit, $offset );
21
22        $topicPostHistory = $this->getTopicPostResults( $topicRootId, $options ) ?: [];
23        $topicSummaryHistory = $this->getTopicSummaryResults( $topicRootId, $options ) ?: [];
24
25        $history = array_merge( $topicPostHistory, $topicSummaryHistory );
26        usort( $history, new SortRevisionsByRevisionId( $options['order'] ) );
27
28        if ( isset( $options['limit'] ) ) {
29            $history = array_splice( $history, 0, $options['limit'] );
30        }
31
32        // See explanation in BoardHistoryQuery::getResults.
33        if ( $direction === 'rev' ) {
34            $history = array_reverse( $history );
35        }
36
37        $this->loadMetadataBatch( $history );
38        $results = $replies = [];
39        foreach ( $history as $revision ) {
40            try {
41                $results[] = $row = new TopicRow;
42                $this->buildResult( $revision, null, $row );
43                if ( $revision instanceof PostRevision ) {
44                    $replyToId = $revision->getReplyToId();
45                    if ( $replyToId ) {
46                        // $revisionId into the key rather than value prevents
47                        // duplicate insertion
48                        $replies[$replyToId->getAlphadecimal()][$revision->getPostId()->getAlphadecimal()] = true;
49                    }
50                }
51            } catch ( FlowException $e ) {
52                MWExceptionHandler::logException( $e );
53            }
54        }
55
56        foreach ( $results as $result ) {
57            if ( $result->revision instanceof PostRevision ) {
58                // @phan-suppress-next-line PhanUndeclaredMethod Type not correctly inferred
59                $alpha = $result->revision->getPostId()->getAlphadecimal();
60                $result->replies = isset( $replies[$alpha] ) ? array_keys( $replies[$alpha] ) : [];
61            }
62        }
63
64        return $results;
65    }
66
67    protected function getTopicPostResults( UUID $topicRootId, $options ) {
68        // Can't use 'PostRevision' because we need to get only the ones with the right topic ID.
69        return $this->doInternalQueries(
70            'PostRevisionTopicHistoryEntry',
71            [
72                'topic_root_id' => $topicRootId,
73            ],
74            $options,
75            self::POST_OVERFETCH_FACTOR
76        );
77    }
78
79    protected function getTopicSummaryResults( UUID $topicRootId, $options ) {
80        return $this->storage->find(
81            'PostSummary',
82            [
83                'rev_type_id' => $topicRootId,
84            ],
85            $options
86        );
87    }
88}