Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / pager / IndexPager.php
1 <?php
2 /**
3 * Efficient paging for SQL queries.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Pager
22 */
23
24 use Wikimedia\Rdbms\IResultWrapper;
25 use Wikimedia\Rdbms\IDatabase;
26 use MediaWiki\Linker\LinkTarget;
27 use MediaWiki\Navigation\PrevNextNavigationRenderer;
28
29 /**
30 * IndexPager is an efficient pager which uses a (roughly unique) index in the
31 * data set to implement paging, rather than a "LIMIT offset,limit" clause.
32 * In MySQL, such a limit/offset clause requires counting through the
33 * specified number of offset rows to find the desired data, which can be
34 * expensive for large offsets.
35 *
36 * ReverseChronologicalPager is a child class of the abstract IndexPager, and
37 * contains some formatting and display code which is specific to the use of
38 * timestamps as indexes. Here is a synopsis of its operation:
39 *
40 * * The query is specified by the offset, limit and direction (dir)
41 * parameters, in addition to any subclass-specific parameters.
42 * * The offset is the non-inclusive start of the DB query. A row with an
43 * index value equal to the offset will never be shown.
44 * * The query may either be done backwards, where the rows are returned by
45 * the database in the opposite order to which they are displayed to the
46 * user, or forwards. This is specified by the "dir" parameter, dir=prev
47 * means backwards, anything else means forwards. The offset value
48 * specifies the start of the database result set, which may be either
49 * the start or end of the displayed data set. This allows "previous"
50 * links to be implemented without knowledge of the index value at the
51 * start of the previous page.
52 * * An additional row beyond the user-specified limit is always requested.
53 * This allows us to tell whether we should display a "next" link in the
54 * case of forwards mode, or a "previous" link in the case of backwards
55 * mode. Determining whether to display the other link (the one for the
56 * page before the start of the database result set) can be done
57 * heuristically by examining the offset.
58 *
59 * * An empty offset indicates that the offset condition should be omitted
60 * from the query. This naturally produces either the first page or the
61 * last page depending on the dir parameter.
62 *
63 * Subclassing the pager to implement concrete functionality should be fairly
64 * simple, please see the examples in HistoryAction.php and
65 * SpecialBlockList.php. You just need to override formatRow(),
66 * getQueryInfo() and getIndexField(). Don't forget to call the parent
67 * constructor if you override it.
68 *
69 * @ingroup Pager
70 */
71 abstract class IndexPager extends ContextSource implements Pager {
72 /** Backwards-compatible constant for $mDefaultDirection field (do not change) */
73 const DIR_ASCENDING = false;
74 /** Backwards-compatible constant for $mDefaultDirection field (do not change) */
75 const DIR_DESCENDING = true;
76
77 /** Backwards-compatible constant for reallyDoQuery() (do not change) */
78 const QUERY_ASCENDING = true;
79 /** Backwards-compatible constant for reallyDoQuery() (do not change) */
80 const QUERY_DESCENDING = false;
81
82 /** @var WebRequest */
83 public $mRequest;
84 /** @var int[] List of default entry limit options to be presented to clients */
85 public $mLimitsShown = [ 20, 50, 100, 250, 500 ];
86 /** @var int The default entry limit choosen for clients */
87 public $mDefaultLimit = 50;
88 /** @var mixed The starting point to enumerate entries */
89 public $mOffset;
90 /** @var int The maximum number of entries to show */
91 public $mLimit;
92 /** @var bool Whether the listing query completed */
93 public $mQueryDone = false;
94 /** @var IDatabase */
95 public $mDb;
96 /** @var stdClass|bool|null Extra row fetched at the end to see if the end was reached */
97 public $mPastTheEndRow;
98
99 /**
100 * The index to actually be used for ordering. This is a single column,
101 * for one ordering, even if multiple orderings are supported.
102 * @var string
103 */
104 protected $mIndexField;
105 /**
106 * An array of secondary columns to order by. These fields are not part of the offset.
107 * This is a column list for one ordering, even if multiple orderings are supported.
108 * @var string[]
109 */
110 protected $mExtraSortFields;
111 /** For pages that support multiple types of ordering, which one to use.
112 * @var string|null
113 */
114 protected $mOrderType;
115 /**
116 * $mDefaultDirection gives the direction to use when sorting results:
117 * DIR_ASCENDING or DIR_DESCENDING. If $mIsBackwards is set, we start from
118 * the opposite end, but we still sort the page itself according to
119 * $mDefaultDirection. For example, if $mDefaultDirection is DIR_ASCENDING
120 * but we're going backwards, we'll display the last page of results, but
121 * the last result will be at the bottom, not the top.
122 *
123 * Like $mIndexField, $mDefaultDirection will be a single value even if the
124 * class supports multiple default directions for different order types.
125 * @var bool
126 */
127 public $mDefaultDirection;
128 /** @var bool */
129 public $mIsBackwards;
130
131 /** @var bool True if the current result set is the first one */
132 public $mIsFirst;
133 /** @var bool */
134 public $mIsLast;
135
136 /** @var mixed */
137 protected $mLastShown;
138 /** @var mixed */
139 protected $mFirstShown;
140 /** @var mixed */
141 protected $mPastTheEndIndex;
142 /** @var array */
143 protected $mDefaultQuery;
144 /** @var string */
145 protected $mNavigationBar;
146
147 /**
148 * Whether to include the offset in the query
149 * @var bool
150 */
151 protected $mIncludeOffset = false;
152
153 /**
154 * Result object for the query. Warning: seek before use.
155 *
156 * @var IResultWrapper
157 */
158 public $mResult;
159
160 public function __construct( IContextSource $context = null ) {
161 if ( $context ) {
162 $this->setContext( $context );
163 }
164
165 $this->mRequest = $this->getRequest();
166
167 # NB: the offset is quoted, not validated. It is treated as an
168 # arbitrary string to support the widest variety of index types. Be
169 # careful outputting it into HTML!
170 $this->mOffset = $this->mRequest->getText( 'offset' );
171
172 # Use consistent behavior for the limit options
173 $this->mDefaultLimit = $this->getUser()->getIntOption( 'rclimit' );
174 if ( !$this->mLimit ) {
175 // Don't override if a subclass calls $this->setLimit() in its constructor.
176 list( $this->mLimit, /* $offset */ ) = $this->mRequest->getLimitOffset();
177 }
178
179 $this->mIsBackwards = ( $this->mRequest->getVal( 'dir' ) == 'prev' );
180 # Let the subclass set the DB here; otherwise use a replica DB for the current wiki
181 $this->mDb = $this->mDb ?: wfGetDB( DB_REPLICA );
182
183 $index = $this->getIndexField(); // column to sort on
184 $extraSort = $this->getExtraSortFields(); // extra columns to sort on for query planning
185 $order = $this->mRequest->getVal( 'order' );
186 if ( is_array( $index ) && isset( $index[$order] ) ) {
187 $this->mOrderType = $order;
188 $this->mIndexField = $index[$order];
189 $this->mExtraSortFields = isset( $extraSort[$order] )
190 ? (array)$extraSort[$order]
191 : [];
192 } elseif ( is_array( $index ) ) {
193 # First element is the default
194 $this->mIndexField = reset( $index );
195 $this->mOrderType = key( $index );
196 $this->mExtraSortFields = isset( $extraSort[$this->mOrderType] )
197 ? (array)$extraSort[$this->mOrderType]
198 : [];
199 } else {
200 # $index is not an array
201 $this->mOrderType = null;
202 $this->mIndexField = $index;
203 $this->mExtraSortFields = (array)$extraSort;
204 }
205
206 if ( !isset( $this->mDefaultDirection ) ) {
207 $dir = $this->getDefaultDirections();
208 $this->mDefaultDirection = is_array( $dir )
209 ? $dir[$this->mOrderType]
210 : $dir;
211 }
212 }
213
214 /**
215 * Get the Database object in use
216 *
217 * @return IDatabase
218 */
219 public function getDatabase() {
220 return $this->mDb;
221 }
222
223 /**
224 * Do the query, using information from the object context. This function
225 * has been kept minimal to make it overridable if necessary, to allow for
226 * result sets formed from multiple DB queries.
227 */
228 public function doQuery() {
229 # Use the child class name for profiling
230 $fname = __METHOD__ . ' (' . static::class . ')';
231 /** @noinspection PhpUnusedLocalVariableInspection */
232 $section = Profiler::instance()->scopedProfileIn( $fname );
233
234 $defaultOrder = ( $this->mDefaultDirection === self::DIR_ASCENDING )
235 ? self::QUERY_ASCENDING
236 : self::QUERY_DESCENDING;
237 $order = $this->mIsBackwards ? self::oppositeOrder( $defaultOrder ) : $defaultOrder;
238
239 # Plus an extra row so that we can tell the "next" link should be shown
240 $queryLimit = $this->mLimit + 1;
241
242 if ( $this->mOffset == '' ) {
243 $isFirst = true;
244 } else {
245 // If there's an offset, we may or may not be at the first entry.
246 // The only way to tell is to run the query in the opposite
247 // direction see if we get a row.
248 $oldIncludeOffset = $this->mIncludeOffset;
249 $this->mIncludeOffset = !$this->mIncludeOffset;
250 $oppositeOrder = self::oppositeOrder( $order );
251 $isFirst = !$this->reallyDoQuery( $this->mOffset, 1, $oppositeOrder )->numRows();
252 $this->mIncludeOffset = $oldIncludeOffset;
253 }
254
255 $this->mResult = $this->reallyDoQuery(
256 $this->mOffset,
257 $queryLimit,
258 $order
259 );
260
261 $this->extractResultInfo( $isFirst, $queryLimit, $this->mResult );
262 $this->mQueryDone = true;
263
264 $this->preprocessResults( $this->mResult );
265 $this->mResult->rewind(); // Paranoia
266 }
267
268 /**
269 * @param bool $order One of the IndexPager::QUERY_* class constants
270 * @return bool The opposite query order as an IndexPager::QUERY_ constant
271 */
272 final protected static function oppositeOrder( $order ) {
273 return ( $order === self::QUERY_ASCENDING )
274 ? self::QUERY_DESCENDING
275 : self::QUERY_ASCENDING;
276 }
277
278 /**
279 * @return IResultWrapper The result wrapper.
280 */
281 function getResult() {
282 return $this->mResult;
283 }
284
285 /**
286 * Set the offset from an other source than the request
287 *
288 * @param int|string $offset
289 */
290 function setOffset( $offset ) {
291 $this->mOffset = $offset;
292 }
293
294 /**
295 * Set the limit from an other source than the request
296 *
297 * Verifies limit is between 1 and 5000
298 *
299 * @param int|string $limit
300 */
301 function setLimit( $limit ) {
302 $limit = (int)$limit;
303 // WebRequest::getLimitOffset() puts a cap of 5000, so do same here.
304 if ( $limit > 5000 ) {
305 $limit = 5000;
306 }
307 if ( $limit > 0 ) {
308 $this->mLimit = $limit;
309 }
310 }
311
312 /**
313 * Get the current limit
314 *
315 * @return int
316 */
317 function getLimit() {
318 return $this->mLimit;
319 }
320
321 /**
322 * Set whether a row matching exactly the offset should be also included
323 * in the result or not. By default this is not the case, but when the
324 * offset is user-supplied this might be wanted.
325 *
326 * @param bool $include
327 */
328 public function setIncludeOffset( $include ) {
329 $this->mIncludeOffset = $include;
330 }
331
332 /**
333 * Extract some useful data from the result object for use by
334 * the navigation bar, put it into $this
335 *
336 * @param bool $isFirst False if there are rows before those fetched (i.e.
337 * if a "previous" link would make sense)
338 * @param int $limit Exact query limit
339 * @param IResultWrapper $res
340 */
341 function extractResultInfo( $isFirst, $limit, IResultWrapper $res ) {
342 $numRows = $res->numRows();
343 if ( $numRows ) {
344 # Remove any table prefix from index field
345 $parts = explode( '.', $this->mIndexField );
346 $indexColumn = end( $parts );
347
348 $row = $res->fetchRow();
349 $firstIndex = $row[$indexColumn];
350
351 # Discard the extra result row if there is one
352 if ( $numRows > $this->mLimit && $numRows > 1 ) {
353 $res->seek( $numRows - 1 );
354 $this->mPastTheEndRow = $res->fetchObject();
355 $this->mPastTheEndIndex = $this->mPastTheEndRow->$indexColumn;
356 $res->seek( $numRows - 2 );
357 $row = $res->fetchRow();
358 $lastIndex = $row[$indexColumn];
359 } else {
360 $this->mPastTheEndRow = null;
361 # Setting indexes to an empty string means that they will be
362 # omitted if they would otherwise appear in URLs. It just so
363 # happens that this is the right thing to do in the standard
364 # UI, in all the relevant cases.
365 $this->mPastTheEndIndex = '';
366 $res->seek( $numRows - 1 );
367 $row = $res->fetchRow();
368 $lastIndex = $row[$indexColumn];
369 }
370 } else {
371 $firstIndex = '';
372 $lastIndex = '';
373 $this->mPastTheEndRow = null;
374 $this->mPastTheEndIndex = '';
375 }
376
377 if ( $this->mIsBackwards ) {
378 $this->mIsFirst = ( $numRows < $limit );
379 $this->mIsLast = $isFirst;
380 $this->mLastShown = $firstIndex;
381 $this->mFirstShown = $lastIndex;
382 } else {
383 $this->mIsFirst = $isFirst;
384 $this->mIsLast = ( $numRows < $limit );
385 $this->mLastShown = $lastIndex;
386 $this->mFirstShown = $firstIndex;
387 }
388 }
389
390 /**
391 * Get some text to go in brackets in the "function name" part of the SQL comment
392 *
393 * @return string
394 */
395 function getSqlComment() {
396 return static::class;
397 }
398
399 /**
400 * Do a query with specified parameters, rather than using the object context
401 *
402 * @note For b/c, query direction is true for ascending and false for descending
403 *
404 * @param string $offset Index offset, inclusive
405 * @param int $limit Exact query limit
406 * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING
407 * @return IResultWrapper
408 */
409 public function reallyDoQuery( $offset, $limit, $order ) {
410 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
411 $this->buildQueryInfo( $offset, $limit, $order );
412
413 return $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
414 }
415
416 /**
417 * Build variables to use by the database wrapper.
418 *
419 * @note For b/c, query direction is true for ascending and false for descending
420 *
421 * @param string $offset Index offset, inclusive
422 * @param int $limit Exact query limit
423 * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING
424 * @return array
425 */
426 protected function buildQueryInfo( $offset, $limit, $order ) {
427 $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
428 $info = $this->getQueryInfo();
429 $tables = $info['tables'];
430 $fields = $info['fields'];
431 $conds = $info['conds'] ?? [];
432 $options = $info['options'] ?? [];
433 $join_conds = $info['join_conds'] ?? [];
434 $sortColumns = array_merge( [ $this->mIndexField ], $this->mExtraSortFields );
435 if ( $order === self::QUERY_ASCENDING ) {
436 $options['ORDER BY'] = $sortColumns;
437 $operator = $this->mIncludeOffset ? '>=' : '>';
438 } else {
439 $orderBy = [];
440 foreach ( $sortColumns as $col ) {
441 $orderBy[] = $col . ' DESC';
442 }
443 $options['ORDER BY'] = $orderBy;
444 $operator = $this->mIncludeOffset ? '<=' : '<';
445 }
446 if ( $offset != '' ) {
447 $conds[] = $this->mIndexField . $operator . $this->mDb->addQuotes( $offset );
448 }
449 $options['LIMIT'] = intval( $limit );
450 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
451 }
452
453 /**
454 * Pre-process results; useful for performing batch existence checks, etc.
455 *
456 * @param IResultWrapper $result
457 */
458 protected function preprocessResults( $result ) {
459 }
460
461 /**
462 * Get the formatted result list. Calls getStartBody(), formatRow() and
463 * getEndBody(), concatenates the results and returns them.
464 *
465 * @return string
466 */
467 public function getBody() {
468 if ( !$this->mQueryDone ) {
469 $this->doQuery();
470 }
471
472 if ( $this->mResult->numRows() ) {
473 # Do any special query batches before display
474 $this->doBatchLookups();
475 }
476
477 # Don't use any extra rows returned by the query
478 $numRows = min( $this->mResult->numRows(), $this->mLimit );
479
480 $s = $this->getStartBody();
481 if ( $numRows ) {
482 if ( $this->mIsBackwards ) {
483 for ( $i = $numRows - 1; $i >= 0; $i-- ) {
484 $this->mResult->seek( $i );
485 $row = $this->mResult->fetchObject();
486 $s .= $this->formatRow( $row );
487 }
488 } else {
489 $this->mResult->seek( 0 );
490 for ( $i = 0; $i < $numRows; $i++ ) {
491 $row = $this->mResult->fetchObject();
492 $s .= $this->formatRow( $row );
493 }
494 }
495 } else {
496 $s .= $this->getEmptyBody();
497 }
498 $s .= $this->getEndBody();
499 return $s;
500 }
501
502 /**
503 * Make a self-link
504 *
505 * @param string $text Text displayed on the link
506 * @param array|null $query Associative array of parameter to be in the query string
507 * @param string|null $type Link type used to create additional attributes, like "rel", "class" or
508 * "title". Valid values (non-exhaustive list): 'first', 'last', 'prev', 'next', 'asc', 'desc'.
509 * @return string HTML fragment
510 */
511 function makeLink( $text, array $query = null, $type = null ) {
512 if ( $query === null ) {
513 return $text;
514 }
515
516 $attrs = [];
517 if ( in_array( $type, [ 'prev', 'next' ] ) ) {
518 $attrs['rel'] = $type;
519 }
520
521 if ( in_array( $type, [ 'asc', 'desc' ] ) ) {
522 $attrs['title'] = $this->msg( $type == 'asc' ? 'sort-ascending' : 'sort-descending' )->text();
523 }
524
525 if ( $type ) {
526 $attrs['class'] = "mw-{$type}link";
527 }
528
529 return Linker::linkKnown(
530 $this->getTitle(),
531 $text,
532 $attrs,
533 $query + $this->getDefaultQuery()
534 );
535 }
536
537 /**
538 * Called from getBody(), before getStartBody() is called and
539 * after doQuery() was called. This will be called only if there
540 * are rows in the result set.
541 *
542 * @return void
543 */
544 protected function doBatchLookups() {
545 }
546
547 /**
548 * Hook into getBody(), allows text to be inserted at the start. This
549 * will be called even if there are no rows in the result set.
550 *
551 * @return string
552 */
553 protected function getStartBody() {
554 return '';
555 }
556
557 /**
558 * Hook into getBody() for the end of the list
559 *
560 * @return string
561 */
562 protected function getEndBody() {
563 return '';
564 }
565
566 /**
567 * Hook into getBody(), for the bit between the start and the
568 * end when there are no rows
569 *
570 * @return string
571 */
572 protected function getEmptyBody() {
573 return '';
574 }
575
576 /**
577 * Get an array of query parameters that should be put into self-links.
578 * By default, all parameters passed in the URL are used, except for a
579 * short blacklist.
580 *
581 * @return array Associative array
582 */
583 function getDefaultQuery() {
584 if ( !isset( $this->mDefaultQuery ) ) {
585 $this->mDefaultQuery = $this->getRequest()->getQueryValues();
586 unset( $this->mDefaultQuery['title'] );
587 unset( $this->mDefaultQuery['dir'] );
588 unset( $this->mDefaultQuery['offset'] );
589 unset( $this->mDefaultQuery['limit'] );
590 unset( $this->mDefaultQuery['order'] );
591 unset( $this->mDefaultQuery['month'] );
592 unset( $this->mDefaultQuery['year'] );
593 }
594 return $this->mDefaultQuery;
595 }
596
597 /**
598 * Get the number of rows in the result set
599 *
600 * @return int
601 */
602 function getNumRows() {
603 if ( !$this->mQueryDone ) {
604 $this->doQuery();
605 }
606 return $this->mResult->numRows();
607 }
608
609 /**
610 * Get a URL query array for the prev, next, first and last links.
611 *
612 * @return array
613 */
614 function getPagingQueries() {
615 if ( !$this->mQueryDone ) {
616 $this->doQuery();
617 }
618
619 # Don't announce the limit everywhere if it's the default
620 $urlLimit = $this->mLimit == $this->mDefaultLimit ? null : $this->mLimit;
621
622 if ( $this->mIsFirst ) {
623 $prev = false;
624 $first = false;
625 } else {
626 $prev = [
627 'dir' => 'prev',
628 'offset' => $this->mFirstShown,
629 'limit' => $urlLimit
630 ];
631 $first = [ 'limit' => $urlLimit ];
632 }
633 if ( $this->mIsLast ) {
634 $next = false;
635 $last = false;
636 } else {
637 $next = [ 'offset' => $this->mLastShown, 'limit' => $urlLimit ];
638 $last = [ 'dir' => 'prev', 'limit' => $urlLimit ];
639 }
640 return [
641 'prev' => $prev,
642 'next' => $next,
643 'first' => $first,
644 'last' => $last
645 ];
646 }
647
648 /**
649 * Returns whether to show the "navigation bar"
650 *
651 * @return bool
652 */
653 function isNavigationBarShown() {
654 if ( !$this->mQueryDone ) {
655 $this->doQuery();
656 }
657 // Hide navigation by default if there is nothing to page
658 return !( $this->mIsFirst && $this->mIsLast );
659 }
660
661 /**
662 * Get paging links. If a link is disabled, the item from $disabledTexts
663 * will be used. If there is no such item, the unlinked text from
664 * $linkTexts will be used. Both $linkTexts and $disabledTexts are arrays
665 * of HTML.
666 *
667 * @param array $linkTexts
668 * @param array $disabledTexts
669 * @return array
670 */
671 function getPagingLinks( $linkTexts, $disabledTexts = [] ) {
672 $queries = $this->getPagingQueries();
673 $links = [];
674
675 foreach ( $queries as $type => $query ) {
676 if ( $query !== false ) {
677 $links[$type] = $this->makeLink(
678 $linkTexts[$type],
679 $queries[$type],
680 $type
681 );
682 } elseif ( isset( $disabledTexts[$type] ) ) {
683 $links[$type] = $disabledTexts[$type];
684 } else {
685 $links[$type] = $linkTexts[$type];
686 }
687 }
688
689 return $links;
690 }
691
692 function getLimitLinks() {
693 $links = [];
694 if ( $this->mIsBackwards ) {
695 $offset = $this->mPastTheEndIndex;
696 } else {
697 $offset = $this->mOffset;
698 }
699 foreach ( $this->mLimitsShown as $limit ) {
700 $links[] = $this->makeLink(
701 $this->getLanguage()->formatNum( $limit ),
702 [ 'offset' => $offset, 'limit' => $limit ],
703 'num'
704 );
705 }
706 return $links;
707 }
708
709 /**
710 * Abstract formatting function. This should return an HTML string
711 * representing the result row $row. Rows will be concatenated and
712 * returned by getBody()
713 *
714 * @param array|stdClass $row Database row
715 * @return string
716 */
717 abstract function formatRow( $row );
718
719 /**
720 * This function should be overridden to provide all parameters
721 * needed for the main paged query. It returns an associative
722 * array with the following elements:
723 * tables => Table(s) for passing to Database::select()
724 * fields => Field(s) for passing to Database::select(), may be *
725 * conds => WHERE conditions
726 * options => option array
727 * join_conds => JOIN conditions
728 *
729 * @return array
730 */
731 abstract function getQueryInfo();
732
733 /**
734 * This function should be overridden to return the name of the index fi-
735 * eld. If the pager supports multiple orders, it may return an array of
736 * 'querykey' => 'indexfield' pairs, so that a request with &count=querykey
737 * will use indexfield to sort. In this case, the first returned key is
738 * the default.
739 *
740 * Needless to say, it's really not a good idea to use a non-unique index
741 * for this! That won't page right.
742 *
743 * @return string|string[]
744 */
745 abstract function getIndexField();
746
747 /**
748 * This function should be overridden to return the names of secondary columns
749 * to order by in addition to the column in getIndexField(). These fields will
750 * not be used in the pager offset or in any links for users.
751 *
752 * If getIndexField() returns an array of 'querykey' => 'indexfield' pairs then
753 * this must return a corresponding array of 'querykey' => [ fields... ] pairs
754 * in order for a request with &count=querykey to use [ fields... ] to sort.
755 *
756 * This is useful for pagers that GROUP BY a unique column (say page_id)
757 * and ORDER BY another (say page_len). Using GROUP BY and ORDER BY both on
758 * page_len,page_id avoids temp tables (given a page_len index). This would
759 * also work if page_id was non-unique but we had a page_len,page_id index.
760 *
761 * @return string[]|array[]
762 */
763 protected function getExtraSortFields() {
764 return [];
765 }
766
767 /**
768 * Return the default sorting direction: DIR_ASCENDING or DIR_DESCENDING.
769 * You can also have an associative array of ordertype => dir,
770 * if multiple order types are supported. In this case getIndexField()
771 * must return an array, and the keys of that must exactly match the keys
772 * of this.
773 *
774 * For backward compatibility, this method's return value will be ignored
775 * if $this->mDefaultDirection is already set when the constructor is
776 * called, for instance if it's statically initialized. In that case the
777 * value of that variable (which must be a boolean) will be used.
778 *
779 * Note that despite its name, this does not return the value of the
780 * $this->mDefaultDirection member variable. That's the default for this
781 * particular instantiation, which is a single value. This is the set of
782 * all defaults for the class.
783 *
784 * @return bool
785 */
786 protected function getDefaultDirections() {
787 return self::DIR_ASCENDING;
788 }
789
790 /**
791 * Generate (prev x| next x) (20|50|100...) type links for paging
792 *
793 * @param LinkTarget $title
794 * @param int $offset
795 * @param int $limit
796 * @param array $query Optional URL query parameter string
797 * @param bool $atend Optional param for specified if this is the last page
798 * @return string
799 */
800 protected function buildPrevNextNavigation( LinkTarget $title, $offset, $limit,
801 array $query = [], $atend = false
802 ) {
803 $prevNext = new PrevNextNavigationRenderer( $this );
804
805 return $prevNext->buildPrevNextNavigation( $title, $offset, $limit, $query, $atend );
806 }
807 }