MediaWiki  1.23.0
IndexPager Class Reference

IndexPager is an efficient pager which uses a (roughly unique) index in the data set to implement paging, rather than a "LIMIT offset,limit" clause. More...

Inheritance diagram for IndexPager:
Collaboration diagram for IndexPager:

Public Member Functions

 __construct (IContextSource $context=null)
 
 doQuery ()
 Do the query, using information from the object context. More...
 
 extractResultInfo ( $isFirst, $limit, ResultWrapper $res)
 Extract some useful data from the result object for use by the navigation bar, put it into $this. More...
 
 formatRow ( $row)
 Abstract formatting function. More...
 
 getBody ()
 Get the formatted result list. More...
 
 getDatabase ()
 Get the Database object in use. More...
 
 getDefaultQuery ()
 Get an array of query parameters that should be put into self-links. More...
 
 getIndexField ()
 This function should be overridden to return the name of the index fi- eld. More...
 
 getLimit ()
 Get the current limit. More...
 
 getLimitLinks ()
 
 getNumRows ()
 Get the number of rows in the result set. More...
 
 getPagingLinks ( $linkTexts, $disabledTexts=array())
 Get paging links. More...
 
 getPagingQueries ()
 Get a URL query array for the prev, next, first and last links. More...
 
 getQueryInfo ()
 This function should be overridden to provide all parameters needed for the main paged query. More...
 
 getResult ()
 
 getSqlComment ()
 Get some text to go in brackets in the "function name" part of the SQL comment. More...
 
 isNavigationBarShown ()
 Returns whether to show the "navigation bar". More...
 
 makeLink ( $text, array $query=null, $type=null)
 Make a self-link. More...
 
 reallyDoQuery ( $offset, $limit, $descending)
 Do a query with specified parameters, rather than using the object context. More...
 
 setIncludeOffset ( $include)
 Set whether a row matching exactly the offset should be also included in the result or not. More...
 
 setLimit ( $limit)
 Set the limit from an other source than the request. More...
 
 setOffset ( $offset)
 Set the offset from an other source than the request. More...
 
- Public Member Functions inherited from ContextSource
 canUseWikiPage ()
 Check whether a WikiPage object can be get with getWikiPage(). More...
 
 exportSession ()
 Export the resolved user IP, HTTP headers, user ID, and session ID. More...
 
 getConfig ()
 Get the Config object. More...
 
 getContext ()
 Get the RequestContext object. More...
 
 getLang ()
 Get the Language object. More...
 
 getLanguage ()
 Get the Language object. More...
 
 getOutput ()
 Get the OutputPage object. More...
 
 getRequest ()
 Get the WebRequest object. More...
 
 getSkin ()
 Get the Skin object. More...
 
 getTitle ()
 Get the Title object. More...
 
 getUser ()
 Get the User object. More...
 
 getWikiPage ()
 Get the WikiPage object. More...
 
 msg ()
 Get a Message object with context set Parameters are the same as wfMessage() More...
 
 setContext (IContextSource $context)
 Set the IContextSource object. More...
 
- Public Member Functions inherited from Pager
 getNavigationBar ()
 

Public Attributes

 $mDb
 
 $mDefaultDirection
 $mDefaultDirection gives the direction to use when sorting results: false for ascending, true for descending. More...
 
 $mDefaultLimit = 50
 
 $mDefaultQuery
 
 $mFirstShown
 
 $mIsBackwards
 
 $mIsFirst
 True if the current result set is the first one. More...
 
 $mIsLast
 
 $mLimit
 
 $mLimitsShown = array( 20, 50, 100, 250, 500 )
 
 $mNavigationBar
 
 $mOffset
 
 $mPastTheEndIndex
 
 $mPastTheEndRow
 
 $mQueryDone = false
 
 $mRequest
 
ResultWrapper $mResult
 Result object for the query. More...
 

