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 string|int 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|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.
99 protected $mIndexField;
101 * An array of secondary columns to order by. These fields are not part of the offset.
102 * This is a column list for one ordering, even if multiple orderings are supported.
104 protected $mExtraSortFields;
105 /** For pages that support multiple types of ordering, which one to use.
107 protected $mOrderType;
109 * $mDefaultDirection gives the direction to use when sorting results:
110 * DIR_ASCENDING or DIR_DESCENDING. If $mIsBackwards is set, we start from
111 * the opposite end, but we still sort the page itself according to
112 * $mDefaultDirection. For example, if $mDefaultDirection is DIR_ASCENDING
113 * but we're going backwards, we'll display the last page of results, but
114 * the last result will be at the bottom, not the top.
116 * Like $mIndexField, $mDefaultDirection will be a single value even if the
117 * class supports multiple default directions for different order types.
119 public $mDefaultDirection;
120 public $mIsBackwards;
122 /** True if the current result set is the first one */
126 protected $mLastShown, $mFirstShown, $mPastTheEndIndex, $mDefaultQuery, $mNavigationBar;
129 * Whether to include the offset in the query
131 protected $mIncludeOffset = false;
134 * Result object for the query. Warning: seek before use.
136 * @var IResultWrapper
140 public function __construct( IContextSource
$context = null ) {
142 $this->setContext( $context );
145 $this->mRequest
= $this->getRequest();
147 # NB: the offset is quoted, not validated. It is treated as an
148 # arbitrary string to support the widest variety of index types. Be
149 # careful outputting it into HTML!
150 $this->mOffset
= $this->mRequest
->getText( 'offset' );
152 # Use consistent behavior for the limit options
153 $this->mDefaultLimit
= $this->getUser()->getIntOption( 'rclimit' );
154 if ( !$this->mLimit
) {
155 // Don't override if a subclass calls $this->setLimit() in its constructor.
156 list( $this->mLimit
, /* $offset */ ) = $this->mRequest
->getLimitOffset();
159 $this->mIsBackwards
= ( $this->mRequest
->getVal( 'dir' ) == 'prev' );
160 # Let the subclass set the DB here; otherwise use a replica DB for the current wiki
161 $this->mDb
= $this->mDb ?
: wfGetDB( DB_REPLICA
);
163 $index = $this->getIndexField(); // column to sort on
164 $extraSort = $this->getExtraSortFields(); // extra columns to sort on for query planning
165 $order = $this->mRequest
->getVal( 'order' );
166 if ( is_array( $index ) && isset( $index[$order] ) ) {
167 $this->mOrderType
= $order;
168 $this->mIndexField
= $index[$order];
169 $this->mExtraSortFields
= isset( $extraSort[$order] )
170 ?
(array)$extraSort[$order]
172 } elseif ( is_array( $index ) ) {
173 # First element is the default
174 $this->mIndexField
= reset( $index );
175 $this->mOrderType
= key( $index );
176 $this->mExtraSortFields
= isset( $extraSort[$this->mOrderType
] )
177 ?
(array)$extraSort[$this->mOrderType
]
180 # $index is not an array
181 $this->mOrderType
= null;
182 $this->mIndexField
= $index;
183 $this->mExtraSortFields
= (array)$extraSort;
186 if ( !isset( $this->mDefaultDirection
) ) {
187 $dir = $this->getDefaultDirections();
188 $this->mDefaultDirection
= is_array( $dir )
189 ?
$dir[$this->mOrderType
]
195 * Get the Database object in use
199 public function getDatabase() {
204 * Do the query, using information from the object context. This function
205 * has been kept minimal to make it overridable if necessary, to allow for
206 * result sets formed from multiple DB queries.
208 public function doQuery() {
209 # Use the child class name for profiling
210 $fname = __METHOD__
. ' (' . static::class . ')';
211 $section = Profiler
::instance()->scopedProfileIn( $fname );
213 $descending = $this->mIsBackwards
214 ?
( $this->mDefaultDirection
=== self
::DIR_DESCENDING
)
215 : ( $this->mDefaultDirection
=== self
::DIR_ASCENDING
);
217 # Plus an extra row so that we can tell the "next" link should be shown
218 $queryLimit = $this->mLimit +
1;
220 if ( $this->mOffset
== '' ) {
223 // If there's an offset, we may or may not be at the first entry.
224 // The only way to tell is to run the query in the opposite
225 // direction see if we get a row.
226 $oldIncludeOffset = $this->mIncludeOffset
;
227 $this->mIncludeOffset
= !$this->mIncludeOffset
;
228 $isFirst = !$this->reallyDoQuery( $this->mOffset
, 1, !$descending )->numRows();
229 $this->mIncludeOffset
= $oldIncludeOffset;
232 $this->mResult
= $this->reallyDoQuery(
238 $this->extractResultInfo( $isFirst, $queryLimit, $this->mResult
);
239 $this->mQueryDone
= true;
241 $this->preprocessResults( $this->mResult
);
242 $this->mResult
->rewind(); // Paranoia
246 * @return IResultWrapper The result wrapper.
248 function getResult() {
249 return $this->mResult
;
253 * Set the offset from an other source than the request
255 * @param int|string $offset
257 function setOffset( $offset ) {
258 $this->mOffset
= $offset;
262 * Set the limit from an other source than the request
264 * Verifies limit is between 1 and 5000
266 * @param int|string $limit
268 function setLimit( $limit ) {
269 $limit = (int)$limit;
270 // WebRequest::getLimitOffset() puts a cap of 5000, so do same here.
271 if ( $limit > 5000 ) {
275 $this->mLimit
= $limit;
280 * Get the current limit
284 function getLimit() {
285 return $this->mLimit
;
289 * Set whether a row matching exactly the offset should be also included
290 * in the result or not. By default this is not the case, but when the
291 * offset is user-supplied this might be wanted.
293 * @param bool $include
295 public function setIncludeOffset( $include ) {
296 $this->mIncludeOffset
= $include;
300 * Extract some useful data from the result object for use by
301 * the navigation bar, put it into $this
303 * @param bool $isFirst False if there are rows before those fetched (i.e.
304 * if a "previous" link would make sense)
305 * @param int $limit Exact query limit
306 * @param IResultWrapper $res
308 function extractResultInfo( $isFirst, $limit, IResultWrapper
$res ) {
309 $numRows = $res->numRows();
311 # Remove any table prefix from index field
312 $parts = explode( '.', $this->mIndexField
);
313 $indexColumn = end( $parts );
315 $row = $res->fetchRow();
316 $firstIndex = $row[$indexColumn];
318 # Discard the extra result row if there is one
319 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
320 $res->seek( $numRows - 1 );
321 $this->mPastTheEndRow
= $res->fetchObject();
322 $this->mPastTheEndIndex
= $this->mPastTheEndRow
->$indexColumn;
323 $res->seek( $numRows - 2 );
324 $row = $res->fetchRow();
325 $lastIndex = $row[$indexColumn];
327 $this->mPastTheEndRow
= null;
328 # Setting indexes to an empty string means that they will be
329 # omitted if they would otherwise appear in URLs. It just so
330 # happens that this is the right thing to do in the standard
331 # UI, in all the relevant cases.
332 $this->mPastTheEndIndex
= '';
333 $res->seek( $numRows - 1 );
334 $row = $res->fetchRow();
335 $lastIndex = $row[$indexColumn];
340 $this->mPastTheEndRow
= null;
341 $this->mPastTheEndIndex
= '';
344 if ( $this->mIsBackwards
) {
345 $this->mIsFirst
= ( $numRows < $limit );
346 $this->mIsLast
= $isFirst;
347 $this->mLastShown
= $firstIndex;
348 $this->mFirstShown
= $lastIndex;
350 $this->mIsFirst
= $isFirst;
351 $this->mIsLast
= ( $numRows < $limit );
352 $this->mLastShown
= $lastIndex;
353 $this->mFirstShown
= $firstIndex;
358 * Get some text to go in brackets in the "function name" part of the SQL comment
362 function getSqlComment() {
363 return static::class;
367 * Do a query with specified parameters, rather than using the object
370 * @param string $offset Index offset, inclusive
371 * @param int $limit Exact query limit
372 * @param bool $descending Query direction, false for ascending, true for descending
373 * @return IResultWrapper
375 public function reallyDoQuery( $offset, $limit, $descending ) {
376 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
377 $this->buildQueryInfo( $offset, $limit, $descending );
379 return $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
383 * Build variables to use by the database wrapper.
385 * @param string $offset Index offset, inclusive
386 * @param int $limit Exact query limit
387 * @param bool $descending Query direction, false for ascending, true for descending
390 protected function buildQueryInfo( $offset, $limit, $descending ) {
391 $fname = __METHOD__
. ' (' . $this->getSqlComment() . ')';
392 $info = $this->getQueryInfo();
393 $tables = $info['tables'];
394 $fields = $info['fields'];
395 $conds = $info['conds'] ??
[];
396 $options = $info['options'] ??
[];
397 $join_conds = $info['join_conds'] ??
[];
398 $sortColumns = array_merge( [ $this->mIndexField
], $this->mExtraSortFields
);
400 $options['ORDER BY'] = $sortColumns;
401 $operator = $this->mIncludeOffset ?
'>=' : '>';
404 foreach ( $sortColumns as $col ) {
405 $orderBy[] = $col . ' DESC';
407 $options['ORDER BY'] = $orderBy;
408 $operator = $this->mIncludeOffset ?
'<=' : '<';
410 if ( $offset != '' ) {
411 $conds[] = $this->mIndexField
. $operator . $this->mDb
->addQuotes( $offset );
413 $options['LIMIT'] = intval( $limit );
414 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
418 * Pre-process results; useful for performing batch existence checks, etc.
420 * @param IResultWrapper $result
422 protected function preprocessResults( $result ) {
426 * Get the formatted result list. Calls getStartBody(), formatRow() and
427 * getEndBody(), concatenates the results and returns them.
431 public function getBody() {
432 if ( !$this->mQueryDone
) {
436 if ( $this->mResult
->numRows() ) {
437 # Do any special query batches before display
438 $this->doBatchLookups();
441 # Don't use any extra rows returned by the query
442 $numRows = min( $this->mResult
->numRows(), $this->mLimit
);
444 $s = $this->getStartBody();
446 if ( $this->mIsBackwards
) {
447 for ( $i = $numRows - 1; $i >= 0; $i-- ) {
448 $this->mResult
->seek( $i );
449 $row = $this->mResult
->fetchObject();
450 $s .= $this->formatRow( $row );
453 $this->mResult
->seek( 0 );
454 for ( $i = 0; $i < $numRows; $i++
) {
455 $row = $this->mResult
->fetchObject();
456 $s .= $this->formatRow( $row );
460 $s .= $this->getEmptyBody();
462 $s .= $this->getEndBody();
469 * @param string $text Text displayed on the link
470 * @param array|null $query Associative array of parameter to be in the query string
471 * @param string|null $type Link type used to create additional attributes, like "rel", "class" or
472 * "title". Valid values (non-exhaustive list): 'first', 'last', 'prev', 'next', 'asc', 'desc'.
473 * @return string HTML fragment
475 function makeLink( $text, array $query = null, $type = null ) {
476 if ( $query === null ) {
481 if ( in_array( $type, [ 'prev', 'next' ] ) ) {
482 $attrs['rel'] = $type;
485 if ( in_array( $type, [ 'asc', 'desc' ] ) ) {
486 $attrs['title'] = $this->msg( $type == 'asc' ?
'sort-ascending' : 'sort-descending' )->text();
490 $attrs['class'] = "mw-{$type}link";
493 return Linker
::linkKnown(
497 $query +
$this->getDefaultQuery()
502 * Called from getBody(), before getStartBody() is called and
503 * after doQuery() was called. This will be called only if there
504 * are rows in the result set.
508 protected function doBatchLookups() {
512 * Hook into getBody(), allows text to be inserted at the start. This
513 * will be called even if there are no rows in the result set.
517 protected function getStartBody() {
522 * Hook into getBody() for the end of the list
526 protected function getEndBody() {
531 * Hook into getBody(), for the bit between the start and the
532 * end when there are no rows
536 protected function getEmptyBody() {
541 * Get an array of query parameters that should be put into self-links.
542 * By default, all parameters passed in the URL are used, except for a
545 * @return array Associative array
547 function getDefaultQuery() {
548 if ( !isset( $this->mDefaultQuery
) ) {
549 $this->mDefaultQuery
= $this->getRequest()->getQueryValues();
550 unset( $this->mDefaultQuery
['title'] );
551 unset( $this->mDefaultQuery
['dir'] );
552 unset( $this->mDefaultQuery
['offset'] );
553 unset( $this->mDefaultQuery
['limit'] );
554 unset( $this->mDefaultQuery
['order'] );
555 unset( $this->mDefaultQuery
['month'] );
556 unset( $this->mDefaultQuery
['year'] );
558 return $this->mDefaultQuery
;
562 * Get the number of rows in the result set
566 function getNumRows() {
567 if ( !$this->mQueryDone
) {
570 return $this->mResult
->numRows();
574 * Get a URL query array for the prev, next, first and last links.
578 function getPagingQueries() {
579 if ( !$this->mQueryDone
) {
583 # Don't announce the limit everywhere if it's the default
584 $urlLimit = $this->mLimit
== $this->mDefaultLimit ?
null : $this->mLimit
;
586 if ( $this->mIsFirst
) {
592 'offset' => $this->mFirstShown
,
595 $first = [ 'limit' => $urlLimit ];
597 if ( $this->mIsLast
) {
601 $next = [ 'offset' => $this->mLastShown
, 'limit' => $urlLimit ];
602 $last = [ 'dir' => 'prev', 'limit' => $urlLimit ];
613 * Returns whether to show the "navigation bar"
617 function isNavigationBarShown() {
618 if ( !$this->mQueryDone
) {
621 // Hide navigation by default if there is nothing to page
622 return !( $this->mIsFirst
&& $this->mIsLast
);
626 * Get paging links. If a link is disabled, the item from $disabledTexts
627 * will be used. If there is no such item, the unlinked text from
628 * $linkTexts will be used. Both $linkTexts and $disabledTexts are arrays
631 * @param array $linkTexts
632 * @param array $disabledTexts
635 function getPagingLinks( $linkTexts, $disabledTexts = [] ) {
636 $queries = $this->getPagingQueries();
639 foreach ( $queries as $type => $query ) {
640 if ( $query !== false ) {
641 $links[$type] = $this->makeLink(
646 } elseif ( isset( $disabledTexts[$type] ) ) {
647 $links[$type] = $disabledTexts[$type];
649 $links[$type] = $linkTexts[$type];
656 function getLimitLinks() {
658 if ( $this->mIsBackwards
) {
659 $offset = $this->mPastTheEndIndex
;
661 $offset = $this->mOffset
;
663 foreach ( $this->mLimitsShown
as $limit ) {
664 $links[] = $this->makeLink(
665 $this->getLanguage()->formatNum( $limit ),
666 [ 'offset' => $offset, 'limit' => $limit ],
674 * Abstract formatting function. This should return an HTML string
675 * representing the result row $row. Rows will be concatenated and
676 * returned by getBody()
678 * @param array|stdClass $row Database row
681 abstract function formatRow( $row );
684 * This function should be overridden to provide all parameters
685 * needed for the main paged query. It returns an associative
686 * array with the following elements:
687 * tables => Table(s) for passing to Database::select()
688 * fields => Field(s) for passing to Database::select(), may be *
689 * conds => WHERE conditions
690 * options => option array
691 * join_conds => JOIN conditions
695 abstract function getQueryInfo();
698 * This function should be overridden to return the name of the index fi-
699 * eld. If the pager supports multiple orders, it may return an array of
700 * 'querykey' => 'indexfield' pairs, so that a request with &count=querykey
701 * will use indexfield to sort. In this case, the first returned key is
704 * Needless to say, it's really not a good idea to use a non-unique index
705 * for this! That won't page right.
707 * @return string|string[]
709 abstract function getIndexField();
712 * This function should be overridden to return the names of secondary columns
713 * to order by in addition to the column in getIndexField(). These fields will
714 * not be used in the pager offset or in any links for users.
716 * If getIndexField() returns an array of 'querykey' => 'indexfield' pairs then
717 * this must return a corresponding array of 'querykey' => [ fields... ] pairs
718 * in order for a request with &count=querykey to use [ fields... ] to sort.
720 * This is useful for pagers that GROUP BY a unique column (say page_id)
721 * and ORDER BY another (say page_len). Using GROUP BY and ORDER BY both on
722 * page_len,page_id avoids temp tables (given a page_len index). This would
723 * also work if page_id was non-unique but we had a page_len,page_id index.
725 * @return string[]|array[]
727 protected function getExtraSortFields() {
732 * Return the default sorting direction: DIR_ASCENDING or DIR_DESCENDING.
733 * You can also have an associative array of ordertype => dir,
734 * if multiple order types are supported. In this case getIndexField()
735 * must return an array, and the keys of that must exactly match the keys
738 * For backward compatibility, this method's return value will be ignored
739 * if $this->mDefaultDirection is already set when the constructor is
740 * called, for instance if it's statically initialized. In that case the
741 * value of that variable (which must be a boolean) will be used.
743 * Note that despite its name, this does not return the value of the
744 * $this->mDefaultDirection member variable. That's the default for this
745 * particular instantiation, which is a single value. This is the set of
746 * all defaults for the class.
750 protected function getDefaultDirections() {
751 return self
::DIR_ASCENDING
;