Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 81
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
LqtDiscussionPager
0.00% covered (danger)
0.00%
0 / 81
0.00% covered (danger)
0.00%
0 / 10
930
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getPageLimit
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 getQueryInfo
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 getRows
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
 formatRow
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 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 getDefaultDirections
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 getNavigationBar
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
30
 getModuleStyles
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getNavClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3use MediaWiki\Pager\IndexPager;
4
5class LqtDiscussionPager extends IndexPager {
6
7    /** @var Article|false */
8    protected $article;
9
10    /** @var string|false */
11    protected $orderType;
12
13    public function __construct( $article, $orderType ) {
14        $this->article = $article;
15        $this->orderType = $orderType;
16
17        parent::__construct();
18
19        $this->setLimit( min( 50, $this->getPageLimit() ) );
20    }
21
22    public function getPageLimit() {
23        $article = $this->article;
24
25        $requestedLimit = $this->getRequest()->getIntOrNull( 'limit' );
26        if ( $requestedLimit ) {
27            return $requestedLimit;
28        }
29
30        if ( $article->getPage()->exists() ) {
31            $pout = $article->getParserOutput();
32            $setLimit = $pout->getPageProperty( 'lqt-page-limit' );
33
34            if ( $setLimit ) {
35                return $setLimit;
36            }
37        }
38
39        return $this->getConfig()->get( 'LiquidThreadsDefaultPageLimit' );
40    }
41
42    public function getQueryInfo() {
43        $queryInfo = [
44            'tables' => [ 'thread' ],
45            'fields' => '*',
46            'conds' => [
47                Threads::articleClause( $this->article->getPage() ),
48                Threads::topLevelClause(),
49                $this->mDb->expr( 'thread_type', '!=', Threads::TYPE_DELETED ),
50            ],
51        ];
52
53        return $queryInfo;
54    }
55
56    public function getRows() {
57        if ( !$this->mQueryDone ) {
58            $this->doQuery();
59        }
60
61        # Don't use any extra rows returned by the query
62        $numRows = min( $this->mResult->numRows(), $this->mLimit );
63
64        $rows = [];
65
66        if ( $numRows ) {
67            if ( $this->mIsBackwards ) {
68                for ( $i = $numRows - 1; $i >= 0; $i-- ) {
69                    $this->mResult->seek( $i );
70                    $row = $this->mResult->fetchObject();
71                    $rows[] = $row;
72                }
73            } else {
74                $this->mResult->seek( 0 );
75                for ( $i = 0; $i < $numRows; $i++ ) {
76                    $row = $this->mResult->fetchObject();
77                    $rows[] = $row;
78                }
79            }
80        }
81
82        return $rows;
83    }
84
85    public function formatRow( $row ) {
86        // No-op, we get the list of rows from getRows()
87        // Return a string to make the function signature happy
88        return '';
89    }
90
91    public function getIndexField() {
92        switch ( $this->orderType ) {
93            case TalkpageView::LQT_NEWEST_CHANGES:
94                return 'thread_sortkey';
95            case TalkpageView::LQT_OLDEST_THREADS:
96            case TalkpageView::LQT_NEWEST_THREADS:
97                return 'thread_created';
98            default:
99                throw new LogicException( "Unknown sort order " . $this->orderType );
100        }
101    }
102
103    public function getDefaultDirections() {
104        switch ( $this->orderType ) {
105            case TalkpageView::LQT_NEWEST_CHANGES:
106            case TalkpageView::LQT_NEWEST_THREADS:
107                return true; // Descending
108            case TalkpageView::LQT_OLDEST_THREADS:
109                return false; // Ascending
110            default:
111                throw new LogicException( "Unknown sort order " . $this->orderType );
112        }
113    }
114
115    /**
116     * A navigation bar with images
117     * Stolen from TablePager because it's pretty.
118     * @return string
119     */
120    public function getNavigationBar() {
121        if ( !$this->isNavigationBarShown() ) {
122            return '';
123        }
124
125        $this->getOutput()->enableOOUI();
126
127        $types = [ 'first', 'prev', 'next', 'last' ];
128
129        $queries = $this->getPagingQueries();
130
131        $buttons = [];
132
133        $title = $this->getTitle();
134
135        foreach ( $types as $type ) {
136            $buttons[] = new \OOUI\ButtonWidget( [
137                // Messages used here:
138                // * table_pager_first
139                // * table_pager_prev
140                // * table_pager_next
141                // * table_pager_last
142                'classes' => [ 'TablePager-button-' . $type ],
143                'flags' => [ 'progressive' ],
144                'framed' => false,
145                'label' => $this->msg( 'table_pager_' . $type )->text(),
146                'href' => $queries[ $type ] ?
147                    $title->getLinkURL( $queries[ $type ] + $this->getDefaultQuery() ) :
148                    null,
149                'icon' => $type === 'prev' ? 'previous' : $type,
150                'disabled' => $queries[ $type ] === false
151            ] );
152        }
153        return new \OOUI\ButtonGroupWidget( [
154            'classes' => [ $this->getNavClass() ],
155            'items' => $buttons,
156        ] );
157    }
158
159    /**
160     * @inheritDoc
161     */
162    public function getModuleStyles() {
163        return array_merge(
164            parent::getModuleStyles(), [ 'oojs-ui.styles.icons-movement' ]
165        );
166    }
167
168    public function getNavClass() {
169        return 'TablePager_nav';
170    }
171}