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
{
29 public $mDefaultDirection = IndexPager
::DIR_DESCENDING
;
34 public function getNavigationBar() {
35 if ( !$this->isNavigationBarShown() ) {
39 if ( isset( $this->mNavigationBar
) ) {
40 return $this->mNavigationBar
;
44 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit
)->escaped(),
45 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit
)->escaped(),
46 'first' => $this->msg( 'histlast' )->escaped(),
47 'last' => $this->msg( 'histfirst' )->escaped()
50 $pagingLinks = $this->getPagingLinks( $linkTexts );
51 $limitLinks = $this->getLimitLinks();
52 $limits = $this->getLanguage()->pipeList( $limitLinks );
53 $firstLastLinks = $this->msg( 'parentheses' )->rawParams( "{$pagingLinks['first']}" .
54 $this->msg( 'pipe-separator' )->escaped() .
55 "{$pagingLinks['last']}" )->escaped();
57 $this->mNavigationBar
= $firstLastLinks . ' ' .
58 $this->msg( 'viewprevnext' )->rawParams(
59 $pagingLinks['prev'], $pagingLinks['next'], $limits )->escaped();
61 return $this->mNavigationBar
;
65 * Set and return the mOffset timestamp such that we can get all revisions with
66 * a timestamp up to the specified parameters.
68 * @param int $year Year up to which we want revisions
69 * @param int $month Month up to which we want revisions
70 * @param int $day [optional] Day up to which we want revisions. Default is end of month.
71 * @return string|null Timestamp or null if year and month are false/invalid
73 public function getDateCond( $year, $month, $day = -1 ) {
78 // Basic validity checks for year and month
79 // If year and month are invalid, don't update the mOffset
80 if ( $year <= 0 && ( $month <= 0 ||
$month >= 13 ) ) {
84 $timestamp = self
::getOffsetDate( $year, $month, $day );
87 // The timestamp used for DB queries is at midnight of the *next* day after the selected date.
88 $selectedDate = new DateTime( $timestamp->getTimestamp( TS_ISO_8601
) );
89 $selectedDate = $selectedDate->modify( '-1 day' );
91 $this->mYear
= (int)$selectedDate->format( 'Y' );
92 $this->mMonth
= (int)$selectedDate->format( 'm' );
93 $this->mDay
= (int)$selectedDate->format( 'd' );
94 $this->mOffset
= $this->mDb
->timestamp( $timestamp->getTimestamp() );
95 } catch ( TimestampException
$e ) {
96 // Invalid user provided timestamp (T149257)
100 return $this->mOffset
;
104 * Core logic of determining the mOffset timestamp such that we can get all items with
105 * a timestamp up to the specified parameters. Given parameters for a day up to which to get
106 * items, this function finds the timestamp of the day just after the end of the range for use
107 * in an database strict inequality filter.
109 * This is separate from getDateCond so we can use this logic in other places, such as in
110 * RangeChronologicalPager, where this function is used to convert year/month/day filter options
113 * @param int $year Year up to which we want revisions
114 * @param int $month Month up to which we want revisions
115 * @param int $day [optional] Day up to which we want revisions. Default is end of month.
116 * @return MWTimestamp Timestamp or null if year and month are false/invalid
118 public static function getOffsetDate( $year, $month, $day = -1 ) {
119 // Given an optional year, month, and day, we need to generate a timestamp
120 // to use as "WHERE rev_timestamp <= result"
121 // Examples: year = 2006 equals < 20070101 (+000000)
122 // year=2005, month=1 equals < 20050201
123 // year=2005, month=12 equals < 20060101
124 // year=2005, month=12, day=5 equals < 20051206
126 // If no year given, assume the current one
127 $timestamp = MWTimestamp
::getInstance();
128 $year = $timestamp->format( 'Y' );
129 // If this month hasn't happened yet this year, go back to last year's month
130 if ( $month > $timestamp->format( 'n' ) ) {
135 if ( $month && $month > 0 && $month < 13 ) {
136 // Day validity check after we have month and year checked
137 $day = checkdate( $month, $day, $year ) ?
$day : false;
139 if ( $day && $day > 0 ) {
140 // If we have a day, we want up to the day immediately afterward
143 // Did we overflow the current month?
144 if ( !checkdate( $month, $day, $year ) ) {
149 // If no day, assume beginning of next month
154 // Did we overflow the current year?
161 // No month implies we want up to the end of the year in question
168 if ( $year > 2032 ) {
172 $ymd = (int)sprintf( "%04d%02d%02d", $year, $month, $day );
174 if ( $ymd > 20320101 ) {
178 return MWTimestamp
::getInstance( "${ymd}000000" );