Merge "Don't purge thumbs for old versions of an image during ?action=purge"
[lhc/web/wiklou.git] / includes / Pager.php
index 1219552..89930e2 100644 (file)
@@ -155,13 +155,13 @@ abstract class IndexPager extends ContextSource implements Pager {
                $index = $this->getIndexField(); // column to sort on
                $extraSort = $this->getExtraSortFields(); // extra columns to sort on for query planning
                $order = $this->mRequest->getVal( 'order' );
-               if( is_array( $index ) && isset( $index[$order] ) ) {
+               if ( is_array( $index ) && isset( $index[$order] ) ) {
                        $this->mOrderType = $order;
                        $this->mIndexField = $index[$order];
                        $this->mExtraSortFields = isset( $extraSort[$order] )
                                ? (array)$extraSort[$order]
                                : array();
-               } elseif( is_array( $index ) ) {
+               } elseif ( is_array( $index ) ) {
                        # First element is the default
                        reset( $index );
                        list( $this->mOrderType, $this->mIndexField ) = each( $index );
@@ -175,7 +175,7 @@ abstract class IndexPager extends ContextSource implements Pager {
                        $this->mExtraSortFields = (array)$extraSort;
                }
 
-               if( !isset( $this->mDefaultDirection ) ) {
+               if ( !isset( $this->mDefaultDirection ) ) {
                        $dir = $this->getDefaultDirections();
                        $this->mDefaultDirection = is_array( $dir )
                                ? $dir[$this->mOrderType]
@@ -206,13 +206,25 @@ abstract class IndexPager extends ContextSource implements Pager {
                # Plus an extra row so that we can tell the "next" link should be shown
                $queryLimit = $this->mLimit + 1;
 
+               if ( $this->mOffset == '' ) {
+                       $isFirst = true;
+               } else {
+                       // If there's an offset, we may or may not be at the first entry.
+                       // The only way to tell is to run the query in the opposite
+                       // direction see if we get a row.
+                       $oldIncludeOffset = $this->mIncludeOffset;
+                       $this->mIncludeOffset = !$this->mIncludeOffset;
+                       $isFirst = !$this->reallyDoQuery( $this->mOffset, 1, !$descending )->numRows();
+                       $this->mIncludeOffset = $oldIncludeOffset;
+               }
+
                $this->mResult = $this->reallyDoQuery(
                        $this->mOffset,
                        $queryLimit,
                        $descending
                );
 
-               $this->extractResultInfo( $this->mOffset, $queryLimit, $this->mResult );
+               $this->extractResultInfo( $isFirst, $queryLimit, $this->mResult );
                $this->mQueryDone = true;
 
                $this->preprocessResults( $this->mResult );
@@ -269,11 +281,12 @@ abstract class IndexPager extends ContextSource implements Pager {
         * Extract some useful data from the result object for use by
         * the navigation bar, put it into $this
         *
-        * @param string $offset index offset, inclusive
+        * @param $isFirst bool: False if there are rows before those fetched (i.e.
+        *     if a "previous" link would make sense)
         * @param $limit Integer: exact query limit
         * @param $res ResultWrapper
         */
-       function extractResultInfo( $offset, $limit, ResultWrapper $res ) {
+       function extractResultInfo( $isFirst, $limit, ResultWrapper $res ) {
                $numRows = $res->numRows();
                if ( $numRows ) {
                        # Remove any table prefix from index field
@@ -311,11 +324,11 @@ abstract class IndexPager extends ContextSource implements Pager {
 
                if ( $this->mIsBackwards ) {
                        $this->mIsFirst = ( $numRows < $limit );
-                       $this->mIsLast = ( $offset == '' );
+                       $this->mIsLast = $isFirst;
                        $this->mLastShown = $firstIndex;
                        $this->mFirstShown = $lastIndex;
                } else {
-                       $this->mIsFirst = ( $offset == '' );
+                       $this->mIsFirst = $isFirst;
                        $this->mIsLast = ( $numRows < $limit );
                        $this->mLastShown = $lastIndex;
                        $this->mFirstShown = $firstIndex;
@@ -443,12 +456,12 @@ abstract class IndexPager extends ContextSource implements Pager {
                }
 
                $attrs = array();
-               if( in_array( $type, array( 'first', 'prev', 'next', 'last' ) ) ) {
+               if ( in_array( $type, array( 'first', 'prev', 'next', 'last' ) ) ) {
                        # HTML5 rel attributes
                        $attrs['rel'] = $type;
                }
 
-               if( $type ) {
+               if ( $type ) {
                        $attrs['class'] = "mw-{$type}link";
                }
 
@@ -580,7 +593,7 @@ abstract class IndexPager extends ContextSource implements Pager {
                        $this->doQuery();
                }
                // Hide navigation by default if there is nothing to page
-               return !($this->mIsFirst && $this->mIsLast);
+               return !( $this->mIsFirst && $this->mIsLast );
        }
 
        /**
@@ -685,7 +698,9 @@ abstract class IndexPager extends ContextSource implements Pager {
         *
         * @return Array
         */
-       protected function getExtraSortFields() { return array(); }
+       protected function getExtraSortFields() {
+               return array();
+       }
 
        /**
         * Return the default sorting direction: false for ascending, true for
@@ -706,7 +721,9 @@ abstract class IndexPager extends ContextSource implements Pager {
         *
         * @return Boolean
         */
-       protected function getDefaultDirections() { return false; }
+       protected function getDefaultDirections() {
+               return false;
+       }
 }
 
 /**
@@ -726,7 +743,7 @@ abstract class AlphabeticPager extends IndexPager {
                        return '';
                }
 
-               if( isset( $this->mNavigationBar ) ) {
+               if ( isset( $this->mNavigationBar ) ) {
                        return $this->mNavigationBar;
                }
 
@@ -749,7 +766,7 @@ abstract class AlphabeticPager extends IndexPager {
                        $this->msg( 'viewprevnext' )->rawParams( $pagingLinks['prev'],
                                $pagingLinks['next'], $limits )->escaped();
 
-               if( !is_array( $this->getIndexField() ) ) {
+               if ( !is_array( $this->getIndexField() ) ) {
                        # Early return to avoid undue nesting
                        return $this->mNavigationBar;
                }
@@ -757,14 +774,14 @@ abstract class AlphabeticPager extends IndexPager {
                $extra = '';
                $first = true;
                $msgs = $this->getOrderTypeMessages();
-               foreach( array_keys( $msgs ) as $order ) {
-                       if( $first ) {
+               foreach ( array_keys( $msgs ) as $order ) {
+                       if ( $first ) {
                                $first = false;
                        } else {
                                $extra .= $this->msg( 'pipe-separator' )->escaped();
                        }
 
-                       if( $order == $this->mOrderType ) {
+                       if ( $order == $this->mOrderType ) {
                                $extra .= $this->msg( $msgs[$order] )->escaped();
                        } else {
                                $extra .= $this->makeLink(
@@ -774,7 +791,7 @@ abstract class AlphabeticPager extends IndexPager {
                        }
                }
 
-               if( $extra !== '' ) {
+               if ( $extra !== '' ) {
                        $extra = ' ' . $this->msg( 'parentheses' )->rawParams( $extra )->escaped();
                        $this->mNavigationBar .= $extra;
                }
@@ -855,9 +872,10 @@ abstract class ReverseChronologicalPager extends IndexPager {
                        $year = $this->mYear;
                } else {
                        // If no year given, assume the current one
-                       $year = gmdate( 'Y' );
+                       $timestamp = MWTimestamp::getInstance();
+                       $year = $timestamp->format( 'Y' );
                        // If this month hasn't happened yet this year, go back to last year's month
-                       if( $this->mMonth > gmdate( 'n' ) ) {
+                       if ( $this->mMonth > $timestamp->format( 'n' ) ) {
                                $year--;
                        }
                }
@@ -904,7 +922,9 @@ abstract class TablePager extends IndexPager {
                }
 
                $this->mSort = $this->getRequest()->getText( 'sort' );
-               if ( !array_key_exists( $this->mSort, $this->getFieldNames() ) ) {
+               if ( !array_key_exists( $this->mSort, $this->getFieldNames() )
+                       || !$this->isFieldSortable( $this->mSort )
+               ) {
                        $this->mSort = $this->getDefaultSort();
                }
                if ( $this->getRequest()->getBool( 'asc' ) ) {
@@ -1123,7 +1143,7 @@ abstract class TablePager extends IndexPager {
                        'next' => 'arrow_disabled_right_25.png',
                        'last' => 'arrow_disabled_last_25.png',
                );
-               if( $this->getLanguage()->isRTL() ) {
+               if ( $this->getLanguage()->isRTL() ) {
                        $keys = array_keys( $labels );
                        $images = array_combine( $keys, array_reverse( $images ) );
                        $disabledImages = array_combine( $keys, array_reverse( $disabledImages ) );
@@ -1153,11 +1173,15 @@ abstract class TablePager extends IndexPager {
        /**
         * Get a "<select>" element which has options for each of the allowed limits
         *
+        * @param $attribs String: Extra attributes to set
         * @return String: HTML fragment
         */
-       public function getLimitSelect() {
+       public function getLimitSelect( $attribs = array() ) {
                $select = new XmlSelect( 'limit', false, $this->mLimit );
                $select->addOptions( $this->getLimitSelectList() );
+               foreach ( $attribs as $name => $value ) {
+                       $select->setAttribute( $name, $value );
+               }
                return $select->getHTML();
        }
 
@@ -1180,7 +1204,7 @@ abstract class TablePager extends IndexPager {
                        # 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 ) ) {
+                       if ( is_int( $value ) ) {
                                $limit = $value;
                                $text = $this->getLanguage()->formatNum( $limit );
                        } else {