Protected Member Functions

 buildQueryInfo ( $offset, $limit, $descending)
 Build variables to use by the database wrapper. More...
 
 doBatchLookups ()
 Called from getBody(), before getStartBody() is called and after doQuery() was called. More...
 
 getDefaultDirections ()
 Return the default sorting direction: false for ascending, true for descending. More...
 
 getEmptyBody ()
 Hook into getBody(), for the bit between the start and the end when there are no rows. More...
 
 getEndBody ()
 Hook into getBody() for the end of the list. More...
 
 getExtraSortFields ()
 This function should be overridden to return the names of secondary columns to order by in addition to the column in getIndexField(). More...
 
 getStartBody ()
 Hook into getBody(), allows text to be inserted at the start. More...
 
 preprocessResults ( $result)
 Pre-process results; useful for performing batch existence checks, etc. More...
 

Protected Attributes

 $mExtraSortFields
 An array of secondary columns to order by. More...
 
 $mIncludeOffset = false
 Whether to include the offset in the query. More...
 
 $mIndexField
 The index to actually be used for ordering. More...
 
 $mLastShown
 
 $mOrderType
 For pages that support multiple types of ordering, which one to use. More...
 

Detailed Description

IndexPager is an efficient pager which uses a (roughly unique) index in the data set to implement paging, rather than a "LIMIT offset,limit" clause.

In MySQL, such a limit/offset clause requires counting through the specified number of offset rows to find the desired data, which can be expensive for large offsets.

ReverseChronologicalPager is a child class of the abstract IndexPager, and contains some formatting and display code which is specific to the use of timestamps as indexes. Here is a synopsis of its operation:

  • The query is specified by the offset, limit and direction (dir) parameters, in addition to any subclass-specific parameters.
  • The offset is the non-inclusive start of the DB query. A row with an index value equal to the offset will never be shown.
  • The query may either be done backwards, where the rows are returned by the database in the opposite order to which they are displayed to the user, or forwards. This is specified by the "dir" parameter, dir=prev means backwards, anything else means forwards. The offset value specifies the start of the database result set, which may be either the start or end of the displayed data set. This allows "previous" links to be implemented without knowledge of the index value at the start of the previous page.
  • An additional row beyond the user-specified limit is always requested. This allows us to tell whether we should display a "next" link in the case of forwards mode, or a "previous" link in the case of backwards mode. Determining whether to display the other link (the one for the page before the start of the database result set) can be done heuristically by examining the offset.
  • An empty offset indicates that the offset condition should be omitted from the query. This naturally produces either the first page or the last page depending on the dir parameter.

Subclassing the pager to implement concrete functionality should be fairly simple, please see the examples in HistoryPage.php and SpecialBlockList.php. You just need to override formatRow(), getQueryInfo() and getIndexField(). Don't forget to call the parent constructor if you override it.

Definition at line 79 of file Pager.php.

Constructor & Destructor Documentation

◆ __construct()

Member Function Documentation

◆ buildQueryInfo()

IndexPager::buildQueryInfo (   $offset,
  $limit,
  $descending 
)
protected

Build variables to use by the database wrapper.

Parameters
string$offsetindex offset, inclusive
$limitInteger: exact query limit
$descendingBoolean: query direction, false for ascending, true for descending
Returns
array

Definition at line 379 of file Pager.php.

References $fname, $limit, $options, $tables, array(), as, getQueryInfo(), and getSqlComment().

Referenced by ImageListPager\reallyDoQuery(), and reallyDoQuery().

◆ doBatchLookups()

IndexPager::doBatchLookups ( )
protected

Called from getBody(), before getStartBody() is called and after doQuery() was called.

This will be called only if there are rows in the result set.

Returns
void

Reimplemented in ContribsPager, HistoryPager, ImageListPager, UsersPager, and ActiveUsersPager.

Definition at line 493 of file Pager.php.

Referenced by getBody().

◆ doQuery()

IndexPager::doQuery ( )

Do the query, using information from the object context.

