From 0a32c179e49085dd6b6c0ef3111497229f4fa10f Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Mon, 27 Oct 2014 10:29:55 -0400 Subject: [PATCH] Maintenance script to clean up mismatched user names in ipblocks For blocks with ipb_user non-zero, the ipb_address field is supposed to match user_name. But due to various bugs over the years, this doesn't hold true. Due to those old bugs, we also have rows with the same ipb_user (and ipb_auto and ipb_anon_only, since those only apply to IP blocks) but different ipb_address. Those also need cleaning up, both to prevent a unique constraint violation and to stop errors in ApiQueryAllUsers. Bug: 34014 Bug: 72560 Change-Id: I519078ec011dbd0043811a78c3f913792a4df7fe --- maintenance/cleanupBlocks.php | 147 ++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 maintenance/cleanupBlocks.php diff --git a/maintenance/cleanupBlocks.php b/maintenance/cleanupBlocks.php new file mode 100644 index 0000000000..1736203b3a --- /dev/null +++ b/maintenance/cleanupBlocks.php @@ -0,0 +1,147 @@ +mDescription = "Cleanup user blocks with user names not matching the 'user' table"; + $this->setBatchSize( 1000 ); + } + + public function execute() { + $db = wfGetDB( DB_MASTER ); + + $max = $db->selectField( 'ipblocks', 'MAX(ipb_user)' ); + + // Step 1: Clean up any duplicate user blocks + for ( $from = 1; $from <= $max; $from += $this->mBatchSize ) { + $to = min( $max, $from + $this->mBatchSize - 1 ); + $this->output( "Cleaning up duplicate ipb_user ($from-$to of $max)\n" ); + + $delete = array(); + + $res = $db->select( + 'ipblocks', + array( 'ipb_user' ), + array( + "ipb_user >= $from", + "ipb_user <= $to", + ), + __METHOD__, + array( + 'GROUP BY' => 'ipb_user', + 'HAVING' => 'COUNT(*) > 1', + ) + ); + foreach ( $res as $row ) { + $bestBlock = null; + $res2 = $db->select( + 'ipblocks', + '*', + array( + 'ipb_user' => $row->ipb_user, + ) + ); + foreach ( $res2 as $row2 ) { + $block = Block::newFromRow( $row2 ); + if ( !$bestBlock ) { + $bestBlock = $block; + continue; + } + + // Find the most-restrictive block. Can't use + // Block::chooseBlock because that's for IP blocks, not + // user blocks. + $keep = null; + if ( $keep === null && $block->getExpiry() !== $bestBlock->getExpiry() ) { + // This works for infinite blocks because 'infinity' > '20141024234513' + $keep = $block->getExpiry() > $bestBlock->getExpiry(); + } + if ( $keep === null ) { + foreach ( array( 'createaccount', 'sendemail', 'editownusertalk' ) as $action ) { + if ( $block->prevents( $action ) xor $bestBlock->prevents( $action ) ) { + $keep = $block->prevents( $action ); + break; + } + } + } + + if ( $keep ) { + $delete[] = $bestBlock->getId(); + $bestBlock = $block; + } else { + $delete[] = $block->getId(); + } + } + } + + if ( $delete ) { + $db->delete( + 'ipblocks', + array( 'ipb_id' => $delete ), + __METHOD__ + ); + } + } + + // Step 2: Update the user name in any blocks where it doesn't match + for ( $from = 1; $from <= $max; $from += $this->mBatchSize ) { + $to = min( $max, $from + $this->mBatchSize - 1 ); + $this->output( "Cleaning up mismatched user name ($from-$to of $max)\n" ); + + $res = $db->select( + array( 'ipblocks', 'user' ), + array( 'ipb_id', 'user_name' ), + array( + 'ipb_user = user_id', + "ipb_user >= $from", + "ipb_user <= $to", + 'ipb_address != user_name', + ), + __METHOD__ + ); + foreach ( $res as $row ) { + $db->update( + 'ipblocks', + array( 'ipb_address' => $row->user_name ), + array( 'ipb_id' => $row->ipb_id ), + __METHOD__ + ); + } + } + + $this->output( "Done!\n" ); + } +} + +$maintClass = "CleanupBlocks"; +require_once RUN_MAINTENANCE_IF_MAIN; -- 2.20.1