MediaWiki master
initEditCount.php
Go to the documentation of this file.
1<?php
25// @codeCoverageIgnoreStart
26require_once __DIR__ . '/Maintenance.php';
27// @codeCoverageIgnoreEnd
28
33
35 public function __construct() {
36 parent::__construct();
37 $this->addOption( 'quick', 'Force the update to be done in a single query' );
38 $this->addOption( 'background', 'Force replication-friendly mode; may be inefficient but avoids'
39 . 'locking tables or lagging replica DBs with large updates; calculates counts on a replica DB'
40 . 'if possible. Background mode will be automatically used if multiple servers are listed in the'
41 . 'load balancer, usually indicating a replication environment.' );
42 $this->addDescription( 'Batch-recalculate user_editcount fields from the revision table' );
43 }
44
45 public function execute() {
46 $dbw = $this->getPrimaryDB();
47
48 // Autodetect mode...
49 if ( $this->hasOption( 'background' ) ) {
50 $backgroundMode = true;
51 } elseif ( $this->hasOption( 'quick' ) ) {
52 $backgroundMode = false;
53 } else {
54 $lb = $this->getServiceContainer()->getDBLoadBalancer();
55 $backgroundMode = $lb->hasReplicaServers();
56 }
57
58 if ( $backgroundMode ) {
59 $this->output( "Using replication-friendly background mode...\n" );
60
61 $dbr = $this->getReplicaDB();
62 $chunkSize = 100;
63 $lastUser = $dbr->newSelectQueryBuilder()
64 ->select( 'MAX(user_id)' )
65 ->from( 'user' )
66 ->caller( __METHOD__ )->fetchField();
67
68 $start = microtime( true );
69 $migrated = 0;
70 for ( $min = 0; $min <= $lastUser; $min += $chunkSize ) {
71 $max = $min + $chunkSize;
72
73 $result = $dbr->newSelectQueryBuilder()
74 ->select( [ 'user_id', 'user_editcount' => "COUNT(actor_rev_user.actor_user)" ] )
75 ->from( 'user' )
76 ->leftJoin( 'revision', 'rev', "user_id = actor_rev_user.actor_user" )
77 ->join( 'actor', 'actor_rev_user', 'actor_rev_user.actor_id = rev_actor' )
78 ->where( $dbr->expr( 'user_id', '>', $min )->and( 'user_id', '<=', $max ) )
79 ->groupBy( 'user_id' )
80 ->caller( __METHOD__ )->fetchResultSet();
81
82 foreach ( $result as $row ) {
83 $dbw->newUpdateQueryBuilder()
84 ->update( 'user' )
85 ->set( [ 'user_editcount' => $row->user_editcount ] )
86 ->where( [ 'user_id' => $row->user_id ] )
87 ->caller( __METHOD__ )->execute();
88 ++$migrated;
89 }
90
91 $delta = microtime( true ) - $start;
92 $rate = ( $delta == 0.0 ) ? 0.0 : $migrated / $delta;
93 $this->output( sprintf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n",
94 WikiMap::getCurrentWikiDbDomain()->getId(),
95 $migrated,
96 min( $max, $lastUser ) / $lastUser * 100.0,
97 $delta,
98 $rate ) );
99
100 $this->waitForReplication();
101 }
102 } else {
103 $this->output( "Using single-query mode...\n" );
104
105 $subquery = $dbw->newSelectQueryBuilder()
106 ->select( 'COUNT(*)' )
107 ->from( 'revision' )
108 ->join( 'actor', 'actor_rev_user', 'actor_rev_user.actor_id = rev_actor' )
109 ->where( 'user_id = actor_rev_user.actor_user' )
110 ->caller( __METHOD__ )->getSQL();
111
112 $dbw->newUpdateQueryBuilder()
113 ->table( 'user' )
114 ->set( [ 'user_editcount' => new RawSQLValue( "($subquery)" ) ] )
115 ->where( IDatabase::ALL_ROWS )
116 ->caller( __METHOD__ )
117 ->execute();
118 }
119
120 $this->output( "Done!\n" );
121 }
122}
123
124// @codeCoverageIgnoreStart
125$maintClass = InitEditCount::class;
126require_once RUN_MAINTENANCE_IF_MAIN;
127// @codeCoverageIgnoreEnd
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
Raw SQL value to be used in query builders.
$maintClass
Interface to a relational database.
Definition IDatabase.php:45