RevisionUnitTest::testConstructFromArray @covers tags
[lhc/web/wiklou.git] / maintenance / cleanupTable.inc
index 57acfd8..3ace09c 100644 (file)
@@ -21,7 +21,7 @@
  * @ingroup Maintenance
  */
 
-require_once( __DIR__ . '/Maintenance.php' );
+require_once __DIR__ . '/Maintenance.php';
 
 /**
  * Generic class to cleanup a database table. Already subclasses Maintenance.
@@ -29,18 +29,19 @@ require_once( __DIR__ . '/Maintenance.php' );
  * @ingroup Maintenance
  */
 class TableCleanup extends Maintenance {
-       protected $defaultParams = array(
+       protected $defaultParams = [
                'table' => 'page',
-               'conds' => array(),
+               'conds' => [],
                'index' => 'page_id',
                'callback' => 'processRow',
-       );
+       ];
 
        protected $dryrun = false;
-       protected $maxLag = 10; # if slaves are lagged more than 10 secs, wait
        public $batchSize = 100;
        public $reportInterval = 100;
 
+       protected $processed, $updated, $count, $startTime, $table;
+
        public function __construct() {
                parent::__construct();
                $this->addOption( 'dry-run', 'Perform a dry run' );
@@ -48,11 +49,12 @@ class TableCleanup extends Maintenance {
 
        public function execute() {
                global $wgUser;
-               $wgUser = User::newFromName( 'Conversion script' );
                $this->dryrun = $this->hasOption( 'dry-run' );
                if ( $this->dryrun ) {
+                       $wgUser = User::newFromName( 'Conversion script' );
                        $this->output( "Checking for bad titles...\n" );
                } else {
+                       $wgUser = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
                        $this->output( "Checking and fixing bad titles...\n" );
                }
                $this->runTable( $this->defaultParams );
@@ -66,6 +68,9 @@ class TableCleanup extends Maintenance {
                $this->table = $table;
        }
 
+       /**
+        * @param int $updated
+        */
        protected function progress( $updated ) {
                $this->updated += $updated;
                $this->processed++;
@@ -96,12 +101,16 @@ class TableCleanup extends Maintenance {
                flush();
        }
 
+       /**
+        * @param array $params
+        * @throws MWException
+        */
        public function runTable( $params ) {
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = $this->getDB( DB_REPLICA );
 
                if ( array_diff( array_keys( $params ),
-                       array( 'table', 'conds', 'index', 'callback' ) ) )
-               {
+                       [ 'table', 'conds', 'index', 'callback' ] )
+               {
                        throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
                }
 
@@ -111,14 +120,13 @@ class TableCleanup extends Maintenance {
                $this->init( $count, $table );
                $this->output( "Processing $table...\n" );
 
-
                $index = (array)$params['index'];
-               $indexConds = array();
-               $options = array(
+               $indexConds = [];
+               $options = [
                        'ORDER BY' => implode( ',', $index ),
                        'LIMIT' => $this->batchSize
-               );
-               $callback = array( $this, $params['callback'] );
+               ];
+               $callback = [ $this, $params['callback'] ];
 
                while ( true ) {
                        $conds = array_merge( $params['conds'], $indexConds );
@@ -150,20 +158,17 @@ class TableCleanup extends Maintenance {
                                        $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
                                }
                        }
-                       $indexConds = array( $nextCond );
+                       $indexConds = [ $nextCond ];
                }
 
                $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
        }
 
+       /**
+        * @param array $matches
+        * @return string
+        */
        protected function hexChar( $matches ) {
                return sprintf( "\\x%02x", ord( $matches[1] ) );
        }
 }
-
-class TableCleanupTest extends TableCleanup {
-       function processRow( $row ) {
-               $this->progress( mt_rand( 0, 1 ) );
-       }
-}
-