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 MediaWiki\Linker\LinkRenderer
;
25 use MediaWiki\Linker\LinkTarget
;
26 use MediaWiki\MediaWikiServices
;
27 use MediaWiki\Navigation\PrevNextNavigationRenderer
;
28 use Wikimedia\Rdbms\IDatabase
;
29 use Wikimedia\Rdbms\IResultWrapper
;
32 * IndexPager is an efficient pager which uses a (roughly unique) index in the
33 * data set to implement paging, rather than a "LIMIT offset,limit" clause.
34 * In MySQL, such a limit/offset clause requires counting through the
35 * specified number of offset rows to find the desired data, which can be
36 * expensive for large offsets.
38 * ReverseChronologicalPager is a child class of the abstract IndexPager, and
39 * contains some formatting and display code which is specific to the use of
40 * timestamps as indexes. Here is a synopsis of its operation:
42 * * The query is specified by the offset, limit and direction (dir)
43 * parameters, in addition to any subclass-specific parameters.
44 * * The offset is the non-inclusive start of the DB query. A row with an
45 * index value equal to the offset will never be shown.
46 * * The query may either be done backwards, where the rows are returned by
47 * the database in the opposite order to which they are displayed to the
48 * user, or forwards. This is specified by the "dir" parameter, dir=prev
49 * means backwards, anything else means forwards. The offset value
50 * specifies the start of the database result set, which may be either
51 * the start or end of the displayed data set. This allows "previous"
52 * links to be implemented without knowledge of the index value at the
53 * start of the previous page.
54 * * An additional row beyond the user-specified limit is always requested.
55 * This allows us to tell whether we should display a "next" link in the
56 * case of forwards mode, or a "previous" link in the case of backwards
57 * mode. Determining whether to display the other link (the one for the
58 * page before the start of the database result set) can be done
59 * heuristically by examining the offset.
61 * * An empty offset indicates that the offset condition should be omitted
62 * from the query. This naturally produces either the first page or the
63 * last page depending on the dir parameter.
65 * Subclassing the pager to implement concrete functionality should be fairly
66 * simple, please see the examples in HistoryAction.php and
67 * SpecialBlockList.php. You just need to override formatRow(),
68 * getQueryInfo() and getIndexField(). Don't forget to call the parent
69 * constructor if you override it.
73 abstract class IndexPager
extends ContextSource
implements Pager
{
74 /** Backwards-compatible constant for $mDefaultDirection field (do not change) */
75 const DIR_ASCENDING
= false;
76 /** Backwards-compatible constant for $mDefaultDirection field (do not change) */
77 const DIR_DESCENDING
= true;
79 /** Backwards-compatible constant for reallyDoQuery() (do not change) */
80 const QUERY_ASCENDING
= true;
81 /** Backwards-compatible constant for reallyDoQuery() (do not change) */
82 const QUERY_DESCENDING
= false;
84 /** @var WebRequest */
86 /** @var int[] List of default entry limit options to be presented to clients */
87 public $mLimitsShown = [ 20, 50, 100, 250, 500 ];
88 /** @var int The default entry limit choosen for clients */
89 public $mDefaultLimit = 50;
90 /** @var mixed The starting point to enumerate entries */
92 /** @var int The maximum number of entries to show */
94 /** @var bool Whether the listing query completed */
95 public $mQueryDone = false;
98 /** @var stdClass|bool|null Extra row fetched at the end to see if the end was reached */
99 public $mPastTheEndRow;
102 * The index to actually be used for ordering. This is a single column,
103 * for one ordering, even if multiple orderings are supported.
106 protected $mIndexField;
108 * An array of secondary columns to order by. These fields are not part of the offset.
109 * This is a column list for one ordering, even if multiple orderings are supported.
112 protected $mExtraSortFields;
113 /** For pages that support multiple types of ordering, which one to use.
116 protected $mOrderType;
118 * $mDefaultDirection gives the direction to use when sorting results:
119 * DIR_ASCENDING or DIR_DESCENDING. If $mIsBackwards is set, we start from
120 * the opposite end, but we still sort the page itself according to
121 * $mDefaultDirection. For example, if $mDefaultDirection is DIR_ASCENDING
122 * but we're going backwards, we'll display the last page of results, but
123 * the last result will be at the bottom, not the top.
125 * Like $mIndexField, $mDefaultDirection will be a single value even if the
126 * class supports multiple default directions for different order types.
129 public $mDefaultDirection;
131 public $mIsBackwards;
133 /** @var bool True if the current result set is the first one */
139 protected $mLastShown;
141 protected $mFirstShown;
143 protected $mPastTheEndIndex;
145 protected $mDefaultQuery;
147 protected $mNavigationBar;
150 * Whether to include the offset in the query
153 protected $mIncludeOffset = false;
156 * Result object for the query. Warning: seek before use.
158 * @var IResultWrapper
162 /** @var LinkRenderer */
163 private $linkRenderer;
165 public function __construct( IContextSource
$context = null, LinkRenderer
$linkRenderer = null ) {
167 $this->setContext( $context );
170 $this->mRequest
= $this->getRequest();
172 # NB: the offset is quoted, not validated. It is treated as an
173 # arbitrary string to support the widest variety of index types. Be
174 # careful outputting it into HTML!
175 $this->mOffset
= $this->mRequest
->getText( 'offset' );
177 # Use consistent behavior for the limit options
178 $this->mDefaultLimit
= $this->getUser()->getIntOption( 'rclimit' );
179 if ( !$this->mLimit
) {
180 // Don't override if a subclass calls $this->setLimit() in its constructor.
181 list( $this->mLimit
, /* $offset */ ) = $this->mRequest
->getLimitOffset();
184 $this->mIsBackwards
= ( $this->mRequest
->getVal( 'dir' ) == 'prev' );
185 # Let the subclass set the DB here; otherwise use a replica DB for the current wiki
186 $this->mDb
= $this->mDb ?
: wfGetDB( DB_REPLICA
);
188 $index = $this->getIndexField(); // column to sort on
189 $extraSort = $this->getExtraSortFields(); // extra columns to sort on for query planning
190 $order = $this->mRequest
->getVal( 'order' );
191 if ( is_array( $index ) && isset( $index[$order] ) ) {
192 $this->mOrderType
= $order;
193 $this->mIndexField
= $index[$order];
194 $this->mExtraSortFields
= isset( $extraSort[$order] )
195 ?
(array)$extraSort[$order]
197 } elseif ( is_array( $index ) ) {
198 # First element is the default
199 $this->mIndexField
= reset( $index );
200 $this->mOrderType
= key( $index );
201 $this->mExtraSortFields
= isset( $extraSort[$this->mOrderType
] )
202 ?
(array)$extraSort[$this->mOrderType
]
205 # $index is not an array
206 $this->mOrderType
= null;
207 $this->mIndexField
= $index;
208 $this->mExtraSortFields
= (array)$extraSort;
211 if ( !isset( $this->mDefaultDirection
) ) {
212 $dir = $this->getDefaultDirections();
213 $this->mDefaultDirection
= is_array( $dir )
214 ?
$dir[$this->mOrderType
]
217 $this->linkRenderer
= $linkRenderer;
221 * Get the Database object in use
225 public function getDatabase() {
230 * Do the query, using information from the object context. This function
231 * has been kept minimal to make it overridable if necessary, to allow for
232 * result sets formed from multiple DB queries.
234 public function doQuery() {
235 # Use the child class name for profiling
236 $fname = __METHOD__
. ' (' . static::class . ')';
237 /** @noinspection PhpUnusedLocalVariableInspection */
238 $section = Profiler
::instance()->scopedProfileIn( $fname );
240 $defaultOrder = ( $this->mDefaultDirection
=== self
::DIR_ASCENDING
)
241 ? self
::QUERY_ASCENDING
242 : self
::QUERY_DESCENDING
;
243 $order = $this->mIsBackwards ? self
::oppositeOrder( $defaultOrder ) : $defaultOrder;
245 # Plus an extra row so that we can tell the "next" link should be shown
246 $queryLimit = $this->mLimit +
1;
248 if ( $this->mOffset
== '' ) {
251 // If there's an offset, we may or may not be at the first entry.
252 // The only way to tell is to run the query in the opposite
253 // direction see if we get a row.
254 $oldIncludeOffset = $this->mIncludeOffset
;
255 $this->mIncludeOffset
= !$this->mIncludeOffset
;
256 $oppositeOrder = self
::oppositeOrder( $order );
257 $isFirst = !$this->reallyDoQuery( $this->mOffset
, 1, $oppositeOrder )->numRows();
258 $this->mIncludeOffset
= $oldIncludeOffset;
261 $this->mResult
= $this->reallyDoQuery(
267 $this->extractResultInfo( $isFirst, $queryLimit, $this->mResult
);
268 $this->mQueryDone
= true;
270 $this->preprocessResults( $this->mResult
);
271 $this->mResult
->rewind(); // Paranoia
275 * @param bool $order One of the IndexPager::QUERY_* class constants
276 * @return bool The opposite query order as an IndexPager::QUERY_ constant
278 final protected static function oppositeOrder( $order ) {
279 return ( $order === self
::QUERY_ASCENDING
)
280 ? self
::QUERY_DESCENDING
281 : self
::QUERY_ASCENDING
;
285 * @return IResultWrapper The result wrapper.
287 function getResult() {
288 return $this->mResult
;
292 * Set the offset from an other source than the request
294 * @param int|string $offset
296 function setOffset( $offset ) {
297 $this->mOffset
= $offset;
301 * Set the limit from an other source than the request
303 * Verifies limit is between 1 and 5000
305 * @param int|string $limit
307 function setLimit( $limit ) {
308 $limit = (int)$limit;
309 // WebRequest::getLimitOffset() puts a cap of 5000, so do same here.
310 if ( $limit > 5000 ) {
314 $this->mLimit
= $limit;
319 * Get the current limit
323 function getLimit() {
324 return $this->mLimit
;
328 * Set whether a row matching exactly the offset should be also included
329 * in the result or not. By default this is not the case, but when the
330 * offset is user-supplied this might be wanted.
332 * @param bool $include
334 public function setIncludeOffset( $include ) {
335 $this->mIncludeOffset
= $include;
339 * Extract some useful data from the result object for use by
340 * the navigation bar, put it into $this
342 * @param bool $isFirst False if there are rows before those fetched (i.e.
343 * if a "previous" link would make sense)
344 * @param int $limit Exact query limit
345 * @param IResultWrapper $res
347 function extractResultInfo( $isFirst, $limit, IResultWrapper
$res ) {
348 $numRows = $res->numRows();
350 # Remove any table prefix from index field
351 $parts = explode( '.', $this->mIndexField
);
352 $indexColumn = end( $parts );
354 $row = $res->fetchRow();
355 $firstIndex = $row[$indexColumn];
357 # Discard the extra result row if there is one
358 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
359 $res->seek( $numRows - 1 );
360 $this->mPastTheEndRow
= $res->fetchObject();
361 $this->mPastTheEndIndex
= $this->mPastTheEndRow
->$indexColumn;
362 $res->seek( $numRows - 2 );
363 $row = $res->fetchRow();
364 $lastIndex = $row[$indexColumn];
366 $this->mPastTheEndRow
= null;
367 # Setting indexes to an empty string means that they will be
368 # omitted if they would otherwise appear in URLs. It just so
369 # happens that this is the right thing to do in the standard
370 # UI, in all the relevant cases.
371 $this->mPastTheEndIndex
= '';
372 $res->seek( $numRows - 1 );
373 $row = $res->fetchRow();
374 $lastIndex = $row[$indexColumn];
379 $this->mPastTheEndRow
= null;
380 $this->mPastTheEndIndex
= '';
383 if ( $this->mIsBackwards
) {
384 $this->mIsFirst
= ( $numRows < $limit );
385 $this->mIsLast
= $isFirst;
386 $this->mLastShown
= $firstIndex;
387 $this->mFirstShown
= $lastIndex;
389 $this->mIsFirst
= $isFirst;
390 $this->mIsLast
= ( $numRows < $limit );
391 $this->mLastShown
= $lastIndex;
392 $this->mFirstShown
= $firstIndex;
397 * Get some text to go in brackets in the "function name" part of the SQL comment
401 function getSqlComment() {
402 return static::class;
406 * Do a query with specified parameters, rather than using the object context
408 * @note For b/c, query direction is true for ascending and false for descending
410 * @param string $offset Index offset, inclusive
411 * @param int $limit Exact query limit
412 * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING
413 * @return IResultWrapper
415 public function reallyDoQuery( $offset, $limit, $order ) {
416 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
417 $this->buildQueryInfo( $offset, $limit, $order );
419 return $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
423 * Build variables to use by the database wrapper.
425 * @note For b/c, query direction is true for ascending and false for descending
427 * @param string $offset Index offset, inclusive
428 * @param int $limit Exact query limit
429 * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING
432 protected function buildQueryInfo( $offset, $limit, $order ) {
433 $fname = __METHOD__
. ' (' . $this->getSqlComment() . ')';
434 $info = $this->getQueryInfo();
435 $tables = $info['tables'];
436 $fields = $info['fields'];
437 $conds = $info['conds'] ??
[];
438 $options = $info['options'] ??
[];
439 $join_conds = $info['join_conds'] ??
[];
440 $sortColumns = array_merge( [ $this->mIndexField
], $this->mExtraSortFields
);
441 if ( $order === self
::QUERY_ASCENDING
) {
442 $options['ORDER BY'] = $sortColumns;
443 $operator = $this->mIncludeOffset ?
'>=' : '>';
446 foreach ( $sortColumns as $col ) {
447 $orderBy[] = $col . ' DESC';
449 $options['ORDER BY'] = $orderBy;
450 $operator = $this->mIncludeOffset ?
'<=' : '<';
452 if ( $offset != '' ) {
453 $conds[] = $this->mIndexField
. $operator . $this->mDb
->addQuotes( $offset );
455 $options['LIMIT'] = intval( $limit );
456 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
460 * Pre-process results; useful for performing batch existence checks, etc.
462 * @param IResultWrapper $result
464 protected function preprocessResults( $result ) {
468 * Get the formatted result list. Calls getStartBody(), formatRow() and
469 * getEndBody(), concatenates the results and returns them.
473 public function getBody() {
474 if ( !$this->mQueryDone
) {
478 if ( $this->mResult
->numRows() ) {
479 # Do any special query batches before display
480 $this->doBatchLookups();
483 # Don't use any extra rows returned by the query
484 $numRows = min( $this->mResult
->numRows(), $this->mLimit
);
486 $s = $this->getStartBody();
488 if ( $this->mIsBackwards
) {
489 for ( $i = $numRows - 1; $i >= 0; $i-- ) {
490 $this->mResult
->seek( $i );
491 $row = $this->mResult
->fetchObject();
492 $s .= $this->formatRow( $row );
495 $this->mResult
->seek( 0 );
496 for ( $i = 0; $i < $numRows; $i++
) {
497 $row = $this->mResult
->fetchObject();
498 $s .= $this->formatRow( $row );
502 $s .= $this->getEmptyBody();
504 $s .= $this->getEndBody();
511 * @param string $text Text displayed on the link
512 * @param array|null $query Associative array of parameter to be in the query string
513 * @param string|null $type Link type used to create additional attributes, like "rel", "class" or
514 * "title". Valid values (non-exhaustive list): 'first', 'last', 'prev', 'next', 'asc', 'desc'.
515 * @return string HTML fragment
517 function makeLink( $text, array $query = null, $type = null ) {
518 if ( $query === null ) {
523 if ( in_array( $type, [ 'prev', 'next' ] ) ) {
524 $attrs['rel'] = $type;
527 if ( in_array( $type, [ 'asc', 'desc' ] ) ) {
528 $attrs['title'] = $this->msg( $type == 'asc' ?
'sort-ascending' : 'sort-descending' )->text();
532 $attrs['class'] = "mw-{$type}link";
535 return $this->getLinkRenderer()->makeKnownLink(
537 new HtmlArmor( $text ),
539 $query +
$this->getDefaultQuery()
544 * Called from getBody(), before getStartBody() is called and
545 * after doQuery() was called. This will be called only if there
546 * are rows in the result set.
550 protected function doBatchLookups() {
554 * Hook into getBody(), allows text to be inserted at the start. This
555 * will be called even if there are no rows in the result set.
559 protected function getStartBody() {
564 * Hook into getBody() for the end of the list
568 protected function getEndBody() {
573 * Hook into getBody(), for the bit between the start and the
574 * end when there are no rows
578 protected function getEmptyBody() {
583 * Get an array of query parameters that should be put into self-links.
584 * By default, all parameters passed in the URL are used, except for a
587 * @return array Associative array
589 function getDefaultQuery() {
590 if ( !isset( $this->mDefaultQuery
) ) {
591 $this->mDefaultQuery
= $this->getRequest()->getQueryValues();
592 unset( $this->mDefaultQuery
['title'] );
593 unset( $this->mDefaultQuery
['dir'] );
594 unset( $this->mDefaultQuery
['offset'] );
595 unset( $this->mDefaultQuery
['limit'] );
596 unset( $this->mDefaultQuery
['order'] );
597 unset( $this->mDefaultQuery
['month'] );
598 unset( $this->mDefaultQuery
['year'] );
600 return $this->mDefaultQuery
;
604 * Get the number of rows in the result set
608 function getNumRows() {
609 if ( !$this->mQueryDone
) {
612 return $this->mResult
->numRows();
616 * Get a URL query array for the prev, next, first and last links.
620 function getPagingQueries() {
621 if ( !$this->mQueryDone
) {
625 # Don't announce the limit everywhere if it's the default
626 $urlLimit = $this->mLimit
== $this->mDefaultLimit ?
null : $this->mLimit
;
628 if ( $this->mIsFirst
) {
634 'offset' => $this->mFirstShown
,
637 $first = [ 'limit' => $urlLimit ];
639 if ( $this->mIsLast
) {
643 $next = [ 'offset' => $this->mLastShown
, 'limit' => $urlLimit ];
644 $last = [ 'dir' => 'prev', 'limit' => $urlLimit ];
655 * Returns whether to show the "navigation bar"
659 function isNavigationBarShown() {
660 if ( !$this->mQueryDone
) {
663 // Hide navigation by default if there is nothing to page
664 return !( $this->mIsFirst
&& $this->mIsLast
);
668 * Get paging links. If a link is disabled, the item from $disabledTexts
669 * will be used. If there is no such item, the unlinked text from
670 * $linkTexts will be used. Both $linkTexts and $disabledTexts are arrays
673 * @param array $linkTexts
674 * @param array $disabledTexts
677 function getPagingLinks( $linkTexts, $disabledTexts = [] ) {
678 $queries = $this->getPagingQueries();
681 foreach ( $queries as $type => $query ) {
682 if ( $query !== false ) {
683 $links[$type] = $this->makeLink(
688 } elseif ( isset( $disabledTexts[$type] ) ) {
689 $links[$type] = $disabledTexts[$type];
691 $links[$type] = $linkTexts[$type];
698 function getLimitLinks() {
700 if ( $this->mIsBackwards
) {
701 $offset = $this->mPastTheEndIndex
;
703 $offset = $this->mOffset
;
705 foreach ( $this->mLimitsShown
as $limit ) {
706 $links[] = $this->makeLink(
707 $this->getLanguage()->formatNum( $limit ),
708 [ 'offset' => $offset, 'limit' => $limit ],
716 * Abstract formatting function. This should return an HTML string
717 * representing the result row $row. Rows will be concatenated and
718 * returned by getBody()
720 * @param array|stdClass $row Database row
723 abstract function formatRow( $row );
726 * This function should be overridden to provide all parameters
727 * needed for the main paged query. It returns an associative
728 * array with the following elements:
729 * tables => Table(s) for passing to Database::select()
730 * fields => Field(s) for passing to Database::select(), may be *
731 * conds => WHERE conditions
732 * options => option array
733 * join_conds => JOIN conditions
737 abstract function getQueryInfo();
740 * This function should be overridden to return the name of the index fi-
741 * eld. If the pager supports multiple orders, it may return an array of
742 * 'querykey' => 'indexfield' pairs, so that a request with &count=querykey
743 * will use indexfield to sort. In this case, the first returned key is
746 * Needless to say, it's really not a good idea to use a non-unique index
747 * for this! That won't page right.
749 * @return string|string[]
751 abstract function getIndexField();
754 * This function should be overridden to return the names of secondary columns
755 * to order by in addition to the column in getIndexField(). These fields will
756 * not be used in the pager offset or in any links for users.
758 * If getIndexField() returns an array of 'querykey' => 'indexfield' pairs then
759 * this must return a corresponding array of 'querykey' => [ fields... ] pairs
760 * in order for a request with &count=querykey to use [ fields... ] to sort.
762 * This is useful for pagers that GROUP BY a unique column (say page_id)
763 * and ORDER BY another (say page_len). Using GROUP BY and ORDER BY both on
764 * page_len,page_id avoids temp tables (given a page_len index). This would
765 * also work if page_id was non-unique but we had a page_len,page_id index.
767 * @return string[]|array[]
769 protected function getExtraSortFields() {
774 * Return the default sorting direction: DIR_ASCENDING or DIR_DESCENDING.
775 * You can also have an associative array of ordertype => dir,
776 * if multiple order types are supported. In this case getIndexField()
777 * must return an array, and the keys of that must exactly match the keys
780 * For backward compatibility, this method's return value will be ignored
781 * if $this->mDefaultDirection is already set when the constructor is
782 * called, for instance if it's statically initialized. In that case the
783 * value of that variable (which must be a boolean) will be used.
785 * Note that despite its name, this does not return the value of the
786 * $this->mDefaultDirection member variable. That's the default for this
787 * particular instantiation, which is a single value. This is the set of
788 * all defaults for the class.
792 protected function getDefaultDirections() {
793 return self
::DIR_ASCENDING
;
797 * Generate (prev x| next x) (20|50|100...) type links for paging
799 * @param LinkTarget $title
802 * @param array $query Optional URL query parameter string
803 * @param bool $atend Optional param for specified if this is the last page
806 protected function buildPrevNextNavigation( LinkTarget
$title, $offset, $limit,
807 array $query = [], $atend = false
809 $prevNext = new PrevNextNavigationRenderer( $this );
811 return $prevNext->buildPrevNextNavigation( $title, $offset, $limit, $query, $atend );
814 protected function getLinkRenderer() {
815 if ( $this->linkRenderer
=== null ) {
816 $this->linkRenderer
= MediaWikiServices
::getInstance()->getLinkRenderer();
818 return $this->linkRenderer
;