MediaWiki REL1_37
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->getDB( DB_PRIMARY );
42
43 // Autodetect mode...
44 if ( $this->hasOption( 'background' ) ) {
45 $backgroundMode = true;
46 } elseif ( $this->hasOption( 'quick' ) ) {
47 $backgroundMode = false;
48 } else {
49 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
50 $backgroundMode = $lb->getServerCount() > 1;
51 }
52
53 $actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' );
54
55 if ( $backgroundMode ) {
56 $this->output( "Using replication-friendly background mode...\n" );
57
58 $dbr = $this->getDB( DB_REPLICA );
59 $chunkSize = 100;
60 $lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __METHOD__ );
61
62 $start = microtime( true );
63 $migrated = 0;
64 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
65 for ( $min = 0; $min <= $lastUser; $min += $chunkSize ) {
66 $max = $min + $chunkSize;
67
68 $revUser = $actorQuery['fields']['rev_user'];
69 $result = $dbr->select(
70 [ 'user', 'rev' => [ 'revision' ] + $actorQuery['tables'] ],
71 [ 'user_id', 'user_editcount' => "COUNT($revUser)" ],
72 "user_id > $min AND user_id <= $max",
73 __METHOD__,
74 [ 'GROUP BY' => 'user_id' ],
75 [ 'rev' => [ 'LEFT JOIN', "user_id = $revUser" ] ] + $actorQuery['joins']
76 );
77
78 foreach ( $result as $row ) {
79 $dbw->update( 'user',
80 [ 'user_editcount' => $row->user_editcount ],
81 [ 'user_id' => $row->user_id ],
82 __METHOD__ );
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 $lbFactory->waitForReplication();
96 }
97 } else {
98 $this->output( "Using single-query mode...\n" );
99
100 $user = $dbw->tableName( 'user' );
101 $subquery = $dbw->selectSQLText(
102 [ 'revision' ] + $actorQuery['tables'],
103 [ 'COUNT(*)' ],
104 [ 'user_id = ' . $actorQuery['fields']['rev_user'] ],
105 __METHOD__,
106 [],
107 $actorQuery['joins']
108 );
109 $dbw->query( "UPDATE $user SET user_editcount=($subquery)", __METHOD__ );
110 }
111
112 $this->output( "Done!\n" );
113 }
114}
115
116$maintClass = InitEditCount::class;
117require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
__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.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
$maintClass
const DB_REPLICA
Definition defines.php:25
const DB_PRIMARY
Definition defines.php:27