130 $this->hookRunner =
new HookRunner( $services->getHookContainer() );
131 $this->dbProvider = $services->getConnectionProvider();
132 $this->userFactory = $services->getUserFactory();
133 $this->jobQueueGroup = $services->getJobQueueGroup();
134 $this->titleFactory = $services->getTitleFactory();
135 $this->logger = LoggerFactory::getInstance(
'Renameuser' );
137 $config = $services->getMainConfig();
143 $this->renamer = $renamer;
144 $this->checkIfUserExists =
true;
146 if ( isset( $options[
'checkIfUserExists'] ) ) {
147 $this->checkIfUserExists = $options[
'checkIfUserExists'];
150 if ( isset( $options[
'debugPrefix'] ) ) {
151 $this->debugPrefix = $options[
'debugPrefix'];
154 if ( isset( $options[
'reason'] ) ) {
155 $this->reason = $options[
'reason'];
159 $this->tablesJob = [];
161 $this->hookRunner->onRenameUserSQL( $this );
176 $dbw = $this->dbProvider->getPrimaryDatabase();
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 );
190 $contribs = $this->userFactory->newFromId( $this->uid )->getEditCount();
194 $this->
debug(
"Starting rename of {$this->old} to {$this->new}" );
195 $dbw->newUpdateQueryBuilder()
197 ->set( [
'user_name' => $this->
new,
'user_touched' => $dbw->timestamp() ] )
198 ->where( [
'user_name' => $this->old,
'user_id' => $this->uid ] )
199 ->caller( __METHOD__ )->execute();
200 $dbw->newUpdateQueryBuilder()
202 ->set( [
'actor_name' => $this->
new ] )
203 ->where( [
'actor_name' => $this->old,
'actor_user' => $this->uid ] )
204 ->caller( __METHOD__ )->execute();
208 $user = $this->userFactory->newFromId( $this->uid );
210 $user->load( IDBAccessObject::READ_LATEST );
211 SessionManager::singleton()->invalidateSessionsForUser( $user );
214 $user->invalidateCache();
217 $dbw->newUpdateQueryBuilder()
218 ->update(
'block_target' )
219 ->set( [
'bt_user_text' => $this->
new ] )
220 ->where( [
'bt_user' => $this->uid,
'bt_user_text' => $this->old ] )
221 ->caller( __METHOD__ )->execute();
226 $oldTitle = $this->titleFactory->makeTitle(
NS_USER, $this->old );
227 $newTitle = $this->titleFactory->makeTitle(
NS_USER, $this->
new );
228 $this->
debug(
"Updating logging table for {$this->old} to {$this->new}" );
233 $dbw->newUpdateQueryBuilder()
234 ->update(
'logging' )
235 ->set( [
'log_title' => $newTitle->getDBkey() ] )
237 'log_type' => $logTypesOnUser,
239 'log_title' => $oldTitle->getDBkey()
241 ->caller( __METHOD__ )->execute();
243 $this->
debug(
"Updating recentchanges table for rename from {$this->old} to {$this->new}" );
244 $dbw->newUpdateQueryBuilder()
245 ->update(
'recentchanges' )
246 ->set( [
'rc_title' => $newTitle->getDBkey() ] )
249 'rc_log_type' => $logTypesOnUser,
251 'rc_title' => $oldTitle->getDBkey()
253 ->caller( __METHOD__ )->execute();
256 foreach ( $this->tables as $table => $fieldSet ) {
257 [ $nameCol, $userCol ] = $fieldSet;
258 $dbw->newUpdateQueryBuilder()
260 ->set( [ $nameCol => $this->
new ] )
261 ->where( [ $nameCol => $this->old, $userCol => $this->uid ] )
262 ->caller( __METHOD__ )->execute();
274 foreach ( $this->tablesJob as $table =>
$params ) {
279 $res = $dbw->newSelectQueryBuilder()
280 ->select( [ $timestampC ] )
282 ->where( [ $userTextC => $this->old, $userIDC => $this->uid ] )
283 ->orderBy( $timestampC, SelectQueryBuilder::SORT_ASC )
284 ->caller( __METHOD__ )->fetchResultSet();
287 $jobParams[
'table'] = $table;
288 $jobParams[
'column'] = $userTextC;
289 $jobParams[
'uidColumn'] = $userIDC;
290 $jobParams[
'timestampColumn'] = $timestampC;
295 $jobParams[
'minTimestamp'] =
'0';
296 $jobParams[
'maxTimestamp'] =
'0';
297 $jobParams[
'count'] = 0;
299 if ( isset(
$params[
'uniqueKey'] ) ) {
300 $jobParams[
'uniqueKey'] =
$params[
'uniqueKey'];
304 foreach ( $res as $row ) {
306 if ( $jobParams[
'count'] === 0 ) {
307 $jobParams[
'minTimestamp'] = $row->$timestampC;
311 $jobParams[
'maxTimestamp'] = $row->$timestampC;
313 $jobParams[
'count']++;
315 if ( $jobParams[
'count'] >= $this->updateRowsPerJob ) {
317 $jobParams[
'minTimestamp'] =
'0';
318 $jobParams[
'maxTimestamp'] =
'0';
319 $jobParams[
'count'] = 0;
323 if ( $jobParams[
'count'] > 0 ) {
330 $logEntry->setPerformer( $this->renamer );
331 $logEntry->setTarget( $oldTitle );
332 $logEntry->setComment( $this->reason );
333 $logEntry->setParameters( [
334 '4::olduser' => $this->old,
335 '5::newuser' => $this->
new,
336 '6::edits' => $contribs
338 $logid = $logEntry->insert();
343 $count = count( $jobs );
345 $this->jobQueueGroup->push( $jobs );
346 $this->
debug(
"Queued $count jobs for rename from {$this->old} to {$this->new}" );
350 $dbw->endAtomic( __METHOD__ );
353 $dbw->onTransactionCommitOrIdle(
354 function () use ( $dbw, $logEntry, $logid, $fname ) {
355 $dbw->startAtomic( $fname );
357 $user = $this->userFactory->newFromId( $this->uid );
358 $user->load( IDBAccessObject::READ_LATEST );
360 $user->saveSettings();
361 $this->hookRunner->onRenameUserComplete( $this->uid, $this->old, $this->
new );
363 $logEntry->publish( $logid );
364 $dbw->endAtomic( $fname );
369 $this->
debug(
"Finished rename from {$this->old} to {$this->new}" );