Fixed spacing and removed unneeded parenthesis
[lhc/web/wiklou.git] / includes / Pager.php
index d70734c..c2bd364 100644 (file)
@@ -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;
@@ -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 );
        }
 
        /**
@@ -1162,13 +1175,26 @@ abstract class TablePager extends IndexPager {
         * @return String: HTML fragment
         */
        public function getLimitSelect() {
+               $select = new XmlSelect( 'limit', false, $this->mLimit );
+               $select->addOptions( $this->getLimitSelectList() );
+               return $select->getHTML();
+       }
+
+       /**
+        * Get a list of items to show in a "<select>" element of limits.
+        * This can be passed directly to XmlSelect::addOptions().
+        *
+        * @since 1.22
+        * @return array
+        */
+       public function getLimitSelectList() {
                # Add the current limit from the query string
                # to avoid that the limit is lost after clicking Go next time
                if ( !in_array( $this->mLimit, $this->mLimitsShown ) ) {
                        $this->mLimitsShown[] = $this->mLimit;
                        sort( $this->mLimitsShown );
                }
-               $s = Html::openElement( 'select', array( 'name' => 'limit' ) ) . "\n";
+               $ret = array();
                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
@@ -1180,10 +1206,9 @@ abstract class TablePager extends IndexPager {
                                $limit = $key;
                                $text = $value;
                        }
-                       $s .= Xml::option( $text, $limit, $limit == $this->mLimit ) . "\n";
+                       $ret[$text] = $limit;
                }
-               $s .= Html::closeElement( 'select' );
-               return $s;
+               return $ret;
        }
 
        /**