Introducing $wgFeed variable. Allows tuning sydication feeds off, when desired.
[lhc/web/wiklou.git] / includes / Pager.php
index b14aa8c..ed7086b 100644 (file)
@@ -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 > 50000 ) {
-                       $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();
@@ -386,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;
@@ -402,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 = "({$pagingLinks['first']} | {$pagingLinks['last']}) " . wfMsgHtml("viewprevnext", $pagingLinks['prev'], $pagingLinks['next'], $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;
@@ -487,7 +545,7 @@ abstract class TablePager extends IndexPager {
        }
 
        function getEndBody() {
-               return '</tbody></table>';
+               return "</tbody></table>\n";
        }
 
        function getEmptyBody() {
@@ -553,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 ) {
@@ -564,12 +622,12 @@ abstract class TablePager extends IndexPager {
                $links = $this->getPagingLinks( $linkTexts, $disabledTexts );
 
                $navClass = htmlspecialchars( $this->getNavClass() );
-               $s = "<table class=\"$navClass\" align=\"center\" cellpadding=\"3\"><tr>";
+               $s = "<table class=\"$navClass\" align=\"center\" cellpadding=\"3\"><tr>\n";
                $cellAttrs = 'valign="top" align="center" width="' . 100 / count( $links ) . '%"';
                foreach ( $labels as $type => $label ) {
                        $s .= "<td $cellAttrs>{$links[$type]}</td>\n";
                }
-               $s .= '</tr></table>';
+               $s .= "</tr></table>\n";
                return $s;
        }
 
@@ -624,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 &nbsp; for empty cells. Do not include the <td> and </td>. 
+        * Format a table cell. The return value should be HTML, but use an empty
+        * string not &nbsp; for empty cells. Do not include the <td> and </td>. 
+        *
+        * 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 );
 
@@ -647,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();
 }
-?>