| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
require_once __DIR__ . '/Maintenance.php'; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class InitUserPreference extends Maintenance { |
| 20 |
public function __construct() { |
| 21 |
parent::__construct(); |
| 22 |
$this->addOption( |
| 23 |
'target', |
| 24 |
'Name of the user preference to initialize', |
| 25 |
true, |
| 26 |
true, |
| 27 |
't' |
| 28 |
); |
| 29 |
$this->addOption( |
| 30 |
'source', |
| 31 |
'Name of the user preference to take the value from', |
| 32 |
true, |
| 33 |
true, |
| 34 |
's' |
| 35 |
); |
| 36 |
$this->setBatchSize( 300 ); |
| 37 |
} |
| 38 |
|
| 39 |
public function execute() { |
| 40 |
$target = $this->getOption( 'target' ); |
| 41 |
$source = $this->getOption( 'source' ); |
| 42 |
$this->output( "Initializing '$target' based on the value of '$source'\n" ); |
| 43 |
|
| 44 |
$dbr = $this->getReplicaDB(); |
| 45 |
$dbw = $this->getPrimaryDB(); |
| 46 |
|
| 47 |
$iterator = new BatchRowIterator( |
| 48 |
$dbr, |
| 49 |
'user_properties', |
| 50 |
[ 'up_user', 'up_property' ], |
| 51 |
$this->getBatchSize() |
| 52 |
); |
| 53 |
$iterator->setFetchColumns( [ 'up_user', 'up_value' ] ); |
| 54 |
$iterator->addConditions( [ |
| 55 |
'up_property' => $source, |
| 56 |
$dbr->expr( 'up_value', '!=', null ), |
| 57 |
$dbr->expr( 'up_value', '!=', 0 ), |
| 58 |
] ); |
| 59 |
$iterator->setCaller( __METHOD__ ); |
| 60 |
|
| 61 |
$processed = 0; |
| 62 |
foreach ( $iterator as $batch ) { |
| 63 |
foreach ( $batch as $row ) { |
| 64 |
$dbw->newInsertQueryBuilder() |
| 65 |
->insertInto( 'user_properties' ) |
| 66 |
->row( [ |
| 67 |
'up_user' => $row->up_user, |
| 68 |
'up_property' => $target, |
| 69 |
'up_value' => $row->up_value, |
| 70 |
] ) |
| 71 |
->onDuplicateKeyUpdate() |
| 72 |
->uniqueIndexFields( [ 'up_user', 'up_property' ] ) |
| 73 |
->set( [ 'up_value' => $row->up_value ] ) |
| 74 |
->caller( __METHOD__ )->execute(); |
| 75 |
|
| 76 |
$processed += $dbw->affectedRows(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$this->output( "Processed $processed user(s)\n" ); |
| 81 |
$this->output( "Finished!\n" ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
$maintClass = InitUserPreference::class; |
| 87 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 88 |
|