Merge "Revert "Use display name in category page subheadings if provided""
[lhc/web/wiklou.git] / includes / pager / ReverseChronologicalPager.php
1 <?php
2 /**
3 * Efficient paging for SQL queries.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Pager
22 */
23
24 /**
25 * IndexPager with a formatted navigation bar
26 * @ingroup Pager
27 */
28 abstract class ReverseChronologicalPager extends IndexPager {
29 public $mDefaultDirection = IndexPager::DIR_DESCENDING;
30 public $mYear;
31 public $mMonth;
32 public $mDay;
33
34 function getNavigationBar() {
35 if ( !$this->isNavigationBarShown() ) {
36 return '';
37 }
38
39 if ( isset( $this->mNavigationBar ) ) {
40 return $this->mNavigationBar;
41 }
42
43 $linkTexts = [
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()
48 ];
49
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();
56
57 $this->mNavigationBar = $firstLastLinks . ' ' .
58 $this->msg( 'viewprevnext' )->rawParams(
59 $pagingLinks['prev'], $pagingLinks['next'], $limits )->escaped();
60
61 return $this->mNavigationBar;
62 }
63
64 /**
65 * Set and return the mOffset timestamp such that we can get all revisions with
66 * a timestamp up to the specified parameters.
67 * @param int $year Year up to which we want revisions
68 * @param int $month Month up to which we want revisions
69 * @param int $day [optional] Day up to which we want revisions. Default is end of month.
70 * @return string|null Timestamp or null if year and month are false/invalid
71 */
72 function getDateCond( $year, $month, $day = -1 ) {
73 $year = intval( $year );
74 $month = intval( $month );
75 $day = intval( $day );
76
77 // Basic validity checks for year and month
78 $this->mYear = $year > 0 ? $year : false;
79 $this->mMonth = ( $month > 0 && $month < 13 ) ? $month : false;
80
81 // If year and month are false, don't update the mOffset
82 if ( !$this->mYear && !$this->mMonth ) {
83 return null;
84 }
85
86 // Given an optional year, month, and day, we need to generate a timestamp
87 // to use as "WHERE rev_timestamp <= result"
88 // Examples: year = 2006 equals < 20070101 (+000000)
89 // year=2005, month=1 equals < 20050201
90 // year=2005, month=12 equals < 20060101
91 // year=2005, month=12, day=5 equals < 20051206
92 if ( $this->mYear ) {
93 $year = $this->mYear;
94 } else {
95 // If no year given, assume the current one
96 $timestamp = MWTimestamp::getInstance();
97 $year = $timestamp->format( 'Y' );
98 // If this month hasn't happened yet this year, go back to last year's month
99 if ( $this->mMonth > $timestamp->format( 'n' ) ) {
100 $year--;
101 }
102 }
103
104 if ( $this->mMonth ) {
105 $month = $this->mMonth;
106
107 // Day validity check after we have month and year checked
108 $this->mDay = checkdate( $month, $day, $year ) ? $day : false;
109
110 if ( $this->mDay ) {
111 // If we have a day, we want up to the day immediately afterward
112 $day = $this->mDay + 1;
113
114 // Did we overflow the current month?
115 if ( !checkdate( $month, $day, $year ) ) {
116 $day = 1;
117 $month++;
118 }
119 } else {
120 // If no day, assume beginning of next month
121 $day = 1;
122 $month++;
123 }
124
125 // Did we overflow the current year?
126 if ( $month > 12 ) {
127 $month = 1;
128 $year++;
129 }
130
131 } else {
132 // No month implies we want up to the end of the year in question
133 $month = 1;
134 $day = 1;
135 $year++;
136 }
137
138 // Y2K38 bug
139 if ( $year > 2032 ) {
140 $year = 2032;
141 }
142
143 $ymd = (int)sprintf( "%04d%02d%02d", $year, $month, $day );
144
145 if ( $ymd > 20320101 ) {
146 $ymd = 20320101;
147 }
148
149 // Treat the given time in the wiki timezone and get a UTC timestamp for the database lookup
150 $timestamp = MWTimestamp::getInstance( "${ymd}000000" );
151 $timestamp->setTimezone( $this->getConfig()->get( 'Localtimezone' ) );
152
153 try {
154 $this->mOffset = $this->mDb->timestamp( $timestamp->getTimestamp() );
155 } catch ( TimestampException $e ) {
156 // Invalid user provided timestamp (T149257)
157 return null;
158 }
159
160 return $this->mOffset;
161 }
162 }