129 $this->hookRunner =
new HookRunner( $services->getHookContainer() );
130 $this->loadBalancer = $services->getDBLoadBalancer();
131 $this->userFactory = $services->getUserFactory();
132 $this->jobQueueGroup = $services->getJobQueueGroup();
133 $this->titleFactory = $services->getTitleFactory();
135 $this->logger = LoggerFactory::getInstance(
'Renameuser' );
140 $this->renamer = $renamer;
141 $this->checkIfUserExists =
true;
143 if ( isset( $options[
'checkIfUserExists'] ) ) {
144 $this->checkIfUserExists = $options[
'checkIfUserExists'];
147 if ( isset( $options[
'debugPrefix'] ) ) {
148 $this->debugPrefix = $options[
'debugPrefix'];
151 if ( isset( $options[
'reason'] ) ) {
152 $this->reason = $options[
'reason'];
156 $this->tablesJob = [];
158 $this->hookRunner->onRenameUserSQL( $this );
174 $contribs = $this->userFactory->newFromId( $this->uid )->getEditCount();
176 $dbw = $this->loadBalancer->getConnection(
DB_PRIMARY );
177 $atomicId = $dbw->startAtomic( __METHOD__, $dbw::ATOMIC_CANCELABLE );
179 $this->hookRunner->onRenameUserPreRename( $this->uid, $this->old, $this->
new );
182 if ( $this->checkIfUserExists && !$this->lockUserAndGetId( $this->old ) ) {
183 $this->
debug(
"User {$this->old} does not exist, bailing out" );
184 $dbw->cancelAtomic( __METHOD__, $atomicId );
191 $this->
debug(
"Starting rename of {$this->old} to {$this->new}" );
192 $dbw->update(
'user',
193 [
'user_name' => $this->
new,
'user_touched' => $dbw->timestamp() ],
194 [
'user_name' => $this->old,
'user_id' => $this->uid ],
197 $dbw->update(
'actor',
198 [
'actor_name' => $this->
new ],
199 [
'actor_name' => $this->old,
'actor_user' => $this->uid ],
205 $user = $this->userFactory->newFromId( $this->uid );
207 $user->load( User::READ_LATEST );
208 SessionManager::singleton()->invalidateSessionsForUser( $user );
211 $user->invalidateCache();
214 $dbw->update(
'ipblocks',
215 [
'ipb_address' => $this->
new ],
216 [
'ipb_user' => $this->uid,
'ipb_address' => $this->old ],
222 $oldTitle = $this->titleFactory->makeTitle(
NS_USER, $this->old );
223 $newTitle = $this->titleFactory->makeTitle(
NS_USER, $this->
new );
224 $this->
debug(
"Updating logging table for {$this->old} to {$this->new}" );
229 $dbw->update(
'logging',
230 [
'log_title' => $newTitle->getDBkey() ],
232 'log_type' => $logTypesOnUser,
234 'log_title' => $oldTitle->getDBkey()
239 $this->
debug(
"Updating recentchanges table for {$this->old} to {$this->new}" );
240 $dbw->update(
'recentchanges',
241 [
'rc_title' => $newTitle->getDBkey() ],
244 'rc_log_type' => $logTypesOnUser,
246 'rc_title' => $oldTitle->getDBkey()
252 foreach ( $this->tables as $table => $fieldSet ) {
253 list( $nameCol, $userCol ) = $fieldSet;
254 $dbw->update( $table,
255 [ $nameCol => $this->
new ],
256 [ $nameCol => $this->old, $userCol => $this->uid ],
270 foreach ( $this->tablesJob as $table => $params ) {
275 $res = $dbw->select( $table,
277 [ $userTextC => $this->old, $userIDC => $this->uid ],
279 [
'ORDER BY' =>
"$timestampC ASC" ]
283 $jobParams[
'table'] = $table;
284 $jobParams[
'column'] = $userTextC;
285 $jobParams[
'uidColumn'] = $userIDC;
286 $jobParams[
'timestampColumn'] = $timestampC;
291 $jobParams[
'minTimestamp'] =
'0';
292 $jobParams[
'maxTimestamp'] =
'0';
293 $jobParams[
'count'] = 0;
295 if ( isset( $params[
'uniqueKey'] ) ) {
296 $jobParams[
'uniqueKey'] = $params[
'uniqueKey'];
300 foreach (
$res as $row ) {
301 # Since the ORDER BY is ASC, set the min timestamp with first row
302 if ( $jobParams[
'count'] === 0 ) {
303 $jobParams[
'minTimestamp'] = $row->$timestampC;
305 # Keep updating the last timestamp, so it should be correct
306 # when the last item is added.
307 $jobParams[
'maxTimestamp'] = $row->$timestampC;
309 $jobParams[
'count']++;
310 # Once a job has $wgUpdateRowsPerJob rows, add it to the queue
311 if ( $jobParams[
'count'] >= $this->updateRowsPerJob ) {
313 $jobParams[
'minTimestamp'] =
'0';
314 $jobParams[
'maxTimestamp'] =
'0';
315 $jobParams[
'count'] = 0;
318 # If there are any job rows left, add it to the queue as one job
319 if ( $jobParams[
'count'] > 0 ) {
326 $logEntry->setPerformer( $this->renamer );
327 $logEntry->setTarget( $oldTitle );
328 $logEntry->setComment( $this->reason );
329 $logEntry->setParameters( [
330 '4::olduser' => $this->old,
331 '5::newuser' => $this->
new,
332 '6::edits' => $contribs
334 $logid = $logEntry->insert();
339 $count = count( $jobs );
341 $this->jobQueueGroup->push( $jobs );
342 $this->
debug(
"Queued $count jobs for {$this->old} to {$this->new}" );
346 $dbw->endAtomic( __METHOD__ );
349 $dbw->onTransactionCommitOrIdle(
350 function () use ( $dbw, $logEntry, $logid, $fname ) {
351 $dbw->startAtomic( $fname );
353 $user = $this->userFactory->newFromId( $this->uid );
354 $user->load( User::READ_LATEST );
356 $user->saveSettings();
357 $this->hookRunner->onRenameUserComplete( $this->uid, $this->old, $this->
new );
359 $logEntry->publish( $logid );
360 $dbw->endAtomic( $fname );
365 $this->
debug(
"Finished rename for {$this->old} to {$this->new}" );