Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / pager / ReverseChronologicalPager.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Pager
20 */
21 use Wikimedia\Timestamp\TimestampException;
22
23 /**
24 * Efficient paging for SQL queries.
25 * IndexPager with a formatted navigation bar.
26 * @ingroup Pager
27 */
28 abstract class ReverseChronologicalPager extends IndexPager {
29 /** @var bool */
30 public $mDefaultDirection = IndexPager::DIR_DESCENDING;
31 /** @var int */
32 public $mYear;
33 /** @var int */
34 public $mMonth;
35 /** @var int */
36 public $mDay;
37
38 public function getNavigationBar() {
39 if ( !$this->isNavigationBarShown() ) {
40 return '';
41 }
42
43 if ( isset( $this->mNavigationBar ) ) {
44 return $this->mNavigationBar;
45 }
46
47 $linkTexts = [
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()
52 ];
53
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();
60
61 $this->mNavigationBar = $firstLastLinks . ' ' .
62 $this->msg( 'viewprevnext' )->rawParams(
63 $pagingLinks['prev'], $pagingLinks['next'], $limits )->escaped();
64
65 return $this->mNavigationBar;
66 }
67
68 /**
69 * Set and return the mOffset timestamp such that we can get all revisions with
70 * a timestamp up to the specified parameters.
71 *
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
76 */
77 public function getDateCond( $year, $month, $day = -1 ) {
78 $year = (int)$year;
79 $month = (int)$month;
80 $day = (int)$day;
81
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 ) ) {
85 return null;
86 }
87
88 $timestamp = self::getOffsetDate( $year, $month, $day );
89
90 try {
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' );
94
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)
101 return null;
102 }
103
104 return $this->mOffset;
105 }
106
107 /**
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.
112 *
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
115 * into a timestamp.
116 *
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
121 */
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
129 if ( $year <= 0 ) {
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' ) ) {
135 $year--;
136 }
137 }
138
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;
142
143 if ( $day && $day > 0 ) {
144 // If we have a day, we want up to the day immediately afterward
145 $day++;
146
147 // Did we overflow the current month?
148 if ( !checkdate( $month, $day, $year ) ) {
149 $day = 1;
150 $month++;
151 }
152 } else {
153 // If no day, assume beginning of next month
154 $day = 1;
155 $month++;
156 }
157
158 // Did we overflow the current year?
159 if ( $month > 12 ) {
160 $month = 1;
161 $year++;
162 }
163
164 } else {
165 // No month implies we want up to the end of the year in question
166 $month = 1;
167 $day = 1;
168 $year++;
169 }
170
171 // Y2K38 bug
172 if ( $year > 2032 ) {
173 $year = 2032;
174 }
175
176 $ymd = (int)sprintf( "%04d%02d%02d", $year, $month, $day );
177
178 if ( $ymd > 20320101 ) {
179 $ymd = 20320101;
180 }
181
182 return MWTimestamp::getInstance( "${ymd}000000" );
183 }
184 }