| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\User\TempUser\TempUserConfig; |
| 25 |
use Wikimedia\Rdbms\IDatabase; |
| 26 |
use Wikimedia\Rdbms\IExpression; |
| 27 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 28 |
|
| 29 |
|
| 30 |
require_once __DIR__ . '/Maintenance.php'; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class PopulateUserIsTemp extends LoggedUpdateMaintenance { |
| 40 |
|
| 41 |
private TempUserConfig $tempUserConfig; |
| 42 |
private IDatabase $dbw; |
| 43 |
private IReadableDatabase $dbr; |
| 44 |
|
| 45 |
public function __construct() { |
| 46 |
parent::__construct(); |
| 47 |
$this->addDescription( 'Populates the user_is_temp field of the user table.' ); |
| 48 |
$this->setBatchSize( 200 ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
protected function getUpdateKey() { |
| 53 |
return __CLASS__; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
protected function doDBUpdates() { |
| 58 |
$this->initServices(); |
| 59 |
|
| 60 |
if ( !$this->tempUserConfig->isKnown() ) { |
| 61 |
|
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
$queryBuilder = $this->dbr->newSelectQueryBuilder() |
| 68 |
->select( 'user_id' ) |
| 69 |
->from( 'user' ) |
| 70 |
->where( [ |
| 71 |
'user_is_temp' => 0, |
| 72 |
$this->tempUserConfig->getMatchCondition( $this->dbr, 'user_name', IExpression::LIKE ), |
| 73 |
] ) |
| 74 |
->limit( $this->getBatchSize() ?? 200 ) |
| 75 |
->caller( __METHOD__ ); |
| 76 |
|
| 77 |
do { |
| 78 |
|
| 79 |
$batch = $queryBuilder->fetchFieldValues(); |
| 80 |
if ( count( $batch ) ) { |
| 81 |
|
| 82 |
$this->dbw->newUpdateQueryBuilder() |
| 83 |
->update( 'user' ) |
| 84 |
->set( [ 'user_is_temp' => 1 ] ) |
| 85 |
->where( [ 'user_id' => $batch ] ) |
| 86 |
->caller( __METHOD__ ) |
| 87 |
->execute(); |
| 88 |
} |
| 89 |
} while ( count( $batch ) >= ( $this->getBatchSize() ?? 200 ) ); |
| 90 |
|
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
protected function initServices(): void { |
| 103 |
$this->tempUserConfig = $this->getServiceContainer()->getTempUserConfig(); |
| 104 |
$this->dbw = $this->getDB( DB_PRIMARY ); |
| 105 |
$this->dbr = $this->getDB( DB_REPLICA ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
$maintClass = PopulateUserIsTemp::class; |
| 111 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 112 |
|