Add number formatting for numeric limits, regression in r55692
[lhc/web/wiklou.git] / includes / Pager.php
index f3d99f6..adfb14a 100644 (file)
@@ -50,7 +50,7 @@ interface Pager {
  *      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
+ *  simple, please see the examples in HistoryPage.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.
@@ -87,6 +87,9 @@ abstract class IndexPager implements Pager {
        public $mDefaultDirection;
        public $mIsBackwards;
 
+       /** True if the current result set is the first one */
+       public $mIsFirst;
+
        /**
         * Result object for the query. Warning: seek before use.
         */
@@ -154,6 +157,26 @@ abstract class IndexPager implements Pager {
 
                wfProfileOut( $fname );
        }
+       
+       /**
+        * Return the result wrapper.
+        */
+       function getResult() {
+               return $this->mResult;
+       }
+       
+       /**
+        * Set the offset from an other source than $wgRequest
+        */
+       function setOffset( $offset ) {
+               $this->mOffset = $offset;
+       }
+       /**
+        * Set the limit from an other source than $wgRequest
+        */
+       function setLimit( $limit ) {
+               $this->mLimit = $limit;
+       }
 
        /**
         * Extract some useful data from the result object for use by
@@ -284,17 +307,18 @@ abstract class IndexPager implements Pager {
                if ( $query === null ) {
                        return $text;
                }
-               if( $type == 'prev' || $type == 'next' ) {
-                       $attrs = "rel=\"$type\"";
-               } elseif( $type == 'first' ) {
-                       $attrs = "rel=\"start\"";
-               } else {
-                       # HTML 4 has no rel="end" . . .
-                       $attrs = '';
+
+               $attrs = array();
+               if( in_array( $type, array( 'first', 'prev', 'next', 'last' ) ) ) {
+                       # HTML5 rel attributes
+                       $attrs['rel'] = $type;
                }
-               return $this->getSkin()->makeKnownLinkObj( $this->getTitle(), $text,
-                               wfArrayToCGI( $query, $this->getDefaultQuery() ), '', '',
-                               $attrs );
+
+               if( $type ) {
+                       $attrs['class'] = "mw-{$type}link";
+               }
+               return $this->getSkin()->link( $this->getTitle(), $text,
+                       $attrs, $query + $this->getDefaultQuery(), array('noclasses','known') );
        }
 
        /**
@@ -396,6 +420,14 @@ abstract class IndexPager implements Pager {
                return array( 'prev' => $prev, 'next' => $next, 'first' => $first, 'last' => $last );
        }
 
+       function isNavigationBarShown() {
+               if ( !$this->mQueryDone ) {
+                       $this->doQuery();
+               }
+               // Hide navigation by default if there is nothing to page
+               return !($this->mIsFirst && $this->mIsLast);
+       }
+
        /**
         * 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
@@ -427,7 +459,7 @@ abstract class IndexPager implements Pager {
                }
                foreach ( $this->mLimitsShown as $limit ) {
                        $links[] = $this->makeLink( $wgLang->formatNum( $limit ),
-                               array( 'offset' => $offset, 'limit' => $limit ) );
+                               array( 'offset' => $offset, 'limit' => $limit ), 'num' );
                }
                return $links;
        }
@@ -447,6 +479,7 @@ abstract class IndexPager implements Pager {
         *    fields => Field(s) for passing to Database::select(), may be *
         *    conds => WHERE conditions
         *    options => option array
+        *    join_conds => JOIN conditions
         */
        abstract function getQueryInfo();
 
@@ -495,6 +528,8 @@ abstract class AlphabeticPager extends IndexPager {
        function getNavigationBar() {
                global $wgLang;
 
+               if ( !$this->isNavigationBarShown() ) return '';
+
                if( isset( $this->mNavigationBar ) ) {
                        return $this->mNavigationBar;
                }
@@ -509,10 +544,10 @@ abstract class AlphabeticPager extends IndexPager {
 
                $pagingLinks = $this->getPagingLinks( $linkTexts );
                $limitLinks = $this->getLimitLinks();
-               $limits = implode( ' | ', $limitLinks );
+               $limits = $wgLang->pipeList( $limitLinks );
 
                $this->mNavigationBar =
-                       "({$pagingLinks['first']} | {$pagingLinks['last']}) " .
+                       "(" . $wgLang->pipeList( array( $pagingLinks['first'], $pagingLinks['last'] ) ) . ") " .
                        wfMsgHtml( 'viewprevnext', $pagingLinks['prev'],
                        $pagingLinks['next'], $limits );
 
@@ -528,7 +563,7 @@ abstract class AlphabeticPager extends IndexPager {
                        if( $first ) {
                                $first = false;
                        } else {
-                               $extra .= ' | ';
+                               $extra .= wfMsgExt( 'pipe-separator' , 'escapenoentities' );
                        }
 
                        if( $order == $this->mOrderType ) {
@@ -576,22 +611,24 @@ abstract class ReverseChronologicalPager extends IndexPager {
        function getNavigationBar() {
                global $wgLang;
 
+               if ( !$this->isNavigationBarShown() ) return '';
+
                if ( isset( $this->mNavigationBar ) ) {
                        return $this->mNavigationBar;
                }
                $nicenumber = $wgLang->formatNum( $this->mLimit );
                $linkTexts = array(
-                       'prev' => wfMsgExt( 'pager-newer-n', array( 'parsemag' ), $nicenumber ),
-                       'next' => wfMsgExt( 'pager-older-n', array( 'parsemag' ), $nicenumber ),
+                       'prev' => wfMsgExt( 'pager-newer-n', array( 'parsemag', 'escape' ), $nicenumber ),
+                       'next' => wfMsgExt( 'pager-older-n', array( 'parsemag', 'escape' ), $nicenumber ),
                        'first' => wfMsgHtml( 'histlast' ),
                        'last' => wfMsgHtml( 'histfirst' )
                );
 
                $pagingLinks = $this->getPagingLinks( $linkTexts );
                $limitLinks = $this->getLimitLinks();
-               $limits = implode( ' | ', $limitLinks );
+               $limits = $wgLang->pipeList( $limitLinks );
 
-               $this->mNavigationBar = "({$pagingLinks['first']} | {$pagingLinks['last']}) " .
+               $this->mNavigationBar = "({$pagingLinks['first']}" . wfMsgExt( 'pipe-separator' , 'escapenoentities' ) . "{$pagingLinks['last']}) " .
                        wfMsgHtml("viewprevnext", $pagingLinks['prev'], $pagingLinks['next'], $limits);
                return $this->mNavigationBar;
        }
@@ -724,22 +761,50 @@ abstract class TablePager extends IndexPager {
        }
 
        function formatRow( $row ) {
-               $s = "<tr>\n";
+               $this->mCurrentRow = $row;      # In case formatValue etc need to know
+               $s = Xml::openElement( 'tr', $this->getRowAttrs($row) );
                $fieldNames = $this->getFieldNames();
-               $this->mCurrentRow = $row;  # In case formatValue needs to know
                foreach ( $fieldNames as $field => $name ) {
                        $value = isset( $row->$field ) ? $row->$field : null;
                        $formatted = strval( $this->formatValue( $field, $value ) );
                        if ( $formatted == '' ) {
                                $formatted = '&nbsp;';
                        }
-                       $class = 'TablePager_col_' . htmlspecialchars( $field );
-                       $s .= "<td class=\"$class\">$formatted</td>\n";
+                       $s .= Xml::tags( 'td', $this->getCellAttrs( $field, $value ), $formatted );
                }
                $s .= "</tr>\n";
                return $s;
        }
 
+       /**
+        * Get a class name to be applied to the given row.
+        * @param object $row The database result row
+        */
+       function getRowClass( $row ) {
+               return '';
+       }
+
+       /**
+        * Get attributes to be applied to the given row.
+        * @param object $row The database result row
+        * @return associative array
+        */
+       function getRowAttrs( $row ) {
+               return array( 'class' => $this->getRowClass( $row ) );
+       }
+
+       /**
+        * Get any extra attributes to be applied to the given cell. Don't 
+        * take this as an excuse to hardcode styles; use classes and 
+        * CSS instead.  Row context is available in $this->mCurrentRow
+        * @param $field The column
+        * @param $value The cell contents
+        * @return associative array
+        */
+       function getCellAttrs( $field, $value ) {
+               return array( 'class' => 'TablePager_col_' . $field );
+       }
+
        function getIndexField() {
                return $this->mSort;
        }
@@ -761,6 +826,9 @@ abstract class TablePager extends IndexPager {
         */
        function getNavigationBar() {
                global $wgStylePath, $wgContLang;
+
+               if ( !$this->isNavigationBarShown() ) return '';
+
                $path = "$wgStylePath/common/images";
                $labels = array(
                        'first' => 'table_pager_first',
@@ -769,17 +837,22 @@ abstract class TablePager extends IndexPager {
                        'last' => 'table_pager_last',
                );
                $images = array(
-                       'first' => $wgContLang->isRTL() ? 'arrow_last_25.png' : 'arrow_first_25.png',
-                       'prev' =>  $wgContLang->isRTL() ? 'arrow_right_25.png' : 'arrow_left_25.png',
-                       'next' =>  $wgContLang->isRTL() ? 'arrow_left_25.png' : 'arrow_right_25.png',
-                       'last' =>  $wgContLang->isRTL() ? 'arrow_first_25.png' : 'arrow_last_25.png',
+                       'first' => 'arrow_first_25.png',
+                       'prev' => 'arrow_left_25.png',
+                       'next' => 'arrow_right_25.png',
+                       'last' => 'arrow_last_25.png',
                );
                $disabledImages = array(
-                       'first' => $wgContLang->isRTL() ? 'arrow_disabled_last_25.png' : 'arrow_disabled_first_25.png',
-                       'prev' =>  $wgContLang->isRTL() ? 'arrow_disabled_right_25.png' : 'arrow_disabled_left_25.png',
-                       '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',
+                       'first' => 'arrow_disabled_first_25.png',
+                       'prev' => 'arrow_disabled_left_25.png',
+                       'next' => 'arrow_disabled_right_25.png',
+                       'last' => 'arrow_disabled_last_25.png',
                );
+               if( $wgContLang->isRTL() ) {
+                       $keys = array_keys( $labels );
+                       $images = array_combine( $keys, array_reverse( $images ) );
+                       $disabledImages = array_combine( $keys, array_reverse( $disabledImages ) );
+               }
 
                $linkTexts = array();
                $disabledTexts = array();
@@ -806,10 +879,19 @@ abstract class TablePager extends IndexPager {
        function getLimitSelect() {
                global $wgLang;
                $s = "<select name=\"limit\">";
-               foreach ( $this->mLimitsShown as $limit ) {
-                       $selected = $limit == $this->mLimit ? 'selected="selected"' : '';
-                       $formattedLimit = $wgLang->formatNum( $limit );
-                       $s .= "<option value=\"$limit\" $selected>$formattedLimit</option>\n";
+               foreach ( $this->mLimitsShown as $key => $value ) {
+                       # The pair is either $index => $limit, in which case the $value
+                       # will be numeric, or $limit => $text, in which case the $value
+                       # will be a string.
+                       if( is_int( $value ) ){
+                               $limit = $value;
+                               $text = $wgLang->formatNum( $limit );
+                       } else {
+                               $limit = $key;
+                               $text = $value;
+                       }
+                       $selected = ( $limit == $this->mLimit ? 'selected="selected"' : '' );
+                       $s .= "<option value=\"$limit\" $selected>$text</option>\n";
                }
                $s .= "</select>";
                return $s;
@@ -839,14 +921,15 @@ abstract class TablePager extends IndexPager {
         * Get a form containing a limit selection dropdown
         */
        function getLimitForm() {
+               global $wgScript;
+
                # Make the select with some explanatory text
-               $url = $this->getTitle()->escapeLocalURL();
                $msgSubmit = wfMsgHtml( 'table_pager_limit_submit' );
                return
-                       "<form method=\"get\" action=\"$url\">" .
+                       Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) . "\n" .          
                        wfMsgHtml( 'table_pager_limit', $this->getLimitSelect() ) .
                        "\n<input type=\"submit\" value=\"$msgSubmit\"/>\n" .
-                       $this->getHiddenFields( 'limit' ) .
+                       $this->getHiddenFields( array( 'limit' ) ) .
                        "</form>\n";
        }