Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopulateCentralId
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doDbUpdates
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace MediaWiki\Extension\GlobalBlocking\Maintenance;
4
5$IP = getenv( 'MW_INSTALL_PATH' );
6if ( $IP === false ) {
7    $IP = __DIR__ . '/../../..';
8}
9require_once "$IP/maintenance/Maintenance.php";
10
11use LoggedUpdateMaintenance;
12use MediaWiki\MediaWikiServices;
13use MediaWiki\User\CentralId\CentralIdLookup;
14use MediaWiki\WikiMap\WikiMap;
15
16/**
17 * Maintenance script for migrating the blocker from a username to a
18 * central id.
19 */
20class PopulateCentralId extends LoggedUpdateMaintenance {
21
22    public function __construct() {
23        parent::__construct();
24        $this->requireExtension( 'GlobalBlocking' );
25    }
26
27    /**
28     * @inheritDoc
29     */
30    public function getUpdateKey() {
31        return 'GlobalBlockingPopulateCentralId';
32    }
33
34    /**
35     * @inheritDoc
36     */
37    public function doDbUpdates() {
38        $dbw = $this->getDB( DB_PRIMARY );
39        $services = MediaWikiServices::getInstance();
40        $lbFactory = $services->getDBLoadBalancerFactory();
41        $lookup = $services->getCentralIdLookup();
42        $wikiId = WikiMap::getCurrentWikiId();
43
44        $batchSize = $this->getBatchSize();
45        $count = 0;
46        $failed = 0;
47        $lastBlock = $dbw->newSelectQueryBuilder()
48            ->select( 'MAX(gb_id)' )
49            ->from( 'globalblocks' )
50            ->caller( __METHOD__ )
51            ->fetchField();
52        if ( !$lastBlock ) {
53            $this->output( "The globalblocks table seems to be empty.\n" );
54            return true;
55        }
56
57        for ( $min = 0; $min < $lastBlock; $min += $batchSize ) {
58            $max = $min + $batchSize;
59            $this->output( "Now processing global blocks with id between {$min} and {$max}...\n" );
60
61            $res = $dbw->newSelectQueryBuilder()
62                ->select( [ 'gb_id', 'gb_by' ] )
63                ->from( 'globalblocks' )
64                ->where( [
65                    'gb_by_central_id' => null,
66                    "gb_by_wiki" => $wikiId,
67                    $dbw->expr( 'gb_id', '>=', $min ),
68                    $dbw->expr( 'gb_id', '<=', $max ),
69                ] )
70                ->caller( __METHOD__ )
71                ->fetchResultSet();
72
73            foreach ( $res as $row ) {
74                $centralId = $lookup->centralIdFromName( $row->gb_by, CentralIdLookup::AUDIENCE_RAW );
75                if ( $centralId === 0 ) {
76                    $failed++;
77                    continue;
78                }
79                $dbw->newUpdateQueryBuilder()
80                    ->update( 'globalblocks' )
81                    ->set( [ 'gb_by_central_id' => $centralId ] )
82                    ->where( [ 'gb_id' => $row->gb_id ] )
83                    ->caller( __METHOD__ )
84                    ->execute();
85            }
86
87            $count += $dbw->affectedRows();
88            $lbFactory->waitForReplication();
89        }
90        $this->output( "Completed migration, updated $count row(s), migration failed for $failed row(s).\n" );
91
92        return true;
93    }
94}
95
96$maintClass = PopulateCentralId::class;
97require_once RUN_MAINTENANCE_IF_MAIN;