Merge "Relax phpdoc of PermissionError to match actual usage"
[lhc/web/wiklou.git] / includes / pager / ReverseChronologicalPager.php
index ee6e26c..4895b4f 100644 (file)
@@ -29,6 +29,7 @@ abstract class ReverseChronologicalPager extends IndexPager {
        public $mDefaultDirection = IndexPager::DIR_DESCENDING;
        public $mYear;
        public $mMonth;
+       public $mDay;
 
        function getNavigationBar() {
                if ( !$this->isNavigationBarShown() ) {
@@ -39,12 +40,12 @@ abstract class ReverseChronologicalPager extends IndexPager {
                        return $this->mNavigationBar;
                }
 
-               $linkTexts = array(
+               $linkTexts = [
                        'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
                        'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
                        'first' => $this->msg( 'histlast' )->escaped(),
                        'last' => $this->msg( 'histfirst' )->escaped()
-               );
+               ];
 
                $pagingLinks = $this->getPagingLinks( $linkTexts );
                $limitLinks = $this->getLimitLinks();
@@ -60,23 +61,34 @@ abstract class ReverseChronologicalPager extends IndexPager {
                return $this->mNavigationBar;
        }
 
-       function getDateCond( $year, $month ) {
+       /**
+        * Set and return the mOffset timestamp such that we can get all revisions with
+        * a timestamp up to the specified parameters.
+        * @param int $year Year up to which we want revisions
+        * @param int $month Month up to which we want revisions
+        * @param int $day [optional] Day up to which we want revisions. Default is end of month.
+        * @return string|null Timestamp or null if year and month are false/invalid
+        */
+       function getDateCond( $year, $month, $day = -1 ) {
                $year = intval( $year );
                $month = intval( $month );
+               $day = intval( $day );
 
-               // Basic validity checks
+               // Basic validity checks for year and month
                $this->mYear = $year > 0 ? $year : false;
                $this->mMonth = ( $month > 0 && $month < 13 ) ? $month : false;
 
-               // Given an optional year and month, we need to generate a timestamp
-               // to use as "WHERE rev_timestamp <= result"
-               // Examples: year = 2006 equals < 20070101 (+000000)
-               // year=2005, month=1    equals < 20050201
-               // year=2005, month=12   equals < 20060101
+               // If year and month are false, don't update the mOffset
                if ( !$this->mYear && !$this->mMonth ) {
                        return;
                }
 
+               // Given an optional year, month, and day, we need to generate a timestamp
+               // to use as "WHERE rev_timestamp <= result"
+               // Examples: year = 2006      equals < 20070101 (+000000)
+               // year=2005, month=1         equals < 20050201
+               // year=2005, month=12        equals < 20060101
+               // year=2005, month=12, day=5 equals < 20051206
                if ( $this->mYear ) {
                        $year = $this->mYear;
                } else {
@@ -90,15 +102,36 @@ abstract class ReverseChronologicalPager extends IndexPager {
                }
 
                if ( $this->mMonth ) {
-                       $month = $this->mMonth + 1;
-                       // For December, we want January 1 of the next year
+                       $month = $this->mMonth;
+
+                       // Day validity check after we have month and year checked
+                       $this->mDay = checkdate( $month, $day, $year ) ? $day : false;
+
+                       if ( $this->mDay ) {
+                               // If we have a day, we want up to the day immediately afterward
+                               $day = $this->mDay + 1;
+
+                               // Did we overflow the current month?
+                               if ( !checkdate( $month, $day, $year ) ) {
+                                       $day = 1;
+                                       $month++;
+                               }
+                       } else {
+                               // If no day, assume beginning of next month
+                               $day = 1;
+                               $month++;
+                       }
+
+                       // Did we overflow the current year?
                        if ( $month > 12 ) {
                                $month = 1;
                                $year++;
                        }
+
                } else {
                        // No month implies we want up to the end of the year in question
                        $month = 1;
+                       $day = 1;
                        $year++;
                }
 
@@ -107,7 +140,7 @@ abstract class ReverseChronologicalPager extends IndexPager {
                        $year = 2032;
                }
 
-               $ymd = (int)sprintf( "%04d%02d01", $year, $month );
+               $ymd = (int)sprintf( "%04d%02d%02d", $year, $month, $day );
 
                if ( $ymd > 20320101 ) {
                        $ymd = 20320101;
@@ -115,8 +148,9 @@ abstract class ReverseChronologicalPager extends IndexPager {
 
                // Treat the given time in the wiki timezone and get a UTC timestamp for the database lookup
                $timestamp = MWTimestamp::getInstance( "${ymd}000000" );
-               $timestamp->setTimeZone( $this->getConfig()->get( 'Localtimezone' ) );
+               $timestamp->setTimezone( $this->getConfig()->get( 'Localtimezone' ) );
 
                $this->mOffset = $this->mDb->timestamp( $timestamp->getTimestamp() );
+               return $this->mOffset;
        }
 }