From 01d6c5325c3dc9e48991c953c10a1a34d6373686 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Wed, 27 Mar 2019 11:43:28 +0100 Subject: [PATCH] maintenance: Rewrite parts of the purgeChangedPages script My motivation to touch this code was a few count() I found confusing. I ended rearranging it a lot to improve readability and clarity. Change-Id: I9a0ea0812d4cc0c6e8ed6134e462dfdf325fd5d9 --- maintenance/purgeChangedPages.php | 32 +++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/maintenance/purgeChangedPages.php b/maintenance/purgeChangedPages.php index 0ef2a999f4..81fb5e39d7 100644 --- a/maintenance/purgeChangedPages.php +++ b/maintenance/purgeChangedPages.php @@ -170,23 +170,31 @@ class PurgeChangedPages extends Maintenance { */ protected function pageableSortedRows( ResultWrapper $res, $column, $limit ) { $rows = iterator_to_array( $res, false ); - $count = count( $rows ); - if ( !$count ) { - return [ [], null ]; // nothing to do - } elseif ( $count < $limit ) { - return [ $rows, $rows[$count - 1]->$column ]; // no more rows left + + // Nothing to do + if ( !$rows ) { + return [ [], null ]; + } + + $lastValue = end( $rows )->$column; + if ( count( $rows ) < $limit ) { + return [ $rows, $lastValue ]; } - $lastValue = $rows[$count - 1]->$column; // should be the highest - for ( $i = $count - 1; $i >= 0; --$i ) { - if ( $rows[$i]->$column === $lastValue ) { - unset( $rows[$i] ); - } else { + + for ( $i = count( $rows ) - 1; $i >= 0; --$i ) { + if ( $rows[$i]->$column !== $lastValue ) { break; } + + unset( $rows[$i] ); + } + + // No more rows left + if ( !$rows ) { + return [ [], null ]; } - $lastValueLeft = count( $rows ) ? $rows[count( $rows ) - 1]->$column : null; - return [ $rows, $lastValueLeft ]; + return [ $rows, end( $rows )->$column ]; } } -- 2.20.1