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.
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.
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
21 use Wikimedia\Timestamp\TimestampException
;
24 * Pager for filtering by a range of dates.
27 abstract class RangeChronologicalPager
extends ReverseChronologicalPager
{
30 protected $rangeConds = [];
33 * Set and return a date range condition using timestamps provided by the user.
34 * We want the revisions between the two timestamps.
35 * Also supports only having a start or end timestamp.
36 * Assumes that the start timestamp comes before the end timestamp.
38 * @param string $startStamp Timestamp of the beginning of the date range (or empty)
39 * @param string $endStamp Timestamp of the end of the date range (or empty)
40 * @return array|null Database conditions to satisfy the specified date range
41 * or null if dates are invalid
43 public function getDateRangeCond( $startStamp, $endStamp ) {
44 $this->rangeConds
= [];
47 if ( $startStamp !== '' ) {
48 $startTimestamp = MWTimestamp
::getInstance( $startStamp );
49 $startOffset = $this->mDb
->timestamp( $startTimestamp->getTimestamp() );
50 $this->rangeConds
[] = $this->mIndexField
. '>=' . $this->mDb
->addQuotes( $startOffset );
53 if ( $endStamp !== '' ) {
54 $endTimestamp = MWTimestamp
::getInstance( $endStamp );
55 $endOffset = $this->mDb
->timestamp( $endTimestamp->getTimestamp() );
56 $this->rangeConds
[] = $this->mIndexField
. '<=' . $this->mDb
->addQuotes( $endOffset );
58 // populate existing variables for compatibility with parent
59 $this->mYear
= (int)$endTimestamp->format( 'Y' );
60 $this->mMonth
= (int)$endTimestamp->format( 'm' );
61 $this->mDay
= (int)$endTimestamp->format( 'd' );
62 $this->mOffset
= $endOffset;
64 } catch ( TimestampException
$ex ) {
68 return $this->rangeConds
;
72 * Takes ReverseChronologicalPager::getDateCond parameters and repurposes
73 * them to work with timestamp-based getDateRangeCond.
75 * @param int $year Year up to which we want revisions
76 * @param int $month Month up to which we want revisions
77 * @param int $day [optional] Day up to which we want revisions. Default is end of month.
78 * @return string|null Timestamp or null if year and month are false/invalid
80 public function getDateCond( $year, $month, $day = -1 ) {
81 // run through getDateRangeCond so rangeConds, mOffset, ... are set
82 $legacyTimestamp = self
::getOffsetDate( $year, $month, $day );
83 // ReverseChronologicalPager uses strict inequality for the end date ('<'),
84 // but this class uses '<=' and expects extending classes to handle modifying the end date.
85 // Therefore, we need to subtract one second from the output of getOffsetDate to make it
86 // work with the '<=' inequality used in this class.
87 $legacyTimestamp->timestamp
= $legacyTimestamp->timestamp
->modify( '-1 second' );
88 $this->getDateRangeCond( '', $legacyTimestamp->getTimestamp( TS_MW
) );
89 return $this->mOffset
;
93 * Build variables to use by the database wrapper.
95 * @param string $offset Index offset, inclusive
96 * @param int $limit Exact query limit
97 * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING
100 protected function buildQueryInfo( $offset, $limit, $order ) {
101 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = parent
::buildQueryInfo(
107 if ( $this->rangeConds
) {
108 $conds = array_merge( $conds, $this->rangeConds
);
111 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];