| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
require_once __DIR__ . '/Maintenance.php'; |
| 27 |
|
| 28 |
|
| 29 |
use MediaWiki\WikiMap\WikiMap; |
| 30 |
use Wikimedia\Rdbms\IDatabase; |
| 31 |
use Wikimedia\Rdbms\RawSQLValue; |
| 32 |
|
| 33 |
class InitEditCount extends Maintenance { |
| 34 |
public function __construct() { |
| 35 |
parent::__construct(); |
| 36 |
$this->addOption( 'quick', 'Force the update to be done in a single query' ); |
| 37 |
$this->addOption( 'background', 'Force replication-friendly mode; may be inefficient but avoids' |
| 38 |
. 'locking tables or lagging replica DBs with large updates; calculates counts on a replica DB' |
| 39 |
. 'if possible. Background mode will be automatically used if multiple servers are listed in the' |
| 40 |
. 'load balancer, usually indicating a replication environment.' ); |
| 41 |
$this->addDescription( 'Batch-recalculate user_editcount fields from the revision table' ); |
| 42 |
} |
| 43 |
|
| 44 |
public function execute() { |
| 45 |
$dbw = $this->getPrimaryDB(); |
| 46 |
|
| 47 |
|
| 48 |
if ( $this->hasOption( 'background' ) ) { |
| 49 |
$backgroundMode = true; |
| 50 |
} elseif ( $this->hasOption( 'quick' ) ) { |
| 51 |
$backgroundMode = false; |
| 52 |
} else { |
| 53 |
$lb = $this->getServiceContainer()->getDBLoadBalancer(); |
| 54 |
$backgroundMode = $lb->hasReplicaServers(); |
| 55 |
} |
| 56 |
|
| 57 |
if ( $backgroundMode ) { |
| 58 |
$this->output( "Using replication-friendly background mode...\n" ); |
| 59 |
|
| 60 |
$dbr = $this->getReplicaDB(); |
| 61 |
$chunkSize = 100; |
| 62 |
$lastUser = $dbr->newSelectQueryBuilder() |
| 63 |
->select( 'MAX(user_id)' ) |
| 64 |
->from( 'user' ) |
| 65 |
->caller( __METHOD__ )->fetchField(); |
| 66 |
|
| 67 |
$start = microtime( true ); |
| 68 |
$migrated = 0; |
| 69 |
for ( $min = 0; $min <= $lastUser; $min += $chunkSize ) { |
| 70 |
$max = $min + $chunkSize; |
| 71 |
|
| 72 |
$result = $dbr->newSelectQueryBuilder() |
| 73 |
->select( [ 'user_id', 'user_editcount' => 'COUNT(rev_actor)' ] ) |
| 74 |
->from( 'user' ) |
| 75 |
->join( 'actor', 'actor_rev_user', 'user_id = actor_rev_user.actor_user' ) |
| 76 |
->leftJoin( 'revision', 'rev', 'actor_rev_user.actor_id = rev.rev_actor' ) |
| 77 |
->where( $dbr->expr( 'user_id', '>', $min )->and( 'user_id', '<=', $max ) ) |
| 78 |
->groupBy( 'user_id' ) |
| 79 |
->caller( __METHOD__ )->fetchResultSet(); |
| 80 |
|
| 81 |
foreach ( $result as $row ) { |
| 82 |
$dbw->newUpdateQueryBuilder() |
| 83 |
->update( 'user' ) |
| 84 |
->set( [ 'user_editcount' => $row->user_editcount ] ) |
| 85 |
->where( [ 'user_id' => $row->user_id ] ) |
| 86 |
->caller( __METHOD__ )->execute(); |
| 87 |
++$migrated; |
| 88 |
} |
| 89 |
|
| 90 |
$delta = microtime( true ) - $start; |
| 91 |
$rate = ( $delta == 0.0 ) ? 0.0 : $migrated / $delta; |
| 92 |
$this->output( sprintf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n", |
| 93 |
WikiMap::getCurrentWikiDbDomain()->getId(), |
| 94 |
$migrated, |
| 95 |
min( $max, $lastUser ) / $lastUser * 100.0, |
| 96 |
$delta, |
| 97 |
$rate ) ); |
| 98 |
|
| 99 |
$this->waitForReplication(); |
| 100 |
} |
| 101 |
} else { |
| 102 |
$this->output( "Using single-query mode...\n" ); |
| 103 |
|
| 104 |
$subquery = $dbw->newSelectQueryBuilder() |
| 105 |
->select( 'COUNT(*)' ) |
| 106 |
->from( 'revision' ) |
| 107 |
->join( 'actor', 'actor_rev_user', 'actor_rev_user.actor_id = rev_actor' ) |
| 108 |
->where( 'user_id = actor_rev_user.actor_user' ) |
| 109 |
->caller( __METHOD__ )->getSQL(); |
| 110 |
|
| 111 |
$dbw->newUpdateQueryBuilder() |
| 112 |
->table( 'user' ) |
| 113 |
->set( [ 'user_editcount' => new RawSQLValue( "($subquery)" ) ] ) |
| 114 |
->where( IDatabase::ALL_ROWS ) |
| 115 |
->caller( __METHOD__ ) |
| 116 |
->execute(); |
| 117 |
} |
| 118 |
|
| 119 |
$this->output( "Done!\n" ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
$maintClass = InitEditCount::class; |
| 125 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 126 |
|