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