MediaWiki master
RenameUserTableJob.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
11
36class RenameUserTableJob extends Job {
38 private $updateRowsPerQuery;
39
41 private $lbFactory;
42
43 public function __construct(
45 array $params,
46 Config $config,
47 ILBFactory $lbFactory
48 ) {
49 parent::__construct( 'renameUserTable', $title, $params );
50
51 $this->updateRowsPerQuery = $config->get( MainConfigNames::UpdateRowsPerQuery );
52 $this->lbFactory = $lbFactory;
53 }
54
56 public function run() {
57 $dbw = $this->lbFactory->getPrimaryDatabase();
58 $ticket = $this->lbFactory->getEmptyTransactionTicket( __METHOD__ );
59 $table = $this->params['table'];
60 $column = $this->params['column'];
61
62 $oldname = $this->params['oldname'];
63 $newname = $this->params['newname'];
64 if ( isset( $this->params['userID'] ) ) {
65 $userID = $this->params['userID'];
66 $uidColumn = $this->params['uidColumn'];
67 } else {
68 $userID = null;
69 $uidColumn = null;
70 }
71 if ( isset( $this->params['timestampColumn'] ) ) {
72 $timestampColumn = $this->params['timestampColumn'];
73 $minTimestamp = $this->params['minTimestamp'];
74 $maxTimestamp = $this->params['maxTimestamp'];
75 } else {
76 $timestampColumn = null;
77 $minTimestamp = null;
78 $maxTimestamp = null;
79 }
80 $uniqueKey = $this->params['uniqueKey'] ?? null;
81 $keyId = $this->params['keyId'] ?? null;
82
83 # Conditions like "*_user_text = 'x'
84 $conds = [ $column => $oldname ];
85 # If user ID given, add that to condition to avoid rename collisions
86 if ( $userID !== null ) {
87 $conds[$uidColumn] = $userID;
88 }
89 # Bound by timestamp if given
90 if ( $timestampColumn !== null ) {
91 $conds[] = $dbw->expr( $timestampColumn, '>=', $minTimestamp );
92 $conds[] = $dbw->expr( $timestampColumn, '<=', $maxTimestamp );
93 # Bound by unique key if given (B/C)
94 } elseif ( $uniqueKey !== null && $keyId !== null ) {
95 $conds[$uniqueKey] = $keyId;
96 } else {
97 throw new InvalidArgumentException( 'Expected ID batch or time range' );
98 }
99
100 # Actually update the rows for this job...
101 if ( $uniqueKey !== null ) {
102 // Select the rows to update by PRIMARY KEY
103 $ids = $dbw->newSelectQueryBuilder()
104 ->select( $uniqueKey )
105 ->from( $table )
106 ->where( $conds )
107 ->caller( __METHOD__ )->fetchFieldValues();
108 # Update these rows by PRIMARY KEY to avoid replica lag
109 foreach ( array_chunk( $ids, $this->updateRowsPerQuery ) as $batch ) {
110 $this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
111
112 $dbw->newUpdateQueryBuilder()
113 ->update( $table )
114 ->set( [ $column => $newname ] )
115 ->where( [ $column => $oldname, $uniqueKey => $batch ] )
116 ->caller( __METHOD__ )->execute();
117 }
118 } else {
119 # Update the chunk of rows directly
120 $dbw->newUpdateQueryBuilder()
121 ->update( $table )
122 ->set( [ $column => $newname ] )
123 ->where( $conds )
124 ->caller( __METHOD__ )->execute();
125 }
126
127 return true;
128 }
129}
131class_alias( RenameUserTableJob::class, 'RenameUserJob' );
Describe and execute a background job.
Definition Job.php:28
array $params
Array of job parameters.
Definition Job.php:33
A class containing constants representing the names of configuration variables.
const UpdateRowsPerQuery
Name constant for the UpdateRowsPerQuery setting, for use with Config::get()
Custom job to perform updates on tables in busier environments.
run()
Run the job.If this method returns false or completes exceptionally, the job runner will retry execut...
__construct(Title $title, array $params, Config $config, ILBFactory $lbFactory)
Represents a title within MediaWiki.
Definition Title.php:69
Interface for configuration instances.
Definition Config.php:18
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
Manager of ILoadBalancer objects and, indirectly, IDatabase connections.