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