Merge "Add MediaWikiTestCase convenience method for mocking a logger"
[lhc/web/wiklou.git] / maintenance / storage / recompressTracked.php
index b213929..7386df8 100644 (file)
@@ -58,6 +58,7 @@ class RecompressTracked {
        public $orphanBatchSize = 1000;
        public $reportingInterval = 10;
        public $numProcs = 1;
+       public $numBatches = 0;
        public $useDiff, $pageBlobClass, $orphanBlobClass;
        public $slavePipes, $slaveProcs, $prevSlaveId;
        public $copyOnly = false;
@@ -195,7 +196,7 @@ class RecompressTracked {
 
                        return false;
                }
-               $row = $dbr->selectRow( 'blob_tracking', '*', false, __METHOD__ );
+               $row = $dbr->selectRow( 'blob_tracking', '*', '', __METHOD__ );
                if ( !$row ) {
                        $this->info( "Warning: blob_tracking table contains no rows, skipping this wiki." );
 
@@ -228,7 +229,7 @@ class RecompressTracked {
 
                $this->slavePipes = $this->slaveProcs = array();
                for ( $i = 0; $i < $this->numProcs; $i++ ) {
-                       $pipes = false;
+                       $pipes = array();
                        $spec = array(
                                array( 'pipe', 'r' ),
                                array( 'file', 'php://stdout', 'w' ),
@@ -340,10 +341,10 @@ class RecompressTracked {
                                break;
                        }
                        foreach ( $res as $row ) {
+                               $startId = $row->bt_page;
                                $this->dispatch( 'doPage', $row->bt_page );
                                $i++;
                        }
-                       $startId = $row->bt_page;
                        $this->report( 'pages', $i, $numPages );
                }
                $this->report( 'pages', $i, $numPages );
@@ -365,7 +366,7 @@ class RecompressTracked {
                if ( $current == $end || $this->numBatches >= $this->reportingInterval ) {
                        $this->numBatches = 0;
                        $this->info( "$label: $current / $end" );
-                       $this->waitForSlaves();
+                       wfWaitForSlaves();
                }
        }
 
@@ -413,6 +414,7 @@ class RecompressTracked {
                        }
                        $ids = array();
                        foreach ( $res as $row ) {
+                               $startId = $row->bt_text_id;
                                $ids[] = $row->bt_text_id;
                                $i++;
                        }
@@ -431,7 +433,6 @@ class RecompressTracked {
                                call_user_func_array( array( $this, 'dispatch' ), $args );
                        }
 
-                       $startId = $row->bt_text_id;
                        $this->report( 'orphans', $i, $numOrphans );
                }
                $this->report( 'orphans', $i, $numOrphans );
@@ -463,7 +464,7 @@ class RecompressTracked {
                                case 'quit':
                                        return;
                        }
-                       $this->waitForSlaves();
+                       wfWaitForSlaves();
                }
        }
 
@@ -513,6 +514,7 @@ class RecompressTracked {
 
                        $lastTextId = 0;
                        foreach ( $res as $row ) {
+                               $startId = $row->bt_text_id;
                                if ( $lastTextId == $row->bt_text_id ) {
                                        // Duplicate (null edit)
                                        continue;
@@ -530,10 +532,9 @@ class RecompressTracked {
                                        $this->debug( "$titleText: committing blob with " . $trx->getSize() . " items" );
                                        $trx->commit();
                                        $trx = new CgzCopyTransaction( $this, $this->pageBlobClass );
-                                       $this->waitForSlaves();
+                                       wfWaitForSlaves();
                                }
                        }
-                       $startId = $row->bt_text_id;
                }
 
                $this->debug( "$titleText: committing blob with " . $trx->getSize() . " items" );
@@ -611,12 +612,12 @@ class RecompressTracked {
                        }
                        $this->debug( 'Incomplete: ' . $res->numRows() . ' rows' );
                        foreach ( $res as $row ) {
+                               $startId = $row->bt_text_id;
                                $this->moveTextRow( $row->bt_text_id, $row->bt_new_url );
                                if ( $row->bt_text_id % 10 == 0 ) {
-                                       $this->waitForSlaves();
+                                       wfWaitForSlaves();
                                }
                        }
-                       $startId = $row->bt_text_id;
                }
        }
 
@@ -681,34 +682,22 @@ class RecompressTracked {
                                $this->debug( "[orphan]: committing blob with " . $trx->getSize() . " rows" );
                                $trx->commit();
                                $trx = new CgzCopyTransaction( $this, $this->orphanBlobClass );
-                               $this->waitForSlaves();
+                               wfWaitForSlaves();
                        }
                }
                $this->debug( "[orphan]: committing blob with " . $trx->getSize() . " rows" );
                $trx->commit();
        }
-
-       /**
-        * Wait for slaves (quietly)
-        */
-       function waitForSlaves() {
-               $lb = wfGetLB();
-               while ( true ) {
-                       list( $host, $maxLag ) = $lb->getMaxLag();
-                       if ( $maxLag < 2 ) {
-                               break;
-                       }
-                       sleep( 5 );
-               }
-       }
 }
 
 /**
  * Class to represent a recompression operation for a single CGZ blob
  */
 class CgzCopyTransaction {
+       /** @var RecompressTracked */
        public $parent;
        public $blobClass;
+       /** @var ConcatenatedGzipHistoryBlob */
        public $cgz;
        public $referrers;
 
@@ -771,13 +760,14 @@ class CgzCopyTransaction {
                        return;
                }
 
-               // Check to see if the target text_ids have been moved already.
-               //
-               // We originally read from the slave, so this can happen when a single
-               // text_id is shared between multiple pages. It's rare, but possible
-               // if a delete/move/undelete cycle splits up a null edit.
-               //
-               // We do a locking read to prevent closer-run race conditions.
+               /* Check to see if the target text_ids have been moved already.
+                *
+                * We originally read from the slave, so this can happen when a single
+                * text_id is shared between multiple pages. It's rare, but possible
+                * if a delete/move/undelete cycle splits up a null edit.
+                *
+                * We do a locking read to prevent closer-run race conditions.
+                */
                $dbw = wfGetDB( DB_MASTER );
                $dbw->begin( __METHOD__ );
                $res = $dbw->select( 'blob_tracking',
@@ -800,7 +790,8 @@ class CgzCopyTransaction {
                                // All have been moved already
                                if ( $originalCount > 1 ) {
                                        // This is suspcious, make noise
-                                       $this->critical( "Warning: concurrent operation detected, are there two conflicting " .
+                                       $this->parent->critical(
+                                               "Warning: concurrent operation detected, are there two conflicting " .
                                                "processes running, doing the same job?" );
                                }