134 $this->hookRunner =
new HookRunner( $services->getHookContainer() );
135 $this->dbProvider = $services->getConnectionProvider();
136 $this->userFactory = $services->getUserFactory();
137 $this->jobQueueGroup = $services->getJobQueueGroup();
138 $this->titleFactory = $services->getTitleFactory();
139 $this->logger = LoggerFactory::getInstance(
'Renameuser' );
141 $config = $services->getMainConfig();
149 $this->renamer = $renamer;
150 $this->checkIfUserExists =
true;
152 if ( isset( $options[
'checkIfUserExists'] ) ) {
153 $this->checkIfUserExists = $options[
'checkIfUserExists'];
156 if ( isset( $options[
'debugPrefix'] ) ) {
157 $this->debugPrefix = $options[
'debugPrefix'];
160 if ( isset( $options[
'reason'] ) ) {
161 $this->reason = $options[
'reason'];
165 $this->tablesJob = [];
167 $this->hookRunner->onRenameUserSQL( $this );
182 $dbw = $this->dbProvider->getPrimaryDatabase();
183 $atomicId = $dbw->startAtomic( __METHOD__, $dbw::ATOMIC_CANCELABLE );
185 $this->hookRunner->onRenameUserPreRename( $this->uid, $this->old, $this->
new );
188 if ( $this->checkIfUserExists && !$this->lockUserAndGetId( $this->old ) ) {
189 $this->
debug(
"User {$this->old} does not exist, bailing out" );
190 $dbw->cancelAtomic( __METHOD__, $atomicId );
196 $contribs = $this->userFactory->newFromId( $this->uid )->getEditCount();
200 $this->
debug(
"Starting rename of {$this->old} to {$this->new}" );
201 $dbw->newUpdateQueryBuilder()
203 ->set( [
'user_name' => $this->
new,
'user_touched' => $dbw->timestamp() ] )
204 ->where( [
'user_name' => $this->old,
'user_id' => $this->uid ] )
205 ->caller( __METHOD__ )->execute();
206 $dbw->newUpdateQueryBuilder()
208 ->set( [
'actor_name' => $this->
new ] )
209 ->where( [
'actor_name' => $this->old,
'actor_user' => $this->uid ] )
210 ->caller( __METHOD__ )->execute();
214 $user = $this->userFactory->newFromId( $this->uid );
216 $user->load( IDBAccessObject::READ_LATEST );
217 SessionManager::singleton()->invalidateSessionsForUser( $user );
220 $user->invalidateCache();
224 $dbw->newUpdateQueryBuilder()
225 ->update(
'ipblocks' )
226 ->set( [
'ipb_address' => $this->
new ] )
227 ->where( [
'ipb_user' => $this->uid,
'ipb_address' => $this->old ] )
228 ->caller( __METHOD__ )->execute();
231 $dbw->newUpdateQueryBuilder()
232 ->update(
'block_target' )
233 ->set( [
'bt_user_text' => $this->
new ] )
234 ->where( [
'bt_user' => $this->uid,
'bt_user_text' => $this->old ] )
235 ->caller( __METHOD__ )->execute();
241 $oldTitle = $this->titleFactory->makeTitle(
NS_USER, $this->old );
242 $newTitle = $this->titleFactory->makeTitle(
NS_USER, $this->
new );
243 $this->
debug(
"Updating logging table for {$this->old} to {$this->new}" );
248 $dbw->newUpdateQueryBuilder()
249 ->update(
'logging' )
250 ->set( [
'log_title' => $newTitle->getDBkey() ] )
252 'log_type' => $logTypesOnUser,
254 'log_title' => $oldTitle->getDBkey()
256 ->caller( __METHOD__ )->execute();
258 $this->
debug(
"Updating recentchanges table for rename from {$this->old} to {$this->new}" );
259 $dbw->newUpdateQueryBuilder()
260 ->update(
'recentchanges' )
261 ->set( [
'rc_title' => $newTitle->getDBkey() ] )
264 'rc_log_type' => $logTypesOnUser,
266 'rc_title' => $oldTitle->getDBkey()
268 ->caller( __METHOD__ )->execute();
271 foreach ( $this->tables as $table => $fieldSet ) {
272 [ $nameCol, $userCol ] = $fieldSet;
273 $dbw->newUpdateQueryBuilder()
275 ->set( [ $nameCol => $this->
new ] )
276 ->where( [ $nameCol => $this->old, $userCol => $this->uid ] )
277 ->caller( __METHOD__ )->execute();
289 foreach ( $this->tablesJob as $table =>
$params ) {
294 $res = $dbw->newSelectQueryBuilder()
295 ->select( [ $timestampC ] )
297 ->where( [ $userTextC => $this->old, $userIDC => $this->uid ] )
298 ->orderBy( $timestampC, SelectQueryBuilder::SORT_ASC )
299 ->caller( __METHOD__ )->fetchResultSet();
302 $jobParams[
'table'] = $table;
303 $jobParams[
'column'] = $userTextC;
304 $jobParams[
'uidColumn'] = $userIDC;
305 $jobParams[
'timestampColumn'] = $timestampC;
310 $jobParams[
'minTimestamp'] =
'0';
311 $jobParams[
'maxTimestamp'] =
'0';
312 $jobParams[
'count'] = 0;
314 if ( isset(
$params[
'uniqueKey'] ) ) {
315 $jobParams[
'uniqueKey'] =
$params[
'uniqueKey'];
319 foreach ( $res as $row ) {
321 if ( $jobParams[
'count'] === 0 ) {
322 $jobParams[
'minTimestamp'] = $row->$timestampC;
326 $jobParams[
'maxTimestamp'] = $row->$timestampC;
328 $jobParams[
'count']++;
330 if ( $jobParams[
'count'] >= $this->updateRowsPerJob ) {
332 $jobParams[
'minTimestamp'] =
'0';
333 $jobParams[
'maxTimestamp'] =
'0';
334 $jobParams[
'count'] = 0;
338 if ( $jobParams[
'count'] > 0 ) {
345 $logEntry->setPerformer( $this->renamer );
346 $logEntry->setTarget( $oldTitle );
347 $logEntry->setComment( $this->reason );
348 $logEntry->setParameters( [
349 '4::olduser' => $this->old,
350 '5::newuser' => $this->
new,
351 '6::edits' => $contribs
353 $logid = $logEntry->insert();
358 $count = count( $jobs );
360 $this->jobQueueGroup->push( $jobs );
361 $this->
debug(
"Queued $count jobs for rename from {$this->old} to {$this->new}" );
365 $dbw->endAtomic( __METHOD__ );
368 $dbw->onTransactionCommitOrIdle(
369 function () use ( $dbw, $logEntry, $logid, $fname ) {
370 $dbw->startAtomic( $fname );
372 $user = $this->userFactory->newFromId( $this->uid );
373 $user->load( IDBAccessObject::READ_LATEST );
375 $user->saveSettings();
376 $this->hookRunner->onRenameUserComplete( $this->uid, $this->old, $this->
new );
378 $logEntry->publish( $logid );
379 $dbw->endAtomic( $fname );
384 $this->
debug(
"Finished rename from {$this->old} to {$this->new}" );