MediaWiki  1.30.0
cleanupBlocks.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
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 
43  $max = $db->selectField( 'ipblocks', 'MAX(ipb_user)' );
44 
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" );
49 
50  $delete = [];
51 
52  $res = $db->select(
53  'ipblocks',
54  [ 'ipb_user' ],
55  [
56  "ipb_user >= $from",
57  "ipb_user <= $to",
58  ],
59  __METHOD__,
60  [
61  'GROUP BY' => 'ipb_user',
62  'HAVING' => 'COUNT(*) > 1',
63  ]
64  );
65  foreach ( $res as $row ) {
66  $bestBlock = null;
67  $res2 = $db->select(
68  'ipblocks',
69  '*',
70  [
71  'ipb_user' => $row->ipb_user,
72  ]
73  );
74  foreach ( $res2 as $row2 ) {
75  $block = Block::newFromRow( $row2 );
76  if ( !$bestBlock ) {
77  $bestBlock = $block;
78  continue;
79  }
80 
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();
88  }
89  if ( $keep === null ) {
90  foreach ( [ 'createaccount', 'sendemail', 'editownusertalk' ] as $action ) {
91  if ( $block->prevents( $action ) xor $bestBlock->prevents( $action ) ) {
92  $keep = $block->prevents( $action );
93  break;
94  }
95  }
96  }
97 
98  if ( $keep ) {
99  $delete[] = $bestBlock->getId();
100  $bestBlock = $block;
101  } else {
102  $delete[] = $block->getId();
103  }
104  }
105  }
106 
107  if ( $delete ) {
108  $db->delete(
109  'ipblocks',
110  [ 'ipb_id' => $delete ],
111  __METHOD__
112  );
113  }
114  }
115 
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" );
120 
121  $res = $db->select(
122  [ 'ipblocks', 'user' ],
123  [ 'ipb_id', 'user_name' ],
124  [
125  'ipb_user = user_id',
126  "ipb_user >= $from",
127  "ipb_user <= $to",
128  'ipb_address != user_name',
129  ],
130  __METHOD__
131  );
132  foreach ( $res as $row ) {
133  $db->update(
134  'ipblocks',
135  [ 'ipb_address' => $row->user_name ],
136  [ 'ipb_id' => $row->ipb_id ],
137  __METHOD__
138  );
139  }
140  }
141 
142  $this->output( "Done!\n" );
143  }
144 }
145 
146 $maintClass = "CleanupBlocks";
147 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:103
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
CleanupBlocks\__construct
__construct()
Default constructor.
Definition: cleanupBlocks.php:34
php
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:35
CleanupBlocks
Maintenance script to clean up user blocks with user names not matching the 'user' table.
Definition: cleanupBlocks.php:32
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$maintClass
$maintClass
Definition: cleanupBlocks.php:146
CleanupBlocks\execute
execute()
Do the actual work.
Definition: cleanupBlocks.php:40
as
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
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
Block\newFromRow
static newFromRow( $row)
Create a new Block object from a database row.
Definition: Block.php:441
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:314