This function has been kept minimal to make it overridable if necessary, to allow for result sets formed from multiple DB queries.

Reimplemented in ImageHistoryPseudoPager, and LogPager.

Definition at line 200 of file Pager.php.

References $fname, $mDefaultDirection, $mIncludeOffset, extractResultInfo(), preprocessResults(), reallyDoQuery(), wfProfileIn(), and wfProfileOut().

Referenced by getBody(), getNumRows(), getPagingQueries(), and isNavigationBarShown().

◆ extractResultInfo()

IndexPager::extractResultInfo (   $isFirst,
  $limit,
ResultWrapper  $res 
)

Extract some useful data from the result object for use by the navigation bar, put it into $this.

Parameters
$isFirstbool: False if there are rows before those fetched (i.e. if a "previous" link would make sense)
$limitInteger: exact query limit
$resResultWrapper

Definition at line 299 of file Pager.php.

References $limit, and $res.

Referenced by doQuery().

◆ formatRow()

IndexPager::formatRow (   $row)
abstract

Abstract formatting function.

This should return an HTML string representing the result row $row. Rows will be concatenated and returned by getBody()

Parameters
array | stdClass$rowDatabase row
Returns
string

Reimplemented in ImageHistoryPseudoPager, TablePager, ContribsPager, NewPagesPager, MergeHistoryPager, HistoryPager, AllmessagesTablePager, LogPager, ProtectedTitlesPager, UsersPager, DeletedContribsPager, ActiveUsersPager, NewFilesPager, and CategoryPager.

Referenced by getBody().

◆ getBody()

IndexPager::getBody ( )

Get the formatted result list.

Calls getStartBody(), formatRow() and getEndBody(), concatenates the results and returns them.

Returns
String

Implements Pager.

Reimplemented in ImageHistoryPseudoPager, and CategoryPager.

Definition at line 419 of file Pager.php.

References $mLimit, $s, doBatchLookups(), doQuery(), formatRow(), getEmptyBody(), getEndBody(), and getStartBody().

◆ getDatabase()

IndexPager::getDatabase ( )

Get the Database object in use.

Returns
DatabaseBase

Reimplemented in DeletedContribsPager.

Definition at line 191 of file Pager.php.

References $mDb.

Referenced by ActiveUsersPager\doBatchLookups(), and ActiveUsersPager\getQueryInfo().

◆ getDefaultDirections()

IndexPager::getDefaultDirections ( )
protected

Return the default sorting direction: false for ascending, true for descending.

You can also have an associative array of ordertype => dir, if multiple order types are supported. In this case getIndexField() must return an array, and the keys of that must exactly match the keys of this.

For backward compatibility, this method's return value will be ignored if $this->mDefaultDirection is already set when the constructor is called, for instance if it's statically initialized. In that case the value of that variable (which must be a boolean) will be used.

Note that despite its name, this does not return the value of the $this->mDefaultDirection member variable. That's the default for this particular instantiation, which is a single value. This is the set of all defaults for the class.

Returns
Boolean

Reimplemented in CategoryPager.

Definition at line 734 of file Pager.php.

Referenced by __construct().

◆ getDefaultQuery()

IndexPager::getDefaultQuery ( )

Get an array of query parameters that should be put into self-links.

By default, all parameters passed in the URL are used, except for a short blacklist.

Returns
array Associative array

Reimplemented in ContribsPager, ImageListPager, UsersPager, CategoryPager, LogPager, and DeletedContribsPager.

Definition at line 531 of file Pager.php.

References $mDefaultQuery, and ContextSource\getRequest().

Referenced by makeLink().

◆ getEmptyBody()

IndexPager::getEmptyBody ( )
protected

Hook into getBody(), for the bit between the start and the end when there are no rows.

Returns
String

Reimplemented in TablePager.

Definition at line 520 of file Pager.php.

Referenced by getBody().

◆ getEndBody()

IndexPager::getEndBody ( )
protected

