Fix use of GenderCache in ApiPageSet::processTitlesArray
[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 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;
30
31 /**
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.
37 *
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:
41 *
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.
60 *
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.
64 *
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.
70 *
71 * @ingroup Pager
72 */
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;
78
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;
83
84 /** @var WebRequest */
85 public $mRequest;
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 */
91 public $mOffset;
92 /** @var int The maximum number of entries to show */
93 public $mLimit;
94 /** @var bool Whether the listing query completed */
95 public $mQueryDone = false;
96 /** @var IDatabase */
97 public $mDb;
98 /** @var stdClass|bool|null Extra row fetched at the end to see if the end was reached */
99 public $mPastTheEndRow;
100
101 /**
102 * The index to actually be used for ordering. This is a single column,
103 * for one ordering, even if multiple orderings are supported.
104 * @var string
105 */
106 protected $mIndexField;
107 /**
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.
110 * @var string[]
111 */
112 protected $mExtraSortFields;
113 /** For pages that support multiple types of ordering, which one to use.
114 * @var string|null
115 */
116 protected $mOrderType;
117 /**
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.
124 *
125 * Like $mIndexField, $mDefaultDirection will be a single value even if the
126 * class supports multiple default directions for different order types.
127 * @var bool
128 */
129 public $mDefaultDirection;
130 /** @var bool */
131 public $mIsBackwards;
132
133 /** @var bool True if the current result set is the first one */
134 public $mIsFirst;
135 /** @var bool */
136 public $mIsLast;
137
138 /** @var mixed */
139 protected $mLastShown;
140 /** @var mixed */
141 protected $mFirstShown;
142 /** @var mixed */
143 protected $mPastTheEndIndex;
144 /** @var array */
145 protected $mDefaultQuery;
146 /** @var string */
147 protected $mNavigationBar;
148
149 /**
150 * Whether to include the offset in the query
151 * @var bool
152 */
153 protected $mIncludeOffset = false;
154
155 /**
156 * Result object for the query. Warning: seek before use.
157 *
158 * @var IResultWrapper
159 */
160 public $mResult;
161
162 /** @var LinkRenderer */
163 private $linkRenderer;
164
165 public function __construct( IContextSource $context = null, LinkRenderer $linkRenderer = null ) {
166 if ( $context ) {
167 $this->setContext( $context );
168 }
169
170 $this->mRequest = $this->getRequest();
171
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' );
176
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();
182 }
183
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 );
187
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]
196 : [];
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]
203 : [];
204 } else {
205 # $index is not an array
206 $this->mOrderType = null;
207 $this->mIndexField = $index;
208 $this->mExtraSortFields = (array)$extraSort;
209 }
210
211 if ( !isset( $this->mDefaultDirection ) ) {
212 $dir = $this->getDefaultDirections();
213 $this->mDefaultDirection = is_array( $dir )
214 ? $dir[$this->mOrderType]
215 : $dir;
216 }
217 $this->linkRenderer = $linkRenderer;
218 }
219
220 /**
221 * Get the Database object in use
222 *
223 * @return IDatabase
224 */
225 public function getDatabase() {
226 return $this->mDb;
227 }
228
229 /**
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.
233 */
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 );
239
240 $defaultOrder = ( $this->mDefaultDirection === self::DIR_ASCENDING )
241 ? self::QUERY_ASCENDING
242 : self::QUERY_DESCENDING;
243 $order = $this->mIsBackwards ? self::oppositeOrder( $defaultOrder ) : $defaultOrder;
244
245 # Plus an extra row so that we can tell the "next" link should be shown
246 $queryLimit = $this->mLimit + 1;
247
248 if ( $this->mOffset == '' ) {
249 $isFirst = true;
250 } else {
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;
259 }
260
261 $this->mResult = $this->reallyDoQuery(
262 $this->mOffset,
263 $queryLimit,
264 $order
265 );
266
267 $this->extractResultInfo( $isFirst, $queryLimit, $this->mResult );
268 $this->mQueryDone = true;
269
270 $this->preprocessResults( $this->mResult );
271 $this->mResult->rewind(); // Paranoia
272 }
273
274 /**
275 * @param bool $order One of the IndexPager::QUERY_* class constants
276 * @return bool The opposite query order as an IndexPager::QUERY_ constant
277 */
278 final protected static function oppositeOrder( $order ) {
279 return ( $order === self::QUERY_ASCENDING )
280 ? self::QUERY_DESCENDING
281 : self::QUERY_ASCENDING;
282 }
283
284 /**
285 * @return IResultWrapper The result wrapper.
286 */
287 function getResult() {
288 return $this->mResult;
289 }
290
291 /**
292 * Set the offset from an other source than the request
293 *
294 * @param int|string $offset
295 */
296 function setOffset( $offset ) {
297 $this->mOffset = $offset;
298 }
299
300 /**
301 * Set the limit from an other source than the request
302 *
303 * Verifies limit is between 1 and 5000
304 *
305 * @param int|string $limit
306 */
307 function setLimit( $limit ) {
308 $limit = (int)$limit;
309 // WebRequest::getLimitOffset() puts a cap of 5000, so do same here.
310 if ( $limit > 5000 ) {
311 $limit = 5000;
312 }
313 if ( $limit > 0 ) {
314 $this->mLimit = $limit;
315 }
316 }
317
318 /**
319 * Get the current limit
320 *
321 * @return int
322 */
323 function getLimit() {
324 return $this->mLimit;
325 }
326
327 /**
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.
331 *
332 * @param bool $include
333 */
334 public function setIncludeOffset( $include ) {
335 $this->mIncludeOffset = $include;
336 }
337
338 /**
339 * Extract some useful data from the result object for use by
340 * the navigation bar, put it into $this
341 *
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
346 */
347 function extractResultInfo( $isFirst, $limit, IResultWrapper $res ) {
348 $numRows = $res->numRows();
349 if ( $numRows ) {
350 # Remove any table prefix from index field
351 $parts = explode( '.', $this->mIndexField );
352 $indexColumn = end( $parts );
353
354 $row = $res->fetchRow();
355 $firstIndex = $row[$indexColumn];
356
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];
365 } else {
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];
375 }
376 } else {
377 $firstIndex = '';
378 $lastIndex = '';
379 $this->mPastTheEndRow = null;
380 $this->mPastTheEndIndex = '';
381 }
382
383 if ( $this->mIsBackwards ) {
384 $this->mIsFirst = ( $numRows < $limit );
385 $this->mIsLast = $isFirst;
386 $this->mLastShown = $firstIndex;
387 $this->mFirstShown = $lastIndex;
388 } else {
389 $this->mIsFirst = $isFirst;
390 $this->mIsLast = ( $numRows < $limit );
391 $this->mLastShown = $lastIndex;
392 $this->mFirstShown = $firstIndex;
393 }
394 }
395
396 /**
397 * Get some text to go in brackets in the "function name" part of the SQL comment
398 *
399 * @return string
400 */
401 function getSqlComment() {
402 return static::class;
403 }
404
405 /**
406 * Do a query with specified parameters, rather than using the object context
407 *
408 * @note For b/c, query direction is true for ascending and false for descending
409 *
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
414 */
415 public function reallyDoQuery( $offset, $limit, $order ) {
416 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
417 $this->buildQueryInfo( $offset, $limit, $order );
418
419 return $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
420 }
421
422 /**
423 * Build variables to use by the database wrapper.
424 *
425 * @note For b/c, query direction is true for ascending and false for descending
426 *
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
430 * @return array
431 */
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 ? '>=' : '>';
444 } else {
445 $orderBy = [];
446 foreach ( $sortColumns as $col ) {
447 $orderBy[] = $col . ' DESC';
448 }
449 $options['ORDER BY'] = $orderBy;
450 $operator = $this->mIncludeOffset ? '<=' : '<';
451 }
452 if ( $offset != '' ) {
453 $conds[] = $this->mIndexField . $operator . $this->mDb->addQuotes( $offset );
454 }
455 $options['LIMIT'] = intval( $limit );
456 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
457 }
458
459 /**
460 * Pre-process results; useful for performing batch existence checks, etc.
461 *
462 * @param IResultWrapper $result
463 */
464 protected function preprocessResults( $result ) {
465 }
466
467 /**
468 * Get the formatted result list. Calls getStartBody(), formatRow() and
469 * getEndBody(), concatenates the results and returns them.
470 *
471 * @return string
472 */
473 public function getBody() {
474 if ( !$this->mQueryDone ) {
475 $this->doQuery();
476 }
477
478 if ( $this->mResult->numRows() ) {
479 # Do any special query batches before display
480 $this->doBatchLookups();
481 }
482
483 # Don't use any extra rows returned by the query
484 $numRows = min( $this->mResult->numRows(), $this->mLimit );
485
486 $s = $this->getStartBody();
487 if ( $numRows ) {
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 );
493 }
494 } else {
495 $this->mResult->seek( 0 );
496 for ( $i = 0; $i < $numRows; $i++ ) {
497 $row = $this->mResult->fetchObject();
498 $s .= $this->formatRow( $row );
499 }
500 }
501 } else {
502 $s .= $this->getEmptyBody();
503 }
504 $s .= $this->getEndBody();
505 return $s;
506 }
507
508 /**
509 * Make a self-link
510 *
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
516 */
517 function makeLink( $text, array $query = null, $type = null ) {
518 if ( $query === null ) {
519 return $text;
520 }
521
522 $attrs = [];
523 if ( in_array( $type, [ 'prev', 'next' ] ) ) {
524 $attrs['rel'] = $type;
525 }
526
527 if ( in_array( $type, [ 'asc', 'desc' ] ) ) {
528 $attrs['title'] = $this->msg( $type == 'asc' ? 'sort-ascending' : 'sort-descending' )->text();
529 }
530
531 if ( $type ) {
532 $attrs['class'] = "mw-{$type}link";
533 }
534
535 return $this->getLinkRenderer()->makeKnownLink(
536 $this->getTitle(),
537 new HtmlArmor( $text ),
538 $attrs,
539 $query + $this->getDefaultQuery()
540 );
541 }
542
543 /**
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.
547 *
548 * @return void
549 */
550 protected function doBatchLookups() {
551 }
552
553 /**
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.
556 *
557 * @return string
558 */
559 protected function getStartBody() {
560 return '';
561 }
562
563 /**
564 * Hook into getBody() for the end of the list
565 *
566 * @return string
567 */
568 protected function getEndBody() {
569 return '';
570 }
571
572 /**
573 * Hook into getBody(), for the bit between the start and the
574 * end when there are no rows
575 *
576 * @return string
577 */
578 protected function getEmptyBody() {
579 return '';
580 }
581
582 /**
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
585 * short blacklist.
586 *
587 * @return array Associative array
588 */
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'] );
599 }
600 return $this->mDefaultQuery;
601 }
602
603 /**
604 * Get the number of rows in the result set
605 *
606 * @return int
607 */
608 function getNumRows() {
609 if ( !$this->mQueryDone ) {
610 $this->doQuery();
611 }
612 return $this->mResult->numRows();
613 }
614
615 /**
616 * Get a URL query array for the prev, next, first and last links.
617 *
618 * @return array
619 */
620 function getPagingQueries() {
621 if ( !$this->mQueryDone ) {
622 $this->doQuery();
623 }
624
625 # Don't announce the limit everywhere if it's the default
626 $urlLimit = $this->mLimit == $this->mDefaultLimit ? null : $this->mLimit;
627
628 if ( $this->mIsFirst ) {
629 $prev = false;
630 $first = false;
631 } else {
632 $prev = [
633 'dir' => 'prev',
634 'offset' => $this->mFirstShown,
635 'limit' => $urlLimit
636 ];
637 $first = [ 'limit' => $urlLimit ];
638 }
639 if ( $this->mIsLast ) {
640 $next = false;
641 $last = false;
642 } else {
643 $next = [ 'offset' => $this->mLastShown, 'limit' => $urlLimit ];
644 $last = [ 'dir' => 'prev', 'limit' => $urlLimit ];
645 }
646 return [
647 'prev' => $prev,
648 'next' => $next,
649 'first' => $first,
650 'last' => $last
651 ];
652 }
653
654 /**
655 * Returns whether to show the "navigation bar"
656 *
657 * @return bool
658 */
659 function isNavigationBarShown() {
660 if ( !$this->mQueryDone ) {
661 $this->doQuery();
662 }
663 // Hide navigation by default if there is nothing to page
664 return !( $this->mIsFirst && $this->mIsLast );
665 }
666
667 /**
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
671 * of HTML.
672 *
673 * @param array $linkTexts
674 * @param array $disabledTexts
675 * @return array
676 */
677 function getPagingLinks( $linkTexts, $disabledTexts = [] ) {
678 $queries = $this->getPagingQueries();
679 $links = [];
680
681 foreach ( $queries as $type => $query ) {
682 if ( $query !== false ) {
683 $links[$type] = $this->makeLink(
684 $linkTexts[$type],
685 $queries[$type],
686 $type
687 );
688 } elseif ( isset( $disabledTexts[$type] ) ) {
689 $links[$type] = $disabledTexts[$type];
690 } else {
691 $links[$type] = $linkTexts[$type];
692 }
693 }
694
695 return $links;
696 }
697
698 function getLimitLinks() {
699 $links = [];
700 if ( $this->mIsBackwards ) {
701 $offset = $this->mPastTheEndIndex;
702 } else {
703 $offset = $this->mOffset;
704 }
705 foreach ( $this->mLimitsShown as $limit ) {
706 $links[] = $this->makeLink(
707 $this->getLanguage()->formatNum( $limit ),
708 [ 'offset' => $offset, 'limit' => $limit ],
709 'num'
710 );
711 }
712 return $links;
713 }
714
715 /**
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()
719 *
720 * @param array|stdClass $row Database row
721 * @return string
722 */
723 abstract function formatRow( $row );
724
725 /**
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
734 *
735 * @return array
736 */
737 abstract function getQueryInfo();
738
739 /**
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
744 * the default.
745 *
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.
748 *
749 * @return string|string[]
750 */
751 abstract function getIndexField();
752
753 /**
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.
757 *
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.
761 *
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.
766 *
767 * @return string[]|array[]
768 */
769 protected function getExtraSortFields() {
770 return [];
771 }
772
773 /**
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
778 * of this.
779 *
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.
784 *
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.
789 *
790 * @return bool
791 */
792 protected function getDefaultDirections() {
793 return self::DIR_ASCENDING;
794 }
795
796 /**
797 * Generate (prev x| next x) (20|50|100...) type links for paging
798 *
799 * @param LinkTarget $title
800 * @param int $offset
801 * @param int $limit
802 * @param array $query Optional URL query parameter string
803 * @param bool $atend Optional param for specified if this is the last page
804 * @return string
805 */
806 protected function buildPrevNextNavigation( LinkTarget $title, $offset, $limit,
807 array $query = [], $atend = false
808 ) {
809 $prevNext = new PrevNextNavigationRenderer( $this );
810
811 return $prevNext->buildPrevNextNavigation( $title, $offset, $limit, $query, $atend );
812 }
813
814 protected function getLinkRenderer() {
815 if ( $this->linkRenderer === null ) {
816 $this->linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
817 }
818 return $this->linkRenderer;
819 }
820 }