Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
MigratePass1
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 migratePassOneReport
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2// pass 1:
3// * generate 'globaluser' entries for each username
4// * go through all usernames in 'globalnames' and for those
5// that can be automatically migrated, go ahead and do it.
6
7$IP = getenv( 'MW_INSTALL_PATH' );
8if ( $IP === false ) {
9    $IP = __DIR__ . '/../../..';
10}
11require_once "$IP/maintenance/Maintenance.php";
12
13use MediaWiki\Extension\CentralAuth\CentralAuthServices;
14use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
15
16class MigratePass1 extends Maintenance {
17
18    /** @var float */
19    protected $start;
20
21    /** @var int */
22    protected $migrated;
23
24    /** @var int */
25    protected $total;
26
27    /** @var string */
28    protected $fromPrefix;
29
30    public function __construct() {
31        parent::__construct();
32        $this->requireExtension( 'CentralAuth' );
33        $this->addDescription( 'Migrates local users to global users where possible' );
34        $this->start = microtime( true );
35        $this->migrated = 0;
36        $this->total = 0;
37        $this->fromPrefix = '';
38    }
39
40    public function execute() {
41        $this->output( "CentralAuth migration pass 1:\n" );
42        $this->output( "Finding accounts which can be migrated without interaction...\n" );
43
44        $dbBackground = CentralAuthServices::getDatabaseManager()->getCentralReplicaDB();
45
46        $result = $dbBackground->newSelectQueryBuilder()
47            ->select( 'gn_name' )
48            ->from( 'globalnames' )
49            ->caller( __METHOD__ )
50            ->fetchResultSet();
51
52        foreach ( $result as $row ) {
53            $this->fromPrefix = $row->gn_name;
54            $central = new CentralAuthUser( $row->gn_name, IDBAccessObject::READ_LATEST );
55            if ( $central->storeAndMigrate() ) {
56                $this->migrated++;
57            }
58            if ( ++$this->total % 1000 == 0 ) {
59                $this->migratePassOneReport();
60            }
61        }
62        $this->migratePassOneReport();
63        $this->output( "done.\n" );
64    }
65
66    private function migratePassOneReport() {
67        $delta = microtime( true ) - $this->start;
68        $this->output( sprintf(
69            "%s processed %d usernames (%.1f/sec), %d (%.1f%%) fully migrated (@ %s)\n",
70            wfTimestamp( TS_DB ),
71            $this->total,
72            $this->total / $delta,
73            $this->migrated,
74            $this->migrated / $this->total * 100.0,
75            $this->fromPrefix
76        ) );
77    }
78}
79
80$maintClass = MigratePass1::class;
81require_once RUN_MAINTENANCE_IF_MAIN;