Hook into getBody() for the end of the list.

Returns
String

Reimplemented in TablePager, ContribsPager, NewPagesPager, HistoryPager, NewFilesPager, and DeletedContribsPager.

Definition at line 510 of file Pager.php.

Referenced by getBody().

◆ getExtraSortFields()

IndexPager::getExtraSortFields ( )
protected

This function should be overridden to return the names of secondary columns to order by in addition to the column in getIndexField().

These fields will not be used in the pager offset or in any links for users.

If getIndexField() returns an array of 'querykey' => 'indexfield' pairs then this must return a corresponding array of 'querykey' => array( fields...) pairs in order for a request with &count=querykey to use array( fields...) to sort.

This is useful for pagers that GROUP BY a unique column (say page_id) and ORDER BY another (say page_len). Using GROUP BY and ORDER BY both on page_len,page_id avoids temp tables (given a page_len index). This would also work if page_id was non-unique but we had a page_len,page_id index.

Returns
Array

Definition at line 711 of file Pager.php.

References array().

Referenced by __construct().

◆ getIndexField()

IndexPager::getIndexField ( )
abstract

This function should be overridden to return the name of the index fi- eld.

If the pager supports multiple orders, it may return an array of 'querykey' => 'indexfield' pairs, so that a request with &count=querykey will use indexfield to sort. In this case, the first returned key is the default.

Needless to say, it's really not a good idea to use a non-unique index for this! That won't page right.

Returns
string|Array

Reimplemented in ImageHistoryPseudoPager, TablePager, ContribsPager, NewPagesPager, ProtectedPagesPager, MergeHistoryPager, HistoryPager, BlockListPager, LogPager, ProtectedTitlesPager, CategoryPager, NewFilesPager, UsersPager, DeletedContribsPager, and ActiveUsersPager.

Referenced by __construct(), and AlphabeticPager\getNavigationBar().

◆ getLimit()

IndexPager::getLimit ( )

Get the current limit.

Returns
int

Definition at line 275 of file Pager.php.

References $mLimit.

◆ getLimitLinks()

◆ getNumRows()

IndexPager::getNumRows ( )

Get the number of rows in the result set.

Returns
Integer

Definition at line 550 of file Pager.php.

References doQuery().

Referenced by HistoryPager\diffButtons(), HistoryPager\getEndBody(), LogPager\getStartBody(), and HistoryPager\submitButton().

◆ getPagingLinks()

IndexPager::getPagingLinks (   $linkTexts,
  $disabledTexts = array() 
)

Get paging links.

If a link is disabled, the item from $disabledTexts will be used. If there is no such item, the unlinked text from $linkTexts will be used. Both $linkTexts and $disabledTexts are arrays of HTML.

Parameters
$linkTextsArray
$disabledTextsArray
Returns
Array

Definition at line 619 of file Pager.php.

References $queries, $query, $type, array(), as, getPagingQueries(), and makeLink().

Referenced by DeletedContribsPager\getNavigationBar(), AlphabeticPager\getNavigationBar(), ReverseChronologicalPager\getNavigationBar(), and TablePager\getNavigationBar().

◆ getPagingQueries()

IndexPager::getPagingQueries ( )

Get a URL query array for the prev, next, first and last links.

Returns
Array

Reimplemented in ImageListPager.

Definition at line 562 of file Pager.php.

References $last, $mLimit, array(), and doQuery().

Referenced by getPagingLinks().

◆ getQueryInfo()

IndexPager::getQueryInfo ( )
abstract

This function should be overridden to provide all parameters needed for the main paged query.

It returns an associative array with the following elements: tables => Table(s) for passing to Database::select() fields => Field(s) for passing to Database::select(), may be * conds => WHERE conditions options => option array join_conds => JOIN conditions

Returns
Array

