Update cssjanus/cssjanus to v1.1.2
[mediawiki.git] / maintenance / cleanupBlocks.php
blob1736203b3a5e660c6e50616cc80001a93da52553
1 <?php
2 /**
3 * Cleans up user blocks with user names not matching the 'user' table
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script to clean up user blocks with user names not matching the
28 * 'user' table.
30 * @ingroup Maintenance
32 class CleanupBlocks extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Cleanup user blocks with user names not matching the 'user' table";
37 $this->setBatchSize( 1000 );
40 public function execute() {
41 $db = wfGetDB( DB_MASTER );
43 $max = $db->selectField( 'ipblocks', 'MAX(ipb_user)' );
45 // Step 1: Clean up any duplicate user blocks
46 for ( $from = 1; $from <= $max; $from += $this->mBatchSize ) {
47 $to = min( $max, $from + $this->mBatchSize - 1 );
48 $this->output( "Cleaning up duplicate ipb_user ($from-$to of $max)\n" );
50 $delete = array();
52 $res = $db->select(
53 'ipblocks',
54 array( 'ipb_user' ),
55 array(
56 "ipb_user >= $from",
57 "ipb_user <= $to",
59 __METHOD__,
60 array(
61 'GROUP BY' => 'ipb_user',
62 'HAVING' => 'COUNT(*) > 1',
65 foreach ( $res as $row ) {
66 $bestBlock = null;
67 $res2 = $db->select(
68 'ipblocks',
69 '*',
70 array(
71 'ipb_user' => $row->ipb_user,
74 foreach ( $res2 as $row2 ) {
75 $block = Block::newFromRow( $row2 );
76 if ( !$bestBlock ) {
77 $bestBlock = $block;
78 continue;
81 // Find the most-restrictive block. Can't use
82 // Block::chooseBlock because that's for IP blocks, not
83 // user blocks.
84 $keep = null;
85 if ( $keep === null && $block->getExpiry() !== $bestBlock->getExpiry() ) {
86 // This works for infinite blocks because 'infinity' > '20141024234513'
87 $keep = $block->getExpiry() > $bestBlock->getExpiry();
89 if ( $keep === null ) {
90 foreach ( array( 'createaccount', 'sendemail', 'editownusertalk' ) as $action ) {
91 if ( $block->prevents( $action ) xor $bestBlock->prevents( $action ) ) {
92 $keep = $block->prevents( $action );
93 break;
98 if ( $keep ) {
99 $delete[] = $bestBlock->getId();
100 $bestBlock = $block;
101 } else {
102 $delete[] = $block->getId();
107 if ( $delete ) {
108 $db->delete(
109 'ipblocks',
110 array( 'ipb_id' => $delete ),
111 __METHOD__
116 // Step 2: Update the user name in any blocks where it doesn't match
117 for ( $from = 1; $from <= $max; $from += $this->mBatchSize ) {
118 $to = min( $max, $from + $this->mBatchSize - 1 );
119 $this->output( "Cleaning up mismatched user name ($from-$to of $max)\n" );
121 $res = $db->select(
122 array( 'ipblocks', 'user' ),
123 array( 'ipb_id', 'user_name' ),
124 array(
125 'ipb_user = user_id',
126 "ipb_user >= $from",
127 "ipb_user <= $to",
128 'ipb_address != user_name',
130 __METHOD__
132 foreach ( $res as $row ) {
133 $db->update(
134 'ipblocks',
135 array( 'ipb_address' => $row->user_name ),
136 array( 'ipb_id' => $row->ipb_id ),
137 __METHOD__
142 $this->output( "Done!\n" );
146 $maintClass = "CleanupBlocks";
147 require_once RUN_MAINTENANCE_IF_MAIN;