Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FixBlockerUsername
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3$IP = getenv( 'MW_INSTALL_PATH' );
4if ( $IP === false ) {
5    $IP = __DIR__ . '/../../..';
6}
7require_once "$IP/maintenance/Maintenance.php";
8
9use MediaWiki\Extension\GlobalBlocking\GlobalBlocking;
10use MediaWiki\MediaWikiServices;
11
12/**
13 * Maintenance script for updating the username of a blocker in globalblocks.
14 *
15 * See https://phabricator.wikimedia.org/T298707.
16 */
17class FixBlockerUsername extends Maintenance {
18
19    public function __construct() {
20        parent::__construct();
21        $this->requireExtension( 'GlobalBlocking' );
22        $this->setBatchSize( 1000 );
23
24        $this->addArg( 'oldname', 'Username of the blocker that should be updated', true );
25        $this->addArg( 'newname', 'The username it should be updated to', true );
26    }
27
28    public function execute() {
29        $oldname = $this->getArg( 0 );
30        $newname = $this->getArg( 1 );
31        $dbw = GlobalBlocking::getPrimaryGlobalBlockingDatabase();
32        $services = MediaWikiServices::getInstance();
33        $lbFactory = $services->getDBLoadBalancerFactory();
34
35        $lastBlock = $dbw->newSelectQueryBuilder()
36            ->select( 'MAX(gb_id)' )
37            ->from( 'globalblocks' )
38            ->caller( __METHOD__ )
39            ->fetchField();
40
41        for ( $min = 0; $min <= $lastBlock; $min += $this->getBatchSize() ) {
42            $max = $min + $this->getBatchSize();
43            $this->output( "Now processing global blocks with id between {$min} and {$max}...\n" );
44
45            $dbw->newUpdateQueryBuilder()
46                ->update( 'globalblocks' )
47                ->set( [ 'gb_by' => $newname ] )
48                ->where( [
49                    'gb_by' => $oldname,
50                    $dbw->expr( 'gb_id', '>=', $min ),
51                    $dbw->expr( 'gb_id', '<=', $max ),
52                ] )
53                ->caller( __METHOD__ )
54                ->execute();
55
56            $lbFactory->waitForReplication();
57        }
58        $this->output( "Updated Blocks made by {$oldname}.\n" );
59    }
60}
61
62$maintClass = FixBlockerUsername::class;
63require_once RUN_MAINTENANCE_IF_MAIN;