5use InvalidArgumentException;
38 private $updateRowsPerQuery;
52 $this->lbFactory = $lbFactory;
55 public function run() {
56 $dbw = $this->lbFactory->getPrimaryDatabase();
57 $table = $this->params[
'table'];
58 $column = $this->params[
'column'];
60 $oldname = $this->params[
'oldname'];
61 $newname = $this->params[
'newname'];
62 if ( isset( $this->params[
'userID'] ) ) {
63 $userID = $this->params[
'userID'];
64 $uidColumn = $this->params[
'uidColumn'];
69 if ( isset( $this->params[
'timestampColumn'] ) ) {
70 $timestampColumn = $this->params[
'timestampColumn'];
71 $minTimestamp = $this->params[
'minTimestamp'];
72 $maxTimestamp = $this->params[
'maxTimestamp'];
74 $timestampColumn =
null;
78 $uniqueKey = $this->params[
'uniqueKey'] ??
null;
79 $keyId = $this->params[
'keyId'] ??
null;
81 # Conditions like "*_user_text = 'x'
82 $conds = [ $column => $oldname ];
83 # If user ID given, add that to condition to avoid rename collisions
84 if ( $userID !== null ) {
85 $conds[$uidColumn] = $userID;
87 # Bound by timestamp if given
88 if ( $timestampColumn !== null ) {
89 $conds[] = $dbw->expr( $timestampColumn, '>=', $minTimestamp );
90 $conds[] = $dbw->expr( $timestampColumn, '<=', $maxTimestamp );
91 # Bound by unique key if given (B/C)
92 } elseif ( $uniqueKey !== null && $keyId !== null ) {
93 $conds[$uniqueKey] = $keyId;
95 throw new InvalidArgumentException( 'Expected ID batch or time range' );
98 # Actually update the rows for this job...
99 if ( $uniqueKey !== null ) {
100 // Select the rows to update by PRIMARY KEY
101 $ids = $dbw->newSelectQueryBuilder()
102 ->select( $uniqueKey )
105 ->caller( __METHOD__ )->fetchFieldValues();
106 # Update these rows by PRIMARY KEY to avoid replica lag
107 foreach ( array_chunk( $ids, $this->updateRowsPerQuery ) as $batch ) {
108 $dbw->commit( __METHOD__, 'flush' );
109 $this->lbFactory->waitForReplication();
111 $dbw->newUpdateQueryBuilder()
113 ->set( [ $column => $newname ] )
114 ->where( [ $column => $oldname, $uniqueKey => $batch ] )
115 ->caller( __METHOD__ )->execute();
118 # Update the chunk of rows directly
119 $dbw->newUpdateQueryBuilder()
121 ->set( [ $column => $newname ] )
123 ->caller( __METHOD__ )->execute();
130class_alias( RenameUserJob::class, 'RenameUserJob' );
Describe and execute a background job.
array $params
Array of job parameters.
A class containing constants representing the names of configuration variables.
const UpdateRowsPerQuery
Name constant for the UpdateRowsPerQuery setting, for use with Config::get()