File: ReverseChronologicalPager.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (330 lines) | stat: -rw-r--r-- 9,481 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

namespace MediaWiki\Pager;

use DateTime;
use MediaWiki\Html\Html;
use MediaWiki\Utils\MWTimestamp;
use Wikimedia\Timestamp\TimestampException;

/**
 * IndexPager with a formatted navigation bar.
 *
 * @stable to extend
 * @ingroup Pager
 */
abstract class ReverseChronologicalPager extends IndexPager {
	/** @var bool */
	public $mDefaultDirection = IndexPager::DIR_DESCENDING;
	/** @var bool Whether to group items by date */
	public $mGroupByDate = false;
	/** @var int */
	public $mYear;
	/** @var int */
	public $mMonth;
	/** @var int */
	public $mDay;
	/** @var string */
	private $lastHeaderDate;
	/** @var string */
	protected $endOffset;

	/**
	 * @param string $date
	 * @return string
	 */
	protected function getHeaderRow( string $date ): string {
		$headingClass = $this->isFirstHeaderRow() ?
			// We use mw-index-pager- prefix here on the anticipation that this method will
			// eventually be upstreamed to apply to other pagers. For now we constrain the
			// change to ReverseChronologicalPager to reduce the risk of pages this touches
			// in case there are any bugs.
			'mw-index-pager-list-header-first mw-index-pager-list-header' :
			'mw-index-pager-list-header';

		$s = $this->isFirstHeaderRow() ? '' : $this->getEndGroup();
		$s .= Html::element( 'h4', [
				'class' => $headingClass,
			],
			$date
		);
		$s .= $this->getStartGroup();
		return $s;
	}

	/**
	 * Determines if a header row is needed based on the current state of the IndexPager.
	 *
	 * @since 1.38
	 * @param string $date Formatted date header
	 * @return bool
	 */
	protected function isHeaderRowNeeded( string $date ): bool {
		if ( !$this->mGroupByDate ) {
			return false;
		}
		return $date && $this->lastHeaderDate !== $date;
	}

	/**
	 * Determines whether the header row is the first that will be outputted to the page.
	 *
	 * @since 1.38
	 * @return bool
	 */
	final protected function isFirstHeaderRow(): bool {
		return $this->lastHeaderDate === null;
	}

	/**
	 * Returns the name of the timestamp field. Subclass can override this to provide the
	 * timestamp field if they are using a aliased field for getIndexField()
	 *
	 * @since 1.40
	 * @return string
	 */
	public function getTimestampField() {
		// This is a chronological pager, so the first column should be some kind of timestamp
		return is_array( $this->mIndexField ) ? $this->mIndexField[0] : $this->mIndexField;
	}

	/**
	 * Get date from the timestamp
	 *
	 * @since 1.38
	 * @param string $timestamp
	 * @return string Formatted date header
	 */
	final protected function getDateFromTimestamp( string $timestamp ) {
		return $this->getLanguage()->userDate( $timestamp, $this->getUser() );
	}

	/**
	 * @inheritDoc
	 */
	protected function getRow( $row ): string {
		$s = '';

		$timestampField = $this->getTimestampField();
		$timestamp = $row->$timestampField ?? null;
		$date = $timestamp ? $this->getDateFromTimestamp( $timestamp ) : null;
		if ( $date && $this->isHeaderRowNeeded( $date ) ) {
			$s .= $this->getHeaderRow( $date );
			$this->lastHeaderDate = $date;
		}

		$s .= $this->formatRow( $row );
		return $s;
	}

	/**
	 * Start a new group of page rows.
	 *
	 * @stable to override
	 * @since 1.38
	 * @return string
	 */
	protected function getStartGroup(): string {
		return "<ul class=\"mw-contributions-list\">\n";
	}

	/**
	 * End an existing group of page rows.
	 *
	 * @stable to override
	 * @since 1.38
	 * @return string
	 */
	protected function getEndGroup(): string {
		return '</ul>';
	}

	/**
	 * @inheritDoc
	 */
	protected function getFooter(): string {
		return $this->getEndGroup();
	}

	/**
	 * @stable to override
	 * @return string HTML
	 */
	public function getNavigationBar() {
		if ( !$this->isNavigationBarShown() ) {
			return '';
		}

		if ( isset( $this->mNavigationBar ) ) {
			return $this->mNavigationBar;
		}

		$navBuilder = $this->getNavigationBuilder()
			->setPrevMsg( 'pager-newer-n' )
			->setNextMsg( 'pager-older-n' )
			->setFirstMsg( 'histlast' )
			->setLastMsg( 'histfirst' );

		$this->mNavigationBar = $navBuilder->getHtml();

		return $this->mNavigationBar;
	}

