4 * Basic pager interface.
7 function getNavigationBar();
12 * IndexPager is an efficient pager which uses a (roughly unique) index in the
13 * data set to implement paging, rather than a "LIMIT offset,limit" clause.
14 * In MySQL, such a limit/offset clause requires counting through the specified number
15 * of offset rows to find the desired data, which can be expensive for large offsets.
17 * ReverseChronologicalPager is a child class of the abstract IndexPager, and contains
18 * some formatting and display code which is specific to the use of timestamps as
19 * indexes. It is currently the only such child class. Here is a synopsis of the operation
20 * of IndexPager and its primary subclass:
22 * * The query is specified by the offset, limit and direction (dir) parameters, in
23 * addition to any subclass-specific parameters.
25 * * The offset is the non-inclusive start of the DB query. A row with an index value
26 * equal to the offset will never be shown.
28 * * The query may either be done backwards, where the rows are returned by the database
29 * in the opposite order to which they are displayed to the user, or forwards. This is
30 * specified by the "dir" parameter, dir=prev means backwards, anything else means
31 * forwards. The offset value specifies the start of the database result set, which
32 * may be either the start or end of the displayed data set. This allows "previous"
33 * links to be implemented without knowledge of the index value at the start of the
36 * * An additional row beyond the user-specified limit is always requested. This allows
37 * us to tell whether we should display a "next" link in the case of forwards mode,
38 * or a "previous" link in the case of backwards mode. Determining whether to
39 * display the other link (the one for the page before the start of the database
40 * result set) can be done heuristically by examining the offset.
42 * * An empty offset indicates that the offset condition should be omitted from the query.
43 * This naturally produces either the first page or the last page depending on the
46 * Subclassing the pager to implement concrete functionality should be fairly simple,
47 * please see the examples in PageHistory.php and SpecialIpblocklist.php. You just need
48 * to override formatRow(), getQueryInfo() and getIndexField(). Don't forget to call the
49 * parent constructor if you override it.
51 abstract class IndexPager
implements Pager
{
53 public $mLimitsShown = array( 20, 50, 100, 250, 500 );
54 public $mDefaultLimit = 50;
55 public $mOffset, $mLimit;
56 public $mQueryDone = false;
58 public $mPastTheEndRow;
60 protected $mIndexField;
63 * Default query direction. false for ascending, true for descending
65 public $mDefaultDirection = false;
68 * Result object for the query. Warning: seek before use.
72 function __construct() {
74 $this->mRequest
= $wgRequest;
76 # NB: the offset is quoted, not validated. It is treated as an arbitrary string
77 # to support the widest variety of index types. Be careful outputting it into
79 $this->mOffset
= $this->mRequest
->getText( 'offset' );
80 $this->mLimit
= $this->mRequest
->getInt( 'limit', $this->mDefaultLimit
);
81 if ( $this->mLimit
<= 0 ) {
82 $this->mLimit
= $this->mDefaultLimit
;
84 $this->mIsBackwards
= ( $this->mRequest
->getVal( 'dir' ) == 'prev' );
85 $this->mIndexField
= $this->getIndexField();
86 $this->mDb
= wfGetDB( DB_SLAVE
);
90 * Do the query, using information from the object context. This function
91 * has been kept minimal to make it overridable if necessary, to allow for
92 * result sets formed from multiple DB queries.
95 # Use the child class name for profiling
96 $fname = __METHOD__
. ' (' . get_class( $this ) . ')';
97 wfProfileIn( $fname );
99 $descending = ( $this->mIsBackwards
== $this->mDefaultDirection
);
100 # Plus an extra row so that we can tell the "next" link should be shown
101 $queryLimit = $this->mLimit +
1;
103 $this->mResult
= $this->reallyDoQuery( $this->mOffset
, $queryLimit, $descending );
104 $this->extractResultInfo( $this->mOffset
, $queryLimit, $this->mResult
);
105 $this->mQueryDone
= true;
107 wfProfileOut( $fname );
111 * Extract some useful data from the result object for use by
112 * the navigation bar, put it into $this
114 function extractResultInfo( $offset, $limit, ResultWrapper
$res ) {
115 $numRows = $res->numRows();
117 $row = $res->fetchRow();
118 $firstIndex = $row[$this->mIndexField
];
120 # Discard the extra result row if there is one
121 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
122 $res->seek( $numRows - 1 );
123 $this->mPastTheEndRow
= $res->fetchObject();
124 $indexField = $this->mIndexField
;
125 $this->mPastTheEndIndex
= $this->mPastTheEndRow
->$indexField;
126 $res->seek( $numRows - 2 );
127 $row = $res->fetchRow();
128 $lastIndex = $row[$this->mIndexField
];
130 $this->mPastTheEndRow
= null;
131 # Setting indexes to an empty string means that they will be omitted
132 # if they would otherwise appear in URLs. It just so happens that this
133 # is the right thing to do in the standard UI, in all the relevant cases.
134 $this->mPastTheEndIndex
= '';
135 $res->seek( $numRows - 1 );
136 $row = $res->fetchRow();
137 $lastIndex = $row[$this->mIndexField
];
142 $this->mPastTheEndRow
= null;
143 $this->mPastTheEndIndex
= '';
146 if ( $this->mIsBackwards
) {
147 $this->mIsFirst
= ( $numRows < $limit );
148 $this->mIsLast
= ( $offset == '' );
149 $this->mLastShown
= $firstIndex;
150 $this->mFirstShown
= $lastIndex;
152 $this->mIsFirst
= ( $offset == '' );
153 $this->mIsLast
= ( $numRows < $limit );
154 $this->mLastShown
= $lastIndex;
155 $this->mFirstShown
= $firstIndex;
160 * Do a query with specified parameters, rather than using the object context
162 * @param string $offset Index offset, inclusive
163 * @param integer $limit Exact query limit
164 * @param boolean $descending Query direction, false for ascending, true for descending
165 * @return ResultWrapper
167 function reallyDoQuery( $offset, $limit, $ascending ) {
168 $fname = __METHOD__
. ' (' . get_class( $this ) . ')';
169 $info = $this->getQueryInfo();
170 $tables = $info['tables'];
171 $fields = $info['fields'];
172 $conds = isset( $info['conds'] ) ?
$info['conds'] : array();
173 $options = isset( $info['options'] ) ?
$info['options'] : array();
175 $options['ORDER BY'] = $this->mIndexField
;
178 $options['ORDER BY'] = $this->mIndexField
. ' DESC';
181 if ( $offset != '' ) {
182 $conds[] = $this->mIndexField
. $operator . $this->mDb
->addQuotes( $offset );
184 $options['LIMIT'] = intval( $limit );
185 $res = $this->mDb
->select( $tables, $fields, $conds, $fname, $options );
186 return new ResultWrapper( $this->mDb
, $res );
190 * Get the formatted result list. Calls getStartBody(), formatRow() and
191 * getEndBody(), concatenates the results and returns them.
194 if ( !$this->mQueryDone
) {
197 # Don't use any extra rows returned by the query
198 $numRows = min( $this->mResult
->numRows(), $this->mLimit
);
200 $s = $this->getStartBody();
202 if ( $this->mIsBackwards
) {
203 for ( $i = $numRows - 1; $i >= 0; $i-- ) {
204 $this->mResult
->seek( $i );
205 $row = $this->mResult
->fetchObject();
206 $s .= $this->formatRow( $row );
209 $this->mResult
->seek( 0 );
210 for ( $i = 0; $i < $numRows; $i++
) {
211 $row = $this->mResult
->fetchObject();
212 $s .= $this->formatRow( $row );
216 $s .= $this->getEndBody();
223 function makeLink($text, $query = NULL) {
224 if ( $query === null ) {
227 return $this->getSkin()->makeKnownLinkObj( $this->getTitle(), $text,
228 wfArrayToCGI( $query, $this->getDefaultQuery() ) );
233 * Hook into getBody(), allows text to be inserted at the start. This
234 * will be called even if there are no rows in the result set.
236 function getStartBody() {
241 * Hook into getBody() for the end of the list
243 function getEndBody() {
248 * Title used for self-links. Override this if you want to be able to
249 * use a title other than $wgTitle
251 function getTitle() {
252 return $GLOBALS['wgTitle'];
256 * Get the current skin. This can be overridden if necessary.
259 if ( !isset( $this->mSkin
) ) {
261 $this->mSkin
= $wgUser->getSkin();
267 * Get an array of query parameters that should be put into self-links.
268 * By default, all parameters passed in the URL are used, except for a
271 function getDefaultQuery() {
272 if ( !isset( $this->mDefaultQuery
) ) {
273 $this->mDefaultQuery
= $_GET;
274 unset( $this->mDefaultQuery
['title'] );
275 unset( $this->mDefaultQuery
['dir'] );
276 unset( $this->mDefaultQuery
['offset'] );
277 unset( $this->mDefaultQuery
['limit'] );
279 return $this->mDefaultQuery
;
283 * Get the number of rows in the result set
285 function getNumRows() {
286 if ( !$this->mQueryDone
) {
289 return $this->mResult
->numRows();
293 * Abstract formatting function. This should return an HTML string
294 * representing the result row $row. Rows will be concatenated and
295 * returned by getBody()
297 abstract function formatRow( $row );
300 * This function should be overridden to provide all parameters
301 * needed for the main paged query. It returns an associative
302 * array with the following elements:
303 * tables => Table(s) for passing to Database::select()
304 * fields => Field(s) for passing to Database::select(), may be *
305 * conds => WHERE conditions
306 * options => option array
308 abstract function getQueryInfo();
311 * This function should be overridden to return the name of the
314 abstract function getIndexField();
318 * IndexPager with a formatted navigation bar
320 abstract class ReverseChronologicalPager
extends IndexPager
{
321 public $mDefaultDirection = true;
323 function __construct() {
324 parent
::__construct();
327 function getNavigationBar() {
330 if ( isset( $this->mNavigationBar
) ) {
331 return $this->mNavigationBar
;
333 if ( !$this->mQueryDone
) {
337 # Don't announce the limit everywhere if it's the default
338 $urlLimit = $this->mLimit
== $this->mDefaultLimit ?
'' : $this->mLimit
;
340 if ( $this->mIsFirst
) {
341 $prevText = wfMsgHtml("prevn", $this->mLimit
);
342 $latestText = wfMsgHtml('histlast');
344 $prevText = $this->makeLink( wfMsgHtml("prevn", $this->mLimit
),
345 array( 'dir' => 'prev', 'offset' => $this->mFirstShown
, 'limit' => $urlLimit ) );
346 $latestText = $this->makeLink( wfMsgHtml('histlast'),
347 array( 'limit' => $urlLimit ) );
349 if ( $this->mIsLast
) {
350 $nextText = wfMsgHtml( 'nextn', $this->mLimit
);
351 $earliestText = wfMsgHtml( 'histfirst' );
353 $nextText = $this->makeLink( wfMsgHtml( 'nextn', $this->mLimit
),
354 array( 'offset' => $this->mLastShown
, 'limit' => $urlLimit ) );
355 $earliestText = $this->makeLink( wfMsgHtml( 'histfirst' ),
356 array( 'dir' => 'prev', 'limit' => $urlLimit ) );
359 foreach ( $this->mLimitsShown
as $limit ) {
360 if ( $limits !== '' ) {
363 if ( $this->mIsBackwards
) {
364 $offset = $this->mPastTheEndIndex
;
366 $offset = $this->mOffset
;
368 $limits .= $this->makeLink( $wgLang->formatNum($limit),
369 array('offset' => $offset, 'limit' => $limit ) );
373 $this->mNavigationBar
= "($latestText | $earliestText) " . wfMsgHtml("viewprevnext", $prevText, $nextText, $limits);
374 return $this->mNavigationBar
;