]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - maintenance/cleanupTable.inc
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / maintenance / cleanupTable.inc
index 67a325103fc016cfd7a3aa4c1ff52775969fe1ee..3ace09cb30e7392042623fd96c4adc95a767e805 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Generic table cleanup class. Already subclasses maintenance
+ * Generic class to cleanup a database table.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * @ingroup Maintenance
  */
 
-require_once( dirname( __FILE__ ) . '/Maintenance.php' );
+require_once __DIR__ . '/Maintenance.php';
 
+/**
+ * Generic class to cleanup a database table. Already subclasses Maintenance.
+ *
+ * @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' );
@@ -43,11 +49,12 @@ class TableCleanup extends Maintenance {
 
        public function execute() {
                global $wgUser;
-               $wgUser->setName( '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 );
@@ -57,10 +64,13 @@ class TableCleanup extends Maintenance {
                $this->processed = 0;
                $this->updated = 0;
                $this->count = $count;
-               $this->startTime = wfTime();
+               $this->startTime = microtime( true );
                $this->table = $table;
        }
 
+       /**
+        * @param int $updated
+        */
        protected function progress( $updated ) {
                $this->updated += $updated;
                $this->processed++;
@@ -70,7 +80,7 @@ class TableCleanup extends Maintenance {
                $portion = $this->processed / $this->count;
                $updateRate = $this->updated / $this->processed;
 
-               $now = wfTime();
+               $now = microtime( true );
                $delta = $now - $this->startTime;
                $estimatedTotalTime = $delta / $portion;
                $eta = $this->startTime + $estimatedTotalTime;
@@ -91,28 +101,32 @@ 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 ) );
                }
 
                $table = $params['table'];
-               $count = $dbr->selectField( $table, 'count(*)', $params['conds'], __METHOD__ );
+               // count(*) would melt the DB for huge tables, we can estimate here
+               $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__ );
                $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 );
@@ -144,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 ) );
-       }
-}
-