Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
EchoUpdatePerUserBlacklist
0.00% covered (danger)
0.00%
0 / 51
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 / 4
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 / 46
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * Update the Per User Blocklist from Usernames to User Ids.
4 *
5 * @ingroup Maintenance
6 */
7
8use MediaWiki\User\User;
9
10require_once getenv( 'MW_INSTALL_PATH' ) !== false
11    ? getenv( 'MW_INSTALL_PATH' ) . '/maintenance/Maintenance.php'
12    : __DIR__ . '/../../../maintenance/Maintenance.php';
13
14/**
15 * Maintenance script that changes the usernames to ids.
16 *
17 * @ingroup Maintenance
18 */
19class EchoUpdatePerUserBlacklist extends LoggedUpdateMaintenance {
20
21    public function __construct() {
22        parent::__construct();
23
24        $this->addDescription( 'Update echo-notifications-blacklist User Preference from Usernames to Ids' );
25        $this->setBatchSize( 100 );
26        $this->requireExtension( 'Echo' );
27    }
28
29    public function getUpdateKey() {
30        return __CLASS__;
31    }
32
33    public function doDBUpdates() {
34        $dbw = $this->getDB( DB_PRIMARY );
35        $dbr = $this->getDB( DB_REPLICA );
36        $iterator = new BatchRowIterator(
37            $dbr,
38            'user_properties',
39            [ 'up_user', 'up_property' ],
40            $this->getBatchSize()
41        );
42        $iterator->setFetchColumns( [
43            'up_user',
44            'up_value'
45        ] );
46        $iterator->addConditions( [
47            'up_property' => 'echo-notifications-blacklist'
48        ] );
49
50        $iterator->setCaller( __METHOD__ );
51
52        $this->output( "Updating Echo Notification Blacklist...\n" );
53
54        $centralIdLookup = $this->getServiceContainer()->getCentralIdLookup();
55        $processed = 0;
56        foreach ( $iterator as $batch ) {
57            foreach ( $batch as $row ) {
58                if ( !$row->up_value ) {
59                    continue;
60                }
61
62                $value = explode( "\n", $row->up_value );
63                $names = array_filter( $value, static function ( $item ) {
64                    return !is_numeric( $item );
65                } );
66
67                // If all of the values are numeric then the user has already been
68                // converted.
69                if ( !$names ) {
70                    continue;
71                }
72
73                $user = User::newFromId( $row->up_user );
74                $ids = $centralIdLookup->centralIdsFromNames( $names, $user );
75
76                $dbw->newUpdateQueryBuilder()
77                    ->update( 'user_properties' )
78                    ->set( [
79                        'up_value'  => implode( "\n", $ids ),
80                    ] )
81                    ->where( [
82                        'up_user' => $row->up_user,
83                        'up_property' => 'echo-notifications-blacklist',
84                    ] )
85                    ->caller( __METHOD__ )
86                    ->execute();
87                $processed += $dbw->affectedRows();
88                $this->waitForReplication();
89            }
90
91            $this->output( "Updated $processed Users\n" );
92        }
93
94        return true;
95    }
96}
97
98$maintClass = EchoUpdatePerUserBlacklist::class;
99require_once RUN_MAINTENANCE_IF_MAIN;