MediaWiki master
initEditCount.php
Go to the documentation of this file.
1<?php
25// @codeCoverageIgnoreStart
26require_once __DIR__ . '/Maintenance.php';
27// @codeCoverageIgnoreEnd
28
32
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 // Autodetect mode...
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(actor_rev_user.actor_user)" ] )
74 ->from( 'user' )
75 ->leftJoin( 'revision', 'rev', "user_id = actor_rev_user.actor_user" )
76 ->join( 'actor', 'actor_rev_user', 'actor_rev_user.actor_id = 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// @codeCoverageIgnoreStart
124$maintClass = InitEditCount::class;
125require_once RUN_MAINTENANCE_IF_MAIN;
126// @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.
waitForReplication()
Wait for replica DBs 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.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
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:48