Fix SQLite patch-(page|template)links-fix-pk.sql column order
[lhc/web/wiklou.git] / maintenance / cleanupBlocks.php
1 <?php
2 /**
3 * Cleans up user blocks with user names not matching the 'user' table
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 use MediaWiki\Block\DatabaseBlock;
27
28 /**
29 * Maintenance script to clean up user blocks with user names not matching the
30 * 'user' table.
31 *
32 * @ingroup Maintenance
33 */
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_MASTER );
44 $blockQuery = DatabaseBlock::getQueryInfo();
45
46 $max = $db->selectField( 'ipblocks', 'MAX(ipb_user)' );
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 >= " . (int)$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. Can't use
89 // DatabaseBlock::chooseBlock because that's for IP blocks, not
90 // user blocks.
91 $keep = null;
92 if ( $keep === null && $block->getExpiry() !== $bestBlock->getExpiry() ) {
93 // This works for infinite blocks because 'infinity' > '20141024234513'
94 $keep = $block->getExpiry() > $bestBlock->getExpiry();
95 }
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();
103 }
104 }
105
106 if ( $keep ) {
107 $delete[] = $bestBlock->getId();
108 $bestBlock = $block;
109 } else {
110 $delete[] = $block->getId();
111 }
112 }
113 }
114
115 if ( $delete ) {
116 $db->delete(
117 'ipblocks',
118 [ 'ipb_id' => $delete ],
119 __METHOD__
120 );
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->select(
130 [ 'ipblocks', 'user' ],
131 [ 'ipb_id', 'user_name' ],
132 [
133 'ipb_user = user_id',
134 "ipb_user >= " . (int)$from,
135 "ipb_user <= " . (int)$to,
136 'ipb_address != user_name',
137 ],
138 __METHOD__
139 );
140 foreach ( $res as $row ) {
141 $db->update(
142 'ipblocks',
143 [ 'ipb_address' => $row->user_name ],
144 [ 'ipb_id' => $row->ipb_id ],
145 __METHOD__
146 );
147 }
148 }
149
150 $this->output( "Done!\n" );
151 }
152 }
153
154 $maintClass = CleanupBlocks::class;
155 require_once RUN_MAINTENANCE_IF_MAIN;