MediaWiki master
cleanupBlocks.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
27
37
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( "Cleanup user blocks with user names not matching the 'user' table" );
41 $this->setBatchSize( 1000 );
42 }
43
44 public function execute() {
45 $db = $this->getPrimaryDB();
46 $blockQuery = DatabaseBlock::getQueryInfo();
47
48 $max = $db->newSelectQueryBuilder()
49 ->select( 'MAX(ipb_user)' )
50 ->from( 'ipblocks' )
51 ->caller( __METHOD__ )
52 ->fetchField();
53
54 // Step 1: Clean up any duplicate user blocks
55 $batchSize = $this->getBatchSize();
56 for ( $from = 1; $from <= $max; $from += $batchSize ) {
57 $to = min( $max, $from + $batchSize - 1 );
58 $this->output( "Cleaning up duplicate ipb_user ($from-$to of $max)\n" );
59
60 $delete = [];
61
62 $res = $db->newSelectQueryBuilder()
63 ->select( 'ipb_user' )
64 ->from( 'ipblocks' )
65 ->where( [
66 'ipb_user >= ' . $from,
67 'ipb_user <= ' . (int)$to,
68 ] )
69 ->groupBy( 'ipb_user' )
70 ->having( 'COUNT(*) > 1' )
71 ->caller( __METHOD__ )
72 ->fetchResultSet();
73 foreach ( $res as $row ) {
74 $bestBlock = null;
75 $res2 = $db->newSelectQueryBuilder()
76 ->select( $blockQuery['fields'] )
77 ->tables( $blockQuery['tables'] )
78 ->where( [
79 'ipb_user' => $row->ipb_user,
80 ] )
81 ->joinConds( $blockQuery['joins'] )
82 ->caller( __METHOD__ )
83 ->fetchResultSet();
84 foreach ( $res2 as $row2 ) {
85 $block = DatabaseBlock::newFromRow( $row2 );
86 if ( !$bestBlock ) {
87 $bestBlock = $block;
88 continue;
89 }
90
91 // Find the most-restrictive block.
92 $keep = null;
93 if ( $keep === null && $block->getExpiry() !== $bestBlock->getExpiry() ) {
94 // This works for infinite blocks because 'infinity' > '20141024234513'
95 $keep = $block->getExpiry() > $bestBlock->getExpiry();
96 }
97 if ( $keep === null ) {
98 if ( $block->isCreateAccountBlocked() xor $bestBlock->isCreateAccountBlocked() ) {
99 $keep = $block->isCreateAccountBlocked();
100 } elseif ( $block->isEmailBlocked() xor $bestBlock->isEmailBlocked() ) {
101 $keep = $block->isEmailBlocked();
102 } elseif ( $block->isUsertalkEditAllowed() xor $bestBlock->isUsertalkEditAllowed() ) {
103 $keep = $block->isUsertalkEditAllowed();
104 }
105 }
106
107 if ( $keep ) {
108 $delete[] = $bestBlock->getId();
109 $bestBlock = $block;
110 } else {
111 $delete[] = $block->getId();
112 }
113 }
114 }
115
116 if ( $delete ) {
117 $db->newDeleteQueryBuilder()
118 ->deleteFrom( 'ipblocks' )
119 ->where( [ 'ipb_id' => $delete ] )
120 ->caller( __METHOD__ )->execute();
121 }
122 }
123
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" );
128
129 $res = $db->newSelectQueryBuilder()
130 ->select( [ 'ipb_id', 'user_name' ] )
131 ->tables( [ 'ipblocks', 'user' ] )
132 ->where( [
133 'ipb_user = user_id',
134 "ipb_user >= " . $from,
135 "ipb_user <= " . (int)$to,
136 'ipb_address != user_name',
137 ] )
138 ->caller( __METHOD__ )
139 ->fetchResultSet();
140 foreach ( $res as $row ) {
141 $db->newUpdateQueryBuilder()
142 ->update( 'ipblocks' )
143 ->set( [ 'ipb_address' => $row->user_name ] )
144 ->where( [ 'ipb_id' => $row->ipb_id ] )
145 ->caller( __METHOD__ )
146 ->execute();
147 }
148 }
149
150 $this->output( "Done!\n" );
151 }
152}
153
154$maintClass = CleanupBlocks::class;
155require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script to clean up user blocks with user names not matching the 'user' table.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
$maintClass