MediaWiki 1.41.2
RenameUserJob.php
Go to the documentation of this file.
1<?php
2
7use Psr\Log\LoggerInterface;
9
34class RenameUserJob extends Job {
36 private $updateRowsPerQuery;
37
39 private $lbFactory;
40
42 private $logger;
43
44 public function __construct(
45 Title $title,
46 $params,
47 Config $config,
48 ILBFactory $lbFactory
49 ) {
50 parent::__construct( 'renameUser', $title, $params );
51
52 $this->updateRowsPerQuery = $config->get( MainConfigNames::UpdateRowsPerQuery );
53 $this->lbFactory = $lbFactory;
54 $this->logger = LoggerFactory::getInstance( 'Renameuser' );
55 }
56
57 public function run() {
58 $dbw = $this->lbFactory->getMainLB()->getMaintenanceConnectionRef( DB_PRIMARY );
59 $table = $this->params['table'];
60 $column = $this->params['column'];
61
62 // It's not worth a hook to let extensions add themselves to that list.
63 // Just check whether the table and column still exist instead.
64 if ( !$dbw->tableExists( $table, __METHOD__ ) ) {
65 $this->logger->info(
66 "Ignoring job {$this->toString()}, table $table does not exist" );
67 return true;
68 } elseif ( !$dbw->fieldExists( $table, $column, __METHOD__ ) ) {
69 $this->logger->info(
70 "Ignoring job {$this->toString()}, column $table.$column does not exist" );
71 return true;
72 }
73
74 $oldname = $this->params['oldname'];
75 $newname = $this->params['newname'];
76 if ( isset( $this->params['userID'] ) ) {
77 $userID = $this->params['userID'];
78 $uidColumn = $this->params['uidColumn'];
79 } else {
80 $userID = null;
81 $uidColumn = null;
82 }
83 if ( isset( $this->params['timestampColumn'] ) ) {
84 $timestampColumn = $this->params['timestampColumn'];
85 $minTimestamp = $this->params['minTimestamp'];
86 $maxTimestamp = $this->params['maxTimestamp'];
87 } else {
88 $timestampColumn = null;
89 $minTimestamp = null;
90 $maxTimestamp = null;
91 }
92 $uniqueKey = $this->params['uniqueKey'] ?? null;
93 $keyId = $this->params['keyId'] ?? null;
94
95 # Conditions like "*_user_text = 'x'
96 $conds = [ $column => $oldname ];
97 # If user ID given, add that to condition to avoid rename collisions
98 if ( $userID !== null ) {
99 $conds[$uidColumn] = $userID;
100 }
101 # Bound by timestamp if given
102 if ( $timestampColumn !== null ) {
103 $conds[] = "$timestampColumn >= " . $dbw->addQuotes( $minTimestamp );
104 $conds[] = "$timestampColumn <= " . $dbw->addQuotes( $maxTimestamp );
105 # Bound by unique key if given (B/C)
106 } elseif ( $uniqueKey !== null && $keyId !== null ) {
107 $conds[$uniqueKey] = $keyId;
108 } else {
109 throw new InvalidArgumentException( 'Expected ID batch or time range' );
110 }
111
112 # Actually update the rows for this job...
113 if ( $uniqueKey !== null ) {
114 # Select the rows to update by PRIMARY KEY
115 $ids = $dbw->selectFieldValues( $table, $uniqueKey, $conds, __METHOD__ );
116 # Update these rows by PRIMARY KEY to avoid replica lag
117 foreach ( array_chunk( $ids, $this->updateRowsPerQuery ) as $batch ) {
118 $dbw->commit( __METHOD__, 'flush' );
119 $this->lbFactory->waitForReplication();
120
121 $dbw->newUpdateQueryBuilder()
122 ->update( $table )
123 ->set( [ $column => $newname ] )
124 ->where( [ $column => $oldname, $uniqueKey => $batch ] )
125 ->caller( __METHOD__ )->execute();
126 }
127 } else {
128 # Update the chunk of rows directly
129 $dbw->newUpdateQueryBuilder()
130 ->update( $table )
131 ->set( [ $column => $newname ] )
132 ->where( $conds )
133 ->caller( __METHOD__ )->execute();
134 }
135
136 return true;
137 }
138}
Class to both describe a background job and handle jobs.
Definition Job.php:40
array $params
Array of job parameters.
Definition Job.php:45
Create PSR-3 logger objects.
A class containing constants representing the names of configuration variables.
Represents a title within MediaWiki.
Definition Title.php:76
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.
const DB_PRIMARY
Definition defines.php:28