MediaWiki  master
ReverseChronologicalPager.php
Go to the documentation of this file.
1 <?php
21 use Wikimedia\Timestamp\TimestampException;
22 
29 abstract class ReverseChronologicalPager extends IndexPager {
33  public $mYear;
35  public $mMonth;
37  public $mDay;
38 
43  public function getNavigationBar() {
44  if ( !$this->isNavigationBarShown() ) {
45  return '';
46  }
47 
48  if ( isset( $this->mNavigationBar ) ) {
49  return $this->mNavigationBar;
50  }
51 
52  $linkTexts = [
53  'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
54  'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
55  'first' => $this->msg( 'histlast' )->escaped(),
56  'last' => $this->msg( 'histfirst' )->escaped()
57  ];
58 
59  $pagingLinks = $this->getPagingLinks( $linkTexts );
60  $limitLinks = $this->getLimitLinks();
61  $limits = $this->getLanguage()->pipeList( $limitLinks );
62  $firstLastLinks = $this->msg( 'parentheses' )->rawParams( "{$pagingLinks['first']}" .
63  $this->msg( 'pipe-separator' )->escaped() .
64  "{$pagingLinks['last']}" )->escaped();
65 
66  $this->mNavigationBar = $firstLastLinks . ' ' .
67  $this->msg( 'viewprevnext' )->rawParams(
68  $pagingLinks['prev'], $pagingLinks['next'], $limits )->escaped();
69 
70  return $this->mNavigationBar;
71  }
72 
84  public function getDateCond( $year, $month, $day = -1 ) {
85  $year = (int)$year;
86  $month = (int)$month;
87  $day = (int)$day;
88 
89  // Basic validity checks for year and month
90  // If year and month are invalid, don't update the mOffset
91  if ( $year <= 0 && ( $month <= 0 || $month >= 13 ) ) {
92  return null;
93  }
94 
95  $timestamp = self::getOffsetDate( $year, $month, $day );
96 
97  try {
98  // The timestamp used for DB queries is at midnight of the *next* day after the selected date.
99  $selectedDate = new DateTime( $timestamp->getTimestamp( TS_ISO_8601 ) );
100  $selectedDate = $selectedDate->modify( '-1 day' );
101 
102  $this->mYear = (int)$selectedDate->format( 'Y' );
103  $this->mMonth = (int)$selectedDate->format( 'm' );
104  $this->mDay = (int)$selectedDate->format( 'd' );
105  $this->mOffset = $this->mDb->timestamp( $timestamp->getTimestamp() );
106  } catch ( TimestampException $e ) {
107  // Invalid user provided timestamp (T149257)
108  return null;
109  }
110 
111  return $this->mOffset;
112  }
113 
129  public static function getOffsetDate( $year, $month, $day = -1 ) {
130  // Given an optional year, month, and day, we need to generate a timestamp
131  // to use as "WHERE rev_timestamp <= result"
132  // Examples: year = 2006 equals < 20070101 (+000000)
133  // year=2005, month=1 equals < 20050201
134  // year=2005, month=12 equals < 20060101
135  // year=2005, month=12, day=5 equals < 20051206
136  if ( $year <= 0 ) {
137  // If no year given, assume the current one
138  $timestamp = MWTimestamp::getInstance();
139  $year = $timestamp->format( 'Y' );
140  // If this month hasn't happened yet this year, go back to last year's month
141  if ( $month > $timestamp->format( 'n' ) ) {
142  $year--;
143  }
144  }
145 
146  if ( $month && $month > 0 && $month < 13 ) {
147  // Day validity check after we have month and year checked
148  $day = checkdate( $month, $day, $year ) ? $day : false;
149 
150  if ( $day && $day > 0 ) {
151  // If we have a day, we want up to the day immediately afterward
152  $day++;
153 
154  // Did we overflow the current month?
155  if ( !checkdate( $month, $day, $year ) ) {
156  $day = 1;
157  $month++;
158  }
159  } else {
160  // If no day, assume beginning of next month
161  $day = 1;
162  $month++;
163  }
164 
165  // Did we overflow the current year?
166  if ( $month > 12 ) {
167  $month = 1;
168  $year++;
169  }
170 
171  } else {
172  // No month implies we want up to the end of the year in question
173  $month = 1;
174  $day = 1;
175  $year++;
176  }
177 
178  // Y2K38 bug
179  if ( $year > 2032 ) {
180  $year = 2032;
181  }
182 
183  $ymd = (int)sprintf( "%04d%02d%02d", $year, $month, $day );
184 
185  if ( $ymd > 20320101 ) {
186  $ymd = 20320101;
187  }
188 
189  return MWTimestamp::getInstance( "${ymd}000000" );
190  }
191 }
ReverseChronologicalPager\getOffsetDate
static getOffsetDate( $year, $month, $day=-1)
Core logic of determining the mOffset timestamp such that we can get all items with a timestamp up to...
Definition: ReverseChronologicalPager.php:129
ReverseChronologicalPager\getDateCond
getDateCond( $year, $month, $day=-1)
Set and return the mOffset timestamp such that we can get all revisions with a timestamp up to the sp...
Definition: ReverseChronologicalPager.php:84
ReverseChronologicalPager\$mMonth
int $mMonth
Definition: ReverseChronologicalPager.php:35
IndexPager\$mNavigationBar
string $mNavigationBar
Definition: IndexPager.php:151
IndexPager\getLimitLinks
getLimitLinks()
Definition: IndexPager.php:825
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:140
ReverseChronologicalPager\$mDefaultDirection
bool $mDefaultDirection
Definition: ReverseChronologicalPager.php:31
MWTimestamp\getInstance
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:41
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:184
ReverseChronologicalPager\$mYear
int $mYear
Definition: ReverseChronologicalPager.php:33
IndexPager\DIR_DESCENDING
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:80
IndexPager\isNavigationBarShown
isNavigationBarShown()
Returns whether to show the "navigation bar" @stable to override.
Definition: IndexPager.php:786
ReverseChronologicalPager\$mDay
int $mDay
Definition: ReverseChronologicalPager.php:37
IndexPager
IndexPager is an efficient pager which uses a (roughly unique) index in the data set to implement pag...
Definition: IndexPager.php:74
ReverseChronologicalPager
Efficient paging for SQL queries.
Definition: ReverseChronologicalPager.php:29
IndexPager\$mOffset
mixed $mOffset
The starting point to enumerate entries.
Definition: IndexPager.php:94
ReverseChronologicalPager\getNavigationBar
getNavigationBar()
Stable to override.
Definition: ReverseChronologicalPager.php:43
IndexPager\getPagingLinks
getPagingLinks( $linkTexts, $disabledTexts=[])
Get paging links.
Definition: IndexPager.php:804