MediaWiki REL1_33
cleanupBlocks.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
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 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 >= " . (int)$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;
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new block object.
Definition Block.php:260
static newFromRow( $row)
Create a new Block object from a database row.
Definition Block.php:505
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.
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
Set the batch size.
$maintClass
$res
Definition database.txt:21
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
require_once RUN_MAINTENANCE_IF_MAIN
const DB_MASTER
Definition defines.php:26