3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use Wikimedia\Timestamp\TimestampException
;
24 * Efficient paging for SQL queries.
25 * IndexPager with a formatted navigation bar.
28 abstract class ReverseChronologicalPager
extends IndexPager
{
30 public $mDefaultDirection = IndexPager
::DIR_DESCENDING
;
38 public function getNavigationBar() {
39 if ( !$this->isNavigationBarShown() ) {
43 if ( isset( $this->mNavigationBar
) ) {
44 return $this->mNavigationBar
;
48 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit
)->escaped(),
49 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit
)->escaped(),
50 'first' => $this->msg( 'histlast' )->escaped(),
51 'last' => $this->msg( 'histfirst' )->escaped()
54 $pagingLinks = $this->getPagingLinks( $linkTexts );
55 $limitLinks = $this->getLimitLinks();
56 $limits = $this->getLanguage()->pipeList( $limitLinks );
57 $firstLastLinks = $this->msg( 'parentheses' )->rawParams( "{$pagingLinks['first']}" .
58 $this->msg( 'pipe-separator' )->escaped() .
59 "{$pagingLinks['last']}" )->escaped();
61 $this->mNavigationBar
= $firstLastLinks . ' ' .
62 $this->msg( 'viewprevnext' )->rawParams(
63 $pagingLinks['prev'], $pagingLinks['next'], $limits )->escaped();
65 return $this->mNavigationBar
;
69 * Set and return the mOffset timestamp such that we can get all revisions with
70 * a timestamp up to the specified parameters.
72 * @param int $year Year up to which we want revisions
73 * @param int $month Month up to which we want revisions
74 * @param int $day [optional] Day up to which we want revisions. Default is end of month.
75 * @return string|null Timestamp or null if year and month are false/invalid
77 public function getDateCond( $year, $month, $day = -1 ) {
82 // Basic validity checks for year and month
83 // If year and month are invalid, don't update the mOffset
84 if ( $year <= 0 && ( $month <= 0 ||
$month >= 13 ) ) {
88 $timestamp = self
::getOffsetDate( $year, $month, $day );
91 // The timestamp used for DB queries is at midnight of the *next* day after the selected date.
92 $selectedDate = new DateTime( $timestamp->getTimestamp( TS_ISO_8601
) );
93 $selectedDate = $selectedDate->modify( '-1 day' );
95 $this->mYear
= (int)$selectedDate->format( 'Y' );
96 $this->mMonth
= (int)$selectedDate->format( 'm' );
97 $this->mDay
= (int)$selectedDate->format( 'd' );
98 $this->mOffset
= $this->mDb
->timestamp( $timestamp->getTimestamp() );
99 } catch ( TimestampException
$e ) {
100 // Invalid user provided timestamp (T149257)
104 return $this->mOffset
;
108 * Core logic of determining the mOffset timestamp such that we can get all items with
109 * a timestamp up to the specified parameters. Given parameters for a day up to which to get
110 * items, this function finds the timestamp of the day just after the end of the range for use
111 * in an database strict inequality filter.
113 * This is separate from getDateCond so we can use this logic in other places, such as in
114 * RangeChronologicalPager, where this function is used to convert year/month/day filter options
117 * @param int $year Year up to which we want revisions
118 * @param int $month Month up to which we want revisions
119 * @param int $day [optional] Day up to which we want revisions. Default is end of month.
120 * @return MWTimestamp Timestamp or null if year and month are false/invalid
122 public static function getOffsetDate( $year, $month, $day = -1 ) {
123 // Given an optional year, month, and day, we need to generate a timestamp
124 // to use as "WHERE rev_timestamp <= result"
125 // Examples: year = 2006 equals < 20070101 (+000000)
126 // year=2005, month=1 equals < 20050201
127 // year=2005, month=12 equals < 20060101
128 // year=2005, month=12, day=5 equals < 20051206
130 // If no year given, assume the current one
131 $timestamp = MWTimestamp
::getInstance();
132 $year = $timestamp->format( 'Y' );
133 // If this month hasn't happened yet this year, go back to last year's month
134 if ( $month > $timestamp->format( 'n' ) ) {
139 if ( $month && $month > 0 && $month < 13 ) {
140 // Day validity check after we have month and year checked
141 $day = checkdate( $month, $day, $year ) ?
$day : false;
143 if ( $day && $day > 0 ) {
144 // If we have a day, we want up to the day immediately afterward
147 // Did we overflow the current month?
148 if ( !checkdate( $month, $day, $year ) ) {
153 // If no day, assume beginning of next month
158 // Did we overflow the current year?
165 // No month implies we want up to the end of the year in question
172 if ( $year > 2032 ) {
176 $ymd = (int)sprintf( "%04d%02d%02d", $year, $month, $day );
178 if ( $ymd > 20320101 ) {
182 return MWTimestamp
::getInstance( "${ymd}000000" );