diff options
author | Func <Funcer@outlook.com> | 2023-02-13 16:05:48 +0800 |
---|---|---|
committer | Func <Funcer@outlook.com> | 2023-02-13 16:05:48 +0800 |
commit | f52e5993f4abfc7e4a79f68e7dafc629da76b663 (patch) | |
tree | 309580a8226ecacb2b826b47c4466cacb236245c | |
parent | 7ad56ac6f87f396c3f8d70a67131e3828b6dd9e1 (diff) | |
download | mediawikicore-f52e5993f4abfc7e4a79f68e7dafc629da76b663.tar.gz mediawikicore-f52e5993f4abfc7e4a79f68e7dafc629da76b663.zip |
pager: Provide a method to override the timestamp field
Subclass can override it to provide the timestamp field if they are
using a aliased field for getIndexField()
Bug: T325943
Change-Id: I239b771888437f9016102e4da64747d956cfbf23
-rw-r--r-- | includes/pager/RangeChronologicalPager.php | 11 | ||||
-rw-r--r-- | includes/pager/ReverseChronologicalPager.php | 17 |
2 files changed, 19 insertions, 9 deletions
diff --git a/includes/pager/RangeChronologicalPager.php b/includes/pager/RangeChronologicalPager.php index 8e41340344c7..ef6133519bcc 100644 --- a/includes/pager/RangeChronologicalPager.php +++ b/includes/pager/RangeChronologicalPager.php @@ -53,13 +53,12 @@ abstract class RangeChronologicalPager extends ReverseChronologicalPager { // Construct the conds array for compatibility with callers and derived classes $this->rangeConds = []; - // This is a chronological pager, so the first column should be some kind of timestamp - $timestampField = is_array( $this->mIndexField ) ? $this->mIndexField[0] : $this->mIndexField; try { if ( $startTime !== '' ) { $startTimestamp = MWTimestamp::getInstance( $startTime ); $this->startOffset = $this->mDb->timestamp( $startTimestamp->getTimestamp() ); - $this->rangeConds[] = $this->mDb->buildComparison( '>=', [ $timestampField => $this->startOffset ] ); + $this->rangeConds[] = $this->mDb->buildComparison( '>=', + [ $this->getTimestampField() => $this->startOffset ] ); } if ( $endTime !== '' ) { @@ -68,7 +67,8 @@ abstract class RangeChronologicalPager extends ReverseChronologicalPager { // add one second for compatibility with existing use cases $endTimestamp->timestamp = $endTimestamp->timestamp->modify( '+1 second' ); $this->endOffset = $this->mDb->timestamp( $endTimestamp->getTimestamp() ); - $this->rangeConds[] = $this->mDb->buildComparison( '<', [ $timestampField => $this->endOffset ] ); + $this->rangeConds[] = $this->mDb->buildComparison( '<', + [ $this->getTimestampField() => $this->endOffset ] ); // populate existing variables for compatibility with parent $this->mYear = (int)$endTimestamp->format( 'Y' ); @@ -93,8 +93,7 @@ abstract class RangeChronologicalPager extends ReverseChronologicalPager { ); // End of the range has been added by ReverseChronologicalPager if ( $this->startOffset ) { - $timestampField = is_array( $this->mIndexField ) ? $this->mIndexField[0] : $this->mIndexField; - $conds[] = $this->mDb->buildComparison( '>=', [ $timestampField => $this->startOffset ] ); + $conds[] = $this->mDb->buildComparison( '>=', [ $this->getTimestampField() => $this->startOffset ] ); } elseif ( $this->rangeConds ) { // Keep compatibility with some derived classes, T325034 $conds = array_merge( $conds, $this->rangeConds ); diff --git a/includes/pager/ReverseChronologicalPager.php b/includes/pager/ReverseChronologicalPager.php index 5a189bd57ab3..23db453e9c7d 100644 --- a/includes/pager/ReverseChronologicalPager.php +++ b/includes/pager/ReverseChronologicalPager.php @@ -89,6 +89,18 @@ abstract class ReverseChronologicalPager extends IndexPager { } /** + * 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 @@ -105,7 +117,7 @@ abstract class ReverseChronologicalPager extends IndexPager { protected function getRow( $row ): string { $s = ''; - $timestampField = is_array( $this->mIndexField ) ? $this->mIndexField[0] : $this->mIndexField; + $timestampField = $this->getTimestampField(); $timestamp = $row->$timestampField ?? null; $date = $timestamp ? $this->getDateFromTimestamp( $timestamp ) : null; if ( $date && $this->isHeaderRowNeeded( $date ) ) { @@ -300,8 +312,7 @@ abstract class ReverseChronologicalPager extends IndexPager { $order ); if ( $this->endOffset ) { - $timestampField = is_array( $this->mIndexField ) ? $this->mIndexField[0] : $this->mIndexField; - $conds[] = $this->mDb->buildComparison( '<', [ $timestampField => $this->endOffset ] ); + $conds[] = $this->mDb->buildComparison( '<', [ $this->getTimestampField() => $this->endOffset ] ); } return [ $tables, $fields, $conds, $fname, $options, $join_conds ]; |