MediaWiki  master
cleanupBlocks.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
27 
34 class CleanupBlocks extends Maintenance {
35 
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 );
40  }
41 
42  public function execute() {
43  $db = $this->getDB( DB_PRIMARY );
44  $blockQuery = DatabaseBlock::getQueryInfo();
45 
46  $max = $db->newSelectQueryBuilder()
47  ->select( 'MAX(ipb_user)' )
48  ->from( 'ipblocks' )
49  ->caller( __METHOD__ )
50  ->fetchField();
51 
52  // Step 1: Clean up any duplicate user blocks
53  $batchSize = $this->getBatchSize();
54  for ( $from = 1; $from <= $max; $from += $batchSize ) {
55  $to = min( $max, $from + $batchSize - 1 );
56  $this->output( "Cleaning up duplicate ipb_user ($from-$to of $max)\n" );
57 
58  $delete = [];
59 
60  $res = $db->newSelectQueryBuilder()
61  ->select( 'ipb_user' )
62  ->from( 'ipblocks' )
63  ->where( [
64  'ipb_user >= ' . $from,
65  'ipb_user <= ' . (int)$to,
66  ] )
67  ->groupBy( 'ipb_user' )
68  ->having( 'COUNT(*) > 1' )
69  ->caller( __METHOD__ )
70  ->fetchResultSet();
71  foreach ( $res as $row ) {
72  $bestBlock = null;
73  $res2 = $db->newSelectQueryBuilder()
74  ->select( $blockQuery['fields'] )
75  ->tables( $blockQuery['tables'] )
76  ->where( [
77  'ipb_user' => $row->ipb_user,
78  ] )
79  ->joinConds( $blockQuery['joins'] )
80  ->caller( __METHOD__ )
81  ->fetchResultSet();
82  foreach ( $res2 as $row2 ) {
83  $block = DatabaseBlock::newFromRow( $row2 );
84  if ( !$bestBlock ) {
85  $bestBlock = $block;
86  continue;
87  }
88 
89  // Find the most-restrictive block.
90  $keep = null;
91  if ( $keep === null && $block->getExpiry() !== $bestBlock->getExpiry() ) {
92  // This works for infinite blocks because 'infinity' > '20141024234513'
93  $keep = $block->getExpiry() > $bestBlock->getExpiry();
94  }
95  if ( $keep === null ) {
96  if ( $block->isCreateAccountBlocked() xor $bestBlock->isCreateAccountBlocked() ) {
97  $keep = $block->isCreateAccountBlocked();
98  } elseif ( $block->isEmailBlocked() xor $bestBlock->isEmailBlocked() ) {
99  $keep = $block->isEmailBlocked();
100  } elseif ( $block->isUsertalkEditAllowed() xor $bestBlock->isUsertalkEditAllowed() ) {
101  $keep = $block->isUsertalkEditAllowed();
102  }
103  }
104 
105  if ( $keep ) {
106  $delete[] = $bestBlock->getId();
107  $bestBlock = $block;
108  } else {
109  $delete[] = $block->getId();
110  }
111  }
112  }
113 
114  if ( $delete ) {
115  $db->delete(
116  'ipblocks',
117  [ 'ipb_id' => $delete ],
118  __METHOD__
119  );
120  }
121  }
122 
123  // Step 2: Update the user name in any blocks where it doesn't match
124  for ( $from = 1; $from <= $max; $from += $batchSize ) {
125  $to = min( $max, $from + $batchSize - 1 );
126  $this->output( "Cleaning up mismatched user name ($from-$to of $max)\n" );
127 
128  $res = $db->newSelectQueryBuilder()
129  ->select( [ 'ipb_id', 'user_name' ] )
130  ->tables( [ 'ipblocks', 'user' ] )
131  ->where( [
132  'ipb_user = user_id',
133  "ipb_user >= " . $from,
134  "ipb_user <= " . (int)$to,
135  'ipb_address != user_name',
136  ] )
137  ->caller( __METHOD__ )
138  ->fetchResultSet();
139  foreach ( $res as $row ) {
140  $db->update(
141  'ipblocks',
142  [ 'ipb_address' => $row->user_name ],
143  [ 'ipb_id' => $row->ipb_id ],
144  __METHOD__
145  );
146  }
147  }
148 
149  $this->output( "Done!\n" );
150  }
151 }
152 
153 $maintClass = CleanupBlocks::class;
154 require_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...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
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
const DB_PRIMARY
Definition: defines.php:28