MediaWiki REL1_30
initEditCount.php
Go to the documentation of this file.
1<?php
25require_once __DIR__ . '/Maintenance.php';
26
28 public function __construct() {
29 parent::__construct();
30 $this->addOption( 'quick', 'Force the update to be done in a single query' );
31 $this->addOption( 'background', 'Force replication-friendly mode; may be inefficient but
32 avoids locking tables or lagging replica DBs with large updates;
33 calculates counts on a replica DB if possible.
34
35Background mode will be automatically used if multiple servers are listed
36in the 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_MASTER );
42 $user = $dbw->tableName( 'user' );
43 $revision = $dbw->tableName( 'revision' );
44
45 // Autodetect mode...
46 if ( $this->hasOption( 'background' ) ) {
47 $backgroundMode = true;
48 } elseif ( $this->hasOption( 'quick' ) ) {
49 $backgroundMode = false;
50 } else {
51 $backgroundMode = wfGetLB()->getServerCount() > 1;
52 }
53
54 if ( $backgroundMode ) {
55 $this->output( "Using replication-friendly background mode...\n" );
56
57 $dbr = $this->getDB( DB_REPLICA );
58 $chunkSize = 100;
59 $lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __METHOD__ );
60
61 $start = microtime( true );
62 $migrated = 0;
63 for ( $min = 0; $min <= $lastUser; $min += $chunkSize ) {
64 $max = $min + $chunkSize;
65 $result = $dbr->query(
66 "SELECT
67 user_id,
68 COUNT(rev_user) AS user_editcount
69 FROM $user
70 LEFT OUTER JOIN $revision ON user_id=rev_user
71 WHERE user_id > $min AND user_id <= $max
72 GROUP BY user_id",
73 __METHOD__ );
74
75 foreach ( $result as $row ) {
76 $dbw->update( 'user',
77 [ 'user_editcount' => $row->user_editcount ],
78 [ 'user_id' => $row->user_id ],
79 __METHOD__ );
80 ++$migrated;
81 }
82
83 $delta = microtime( true ) - $start;
84 $rate = ( $delta == 0.0 ) ? 0.0 : $migrated / $delta;
85 $this->output( sprintf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n",
86 wfWikiID(),
87 $migrated,
88 min( $max, $lastUser ) / $lastUser * 100.0,
89 $delta,
90 $rate ) );
91
93 }
94 } else {
95 $this->output( "Using single-query mode...\n" );
96 $sql = "UPDATE $user SET user_editcount=(SELECT COUNT(*) FROM $revision WHERE rev_user=user_id)";
97 $dbw->query( $sql );
98 }
99
100 $this->output( "Done!\n" );
101 }
102}
103
104$maintClass = "InitEditCount";
105require_once RUN_MAINTENANCE_IF_MAIN;
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
wfGetLB( $wiki=false)
Get a load balancer object.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
hasOption( $name)
Checks to see if a particular param exists.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
if(! $regexes) $dbr
Definition cleanup.php:94
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
$maintClass
require_once RUN_MAINTENANCE_IF_MAIN
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26