Debian Codegraph @

extensions/Echo/maintenance/updatePerUserBlacklist.php

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