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