Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
HistoryPager
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 6
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 doQuery
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
110
 setIncludeOffset
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatRow
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getQueryInfo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getIndexField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Flow\Data\Pager;
4
5use Flow\Exception\FlowException;
6use Flow\Exception\InvalidDataException;
7use Flow\Formatter\BoardHistoryQuery;
8use Flow\Formatter\FormatterRow;
9use Flow\Formatter\PostHistoryQuery;
10use Flow\Formatter\TopicHistoryQuery;
11use Flow\Model\UUID;
12use MediaWiki\MediaWikiServices;
13use MediaWiki\Pager\ReverseChronologicalPager;
14
15class HistoryPager extends ReverseChronologicalPager {
16    /**
17     * @var BoardHistoryQuery|TopicHistoryQuery|PostHistoryQuery
18     */
19    protected $query;
20
21    /**
22     * @var UUID
23     */
24    protected $id;
25
26    /**
27     * @var FormatterRow[]
28     */
29    public $mResult;
30
31    /**
32     * @param BoardHistoryQuery|TopicHistoryQuery|PostHistoryQuery $query
33     * @param UUID $id
34     */
35    public function __construct( /* BoardHistoryQuery|TopicHistoryQuery|PostHistoryQuery */ $query, UUID $id ) {
36        $this->query = $query;
37        $this->id = $id;
38
39        $this->mDefaultLimit = MediaWikiServices::getInstance()->getUserOptionsLookup()
40            ->getIntOption( $this->getUser(), 'rclimit' );
41        $this->mIsBackwards = $this->getRequest()->getVal( 'dir' ) == 'prev';
42    }
43
44    public function doQuery() {
45        $direction = $this->mIsBackwards ? 'rev' : 'fwd';
46
47        // over-fetch so we can figure out if there's anything after what we're showing
48        $this->mResult = $this->query->getResults( $this->id, $this->getLimit() + 1, UUID::create( $this->mOffset ), $direction );
49        if ( !$this->mResult ) {
50            throw new InvalidDataException(
51                'Unable to load history for ' . $this->id->getAlphadecimal(),
52                'fail-load-history'
53            );
54        }
55        $this->mQueryDone = true;
56
57        // we over-fetched, now get rid of redundant value for our "real" data
58        $overfetched = null;
59        if ( count( $this->mResult ) > $this->getLimit() ) {
60            // when traversing history reverse, the overfetched entry will be at
61            // the beginning of the list; in normal mode it'll be last
62            if ( $this->mIsBackwards ) {
63                $overfetched = array_shift( $this->mResult );
64            } else {
65                $overfetched = array_pop( $this->mResult );
66            }
67        }
68
69        // set some properties that'll be used to generate navigation bar
70        $this->mLastShown = [ end( $this->mResult )->revision->getRevisionId()->getAlphadecimal() ];
71        $this->mFirstShown = [ $this->mResult[0]->revision->getRevisionId()->getAlphadecimal() ];
72
73        /*
74         * By overfetching, we've already figured out if there's additional
75         * entries at the next page (according to the current direction). Now
76         * go fetch 1 more in the other direction (the one we likely came from,
77         * when navigating)
78         */
79        $nextOffset = $this->mIsBackwards ? $this->mFirstShown : $this->mLastShown;
80        $nextOffset = UUID::create( $nextOffset[0] );
81        $reverseDirection = $this->mIsBackwards ? 'fwd' : 'rev';
82        $this->mIsLast = !$overfetched;
83        $this->mIsFirst = !$this->mOffset ||
84            !$this->query->getResults( $this->id, 1, $nextOffset, $reverseDirection );
85
86        if ( $this->mIsBackwards ) {
87            // swap values if we're going backwards
88            [ $this->mIsFirst, $this->mIsLast ] = [ $this->mIsLast, $this->mIsFirst ];
89
90            // id of the overfetched entry, used to build new links starting at
91            // this offset
92            if ( $overfetched ) {
93                $this->mPastTheEndIndex = [ $overfetched->revision->getRevisionId()->getAlphadecimal() ];
94            }
95        }
96    }
97
98    /**
99     * Override pointless parent method.
100     *
101     * @param bool $include
102     * @throws FlowException
103     * @suppress PhanPluginNeverReturnMethod LSP/ISP violation.
104     */
105    public function setIncludeOffset( $include ) {
106        throw new FlowException( __METHOD__ . ' is not implemented.' );
107    }
108
109    // abstract functions required to extend ReverseChronologicalPager
110
111    /**
112     * @param array|\stdClass $row
113     * @throws FlowException
114     * @suppress PhanPluginNeverReturnMethod LSP/ISP violation.
115     */
116    public function formatRow( $row ) {
117        throw new FlowException( __METHOD__ . ' is not implemented.' );
118    }
119
120    public function getQueryInfo() {
121        return [];
122    }
123
124    public function getIndexField() {
125        return '';
126    }
127}