Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[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 use Wikimedia\Timestamp\TimestampException;
24
25 /**
26 * IndexPager with a formatted navigation bar
27 * @ingroup Pager
28 */
29 abstract class ReverseChronologicalPager extends IndexPager {
30 public $mDefaultDirection = IndexPager::DIR_DESCENDING;
31 public $mYear;
32 public $mMonth;
33 public $mDay;
34
35 function getNavigationBar() {
36 if ( !$this->isNavigationBarShown() ) {
37 return '';
38 }
39
40 if ( isset( $this->mNavigationBar ) ) {
41 return $this->mNavigationBar;
42 }
43
44 $linkTexts = [
45 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
46 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
47 'first' => $this->msg( 'histlast' )->escaped(),
48 'last' => $this->msg( 'histfirst' )->escaped()
49 ];
50
51 $pagingLinks = $this->getPagingLinks( $linkTexts );
52 $limitLinks = $this->getLimitLinks();
53 $limits = $this->getLanguage()->pipeList( $limitLinks );
54 $firstLastLinks = $this->msg( 'parentheses' )->rawParams( "{$pagingLinks['first']}" .
55 $this->msg( 'pipe-separator' )->escaped() .
56 "{$pagingLinks['last']}" )->escaped();
57
58 $this->mNavigationBar = $firstLastLinks . ' ' .
59 $this->msg( 'viewprevnext' )->rawParams(
60 $pagingLinks['prev'], $pagingLinks['next'], $limits )->escaped();
61
62 return $this->mNavigationBar;
63 }
64
65 /**
66 * Set and return the mOffset timestamp such that we can get all revisions with
67 * 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
72 */
73 function getDateCond( $year, $month, $day = -1 ) {
74 $year = intval( $year );
75 $month = intval( $month );
76 $day = intval( $day );
77
78 // Basic validity checks for year and month
79 $this->mYear = $year > 0 ? $year : false;
80 $this->mMonth = ( $month > 0 && $month < 13 ) ? $month : false;
81
82 // If year and month are false, don't update the mOffset
83 if ( !$this->mYear && !$this->mMonth ) {
84 return null;
85 }
86
87 // Given an optional year, month, and day, we need to generate a timestamp
88 // to use as "WHERE rev_timestamp <= result"
89 // Examples: year = 2006 equals < 20070101 (+000000)
90 // year=2005, month=1 equals < 20050201
91 // year=2005, month=12 equals < 20060101
92 // year=2005, month=12, day=5 equals < 20051206
93 if ( $this->mYear ) {
94 $year = $this->mYear;
95 } else {
96 // If no year given, assume the current one
97 $timestamp = MWTimestamp::getInstance();
98 $year = $timestamp->format( 'Y' );
99 // If this month hasn't happened yet this year, go back to last year's month
100 if ( $this->mMonth > $timestamp->format( 'n' ) ) {
101 $year--;
102 }
103 }
104
105 if ( $this->mMonth ) {
106 $month = $this->mMonth;
107
108 // Day validity check after we have month and year checked
109 $this->mDay = checkdate( $month, $day, $year ) ? $day : false;
110
111 if ( $this->mDay ) {
112 // If we have a day, we want up to the day immediately afterward
113 $day = $this->mDay + 1;
114
115 // Did we overflow the current month?
116 if ( !checkdate( $month, $day, $year ) ) {
117 $day = 1;
118 $month++;
119 }
120 } else {
121 // If no day, assume beginning of next month
122 $day = 1;
123 $month++;
124 }
125
126 // Did we overflow the current year?
127 if ( $month > 12 ) {
128 $month = 1;
129 $year++;
130 }
131
132 } else {
133 // No month implies we want up to the end of the year in question
134 $month = 1;
135 $day = 1;
136 $year++;
137 }
138
139 // Y2K38 bug
140 if ( $year > 2032 ) {
141 $year = 2032;
142 }
143
144 $ymd = (int)sprintf( "%04d%02d%02d", $year, $month, $day );
145
146 if ( $ymd > 20320101 ) {
147 $ymd = 20320101;
148 }
149
150 // Treat the given time in the wiki timezone and get a UTC timestamp for the database lookup
151 $timestamp = MWTimestamp::getInstance( "${ymd}000000" );
152 $timestamp->setTimezone( $this->getConfig()->get( 'Localtimezone' ) );
153
154 try {
155 $this->mOffset = $this->mDb->timestamp( $timestamp->getTimestamp() );
156 } catch ( TimestampException $e ) {
157 // Invalid user provided timestamp (T149257)
158 return null;
159 }
160
161 return $this->mOffset;
162 }
163 }