Merge "Fix \n handling for HTMLUsersMultiselectField"
[lhc/web/wiklou.git] / includes / pager / RangeChronologicalPager.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 * Pager for filtering by a range of dates.
25 * @ingroup Pager
26 */
27 abstract class RangeChronologicalPager extends ReverseChronologicalPager {
28
29 protected $rangeConds = [];
30
31 /**
32 * Set and return a date range condition using timestamps provided by the user.
33 * We want the revisions between the two timestamps.
34 * Also supports only having a start or end timestamp.
35 * Assumes that the start timestamp comes before the end timestamp.
36 *
37 * @param string $startStamp Timestamp of the beginning of the date range (or empty)
38 * @param string $endStamp Timestamp of the end of the date range (or empty)
39 * @return array|null Database conditions to satisfy the specified date range
40 * or null if dates are invalid
41 */
42 public function getDateRangeCond( $startStamp, $endStamp ) {
43 $this->rangeConds = [];
44
45 try {
46 if ( $startStamp !== '' ) {
47 $startTimestamp = MWTimestamp::getInstance( $startStamp );
48 $startTimestamp->setTimezone( $this->getConfig()->get( 'Localtimezone' ) );
49 $startOffset = $this->mDb->timestamp( $startTimestamp->getTimestamp() );
50 $this->rangeConds[] = $this->mIndexField . '>=' . $this->mDb->addQuotes( $startOffset );
51 }
52
53 if ( $endStamp !== '' ) {
54 $endTimestamp = MWTimestamp::getInstance( $endStamp );
55 $endTimestamp->setTimezone( $this->getConfig()->get( 'Localtimezone' ) );
56 $endOffset = $this->mDb->timestamp( $endTimestamp->getTimestamp() );
57 $this->rangeConds[] = $this->mIndexField . '<=' . $this->mDb->addQuotes( $endOffset );
58
59 // populate existing variables for compatibility with parent
60 $this->mYear = (int)$endTimestamp->format( 'Y' );
61 $this->mMonth = (int)$endTimestamp->format( 'm' );
62 $this->mDay = (int)$endTimestamp->format( 'd' );
63 $this->mOffset = $endOffset;
64 }
65 } catch ( TimestampException $ex ) {
66 return null;
67 }
68
69 return $this->rangeConds;
70 }
71
72 /**
73 * Takes ReverseChronologicalPager::getDateCond parameters and repurposes
74 * them to work with timestamp-based getDateRangeCond.
75 *
76 * @param int $year Year up to which we want revisions
77 * @param int $month Month up to which we want revisions
78 * @param int $day [optional] Day up to which we want revisions. Default is end of month.
79 * @return string|null Timestamp or null if year and month are false/invalid
80 */
81 public function getDateCond( $year, $month, $day = -1 ) {
82 // run through getDateRangeCond so rangeConds, mOffset, ... are set
83 $legacyTimestamp = self::getOffsetDate( $year, $month, $day );
84 // ReverseChronologicalPager uses strict inequality for the end date ('<'),
85 // but this class uses '<=' and expects extending classes to handle modifying the end date.
86 // Therefore, we need to subtract one second from the output of getOffsetDate to make it
87 // work with the '<=' inequality used in this class.
88 $legacyTimestamp->timestamp = $legacyTimestamp->timestamp->modify( '-1 second' );
89 $this->getDateRangeCond( '', $legacyTimestamp->getTimestamp( TS_MW ) );
90 return $this->mOffset;
91 }
92
93 /**
94 * Build variables to use by the database wrapper.
95 *
96 * @param string $offset Index offset, inclusive
97 * @param int $limit Exact query limit
98 * @param bool $descending Query direction, false for ascending, true for descending
99 * @return array
100 */
101 protected function buildQueryInfo( $offset, $limit, $descending ) {
102 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = parent::buildQueryInfo(
103 $offset,
104 $limit,
105 $descending
106 );
107
108 if ( $this->rangeConds ) {
109 $conds = array_merge( $conds, $this->rangeConds );
110 }
111
112 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
113 }
114 }