maintenance: Rewrite parts of the purgeChangedPages script
authorThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Wed, 27 Mar 2019 10:43:28 +0000 (11:43 +0100)
committerBryanDavis <bdavis@wikimedia.org>
Tue, 2 Apr 2019 04:35:20 +0000 (04:35 +0000)
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

index 0ef2a99..81fb5e3 100644 (file)
@@ -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 ];
        }
 }