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