3 * Efficient paging for SQL queries.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use Wikimedia\Rdbms\IResultWrapper
;
25 use Wikimedia\Rdbms\IDatabase
;
28 * IndexPager is an efficient pager which uses a (roughly unique) index in the
29 * data set to implement paging, rather than a "LIMIT offset,limit" clause.
30 * In MySQL, such a limit/offset clause requires counting through the
31 * specified number of offset rows to find the desired data, which can be
32 * expensive for large offsets.
34 * ReverseChronologicalPager is a child class of the abstract IndexPager, and
35 * contains some formatting and display code which is specific to the use of
36 * timestamps as indexes. Here is a synopsis of its operation:
38 * * The query is specified by the offset, limit and direction (dir)
39 * parameters, in addition to any subclass-specific parameters.
40 * * The offset is the non-inclusive start of the DB query. A row with an
41 * index value equal to the offset will never be shown.
42 * * The query may either be done backwards, where the rows are returned by
43 * the database in the opposite order to which they are displayed to the
44 * user, or forwards. This is specified by the "dir" parameter, dir=prev
45 * means backwards, anything else means forwards. The offset value
46 * specifies the start of the database result set, which may be either
47 * the start or end of the displayed data set. This allows "previous"
48 * links to be implemented without knowledge of the index value at the
49 * start of the previous page.
50 * * An additional row beyond the user-specified limit is always requested.
51 * This allows us to tell whether we should display a "next" link in the
52 * case of forwards mode, or a "previous" link in the case of backwards
53 * mode. Determining whether to display the other link (the one for the
54 * page before the start of the database result set) can be done
55 * heuristically by examining the offset.
57 * * An empty offset indicates that the offset condition should be omitted
58 * from the query. This naturally produces either the first page or the
59 * last page depending on the dir parameter.
61 * Subclassing the pager to implement concrete functionality should be fairly
62 * simple, please see the examples in HistoryAction.php and
63 * SpecialBlockList.php. You just need to override formatRow(),
64 * getQueryInfo() and getIndexField(). Don't forget to call the parent
65 * constructor if you override it.
69 abstract class IndexPager
extends ContextSource
implements Pager
{
71 * Constants for the $mDefaultDirection field.
73 * These are boolean for historical reasons and should stay boolean for backwards-compatibility.
75 const DIR_ASCENDING
= false;
76 const DIR_DESCENDING
= true;
78 /** @var WebRequest */
80 /** @var int[] List of default entry limit options to be presented to clients */
81 public $mLimitsShown = [ 20, 50, 100, 250, 500 ];
82 /** @var int The default entry limit choosen for clients */
83 public $mDefaultLimit = 50;
84 /** @var mixed The starting point to enumerate entries */
86 /** @var int The maximum number of entries to show */
88 /** @var bool Whether the listing query completed */
89 public $mQueryDone = false;
92 /** @var stdClass|bool|null Extra row fetched at the end to see if the end was reached */
93 public $mPastTheEndRow;
96 * The index to actually be used for ordering. This is a single column,
97 * for one ordering, even if multiple orderings are supported.
100 protected $mIndexField;
102 * An array of secondary columns to order by. These fields are not part of the offset.
103 * This is a column list for one ordering, even if multiple orderings are supported.
106 protected $mExtraSortFields;
107 /** For pages that support multiple types of ordering, which one to use.
110 protected $mOrderType;
112 * $mDefaultDirection gives the direction to use when sorting results:
113 * DIR_ASCENDING or DIR_DESCENDING. If $mIsBackwards is set, we start from
114 * the opposite end, but we still sort the page itself according to
115 * $mDefaultDirection. For example, if $mDefaultDirection is DIR_ASCENDING
116 * but we're going backwards, we'll display the last page of results, but
117 * the last result will be at the bottom, not the top.
119 * Like $mIndexField, $mDefaultDirection will be a single value even if the
120 * class supports multiple default directions for different order types.
123 public $mDefaultDirection;
125 public $mIsBackwards;
127 /** @var bool True if the current result set is the first one */
133 protected $mLastShown;
135 protected $mFirstShown;
137 protected $mPastTheEndIndex;
139 protected $mDefaultQuery;
141 protected $mNavigationBar;
144 * Whether to include the offset in the query
147 protected $mIncludeOffset = false;
150 * Result object for the query. Warning: seek before use.
152 * @var IResultWrapper
156 public function __construct( IContextSource
$context = null ) {
158 $this->setContext( $context );
161 $this->mRequest
= $this->getRequest();
163 # NB: the offset is quoted, not validated. It is treated as an
164 # arbitrary string to support the widest variety of index types. Be
165 # careful outputting it into HTML!
166 $this->mOffset
= $this->mRequest
->getText( 'offset' );
168 # Use consistent behavior for the limit options
169 $this->mDefaultLimit
= $this->getUser()->getIntOption( 'rclimit' );
170 if ( !$this->mLimit
) {
171 // Don't override if a subclass calls $this->setLimit() in its constructor.
172 list( $this->mLimit
, /* $offset */ ) = $this->mRequest
->getLimitOffset();
175 $this->mIsBackwards
= ( $this->mRequest
->getVal( 'dir' ) == 'prev' );
176 # Let the subclass set the DB here; otherwise use a replica DB for the current wiki
177 $this->mDb
= $this->mDb ?
: wfGetDB( DB_REPLICA
);
179 $index = $this->getIndexField(); // column to sort on
180 $extraSort = $this->getExtraSortFields(); // extra columns to sort on for query planning
181 $order = $this->mRequest
->getVal( 'order' );
182 if ( is_array( $index ) && isset( $index[$order] ) ) {
183 $this->mOrderType
= $order;
184 $this->mIndexField
= $index[$order];
185 $this->mExtraSortFields
= isset( $extraSort[$order] )
186 ?
(array)$extraSort[$order]
188 } elseif ( is_array( $index ) ) {
189 # First element is the default
190 $this->mIndexField
= reset( $index );
191 $this->mOrderType
= key( $index );
192 $this->mExtraSortFields
= isset( $extraSort[$this->mOrderType
] )
193 ?
(array)$extraSort[$this->mOrderType
]
196 # $index is not an array
197 $this->mOrderType
= null;
198 $this->mIndexField
= $index;
199 $this->mExtraSortFields
= (array)$extraSort;
202 if ( !isset( $this->mDefaultDirection
) ) {
203 $dir = $this->getDefaultDirections();
204 $this->mDefaultDirection
= is_array( $dir )
205 ?
$dir[$this->mOrderType
]
211 * Get the Database object in use
215 public function getDatabase() {
220 * Do the query, using information from the object context. This function
221 * has been kept minimal to make it overridable if necessary, to allow for
222 * result sets formed from multiple DB queries.
224 public function doQuery() {
225 # Use the child class name for profiling
226 $fname = __METHOD__
. ' (' . static::class . ')';
227 $section = Profiler
::instance()->scopedProfileIn( $fname );
229 $descending = $this->mIsBackwards
230 ?
( $this->mDefaultDirection
=== self
::DIR_DESCENDING
)
231 : ( $this->mDefaultDirection
=== self
::DIR_ASCENDING
);
233 # Plus an extra row so that we can tell the "next" link should be shown
234 $queryLimit = $this->mLimit +
1;
236 if ( $this->mOffset
== '' ) {
239 // If there's an offset, we may or may not be at the first entry.
240 // The only way to tell is to run the query in the opposite
241 // direction see if we get a row.
242 $oldIncludeOffset = $this->mIncludeOffset
;
243 $this->mIncludeOffset
= !$this->mIncludeOffset
;
244 $isFirst = !$this->reallyDoQuery( $this->mOffset
, 1, !$descending )->numRows();
245 $this->mIncludeOffset
= $oldIncludeOffset;
248 $this->mResult
= $this->reallyDoQuery(
254 $this->extractResultInfo( $isFirst, $queryLimit, $this->mResult
);
255 $this->mQueryDone
= true;
257 $this->preprocessResults( $this->mResult
);
258 $this->mResult
->rewind(); // Paranoia
262 * @return IResultWrapper The result wrapper.
264 function getResult() {
265 return $this->mResult
;
269 * Set the offset from an other source than the request
271 * @param int|string $offset
273 function setOffset( $offset ) {
274 $this->mOffset
= $offset;
278 * Set the limit from an other source than the request
280 * Verifies limit is between 1 and 5000
282 * @param int|string $limit
284 function setLimit( $limit ) {
285 $limit = (int)$limit;
286 // WebRequest::getLimitOffset() puts a cap of 5000, so do same here.
287 if ( $limit > 5000 ) {
291 $this->mLimit
= $limit;
296 * Get the current limit
300 function getLimit() {
301 return $this->mLimit
;
305 * Set whether a row matching exactly the offset should be also included
306 * in the result or not. By default this is not the case, but when the
307 * offset is user-supplied this might be wanted.
309 * @param bool $include
311 public function setIncludeOffset( $include ) {
312 $this->mIncludeOffset
= $include;
316 * Extract some useful data from the result object for use by
317 * the navigation bar, put it into $this
319 * @param bool $isFirst False if there are rows before those fetched (i.e.
320 * if a "previous" link would make sense)
321 * @param int $limit Exact query limit
322 * @param IResultWrapper $res
324 function extractResultInfo( $isFirst, $limit, IResultWrapper
$res ) {
325 $numRows = $res->numRows();
327 # Remove any table prefix from index field
328 $parts = explode( '.', $this->mIndexField
);
329 $indexColumn = end( $parts );
331 $row = $res->fetchRow();
332 $firstIndex = $row[$indexColumn];
334 # Discard the extra result row if there is one
335 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
336 $res->seek( $numRows - 1 );
337 $this->mPastTheEndRow
= $res->fetchObject();
338 $this->mPastTheEndIndex
= $this->mPastTheEndRow
->$indexColumn;
339 $res->seek( $numRows - 2 );
340 $row = $res->fetchRow();
341 $lastIndex = $row[$indexColumn];
343 $this->mPastTheEndRow
= null;
344 # Setting indexes to an empty string means that they will be
345 # omitted if they would otherwise appear in URLs. It just so
346 # happens that this is the right thing to do in the standard
347 # UI, in all the relevant cases.
348 $this->mPastTheEndIndex
= '';
349 $res->seek( $numRows - 1 );
350 $row = $res->fetchRow();
351 $lastIndex = $row[$indexColumn];
356 $this->mPastTheEndRow
= null;
357 $this->mPastTheEndIndex
= '';
360 if ( $this->mIsBackwards
) {
361 $this->mIsFirst
= ( $numRows < $limit );
362 $this->mIsLast
= $isFirst;
363 $this->mLastShown
= $firstIndex;
364 $this->mFirstShown
= $lastIndex;
366 $this->mIsFirst
= $isFirst;
367 $this->mIsLast
= ( $numRows < $limit );
368 $this->mLastShown
= $lastIndex;
369 $this->mFirstShown
= $firstIndex;
374 * Get some text to go in brackets in the "function name" part of the SQL comment
378 function getSqlComment() {
379 return static::class;
383 * Do a query with specified parameters, rather than using the object
386 * @param string $offset Index offset, inclusive
387 * @param int $limit Exact query limit
388 * @param bool $descending Query direction, false for ascending, true for descending
389 * @return IResultWrapper
391 public function reallyDoQuery( $offset, $limit, $descending ) {
392 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
393 $this->buildQueryInfo( $offset, $limit, $descending );
395 return $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
399 * Build variables to use by the database wrapper.
401 * @param string $offset Index offset, inclusive
402 * @param int $limit Exact query limit
403 * @param bool $descending Query direction, false for ascending, true for descending
406 protected function buildQueryInfo( $offset, $limit, $descending ) {
407 $fname = __METHOD__
. ' (' . $this->getSqlComment() . ')';
408 $info = $this->getQueryInfo();
409 $tables = $info['tables'];
410 $fields = $info['fields'];
411 $conds = $info['conds'] ??
[];
412 $options = $info['options'] ??
[];
413 $join_conds = $info['join_conds'] ??
[];
414 $sortColumns = array_merge( [ $this->mIndexField
], $this->mExtraSortFields
);
416 $options['ORDER BY'] = $sortColumns;
417 $operator = $this->mIncludeOffset ?
'>=' : '>';
420 foreach ( $sortColumns as $col ) {
421 $orderBy[] = $col . ' DESC';
423 $options['ORDER BY'] = $orderBy;
424 $operator = $this->mIncludeOffset ?
'<=' : '<';
426 if ( $offset != '' ) {
427 $conds[] = $this->mIndexField
. $operator . $this->mDb
->addQuotes( $offset );
429 $options['LIMIT'] = intval( $limit );
430 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
434 * Pre-process results; useful for performing batch existence checks, etc.
436 * @param IResultWrapper $result
438 protected function preprocessResults( $result ) {
442 * Get the formatted result list. Calls getStartBody(), formatRow() and
443 * getEndBody(), concatenates the results and returns them.
447 public function getBody() {
448 if ( !$this->mQueryDone
) {
452 if ( $this->mResult
->numRows() ) {
453 # Do any special query batches before display
454 $this->doBatchLookups();
457 # Don't use any extra rows returned by the query
458 $numRows = min( $this->mResult
->numRows(), $this->mLimit
);
460 $s = $this->getStartBody();
462 if ( $this->mIsBackwards
) {
463 for ( $i = $numRows - 1; $i >= 0; $i-- ) {
464 $this->mResult
->seek( $i );
465 $row = $this->mResult
->fetchObject();
466 $s .= $this->formatRow( $row );
469 $this->mResult
->seek( 0 );
470 for ( $i = 0; $i < $numRows; $i++
) {
471 $row = $this->mResult
->fetchObject();
472 $s .= $this->formatRow( $row );
476 $s .= $this->getEmptyBody();
478 $s .= $this->getEndBody();
485 * @param string $text Text displayed on the link
486 * @param array|null $query Associative array of parameter to be in the query string
487 * @param string|null $type Link type used to create additional attributes, like "rel", "class" or
488 * "title". Valid values (non-exhaustive list): 'first', 'last', 'prev', 'next', 'asc', 'desc'.
489 * @return string HTML fragment
491 function makeLink( $text, array $query = null, $type = null ) {
492 if ( $query === null ) {
497 if ( in_array( $type, [ 'prev', 'next' ] ) ) {
498 $attrs['rel'] = $type;
501 if ( in_array( $type, [ 'asc', 'desc' ] ) ) {
502 $attrs['title'] = $this->msg( $type == 'asc' ?
'sort-ascending' : 'sort-descending' )->text();
506 $attrs['class'] = "mw-{$type}link";
509 return Linker
::linkKnown(
513 $query +
$this->getDefaultQuery()
518 * Called from getBody(), before getStartBody() is called and
519 * after doQuery() was called. This will be called only if there
520 * are rows in the result set.
524 protected function doBatchLookups() {
528 * Hook into getBody(), allows text to be inserted at the start. This
529 * will be called even if there are no rows in the result set.
533 protected function getStartBody() {
538 * Hook into getBody() for the end of the list
542 protected function getEndBody() {
547 * Hook into getBody(), for the bit between the start and the
548 * end when there are no rows
552 protected function getEmptyBody() {
557 * Get an array of query parameters that should be put into self-links.
558 * By default, all parameters passed in the URL are used, except for a
561 * @return array Associative array
563 function getDefaultQuery() {
564 if ( !isset( $this->mDefaultQuery
) ) {
565 $this->mDefaultQuery
= $this->getRequest()->getQueryValues();
566 unset( $this->mDefaultQuery
['title'] );
567 unset( $this->mDefaultQuery
['dir'] );
568 unset( $this->mDefaultQuery
['offset'] );
569 unset( $this->mDefaultQuery
['limit'] );
570 unset( $this->mDefaultQuery
['order'] );
571 unset( $this->mDefaultQuery
['month'] );
572 unset( $this->mDefaultQuery
['year'] );
574 return $this->mDefaultQuery
;
578 * Get the number of rows in the result set
582 function getNumRows() {
583 if ( !$this->mQueryDone
) {
586 return $this->mResult
->numRows();
590 * Get a URL query array for the prev, next, first and last links.
594 function getPagingQueries() {
595 if ( !$this->mQueryDone
) {
599 # Don't announce the limit everywhere if it's the default
600 $urlLimit = $this->mLimit
== $this->mDefaultLimit ?
null : $this->mLimit
;
602 if ( $this->mIsFirst
) {
608 'offset' => $this->mFirstShown
,
611 $first = [ 'limit' => $urlLimit ];
613 if ( $this->mIsLast
) {
617 $next = [ 'offset' => $this->mLastShown
, 'limit' => $urlLimit ];
618 $last = [ 'dir' => 'prev', 'limit' => $urlLimit ];
629 * Returns whether to show the "navigation bar"
633 function isNavigationBarShown() {
634 if ( !$this->mQueryDone
) {
637 // Hide navigation by default if there is nothing to page
638 return !( $this->mIsFirst
&& $this->mIsLast
);
642 * Get paging links. If a link is disabled, the item from $disabledTexts
643 * will be used. If there is no such item, the unlinked text from
644 * $linkTexts will be used. Both $linkTexts and $disabledTexts are arrays
647 * @param array $linkTexts
648 * @param array $disabledTexts
651 function getPagingLinks( $linkTexts, $disabledTexts = [] ) {
652 $queries = $this->getPagingQueries();
655 foreach ( $queries as $type => $query ) {
656 if ( $query !== false ) {
657 $links[$type] = $this->makeLink(
662 } elseif ( isset( $disabledTexts[$type] ) ) {
663 $links[$type] = $disabledTexts[$type];
665 $links[$type] = $linkTexts[$type];
672 function getLimitLinks() {
674 if ( $this->mIsBackwards
) {
675 $offset = $this->mPastTheEndIndex
;
677 $offset = $this->mOffset
;
679 foreach ( $this->mLimitsShown
as $limit ) {
680 $links[] = $this->makeLink(
681 $this->getLanguage()->formatNum( $limit ),
682 [ 'offset' => $offset, 'limit' => $limit ],
690 * Abstract formatting function. This should return an HTML string
691 * representing the result row $row. Rows will be concatenated and
692 * returned by getBody()
694 * @param array|stdClass $row Database row
697 abstract function formatRow( $row );
700 * This function should be overridden to provide all parameters
701 * needed for the main paged query. It returns an associative
702 * array with the following elements:
703 * tables => Table(s) for passing to Database::select()
704 * fields => Field(s) for passing to Database::select(), may be *
705 * conds => WHERE conditions
706 * options => option array
707 * join_conds => JOIN conditions
711 abstract function getQueryInfo();
714 * This function should be overridden to return the name of the index fi-
715 * eld. If the pager supports multiple orders, it may return an array of
716 * 'querykey' => 'indexfield' pairs, so that a request with &count=querykey
717 * will use indexfield to sort. In this case, the first returned key is
720 * Needless to say, it's really not a good idea to use a non-unique index
721 * for this! That won't page right.
723 * @return string|string[]
725 abstract function getIndexField();
728 * This function should be overridden to return the names of secondary columns
729 * to order by in addition to the column in getIndexField(). These fields will
730 * not be used in the pager offset or in any links for users.
732 * If getIndexField() returns an array of 'querykey' => 'indexfield' pairs then
733 * this must return a corresponding array of 'querykey' => [ fields... ] pairs
734 * in order for a request with &count=querykey to use [ fields... ] to sort.
736 * This is useful for pagers that GROUP BY a unique column (say page_id)
737 * and ORDER BY another (say page_len). Using GROUP BY and ORDER BY both on
738 * page_len,page_id avoids temp tables (given a page_len index). This would
739 * also work if page_id was non-unique but we had a page_len,page_id index.
741 * @return string[]|array[]
743 protected function getExtraSortFields() {
748 * Return the default sorting direction: DIR_ASCENDING or DIR_DESCENDING.
749 * You can also have an associative array of ordertype => dir,
750 * if multiple order types are supported. In this case getIndexField()
751 * must return an array, and the keys of that must exactly match the keys
754 * For backward compatibility, this method's return value will be ignored
755 * if $this->mDefaultDirection is already set when the constructor is
756 * called, for instance if it's statically initialized. In that case the
757 * value of that variable (which must be a boolean) will be used.
759 * Note that despite its name, this does not return the value of the
760 * $this->mDefaultDirection member variable. That's the default for this
761 * particular instantiation, which is a single value. This is the set of
762 * all defaults for the class.
766 protected function getDefaultDirections() {
767 return self
::DIR_ASCENDING
;