| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\RenameUser; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use Job; |
| 7 |
use MediaWiki\Config\Config; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\Title\Title; |
| 10 |
use Wikimedia\Rdbms\ILBFactory; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class RenameUserJob extends Job { |
| 37 |
|
| 38 |
private $updateRowsPerQuery; |
| 39 |
|
| 40 |
|
| 41 |
private $lbFactory; |
| 42 |
|
| 43 |
public function __construct( |
| 44 |
Title $title, |
| 45 |
$params, |
| 46 |
Config $config, |
| 47 |
ILBFactory $lbFactory |
| 48 |
) { |
| 49 |
parent::__construct( 'renameUser', $title, $params ); |
| 50 |
|
| 51 |
$this->updateRowsPerQuery = $config->get( MainConfigNames::UpdateRowsPerQuery ); |
| 52 |
$this->lbFactory = $lbFactory; |
| 53 |
} |
| 54 |
|
| 55 |
public function run() { |
| 56 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 57 |
$ticket = $this->lbFactory->getEmptyTransactionTicket( __METHOD__ ); |
| 58 |
$table = $this->params['table']; |
| 59 |
$column = $this->params['column']; |
| 60 |
|
| 61 |
$oldname = $this->params['oldname']; |
| 62 |
$newname = $this->params['newname']; |
| 63 |
if ( isset( $this->params['userID'] ) ) { |
| 64 |
$userID = $this->params['userID']; |
| 65 |
$uidColumn = $this->params['uidColumn']; |
| 66 |
} else { |
| 67 |
$userID = null; |
| 68 |
$uidColumn = null; |
| 69 |
} |
| 70 |
if ( isset( $this->params['timestampColumn'] ) ) { |
| 71 |
$timestampColumn = $this->params['timestampColumn']; |
| 72 |
$minTimestamp = $this->params['minTimestamp']; |
| 73 |
$maxTimestamp = $this->params['maxTimestamp']; |
| 74 |
} else { |
| 75 |
$timestampColumn = null; |
| 76 |
$minTimestamp = null; |
| 77 |
$maxTimestamp = null; |
| 78 |
} |
| 79 |
$uniqueKey = $this->params['uniqueKey'] ?? null; |
| 80 |
$keyId = $this->params['keyId'] ?? null; |
| 81 |
|
| 82 |
|
| 83 |
$conds = [ $column => $oldname ]; |
| 84 |
|
| 85 |
if ( $userID !== null ) { |
| 86 |
$conds[$uidColumn] = $userID; |
| 87 |
} |
| 88 |
|
| 89 |
if ( $timestampColumn !== null ) { |
| 90 |
$conds[] = $dbw->expr( $timestampColumn, '>=', $minTimestamp ); |
| 91 |
$conds[] = $dbw->expr( $timestampColumn, '<=', $maxTimestamp ); |
| 92 |
|
| 93 |
} elseif ( $uniqueKey !== null && $keyId !== null ) { |
| 94 |
$conds[$uniqueKey] = $keyId; |
| 95 |
} else { |
| 96 |
throw new InvalidArgumentException( 'Expected ID batch or time range' ); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
if ( $uniqueKey !== null ) { |
| 101 |
|
| 102 |
$ids = $dbw->newSelectQueryBuilder() |
| 103 |
->select( $uniqueKey ) |
| 104 |
->from( $table ) |
| 105 |
->where( $conds ) |
| 106 |
->caller( __METHOD__ )->fetchFieldValues(); |
| 107 |
|
| 108 |
foreach ( array_chunk( $ids, $this->updateRowsPerQuery ) as $batch ) { |
| 109 |
$this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket ); |
| 110 |
|
| 111 |
$dbw->newUpdateQueryBuilder() |
| 112 |
->update( $table ) |
| 113 |
->set( [ $column => $newname ] ) |
| 114 |
->where( [ $column => $oldname, $uniqueKey => $batch ] ) |
| 115 |
->caller( __METHOD__ )->execute(); |
| 116 |
} |
| 117 |
} else { |
| 118 |
|
| 119 |
$dbw->newUpdateQueryBuilder() |
| 120 |
->update( $table ) |
| 121 |
->set( [ $column => $newname ] ) |
| 122 |
->where( $conds ) |
| 123 |
->caller( __METHOD__ )->execute(); |
| 124 |
} |
| 125 |
|
| 126 |
return true; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
class_alias( RenameUserJob::class, 'RenameUserJob' ); |