| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Pager; |
| 22 |
|
| 23 |
use DateTime; |
| 24 |
use MediaWiki\Html\Html; |
| 25 |
use MediaWiki\Utils\MWTimestamp; |
| 26 |
use Wikimedia\Timestamp\TimestampException; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
abstract class ReverseChronologicalPager extends IndexPager { |
| 35 |
|
| 36 |
public $mDefaultDirection = IndexPager::DIR_DESCENDING; |
| 37 |
|
| 38 |
public $mGroupByDate = false; |
| 39 |
|
| 40 |
public $mYear; |
| 41 |
|
| 42 |
public $mMonth; |
| 43 |
|
| 44 |
public $mDay; |
| 45 |
|
| 46 |
private $lastHeaderDate; |
| 47 |
|
| 48 |
protected $endOffset; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
protected function getHeaderRow( string $date ): string { |
| 55 |
$headingClass = $this->isFirstHeaderRow() ? |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
'mw-index-pager-list-header-first mw-index-pager-list-header' : |
| 61 |
'mw-index-pager-list-header'; |
| 62 |
|
| 63 |
$s = $this->isFirstHeaderRow() ? '' : $this->getEndGroup(); |
| 64 |
$s .= Html::element( 'h4', [ |
| 65 |
'class' => $headingClass, |
| 66 |
], |
| 67 |
$date |
| 68 |
); |
| 69 |
$s .= $this->getStartGroup(); |
| 70 |
return $s; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
protected function isHeaderRowNeeded( string $date ): bool { |
| 81 |
if ( !$this->mGroupByDate ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
return $date && $this->lastHeaderDate !== $date; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
final protected function isFirstHeaderRow(): bool { |
| 94 |
return $this->lastHeaderDate === null; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function getTimestampField() { |
| 105 |
|
| 106 |
return is_array( $this->mIndexField ) ? $this->mIndexField[0] : $this->mIndexField; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
final protected function getDateFromTimestamp( string $timestamp ) { |
| 117 |
return $this->getLanguage()->userDate( $timestamp, $this->getUser() ); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
protected function getRow( $row ): string { |
| 124 |
$s = ''; |
| 125 |
|
| 126 |
$timestampField = $this->getTimestampField(); |
| 127 |
$timestamp = $row->$timestampField ?? null; |
| 128 |
$date = $timestamp ? $this->getDateFromTimestamp( $timestamp ) : null; |
| 129 |
if ( $date && $this->isHeaderRowNeeded( $date ) ) { |
| 130 |
$s .= $this->getHeaderRow( $date ); |
| 131 |
$this->lastHeaderDate = $date; |
| 132 |
} |
| 133 |
|
| 134 |
$s .= $this->formatRow( $row ); |
| 135 |
return $s; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
protected function getStartGroup(): string { |
| 146 |
return "<ul class=\"mw-contributions-list\">\n"; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
protected function getEndGroup(): string { |
| 157 |
return '</ul>'; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
protected function getFooter(): string { |
| 164 |
return $this->getEndGroup(); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public function getNavigationBar() { |
| 172 |
if ( !$this->isNavigationBarShown() ) { |
| 173 |
return ''; |
| 174 |
} |
| 175 |
|
| 176 |
if ( isset( $this->mNavigationBar ) ) { |
| 177 |
return $this->mNavigationBar; |
| 178 |
} |
| 179 |
|
| 180 |
$navBuilder = $this->getNavigationBuilder() |
| 181 |
->setPrevMsg( 'pager-newer-n' ) |
| 182 |
->setNextMsg( 'pager-older-n' ) |
| 183 |
->setFirstMsg( 'histlast' ) |
| 184 |
->setLastMsg( 'histfirst' ); |
| 185 |
|
| 186 |
$this->mNavigationBar = $navBuilder->getHtml(); |
| 187 |
|
| 188 |
return $this->mNavigationBar; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function getDateCond( $year, $month, $day = -1 ) { |
| 203 |
$year = (int)$year; |
| 204 |
$month = (int)$month; |
| 205 |
$day = (int)$day; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
if ( $year <= 0 && ( $month <= 0 || $month >= 13 ) ) { |
| 210 |
return null; |
| 211 |
} |
| 212 |
|
| 213 |
$timestamp = self::getOffsetDate( $year, $month, $day ); |
| 214 |
|
| 215 |
try { |
| 216 |
|
| 217 |
$selectedDate = new DateTime( $timestamp->getTimestamp( TS_ISO_8601 ) ); |
| 218 |
$selectedDate = $selectedDate->modify( '-1 day' ); |
| 219 |
|
| 220 |
$this->mYear = (int)$selectedDate->format( 'Y' ); |
| 221 |
$this->mMonth = (int)$selectedDate->format( 'm' ); |
| 222 |
$this->mDay = (int)$selectedDate->format( 'd' ); |
| 223 |
|
| 224 |
$this->endOffset = $this->mDb->timestamp( $timestamp->getTimestamp() ); |
| 225 |
} catch ( TimestampException $e ) { |
| 226 |
|
| 227 |
return null; |
| 228 |
} |
| 229 |
|
| 230 |
return $this->endOffset; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public static function getOffsetDate( $year, $month, $day = -1 ) { |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
if ( $year <= 0 ) { |
| 256 |
|
| 257 |
$timestamp = MWTimestamp::getInstance(); |
| 258 |
$year = $timestamp->format( 'Y' ); |
| 259 |
|
| 260 |
if ( $month > $timestamp->format( 'n' ) ) { |
| 261 |
$year--; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if ( $month && $month > 0 && $month < 13 ) { |
| 266 |
|
| 267 |
$day = checkdate( $month, $day, $year ) ? $day : false; |
| 268 |
|
| 269 |
if ( $day && $day > 0 ) { |
| 270 |
|
| 271 |
$day++; |
| 272 |
|
| 273 |
|
| 274 |
if ( !checkdate( $month, $day, $year ) ) { |
| 275 |
$day = 1; |
| 276 |
$month++; |
| 277 |
} |
| 278 |
} else { |
| 279 |
|
| 280 |
$day = 1; |
| 281 |
$month++; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
if ( $month > 12 ) { |
| 286 |
$month = 1; |
| 287 |
$year++; |
| 288 |
} |
| 289 |
|
| 290 |
} else { |
| 291 |
|
| 292 |
$month = 1; |
| 293 |
$day = 1; |
| 294 |
$year++; |
| 295 |
} |
| 296 |
|
| 297 |
$ymd = sprintf( "%04d%02d%02d", $year, $month, $day ); |
| 298 |
|
| 299 |
return MWTimestamp::getInstance( "{$ymd}000000" ); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
public function getEndOffset() { |
| 309 |
return $this->endOffset; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
protected function buildQueryInfo( $offset, $limit, $order ) { |
| 316 |
[ $tables, $fields, $conds, $fname, $options, $join_conds ] = parent::buildQueryInfo( |
| 317 |
$offset, |
| 318 |
$limit, |
| 319 |
$order |
| 320 |
); |
| 321 |
if ( $this->endOffset ) { |
| 322 |
$conds[] = $this->mDb->expr( $this->getTimestampField(), '<', $this->endOffset ); |
| 323 |
} |
| 324 |
|
| 325 |
return [ $tables, $fields, $conds, $fname, $options, $join_conds ]; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
class_alias( ReverseChronologicalPager::class, 'ReverseChronologicalPager' ); |