Reimplemented in ImageHistoryPseudoPager, ContribsPager, NewPagesPager, ProtectedPagesPager, MergeHistoryPager, AllmessagesTablePager, HistoryPager, BlockListPager, ProtectedTitlesPager, LogPager, ImageListPager, CategoryPager, UsersPager, ActiveUsersPager, NewFilesPager, and DeletedContribsPager.

Referenced by buildQueryInfo().

◆ getResult()

IndexPager::getResult ( )
Returns
ResultWrapper The result wrapper.

Definition at line 239 of file Pager.php.

References $mResult.

◆ getSqlComment()

IndexPager::getSqlComment ( )

Get some text to go in brackets in the "function name" part of the SQL comment.

Returns
String

Reimplemented in ContribsPager, and HistoryPager.

Definition at line 353 of file Pager.php.

Referenced by buildQueryInfo().

◆ getStartBody()

IndexPager::getStartBody ( )
protected

Hook into getBody(), allows text to be inserted at the start.

This will be called even if there are no rows in the result set.

Returns
String

Reimplemented in TablePager, ContribsPager, NewPagesPager, HistoryPager, MergeHistoryPager, AllmessagesTablePager, LogPager, ProtectedTitlesPager, NewFilesPager, and DeletedContribsPager.

Definition at line 501 of file Pager.php.

Referenced by getBody().

◆ isNavigationBarShown()

IndexPager::isNavigationBarShown ( )

Returns whether to show the "navigation bar".

Returns
Boolean

Definition at line 601 of file Pager.php.

References $mIsLast, and doQuery().

Referenced by AlphabeticPager\getNavigationBar(), ReverseChronologicalPager\getNavigationBar(), and TablePager\getNavigationBar().

◆ makeLink()

IndexPager::makeLink (   $text,
array  $query = null,
  $type = null 
)

Make a self-link.

Parameters
string$texttext displayed on the link
array$queryassociative array of parameter to be in the query string
string$typevalue of the "rel" attribute
Returns
String: HTML fragment

Definition at line 463 of file Pager.php.

References $query, $type, array(), getDefaultQuery(), ContextSource\getTitle(), and Linker\linkKnown().

Referenced by getLimitLinks(), AlphabeticPager\getNavigationBar(), getPagingLinks(), and TablePager\getStartBody().

◆ preprocessResults()

IndexPager::preprocessResults (   $result)
protected

Pre-process results; useful for performing batch existence checks, etc.

Parameters
$resultResultWrapper

Reimplemented in BlockListPager, and ProtectedPagesPager.

Definition at line 411 of file Pager.php.

Referenced by doQuery().

◆ reallyDoQuery()

IndexPager::reallyDoQuery (   $offset,
  $limit,
  $descending 
)

Do a query with specified parameters, rather than using the object context.

Parameters
string$offsetindex offset, inclusive
$limitInteger: exact query limit
$descendingBoolean: query direction, false for ascending, true for descending
Returns
ResultWrapper

Reimplemented in ContribsPager, AllmessagesTablePager, and ImageListPager.

Definition at line 366 of file Pager.php.

References $fname, $limit, $options, $tables, buildQueryInfo(), and list.

Referenced by doQuery().

◆ setIncludeOffset()

IndexPager::setIncludeOffset (   $include)

Set whether a row matching exactly the offset should be also included in the result or not.

By default this is not the case, but when the offset is user-supplied this might be wanted.

Parameters
$includebool

Definition at line 286 of file Pager.php.

Referenced by CategoryPager\__construct().

◆ setLimit()

IndexPager::setLimit (   $limit)

Set the limit from an other source than the request.

Verifies limit is between 1 and 5000

Parameters
$limitInt|String

Definition at line 259 of file Pager.php.

References $limit.

Referenced by NewFilesPager\__construct().

◆ setOffset()

IndexPager::setOffset (   $offset)

Set the offset from an other source than the request.

Parameters
$offsetInt|String

Definition at line 248 of file Pager.php.

Referenced by CategoryPager\__construct().

Member Data Documentation

