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;
79 public $mLimitsShown = [ 20, 50, 100, 250, 500 ];
80 public $mDefaultLimit = 50;
81 public $mOffset, $mLimit;
82 public $mQueryDone = false;
85 public $mPastTheEndRow;
88 * The index to actually be used for ordering. This is a single column,
89 * for one ordering, even if multiple orderings are supported.
91 protected $mIndexField;
93 * An array of secondary columns to order by. These fields are not part of the offset.
94 * This is a column list for one ordering, even if multiple orderings are supported.
96 protected $mExtraSortFields;
97 /** For pages that support multiple types of ordering, which one to use.
99 protected $mOrderType;
101 * $mDefaultDirection gives the direction to use when sorting results:
102 * DIR_ASCENDING or DIR_DESCENDING. If $mIsBackwards is set, we
103 * start from the opposite end, but we still sort the page itself according
104 * to $mDefaultDirection. E.g., if $mDefaultDirection is false but we're
105 * going backwards, we'll display the last page of results, but the last
106 * result will be at the bottom, not the top.
108 * Like $mIndexField, $mDefaultDirection will be a single value even if the
109 * class supports multiple default directions for different order types.
111 public $mDefaultDirection;
112 public $mIsBackwards;
114 /** True if the current result set is the first one */
118 protected $mLastShown, $mFirstShown, $mPastTheEndIndex, $mDefaultQuery, $mNavigationBar;
121 * Whether to include the offset in the query
123 protected $mIncludeOffset = false;
126 * Result object for the query. Warning: seek before use.
128 * @var IResultWrapper
132 public function __construct( IContextSource
$context = null ) {
134 $this->setContext( $context );
137 $this->mRequest
= $this->getRequest();
139 # NB: the offset is quoted, not validated. It is treated as an
140 # arbitrary string to support the widest variety of index types. Be
141 # careful outputting it into HTML!
142 $this->mOffset
= $this->mRequest
->getText( 'offset' );
144 # Use consistent behavior for the limit options
145 $this->mDefaultLimit
= $this->getUser()->getIntOption( 'rclimit' );
146 if ( !$this->mLimit
) {
147 // Don't override if a subclass calls $this->setLimit() in its constructor.
148 list( $this->mLimit
, /* $offset */ ) = $this->mRequest
->getLimitOffset();
151 $this->mIsBackwards
= ( $this->mRequest
->getVal( 'dir' ) == 'prev' );
152 # Let the subclass set the DB here; otherwise use a replica DB for the current wiki
153 $this->mDb
= $this->mDb ?
: wfGetDB( DB_REPLICA
);
155 $index = $this->getIndexField(); // column to sort on
156 $extraSort = $this->getExtraSortFields(); // extra columns to sort on for query planning
157 $order = $this->mRequest
->getVal( 'order' );
158 if ( is_array( $index ) && isset( $index[$order] ) ) {
159 $this->mOrderType
= $order;
160 $this->mIndexField
= $index[$order];
161 $this->mExtraSortFields
= isset( $extraSort[$order] )
162 ?
(array)$extraSort[$order]
164 } elseif ( is_array( $index ) ) {
165 # First element is the default
166 $this->mIndexField
= reset( $index );
167 $this->mOrderType
= key( $index );
168 $this->mExtraSortFields
= isset( $extraSort[$this->mOrderType
] )
169 ?
(array)$extraSort[$this->mOrderType
]
172 # $index is not an array
173 $this->mOrderType
= null;
174 $this->mIndexField
= $index;
175 $this->mExtraSortFields
= (array)$extraSort;
178 if ( !isset( $this->mDefaultDirection
) ) {
179 $dir = $this->getDefaultDirections();
180 $this->mDefaultDirection
= is_array( $dir )
181 ?
$dir[$this->mOrderType
]
187 * Get the Database object in use
191 public function getDatabase() {
196 * Do the query, using information from the object context. This function
197 * has been kept minimal to make it overridable if necessary, to allow for
198 * result sets formed from multiple DB queries.
200 public function doQuery() {
201 # Use the child class name for profiling
202 $fname = __METHOD__
. ' (' . static::class . ')';
203 $section = Profiler
::instance()->scopedProfileIn( $fname );
205 // @todo This should probably compare to DIR_DESCENDING and DIR_ASCENDING constants
206 $descending = ( $this->mIsBackwards
== $this->mDefaultDirection
);
207 # Plus an extra row so that we can tell the "next" link should be shown
208 $queryLimit = $this->mLimit +
1;
210 if ( $this->mOffset
== '' ) {
213 // If there's an offset, we may or may not be at the first entry.
214 // The only way to tell is to run the query in the opposite
215 // direction see if we get a row.
216 $oldIncludeOffset = $this->mIncludeOffset
;
217 $this->mIncludeOffset
= !$this->mIncludeOffset
;
218 $isFirst = !$this->reallyDoQuery( $this->mOffset
, 1, !$descending )->numRows();
219 $this->mIncludeOffset
= $oldIncludeOffset;
222 $this->mResult
= $this->reallyDoQuery(
228 $this->extractResultInfo( $isFirst, $queryLimit, $this->mResult
);
229 $this->mQueryDone
= true;
231 $this->preprocessResults( $this->mResult
);
232 $this->mResult
->rewind(); // Paranoia
236 * @return IResultWrapper The result wrapper.
238 function getResult() {
239 return $this->mResult
;
243 * Set the offset from an other source than the request
245 * @param int|string $offset
247 function setOffset( $offset ) {
248 $this->mOffset
= $offset;
252 * Set the limit from an other source than the request
254 * Verifies limit is between 1 and 5000
256 * @param int|string $limit
258 function setLimit( $limit ) {
259 $limit = (int)$limit;
260 // WebRequest::getLimitOffset() puts a cap of 5000, so do same here.
261 if ( $limit > 5000 ) {
265 $this->mLimit
= $limit;
270 * Get the current limit
274 function getLimit() {
275 return $this->mLimit
;
279 * Set whether a row matching exactly the offset should be also included
280 * in the result or not. By default this is not the case, but when the
281 * offset is user-supplied this might be wanted.
283 * @param bool $include
285 public function setIncludeOffset( $include ) {
286 $this->mIncludeOffset
= $include;
290 * Extract some useful data from the result object for use by
291 * the navigation bar, put it into $this
293 * @param bool $isFirst False if there are rows before those fetched (i.e.
294 * if a "previous" link would make sense)
295 * @param int $limit Exact query limit
296 * @param IResultWrapper $res
298 function extractResultInfo( $isFirst, $limit, IResultWrapper
$res ) {
299 $numRows = $res->numRows();
301 # Remove any table prefix from index field
302 $parts = explode( '.', $this->mIndexField
);
303 $indexColumn = end( $parts );
305 $row = $res->fetchRow();
306 $firstIndex = $row[$indexColumn];
308 # Discard the extra result row if there is one
309 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
310 $res->seek( $numRows - 1 );
311 $this->mPastTheEndRow
= $res->fetchObject();
312 $this->mPastTheEndIndex
= $this->mPastTheEndRow
->$indexColumn;
313 $res->seek( $numRows - 2 );
314 $row = $res->fetchRow();
315 $lastIndex = $row[$indexColumn];
317 $this->mPastTheEndRow
= null;
318 # Setting indexes to an empty string means that they will be
319 # omitted if they would otherwise appear in URLs. It just so
320 # happens that this is the right thing to do in the standard
321 # UI, in all the relevant cases.
322 $this->mPastTheEndIndex
= '';
323 $res->seek( $numRows - 1 );
324 $row = $res->fetchRow();
325 $lastIndex = $row[$indexColumn];
330 $this->mPastTheEndRow
= null;
331 $this->mPastTheEndIndex
= '';
334 if ( $this->mIsBackwards
) {
335 $this->mIsFirst
= ( $numRows < $limit );
336 $this->mIsLast
= $isFirst;
337 $this->mLastShown
= $firstIndex;
338 $this->mFirstShown
= $lastIndex;
340 $this->mIsFirst
= $isFirst;
341 $this->mIsLast
= ( $numRows < $limit );
342 $this->mLastShown
= $lastIndex;
343 $this->mFirstShown
= $firstIndex;
348 * Get some text to go in brackets in the "function name" part of the SQL comment
352 function getSqlComment() {
353 return static::class;
357 * Do a query with specified parameters, rather than using the object
360 * @param string $offset Index offset, inclusive
361 * @param int $limit Exact query limit
362 * @param bool $descending Query direction, false for ascending, true for descending
363 * @return IResultWrapper
365 public function reallyDoQuery( $offset, $limit, $descending ) {
366 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
367 $this->buildQueryInfo( $offset, $limit, $descending );
369 return $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
373 * Build variables to use by the database wrapper.
375 * @param string $offset Index offset, inclusive
376 * @param int $limit Exact query limit
377 * @param bool $descending Query direction, false for ascending, true for descending
380 protected function buildQueryInfo( $offset, $limit, $descending ) {
381 $fname = __METHOD__
. ' (' . $this->getSqlComment() . ')';
382 $info = $this->getQueryInfo();
383 $tables = $info['tables'];
384 $fields = $info['fields'];
385 $conds = $info['conds'] ??
[];
386 $options = $info['options'] ??
[];
387 $join_conds = $info['join_conds'] ??
[];
388 $sortColumns = array_merge( [ $this->mIndexField
], $this->mExtraSortFields
);
390 $options['ORDER BY'] = $sortColumns;
391 $operator = $this->mIncludeOffset ?
'>=' : '>';
394 foreach ( $sortColumns as $col ) {
395 $orderBy[] = $col . ' DESC';
397 $options['ORDER BY'] = $orderBy;
398 $operator = $this->mIncludeOffset ?
'<=' : '<';
400 if ( $offset != '' ) {
401 $conds[] = $this->mIndexField
. $operator . $this->mDb
->addQuotes( $offset );
403 $options['LIMIT'] = intval( $limit );
404 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
408 * Pre-process results; useful for performing batch existence checks, etc.
410 * @param IResultWrapper $result
412 protected function preprocessResults( $result ) {
416 * Get the formatted result list. Calls getStartBody(), formatRow() and
417 * getEndBody(), concatenates the results and returns them.
421 public function getBody() {
422 if ( !$this->mQueryDone
) {
426 if ( $this->mResult
->numRows() ) {
427 # Do any special query batches before display
428 $this->doBatchLookups();
431 # Don't use any extra rows returned by the query
432 $numRows = min( $this->mResult
->numRows(), $this->mLimit
);
434 $s = $this->getStartBody();
436 if ( $this->mIsBackwards
) {
437 for ( $i = $numRows - 1; $i >= 0; $i-- ) {
438 $this->mResult
->seek( $i );
439 $row = $this->mResult
->fetchObject();
440 $s .= $this->formatRow( $row );
443 $this->mResult
->seek( 0 );
444 for ( $i = 0; $i < $numRows; $i++
) {
445 $row = $this->mResult
->fetchObject();
446 $s .= $this->formatRow( $row );
450 $s .= $this->getEmptyBody();
452 $s .= $this->getEndBody();
459 * @param string $text Text displayed on the link
460 * @param array|null $query Associative array of parameter to be in the query string
461 * @param string|null $type Link type used to create additional attributes, like "rel", "class" or
462 * "title". Valid values (non-exhaustive list): 'first', 'last', 'prev', 'next', 'asc', 'desc'.
463 * @return string HTML fragment
465 function makeLink( $text, array $query = null, $type = null ) {
466 if ( $query === null ) {
471 if ( in_array( $type, [ 'prev', 'next' ] ) ) {
472 $attrs['rel'] = $type;
475 if ( in_array( $type, [ 'asc', 'desc' ] ) ) {
476 $attrs['title'] = $this->msg( $type == 'asc' ?
'sort-ascending' : 'sort-descending' )->text();
480 $attrs['class'] = "mw-{$type}link";
483 return Linker
::linkKnown(
487 $query +
$this->getDefaultQuery()
492 * Called from getBody(), before getStartBody() is called and
493 * after doQuery() was called. This will be called only if there
494 * are rows in the result set.
498 protected function doBatchLookups() {
502 * Hook into getBody(), allows text to be inserted at the start. This
503 * will be called even if there are no rows in the result set.
507 protected function getStartBody() {
512 * Hook into getBody() for the end of the list
516 protected function getEndBody() {
521 * Hook into getBody(), for the bit between the start and the
522 * end when there are no rows
526 protected function getEmptyBody() {
531 * Get an array of query parameters that should be put into self-links.
532 * By default, all parameters passed in the URL are used, except for a
535 * @return array Associative array
537 function getDefaultQuery() {
538 if ( !isset( $this->mDefaultQuery
) ) {
539 $this->mDefaultQuery
= $this->getRequest()->getQueryValues();
540 unset( $this->mDefaultQuery
['title'] );
541 unset( $this->mDefaultQuery
['dir'] );
542 unset( $this->mDefaultQuery
['offset'] );
543 unset( $this->mDefaultQuery
['limit'] );
544 unset( $this->mDefaultQuery
['order'] );
545 unset( $this->mDefaultQuery
['month'] );
546 unset( $this->mDefaultQuery
['year'] );
548 return $this->mDefaultQuery
;
552 * Get the number of rows in the result set
556 function getNumRows() {
557 if ( !$this->mQueryDone
) {
560 return $this->mResult
->numRows();
564 * Get a URL query array for the prev, next, first and last links.
568 function getPagingQueries() {
569 if ( !$this->mQueryDone
) {
573 # Don't announce the limit everywhere if it's the default
574 $urlLimit = $this->mLimit
== $this->mDefaultLimit ?
null : $this->mLimit
;
576 if ( $this->mIsFirst
) {
582 'offset' => $this->mFirstShown
,
585 $first = [ 'limit' => $urlLimit ];
587 if ( $this->mIsLast
) {
591 $next = [ 'offset' => $this->mLastShown
, 'limit' => $urlLimit ];
592 $last = [ 'dir' => 'prev', 'limit' => $urlLimit ];
603 * Returns whether to show the "navigation bar"
607 function isNavigationBarShown() {
608 if ( !$this->mQueryDone
) {
611 // Hide navigation by default if there is nothing to page
612 return !( $this->mIsFirst
&& $this->mIsLast
);
616 * Get paging links. If a link is disabled, the item from $disabledTexts
617 * will be used. If there is no such item, the unlinked text from
618 * $linkTexts will be used. Both $linkTexts and $disabledTexts are arrays
621 * @param array $linkTexts
622 * @param array $disabledTexts
625 function getPagingLinks( $linkTexts, $disabledTexts = [] ) {
626 $queries = $this->getPagingQueries();
629 foreach ( $queries as $type => $query ) {
630 if ( $query !== false ) {
631 $links[$type] = $this->makeLink(
636 } elseif ( isset( $disabledTexts[$type] ) ) {
637 $links[$type] = $disabledTexts[$type];
639 $links[$type] = $linkTexts[$type];
646 function getLimitLinks() {
648 if ( $this->mIsBackwards
) {
649 $offset = $this->mPastTheEndIndex
;
651 $offset = $this->mOffset
;
653 foreach ( $this->mLimitsShown
as $limit ) {
654 $links[] = $this->makeLink(
655 $this->getLanguage()->formatNum( $limit ),
656 [ 'offset' => $offset, 'limit' => $limit ],
664 * Abstract formatting function. This should return an HTML string
665 * representing the result row $row. Rows will be concatenated and
666 * returned by getBody()
668 * @param array|stdClass $row Database row
671 abstract function formatRow( $row );
674 * This function should be overridden to provide all parameters
675 * needed for the main paged query. It returns an associative
676 * array with the following elements:
677 * tables => Table(s) for passing to Database::select()
678 * fields => Field(s) for passing to Database::select(), may be *
679 * conds => WHERE conditions
680 * options => option array
681 * join_conds => JOIN conditions
685 abstract function getQueryInfo();
688 * This function should be overridden to return the name of the index fi-
689 * eld. If the pager supports multiple orders, it may return an array of
690 * 'querykey' => 'indexfield' pairs, so that a request with &count=querykey
691 * will use indexfield to sort. In this case, the first returned key is
694 * Needless to say, it's really not a good idea to use a non-unique index
695 * for this! That won't page right.
697 * @return string|array
699 abstract function getIndexField();
702 * This function should be overridden to return the names of secondary columns
703 * to order by in addition to the column in getIndexField(). These fields will
704 * not be used in the pager offset or in any links for users.
706 * If getIndexField() returns an array of 'querykey' => 'indexfield' pairs then
707 * this must return a corresponding array of 'querykey' => [ fields... ] pairs
708 * in order for a request with &count=querykey to use [ fields... ] to sort.
710 * This is useful for pagers that GROUP BY a unique column (say page_id)
711 * and ORDER BY another (say page_len). Using GROUP BY and ORDER BY both on
712 * page_len,page_id avoids temp tables (given a page_len index). This would
713 * also work if page_id was non-unique but we had a page_len,page_id index.
717 protected function getExtraSortFields() {
722 * Return the default sorting direction: DIR_ASCENDING or DIR_DESCENDING.
723 * You can also have an associative array of ordertype => dir,
724 * if multiple order types are supported. In this case getIndexField()
725 * must return an array, and the keys of that must exactly match the keys
728 * For backward compatibility, this method's return value will be ignored
729 * if $this->mDefaultDirection is already set when the constructor is
730 * called, for instance if it's statically initialized. In that case the
731 * value of that variable (which must be a boolean) will be used.
733 * Note that despite its name, this does not return the value of the
734 * $this->mDefaultDirection member variable. That's the default for this
735 * particular instantiation, which is a single value. This is the set of
736 * all defaults for the class.
740 protected function getDefaultDirections() {
741 return self
::DIR_ASCENDING
;