Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
BoardHistoryQuery
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 getResults
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
90
 getHeaderResults
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getTopicListResults
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\UUID;
8use MWExceptionHandler;
9
10class BoardHistoryQuery extends HistoryQuery {
11    /**
12     * @param UUID $boardWorkflowId
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 $boardWorkflowId, $limit = 50, ?UUID $offset = null, $direction = 'fwd' ) {
19        $options = $this->getOptions( $direction, $limit, $offset );
20
21        $headerHistory = $this->getHeaderResults( $boardWorkflowId, $options ) ?: [];
22        $topicListHistory = $this->getTopicListResults( $boardWorkflowId, $options ) ?: [];
23        $topicSummaryHistory = $this->getTopicSummaryResults( $boardWorkflowId, $options ) ?: [];
24
25        $history = array_merge( $headerHistory, $topicListHistory, $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        // We can't use ORDER BY ... DESC in SQL, because that will give us the
33        // largest entries that are > some offset.  We want the smallest entries
34        // that are > that offset, but ordered in descending order.  Core solves
35        // this problem in IndexPager->getBody by iterating in descending order.
36        if ( $direction === 'rev' ) {
37            $history = array_reverse( $history );
38        }
39
40        // fetch any necessary metadata
41        $this->loadMetadataBatch( $history );
42        // build rows with the extra metadata
43        $results = [];
44        foreach ( $history as $revision ) {
45            try {
46                $result = $this->buildResult( $revision, 'rev_id' );
47            } catch ( FlowException $e ) {
48                $result = false;
49                MWExceptionHandler::logException( $e );
50            }
51            if ( $result ) {
52                $results[] = $result;
53            }
54        }
55
56        return $results;
57    }
58
59    protected function getHeaderResults( UUID $boardWorkflowId, $options ) {
60        return $this->storage->find(
61            'Header',
62            [
63                'rev_type_id' => $boardWorkflowId,
64            ],
65            $options
66        );
67    }
68
69    protected function getTopicListResults( UUID $boardWorkflowId, $options ) {
70        // Can't use 'PostRevision' because we need to get only the ones with the right board ID.
71        return $this->doInternalQueries(
72            'PostRevisionBoardHistoryEntry',
73            [
74                'topic_list_id' => $boardWorkflowId,
75            ],
76            $options,
77            self::POST_OVERFETCH_FACTOR
78        );
79    }
80
81    protected function getTopicSummaryResults( UUID $boardWorkflowId, $options ) {
82        return $this->storage->find(
83            'PostSummaryBoardHistoryEntry',
84            [
85                'topic_list_id' => $boardWorkflowId,
86            ],
87            $options
88        );
89    }
90}