3 * Cleans up user blocks with user names not matching the 'user' table
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
26 use MediaWiki\Block\DatabaseBlock
;
29 * Maintenance script to clean up user blocks with user names not matching the
32 * @ingroup Maintenance
34 class CleanupBlocks
extends Maintenance
{
36 public function __construct() {
37 parent
::__construct();
38 $this->addDescription( "Cleanup user blocks with user names not matching the 'user' table" );
39 $this->setBatchSize( 1000 );
42 public function execute() {
43 $db = $this->getDB( DB_MASTER
);
44 $blockQuery = DatabaseBlock
::getQueryInfo();
46 $max = $db->selectField( 'ipblocks', 'MAX(ipb_user)' );
48 // Step 1: Clean up any duplicate user blocks
49 $batchSize = $this->getBatchSize();
50 for ( $from = 1; $from <= $max; $from +
= $batchSize ) {
51 $to = min( $max, $from +
$batchSize - 1 );
52 $this->output( "Cleaning up duplicate ipb_user ($from-$to of $max)\n" );
60 "ipb_user >= " . (int)$from,
61 "ipb_user <= " . (int)$to,
65 'GROUP BY' => 'ipb_user',
66 'HAVING' => 'COUNT(*) > 1',
69 foreach ( $res as $row ) {
72 $blockQuery['tables'],
73 $blockQuery['fields'],
75 'ipb_user' => $row->ipb_user
,
81 foreach ( $res2 as $row2 ) {
82 $block = DatabaseBlock
::newFromRow( $row2 );
88 // Find the most-restrictive block. Can't use
89 // DatabaseBlock::chooseBlock because that's for IP blocks, not
92 if ( $keep === null && $block->getExpiry() !== $bestBlock->getExpiry() ) {
93 // This works for infinite blocks because 'infinity' > '20141024234513'
94 $keep = $block->getExpiry() > $bestBlock->getExpiry();
96 if ( $keep === null ) {
97 if ( $block->isCreateAccountBlocked() xor $bestBlock->isCreateAccountBlocked() ) {
98 $keep = $block->isCreateAccountBlocked();
99 } elseif ( $block->isEmailBlocked() xor $bestBlock->isEmailBlocked() ) {
100 $keep = $block->isEmailBlocked();
101 } elseif ( $block->isUsertalkEditAllowed() xor $bestBlock->isUsertalkEditAllowed() ) {
102 $keep = $block->isUsertalkEditAllowed();
107 $delete[] = $bestBlock->getId();
110 $delete[] = $block->getId();
118 [ 'ipb_id' => $delete ],
124 // Step 2: Update the user name in any blocks where it doesn't match
125 for ( $from = 1; $from <= $max; $from +
= $batchSize ) {
126 $to = min( $max, $from +
$batchSize - 1 );
127 $this->output( "Cleaning up mismatched user name ($from-$to of $max)\n" );
130 [ 'ipblocks', 'user' ],
131 [ 'ipb_id', 'user_name' ],
133 'ipb_user = user_id',
134 "ipb_user >= " . (int)$from,
135 "ipb_user <= " . (int)$to,
136 'ipb_address != user_name',
140 foreach ( $res as $row ) {
143 [ 'ipb_address' => $row->user_name
],
144 [ 'ipb_id' => $row->ipb_id
],
150 $this->output( "Done!\n" );
154 $maintClass = CleanupBlocks
::class;
155 require_once RUN_MAINTENANCE_IF_MAIN
;