Update IPSet use statements
[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 /**
27 * Maintenance script to clean up user blocks with user names not matching the
28 * 'user' table.
29 *
30 * @ingroup Maintenance
31 */
32 class CleanupBlocks extends Maintenance {
33
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( "Cleanup user blocks with user names not matching the 'user' table" );
37 $this->setBatchSize( 1000 );
38 }
39
40 public function execute() {
41 $db = $this->getDB( DB_MASTER );
42 $blockQuery = Block::getQueryInfo();
43
44 $max = $db->selectField( 'ipblocks', 'MAX(ipb_user)' );
45
46 // Step 1: Clean up any duplicate user blocks
47 $batchSize = $this->getBatchSize();
48 for ( $from = 1; $from <= $max; $from += $batchSize ) {
49 $to = min( $max, $from + $batchSize - 1 );
50 $this->output( "Cleaning up duplicate ipb_user ($from-$to of $max)\n" );
51
52 $delete = [];
53
54 $res = $db->select(
55 'ipblocks',
56 [ 'ipb_user' ],
57 [
58 "ipb_user >= " . (int)$from,
59 "ipb_user <= " . (int)$to,
60 ],
61 __METHOD__,
62 [
63 'GROUP BY' => 'ipb_user',
64 'HAVING' => 'COUNT(*) > 1',
65 ]
66 );
67 foreach ( $res as $row ) {
68 $bestBlock = null;
69 $res2 = $db->select(
70 $blockQuery['tables'],
71 $blockQuery['fields'],
72 [
73 'ipb_user' => $row->ipb_user,
74 ],
75 __METHOD__,
76 [],
77 $blockQuery['joins']
78 );
79 foreach ( $res2 as $row2 ) {
80 $block = Block::newFromRow( $row2 );
81 if ( !$bestBlock ) {
82 $bestBlock = $block;
83 continue;
84 }
85
86 // Find the most-restrictive block. Can't use
87 // Block::chooseBlock because that's for IP blocks, not
88 // user blocks.
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 foreach ( [ 'createaccount', 'sendemail', 'editownusertalk' ] as $action ) {
96 if ( $block->prevents( $action ) xor $bestBlock->prevents( $action ) ) {
97 $keep = $block->prevents( $action );
98 break;
99 }
100 }
101 }
102
103 if ( $keep ) {
104 $delete[] = $bestBlock->getId();
105 $bestBlock = $block;
106 } else {
107 $delete[] = $block->getId();
108 }
109 }
110 }
111
112 if ( $delete ) {
113 $db->delete(
114 'ipblocks',
115 [ 'ipb_id' => $delete ],
116 __METHOD__
117 );
118 }
119 }
120
121 // Step 2: Update the user name in any blocks where it doesn't match
122 for ( $from = 1; $from <= $max; $from += $batchSize ) {
123 $to = min( $max, $from + $batchSize - 1 );
124 $this->output( "Cleaning up mismatched user name ($from-$to of $max)\n" );
125
126 $res = $db->select(
127 [ 'ipblocks', 'user' ],
128 [ 'ipb_id', 'user_name' ],
129 [
130 'ipb_user = user_id',
131 "ipb_user >= " . (int)$from,
132 "ipb_user <= " . (int)$to,
133 'ipb_address != user_name',
134 ],
135 __METHOD__
136 );
137 foreach ( $res as $row ) {
138 $db->update(
139 'ipblocks',
140 [ 'ipb_address' => $row->user_name ],
141 [ 'ipb_id' => $row->ipb_id ],
142 __METHOD__
143 );
144 }
145 }
146
147 $this->output( "Done!\n" );
148 }
149 }
150
151 $maintClass = "CleanupBlocks";
152 require_once RUN_MAINTENANCE_IF_MAIN;