◆ $mDb

IndexPager::$mDb

Definition at line 85 of file Pager.php.

Referenced by getDatabase(), and LogPager\limitTitle().

◆ $mDefaultDirection

IndexPager::$mDefaultDirection

$mDefaultDirection gives the direction to use when sorting results: false for ascending, true for descending.

If $mIsBackwards is set, we start from the opposite end, but we still sort the page itself according to $mDefaultDirection. E.g., if $mDefaultDirection is false but we're going backwards, we'll display the last page of results, but the last result will be at the bottom, not the top.

Like $mIndexField, $mDefaultDirection will be a single value even if the class supports multiple default directions for different order types.

Definition at line 112 of file Pager.php.

Referenced by doQuery().

◆ $mDefaultLimit

IndexPager::$mDefaultLimit = 50

Definition at line 82 of file Pager.php.

◆ $mDefaultQuery

IndexPager::$mDefaultQuery

Definition at line 119 of file Pager.php.

Referenced by CategoryPager\getDefaultQuery(), and getDefaultQuery().

◆ $mExtraSortFields

IndexPager::$mExtraSortFields
protected

An array of secondary columns to order by.

These fields are not part of the offset. This is a column list for one ordering, even if multiple orderings are supported.

Definition at line 97 of file Pager.php.

◆ $mFirstShown

IndexPager::$mFirstShown

Definition at line 119 of file Pager.php.

◆ $mIncludeOffset

IndexPager::$mIncludeOffset = false
protected

Whether to include the offset in the query.

Definition at line 124 of file Pager.php.

Referenced by doQuery().

◆ $mIndexField

IndexPager::$mIndexField
protected

The index to actually be used for ordering.

This is a single column, for one ordering, even if multiple orderings are supported.

Definition at line 92 of file Pager.php.

Referenced by ImageListPager\reallyDoQuery().

◆ $mIsBackwards

IndexPager::$mIsBackwards

Definition at line 113 of file Pager.php.

◆ $mIsFirst

IndexPager::$mIsFirst

True if the current result set is the first one.

Definition at line 116 of file Pager.php.

Referenced by HistoryPager\formatRow(), and HistoryPager\getEndBody().

◆ $mIsLast

IndexPager::$mIsLast

Definition at line 117 of file Pager.php.

Referenced by isNavigationBarShown().

◆ $mLastShown

IndexPager::$mLastShown
protected

Definition at line 119 of file Pager.php.

◆ $mLimit

IndexPager::$mLimit

Definition at line 83 of file Pager.php.

Referenced by getBody(), getLimit(), TablePager\getLimitSelectList(), and getPagingQueries().

◆ $mLimitsShown

IndexPager::$mLimitsShown = array( 20, 50, 100, 250, 500 )

Definition at line 81 of file Pager.php.

◆ $mNavigationBar

IndexPager::$mNavigationBar

◆ $mOffset

IndexPager::$mOffset

Definition at line 83 of file Pager.php.

Referenced by getLimitLinks().

◆ $mOrderType

IndexPager::$mOrderType
protected

For pages that support multiple types of ordering, which one to use.

Definition at line 100 of file Pager.php.

Referenced by __construct().

◆ $mPastTheEndIndex

IndexPager::$mPastTheEndIndex

Definition at line 119 of file Pager.php.

Referenced by getLimitLinks().

◆ $mPastTheEndRow

IndexPager::$mPastTheEndRow

Definition at line 86 of file Pager.php.

Referenced by HistoryPager\getEndBody().

◆ $mQueryDone

IndexPager::$mQueryDone = false

Definition at line 84 of file Pager.php.

◆ $mRequest

IndexPager::$mRequest

Definition at line 80 of file Pager.php.

◆ $mResult

ResultWrapper IndexPager::$mResult

Result object for the query.

Warning: seek before use.

Definition at line 130 of file Pager.php.

Referenced by getResult().


The documentation for this class was generated from the following file: