Merge "RCFilters: Move label prefixes from dm.ItemModel to ui.TagItemWidget"
[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 for ( $from = 1; $from <= $max; $from += $this->mBatchSize ) {
48 $to = min( $max, $from + $this->mBatchSize - 1 );
49 $this->output( "Cleaning up duplicate ipb_user ($from-$to of $max)\n" );
50
51 $delete = [];
52
53 $res = $db->select(
54 'ipblocks',
55 [ 'ipb_user' ],
56 [
57 "ipb_user >= $from",
58 "ipb_user <= $to",
59 ],
60 __METHOD__,
61 [
62 'GROUP BY' => 'ipb_user',
63 'HAVING' => 'COUNT(*) > 1',
64 ]
65 );
66 foreach ( $res as $row ) {
67 $bestBlock = null;
68 $res2 = $db->select(
69 $blockQuery['tables'],
70 $blockQuery['fields'],
71 [
72 'ipb_user' => $row->ipb_user,
73 ],
74 __METHOD__,
75 [],
76 $blockQuery['joins']
77 );
78 foreach ( $res2 as $row2 ) {
79 $block = Block::newFromRow( $row2 );
80 if ( !$bestBlock ) {
81 $bestBlock = $block;
82 continue;
83 }
84
85 // Find the most-restrictive block. Can't use
86 // Block::chooseBlock because that's for IP blocks, not
87 // user blocks.
88 $keep = null;
89 if ( $keep === null && $block->getExpiry() !== $bestBlock->getExpiry() ) {
90 // This works for infinite blocks because 'infinity' > '20141024234513'
91 $keep = $block->getExpiry() > $bestBlock->getExpiry();
92 }
93 if ( $keep === null ) {
94 foreach ( [ 'createaccount', 'sendemail', 'editownusertalk' ] as $action ) {
95 if ( $block->prevents( $action ) xor $bestBlock->prevents( $action ) ) {
96 $keep = $block->prevents( $action );
97 break;
98 }
99 }
100 }
101
102 if ( $keep ) {
103 $delete[] = $bestBlock->getId();
104 $bestBlock = $block;
105 } else {
106 $delete[] = $block->getId();
107 }
108 }
109 }
110
111 if ( $delete ) {
112 $db->delete(
113 'ipblocks',
114 [ 'ipb_id' => $delete ],
115 __METHOD__
116 );
117 }
118 }
119
120 // Step 2: Update the user name in any blocks where it doesn't match
121 for ( $from = 1; $from <= $max; $from += $this->mBatchSize ) {
122 $to = min( $max, $from + $this->mBatchSize - 1 );
123 $this->output( "Cleaning up mismatched user name ($from-$to of $max)\n" );
124
125 $res = $db->select(
126 [ 'ipblocks', 'user' ],
127 [ 'ipb_id', 'user_name' ],
128 [
129 'ipb_user = user_id',
130 "ipb_user >= $from",
131 "ipb_user <= $to",
132 'ipb_address != user_name',
133 ],
134 __METHOD__
135 );
136 foreach ( $res as $row ) {
137 $db->update(
138 'ipblocks',
139 [ 'ipb_address' => $row->user_name ],
140 [ 'ipb_id' => $row->ipb_id ],
141 __METHOD__
142 );
143 }
144 }
145
146 $this->output( "Done!\n" );
147 }
148 }
149
150 $maintClass = "CleanupBlocks";
151 require_once RUN_MAINTENANCE_IF_MAIN;