addDescription( <<addOption( 'rev-id', 'The rev_id to start copying from. Default: 0', false, true ); $this->addOption( 'throttle', 'Wait this many milliseconds after copying each batch of revisions. Default: 0', false, true ); $this->addOption( 'force', 'Run regardless of whether the database says it\'s been run already' ); } public function doDBUpdates() { $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); $dbw = $this->getDB( DB_MASTER ); $throttle = intval( $this->getOption( 'throttle', 0 ) ); $start = $this->getOption( 'rev-id', 0 ); $end = $dbw->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__ ); $blockStart = $start; $revCount = 0; $this->output( "Copying IP revisions to ip_changes, from rev_id $start to rev_id $end\n" ); while ( $blockStart <= $end ) { $rows = $dbw->select( 'revision', [ 'rev_id', 'rev_timestamp', 'rev_user_text' ], [ "rev_id >= $blockStart", 'rev_user' => 0 ], __METHOD__, [ 'ORDER BY' => 'rev_id ASC', 'LIMIT' => $this->mBatchSize ] ); if ( !$rows || $rows->numRows() === 0 ) { break; } $this->output( "...checking $this->mBatchSize revisions for IP edits that need copying, " . "starting with rev_id $blockStart\n" ); foreach ( $rows as $row ) { // Double-check to make sure this is an IP, e.g. not maintenance user or imported revision. if ( IP::isValid( $row->rev_user_text ) ) { $dbw->insert( 'ip_changes', [ 'ipc_rev_id' => $row->rev_id, 'ipc_rev_timestamp' => $row->rev_timestamp, 'ipc_hex' => IP::toHex( $row->rev_user_text ), ], __METHOD__, 'IGNORE' ); $revCount++; } $blockStart = (int)$row->rev_id; } $blockStart++; $lbFactory->waitForReplication(); usleep( $throttle * 1000 ); } $this->output( "$revCount IP revisions copied.\n" ); return true; } protected function getUpdateKey() { return 'populate ip_changes'; } } $maintClass = "PopulateIpChanges"; require_once RUN_MAINTENANCE_IF_MAIN;