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