	/**
	 * Set and return the offset timestamp such that we can get all revisions with
	 * a timestamp up to the specified parameters.
	 *
	 * @stable to override
	 *
	 * @param int $year Year up to which we want revisions
	 * @param int $month Month up to which we want revisions
	 * @param int $day [optional] Day up to which we want revisions. Default is end of month.
	 * @return string|null Timestamp or null if year and month are false/invalid
	 */
	public function getDateCond( $year, $month, $day = -1 ) {
		$year = (int)$year;
		$month = (int)$month;
		$day = (int)$day;

		// Basic validity checks for year and month
		// If year and month are invalid, don't update the offset
		if ( $year <= 0 && ( $month <= 0 || $month >= 13 ) ) {
			return null;
		}

		$timestamp = self::getOffsetDate( $year, $month, $day );

		try {
			// The timestamp used for DB queries is at midnight of the *next* day after the selected date.
			$selectedDate = new DateTime( $timestamp->getTimestamp( TS_ISO_8601 ) );
			$selectedDate = $selectedDate->modify( '-1 day' );

			$this->mYear = (int)$selectedDate->format( 'Y' );
			$this->mMonth = (int)$selectedDate->format( 'm' );
			$this->mDay = (int)$selectedDate->format( 'd' );
			// Don't mess with mOffset which IndexPager uses
			$this->endOffset = $this->mDb->timestamp( $timestamp->getTimestamp() );
		} catch ( TimestampException $e ) {
			// Invalid user provided timestamp (T149257)
			return null;
		}

		return $this->endOffset;
	}

	/**
	 * Core logic of determining the offset timestamp such that we can get all items with
	 * a timestamp up to the specified parameters. Given parameters for a day up to which to get
	 * items, this function finds the timestamp of the day just after the end of the range for use
	 * in a database strict inequality filter.
	 *
	 * This is separate from getDateCond so we can use this logic in other places, such as in
	 * RangeChronologicalPager, where this function is used to convert year/month/day filter options
	 * into a timestamp.
	 *
	 * @param int $year Year up to which we want revisions
	 * @param int $month Month up to which we want revisions
	 * @param int $day [optional] Day up to which we want revisions. Default is end of month.
	 * @return MWTimestamp Timestamp or null if year and month are false/invalid
	 */
	public static function getOffsetDate( $year, $month, $day = -1 ) {
		// Given an optional year, month, and day, we need to generate a timestamp
		// to use as "WHERE rev_timestamp <= result"
		// Examples: year = 2006      equals < 20070101 (+000000)
		// year=2005, month=1         equals < 20050201
		// year=2005, month=12        equals < 20060101
		// year=2005, month=12, day=5 equals < 20051206
		if ( $year <= 0 ) {
			// If no year given, assume the current one
			$timestamp = MWTimestamp::getInstance();
			$year = $timestamp->format( 'Y' );
			// If this month hasn't happened yet this year, go back to last year's month
			if ( $month > $timestamp->format( 'n' ) ) {
				$year--;
			}
		}

		if ( $month && $month > 0 && $month < 13 ) {
			// Day validity check after we have month and year checked
			$day = checkdate( $month, $day, $year ) ? $day : false;

			if ( $day && $day > 0 ) {
				// If we have a day, we want up to the day immediately afterward
				$day++;

				// Did we overflow the current month?
				if ( !checkdate( $month, $day, $year ) ) {
					$day = 1;
					$month++;
				}
			} else {
				// If no day, assume beginning of next month
				$day = 1;
				$month++;
			}

			// Did we overflow the current year?
			if ( $month > 12 ) {
				$month = 1;
				$year++;
			}

		} else {
			// No month implies we want up to the end of the year in question
			$month = 1;
			$day = 1;
			$year++;
		}

		$ymd = sprintf( "%04d%02d%02d", $year, $month, $day );

		return MWTimestamp::getInstance( "{$ymd}000000" );
	}

	/**
	 * Return the end offset, extensions can use this if they are not in the context of subclass.
	 *
	 * @since 1.40
	 * @return string
	 */
	public function getEndOffset() {
		return $this->endOffset;
	}

	/**
	 * @inheritDoc
	 */
	protected function buildQueryInfo( $offset, $limit, $order ) {
		[ $tables, $fields, $conds, $fname, $options, $join_conds ] = parent::buildQueryInfo(
			$offset,
			$limit,
			$order
		);
		if ( $this->endOffset ) {
			$conds[] = $this->mDb->expr( $this->getTimestampField(), '<', $this->endOffset );
		}

		return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
	}
}

/** @deprecated class alias since 1.41 */
class_alias( ReverseChronologicalPager::class, 'ReverseChronologicalPager' );