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