X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FPager.php;h=ed7086b44b70d48c7952f8376b8410685dbcc90a;hb=206bbe7dc82d8527aa12ab0d5ad21449d7ab3b84;hp=decf154bb7cc9e4980d5b4f6548662d422729aa8;hpb=e03dcb059f42ec036d6a1ecb7fd744061c009735;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/Pager.php b/includes/Pager.php index decf154bb7..ed7086b44b 100644 --- a/includes/Pager.php +++ b/includes/Pager.php @@ -2,6 +2,7 @@ /** * Basic pager interface. + * @addtogroup Pager */ interface Pager { function getNavigationBar(); @@ -11,41 +12,44 @@ interface Pager { /** * 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. + * 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: + * 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 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. * - * * 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. + * * 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. * - * * 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. + * Subclassing the pager to implement concrete functionality should be fairly + * simple, please see the examples in PageHistory.php and + * SpecialIpblocklist.php. You just need to override formatRow(), + * getQueryInfo() and getIndexField(). Don't forget to call the parent + * constructor if you override it. * - * * 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 PageHistory.php and SpecialIpblocklist.php. You just need - * to override formatRow(), getQueryInfo() and getIndexField(). Don't forget to call the - * parent constructor if you override it. + * @addtogroup Pager */ abstract class IndexPager implements Pager { public $mRequest; @@ -69,17 +73,18 @@ abstract class IndexPager implements Pager { public $mResult; function __construct() { - global $wgRequest; + global $wgRequest, $wgUser; $this->mRequest = $wgRequest; - - # NB: the offset is quoted, not validated. It is treated as an arbitrary string - # to support the widest variety of index types. Be careful outputting it into - # HTML! + + # NB: the offset is quoted, not validated. It is treated as an + # arbitrary string to support the widest variety of index types. Be + # careful outputting it into HTML! $this->mOffset = $this->mRequest->getText( 'offset' ); - $this->mLimit = $this->mRequest->getInt( 'limit', $this->mDefaultLimit ); - if ( $this->mLimit <= 0 ) { - $this->mLimit = $this->mDefaultLimit; - } + + # Use consistent behavior for the limit options + $this->mDefaultLimit = intval( $wgUser->getOption( 'rclimit' ) ); + list( $this->mLimit, /* $offset */ ) = $this->mRequest->getLimitOffset(); + $this->mIsBackwards = ( $this->mRequest->getVal( 'dir' ) == 'prev' ); $this->mIndexField = $this->getIndexField(); $this->mDb = wfGetDB( DB_SLAVE ); @@ -102,6 +107,9 @@ abstract class IndexPager implements Pager { $this->mResult = $this->reallyDoQuery( $this->mOffset, $queryLimit, $descending ); $this->extractResultInfo( $this->mOffset, $queryLimit, $this->mResult ); $this->mQueryDone = true; + + $this->preprocessResults( $this->mResult ); + $this->mResult->rewind(); // Paranoia wfProfileOut( $fname ); } @@ -127,9 +135,10 @@ abstract class IndexPager implements Pager { $lastIndex = $row[$this->mIndexField]; } else { $this->mPastTheEndRow = null; - # Setting indexes to an empty string means that they will be omitted - # if they would otherwise appear in URLs. It just so happens that this - # is the right thing to do in the standard UI, in all the relevant cases. + # Setting indexes to an empty string means that they will be + # omitted if they would otherwise appear in URLs. It just so + # happens that this is the right thing to do in the standard + # UI, in all the relevant cases. $this->mPastTheEndIndex = ''; $res->seek( $numRows - 1 ); $row = $res->fetchRow(); @@ -156,21 +165,22 @@ abstract class IndexPager implements Pager { } /** - * Do a query with specified parameters, rather than using the object context + * Do a query with specified parameters, rather than using the object + * context * * @param string $offset Index offset, inclusive * @param integer $limit Exact query limit * @param boolean $descending Query direction, false for ascending, true for descending * @return ResultWrapper */ - function reallyDoQuery( $offset, $limit, $ascending ) { + function reallyDoQuery( $offset, $limit, $descending ) { $fname = __METHOD__ . ' (' . get_class( $this ) . ')'; $info = $this->getQueryInfo(); $tables = $info['tables']; $fields = $info['fields']; $conds = isset( $info['conds'] ) ? $info['conds'] : array(); $options = isset( $info['options'] ) ? $info['options'] : array(); - if ( $ascending ) { + if ( $descending ) { $options['ORDER BY'] = $this->mIndexField; $operator = '>'; } else { @@ -185,6 +195,13 @@ abstract class IndexPager implements Pager { return new ResultWrapper( $this->mDb, $res ); } + /** + * Pre-process results; useful for performing batch existence checks, etc. + * + * @param ResultWrapper $result Result wrapper + */ + protected function preprocessResults( $result ) {} + /** * Get the formatted result list. Calls getStartBody(), formatRow() and * getEndBody(), concatenates the results and returns them. @@ -323,13 +340,14 @@ abstract class IndexPager implements Pager { $next = array( 'offset' => $this->mLastShown, 'limit' => $urlLimit ); $last = array( 'dir' => 'prev', 'limit' => $urlLimit ); } - return compact( 'prev', 'next', 'first', 'last' ); + return array( 'prev' => $prev, 'next' => $next, 'first' => $first, 'last' => $last ); } /** - * 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. + * 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. */ function getPagingLinks( $linkTexts, $disabledTexts = array() ) { $queries = $this->getPagingQueries(); @@ -358,6 +376,7 @@ abstract class IndexPager implements Pager { $links[] = $this->makeLink( $wgLang->formatNum( $limit ), array( 'offset' => $offset, 'limit' => $limit ) ); } + return $links; } /** @@ -385,8 +404,45 @@ abstract class IndexPager implements Pager { abstract function getIndexField(); } + +/** + * IndexPager with an alphabetic list and a formatted navigation bar + * @addtogroup Pager + */ +abstract class AlphabeticPager extends IndexPager { + public $mDefaultDirection = false; + + function __construct() { + parent::__construct(); + } + + /** + * Shamelessly stolen bits from ReverseChronologicalPager, d + * didn't want to do class magic as may be still revamped + */ + function getNavigationBar() { + global $wgLang; + + $linkTexts = array( + 'prev' => wfMsgHtml( 'prevn', $wgLang->formatNum( $this->mLimit ) ), + 'next' => wfMsgHtml( 'nextn', $wgLang->formatNum($this->mLimit ) ), + 'first' => wfMsgHtml( 'page_first' ), /* Introduced the message */ + 'last' => wfMsgHtml( 'page_last' ) /* Introduced the message */ + ); + + $pagingLinks = $this->getPagingLinks( $linkTexts ); + $limitLinks = $this->getLimitLinks(); + $limits = implode( ' | ', $limitLinks ); + + $this->mNavigationBar = "({$pagingLinks['first']} | {$pagingLinks['last']}) " . wfMsgHtml("viewprevnext", $pagingLinks['prev'], $pagingLinks['next'], $limits); + return $this->mNavigationBar; + + } +} + /** * IndexPager with a formatted navigation bar + * @addtogroup Pager */ abstract class ReverseChronologicalPager extends IndexPager { public $mDefaultDirection = true; @@ -401,24 +457,27 @@ abstract class ReverseChronologicalPager extends IndexPager { if ( isset( $this->mNavigationBar ) ) { return $this->mNavigationBar; } + $nicenumber = $wgLang->formatNum( $this->mLimit ); $linkTexts = array( - 'prev' => wfMsgHtml( "prevn", $this->mLimit ), - 'next' => wfMsgHtml( 'nextn', $this->mLimit ), - 'first' => wfMsgHtml('histlast'), + 'prev' => wfMsgExt( 'pager-newer-n', array( 'parsemag' ), $nicenumber ), + 'next' => wfMsgExt( 'pager-older-n', array( 'parsemag' ), $nicenumber ), + 'first' => wfMsgHtml( 'histlast' ), 'last' => wfMsgHtml( 'histfirst' ) ); $pagingLinks = $this->getPagingLinks( $linkTexts ); $limitLinks = $this->getLimitLinks(); $limits = implode( ' | ', $limitLinks ); - - $this->mNavigationBar = "($latestText | $earliestText) " . wfMsgHtml("viewprevnext", $prevText, $nextText, $limits); + + $this->mNavigationBar = "({$pagingLinks['first']} | {$pagingLinks['last']}) " . + wfMsgHtml("viewprevnext", $pagingLinks['prev'], $pagingLinks['next'], $limits); return $this->mNavigationBar; } } /** * Table-based display with a user-selectable sort order + * @addtogroup Pager */ abstract class TablePager extends IndexPager { var $mSort; @@ -441,8 +500,13 @@ abstract class TablePager extends IndexPager { function getStartBody() { global $wgStylePath; - $s = "\n"; + $tableClass = htmlspecialchars( $this->getTableClass() ); + $sortClass = htmlspecialchars( $this->getSortHeaderClass() ); + + $s = "
\n"; $fields = $this->getFieldNames(); + + # Make table header foreach ( $fields as $field => $name ) { if ( strval( $name ) == '' ) { $s .= "\n"; @@ -468,7 +532,7 @@ abstract class TablePager extends IndexPager { $link = $this->makeLink( "\"$alt\"" . htmlspecialchars( $name ), $query ); - $s .= "\n"; + $s .= "\n"; } else { $s .= '\n"; } @@ -481,7 +545,7 @@ abstract class TablePager extends IndexPager { } function getEndBody() { - return '
 $link$link' . $this->makeLink( htmlspecialchars( $name ), $query ) . "
'; + return "\n"; } function getEmptyBody() { @@ -500,7 +564,8 @@ abstract class TablePager extends IndexPager { if ( $formatted == '' ) { $formatted = ' '; } - $s .= "$formatted\n"; + $class = 'TablePager_col_' . htmlspecialchars( $field ); + $s .= "$formatted\n"; } $s .= "\n"; return $s; @@ -510,6 +575,18 @@ abstract class TablePager extends IndexPager { return $this->mSort; } + function getTableClass() { + return 'TablePager'; + } + + function getNavClass() { + return 'TablePager_nav'; + } + + function getSortHeaderClass() { + return 'TablePager_sort'; + } + /** * A navigation bar with images */ @@ -534,7 +611,7 @@ abstract class TablePager extends IndexPager { 'next' => $wgContLang->isRTL() ? 'arrow_disabled_left_25.png' : 'arrow_disabled_right_25.png', 'last' => $wgContLang->isRTL() ? 'arrow_disabled_first_25.png' : 'arrow_disabled_last_25.png', ); - + $linkTexts = array(); $disabledTexts = array(); foreach ( $labels as $type => $label ) { @@ -544,12 +621,13 @@ abstract class TablePager extends IndexPager { } $links = $this->getPagingLinks( $linkTexts, $disabledTexts ); - $s = ''; + $navClass = htmlspecialchars( $this->getNavClass() ); + $s = "
\n"; $cellAttrs = 'valign="top" align="center" width="' . 100 / count( $links ) . '%"'; foreach ( $labels as $type => $label ) { $s .= "\n"; } - $s .= '
{$links[$type]}
'; + $s .= "\n"; return $s; } @@ -604,20 +682,22 @@ abstract class TablePager extends IndexPager { } /** - * Return true if the named field should be sortable by the UI, false otherwise + * Return true if the named field should be sortable by the UI, false + * otherwise + * * @param string $field */ abstract function isFieldSortable( $field ); /** - * Format a table cell. The return value should be HTML, but use an empty string - * not   for empty cells. Do not include the and . + * Format a table cell. The return value should be HTML, but use an empty + * string not   for empty cells. Do not include the and . + * + * The current result row is available as $this->mCurrentRow, in case you + * need more context. * * @param string $name The database field name * @param string $value The value retrieved from the database - * - * The current result row is available as $this->mCurrentRow, in case you need - * more context. */ abstract function formatValue( $name, $value ); @@ -627,10 +707,9 @@ abstract class TablePager extends IndexPager { abstract function getDefaultSort(); /** - * An array mapping database field names to a textual description of the field - * name, for use in the table header. The description should be plain text, it - * will be HTML-escaped later. + * An array mapping database field names to a textual description of the + * field name, for use in the table header. The description should be plain + * text, it will be HTML-escaped later. */ abstract function getFieldNames(); } -?>