Merge "Add day to date filter for ReverseChronologicalPager"
[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 [optional] Year up to which we want revisions. Default is current year.
68 * @param int $month [optional] Month up to which we want revisions. Default is end of year.
69 * @param int $day [optional] Day up to which we want revisions. Default is end of month.
70 * @return string Timestamp
71 */
72 function getDateCond( $year = -1, $month = -1, $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 // Given an optional year, month, and day, we need to generate a timestamp
82 // to use as "WHERE rev_timestamp <= result"
83 // Examples: year = 2006 equals < 20070101 (+000000)
84 // year=2005, month=1 equals < 20050201
85 // year=2005, month=12 equals < 20060101
86 // year=2005, month=12, day=5 equals < 20051206
87 if ( $this->mYear ) {
88 $year = $this->mYear;
89 } else {
90 // If no year given, assume the current one
91 $timestamp = MWTimestamp::getInstance();
92 $year = $timestamp->format( 'Y' );
93 // If this month hasn't happened yet this year, go back to last year's month
94 if ( $this->mMonth > $timestamp->format( 'n' ) ) {
95 $year--;
96 }
97 }
98
99 if ( $this->mMonth ) {
100 $month = $this->mMonth;
101
102 // Day validity check after we have month and year checked
103 $this->mDay = checkdate( $month, $day, $year ) ? $day : false;
104
105 if ( $this->mDay ) {
106 // If we have a day, we want up to the day immediately afterward
107 $day = $this->mDay + 1;
108
109 // Did we overflow the current month?
110 if ( !checkdate( $month, $day, $year ) ) {
111 $day = 1;
112 $month++;
113 }
114 } else {
115 // If no day, assume beginning of next month
116 $day = 1;
117 $month++;
118 }
119
120 // Did we overflow the current year?
121 if ( $month > 12 ) {
122 $month = 1;
123 $year++;
124 }
125
126 } else {
127 // No month implies we want up to the end of the year in question
128 $month = 1;
129 $day = 1;
130 $year++;
131 }
132
133 // Y2K38 bug
134 if ( $year > 2032 ) {
135 $year = 2032;
136 }
137
138 $ymd = (int)sprintf( "%04d%02d%02d", $year, $month, $day );
139
140 if ( $ymd > 20320101 ) {
141 $ymd = 20320101;
142 }
143
144 // Treat the given time in the wiki timezone and get a UTC timestamp for the database lookup
145 $timestamp = MWTimestamp::getInstance( "${ymd}000000" );
146 $timestamp->setTimezone( $this->getConfig()->get( 'Localtimezone' ) );
147
148 $this->mOffset = $this->mDb->timestamp( $timestamp->getTimestamp() );
149 return $this->mOffset;
150 }
151 }