| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\RenameUser; |
| 4 |
|
| 5 |
use JobQueueGroup; |
| 6 |
use JobSpecification; |
| 7 |
use ManualLogEntry; |
| 8 |
use MediaWiki\HookContainer\HookRunner; |
| 9 |
use MediaWiki\Logger\LoggerFactory; |
| 10 |
use MediaWiki\MainConfigNames; |
| 11 |
use MediaWiki\MediaWikiServices; |
| 12 |
use MediaWiki\Session\SessionManager; |
| 13 |
use MediaWiki\Specials\SpecialLog; |
| 14 |
use MediaWiki\Title\TitleFactory; |
| 15 |
use MediaWiki\User\User; |
| 16 |
use MediaWiki\User\UserFactory; |
| 17 |
use Psr\Log\LoggerInterface; |
| 18 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 19 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 20 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class RenameuserSQL { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public $old; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public $new; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public $uid; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public $tables; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public $tablesJob; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public $checkIfUserExists; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private $renamer; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private $reason = ''; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
private $debugPrefix = ''; |
| 89 |
|
| 90 |
|
| 91 |
public const NAME_COL = 0; |
| 92 |
public const UID_COL = 1; |
| 93 |
public const TIME_COL = 2; |
| 94 |
|
| 95 |
|
| 96 |
private $hookRunner; |
| 97 |
|
| 98 |
|
| 99 |
private $dbProvider; |
| 100 |
|
| 101 |
|
| 102 |
private $userFactory; |
| 103 |
|
| 104 |
|
| 105 |
private $jobQueueGroup; |
| 106 |
|
| 107 |
|
| 108 |
private $titleFactory; |
| 109 |
|
| 110 |
|
| 111 |
private $logger; |
| 112 |
|
| 113 |
|
| 114 |
private $updateRowsPerJob; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function __construct( $old, $new, $uid, User $renamer, $options = [] ) { |
| 129 |
$services = MediaWikiServices::getInstance(); |
| 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' ); |
| 136 |
|
| 137 |
$config = $services->getMainConfig(); |
| 138 |
$this->updateRowsPerJob = $config->get( MainConfigNames::UpdateRowsPerJob ); |
| 139 |
|
| 140 |
$this->old = $old; |
| 141 |
$this->new = $new; |
| 142 |
$this->uid = $uid; |
| 143 |
$this->renamer = $renamer; |
| 144 |
$this->checkIfUserExists = true; |
| 145 |
|
| 146 |
if ( isset( $options['checkIfUserExists'] ) ) { |
| 147 |
$this->checkIfUserExists = $options['checkIfUserExists']; |
| 148 |
} |
| 149 |
|
| 150 |
if ( isset( $options['debugPrefix'] ) ) { |
| 151 |
$this->debugPrefix = $options['debugPrefix']; |
| 152 |
} |
| 153 |
|
| 154 |
if ( isset( $options['reason'] ) ) { |
| 155 |
$this->reason = $options['reason']; |
| 156 |
} |
| 157 |
|
| 158 |
$this->tables = []; |
| 159 |
$this->tablesJob = []; |
| 160 |
|
| 161 |
$this->hookRunner->onRenameUserSQL( $this ); |
| 162 |
} |
| 163 |
|
| 164 |
protected function debug( $msg ) { |
| 165 |
if ( $this->debugPrefix ) { |
| 166 |
$msg = "{$this->debugPrefix}: $msg"; |
| 167 |
} |
| 168 |
$this->logger->debug( $msg ); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function rename() { |
| 176 |
$dbw = $this->dbProvider->getPrimaryDatabase(); |
| 177 |
$atomicId = $dbw->startAtomic( __METHOD__, $dbw::ATOMIC_CANCELABLE ); |
| 178 |
|
| 179 |
$this->hookRunner->onRenameUserPreRename( $this->uid, $this->old, $this->new ); |
| 180 |
|
| 181 |
|
| 182 |
if ( $this->checkIfUserExists && !$this->lockUserAndGetId( $this->old ) ) { |
| 183 |
$this->debug( "User {$this->old} does not exist, bailing out" ); |
| 184 |
$dbw->cancelAtomic( __METHOD__, $atomicId ); |
| 185 |
|
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
$contribs = $this->userFactory->newFromId( $this->uid )->getEditCount(); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
$this->debug( "Starting rename of {$this->old} to {$this->new}" ); |
| 195 |
$dbw->newUpdateQueryBuilder() |
| 196 |
->update( 'user' ) |
| 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() |
| 201 |
->update( 'actor' ) |
| 202 |
->set( [ 'actor_name' => $this->new ] ) |
| 203 |
->where( [ 'actor_name' => $this->old, 'actor_user' => $this->uid ] ) |
| 204 |
->caller( __METHOD__ )->execute(); |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
$user = $this->userFactory->newFromId( $this->uid ); |
| 209 |
|
| 210 |
$user->load( IDBAccessObject::READ_LATEST ); |
| 211 |
SessionManager::singleton()->invalidateSessionsForUser( $user ); |
| 212 |
|
| 213 |
|
| 214 |
$user->invalidateCache(); |
| 215 |
|
| 216 |
|
| 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(); |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 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}" ); |
| 229 |
|
| 230 |
|
| 231 |
$logTypesOnUser = array_diff( SpecialLog::getLogTypesOnUser(), [ 'renameuser' ] ); |
| 232 |
|
| 233 |
$dbw->newUpdateQueryBuilder() |
| 234 |
->update( 'logging' ) |
| 235 |
->set( [ 'log_title' => $newTitle->getDBkey() ] ) |
| 236 |
->where( [ |
| 237 |
'log_type' => $logTypesOnUser, |
| 238 |
'log_namespace' => NS_USER, |
| 239 |
'log_title' => $oldTitle->getDBkey() |
| 240 |
] ) |
| 241 |
->caller( __METHOD__ )->execute(); |
| 242 |
|
| 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() ] ) |
| 247 |
->where( [ |
| 248 |
'rc_type' => RC_LOG, |
| 249 |
'rc_log_type' => $logTypesOnUser, |
| 250 |
'rc_namespace' => NS_USER, |
| 251 |
'rc_title' => $oldTitle->getDBkey() |
| 252 |
] ) |
| 253 |
->caller( __METHOD__ )->execute(); |
| 254 |
|
| 255 |
|
| 256 |
foreach ( $this->tables as $table => $fieldSet ) { |
| 257 |
[ $nameCol, $userCol ] = $fieldSet; |
| 258 |
$dbw->newUpdateQueryBuilder() |
| 259 |
->update( $table ) |
| 260 |
->set( [ $nameCol => $this->new ] ) |
| 261 |
->where( [ $nameCol => $this->old, $userCol => $this->uid ] ) |
| 262 |
->caller( __METHOD__ )->execute(); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
$jobs = []; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
foreach ( $this->tablesJob as $table => $params ) { |
| 275 |
$userTextC = $params[self::NAME_COL]; |
| 276 |
$userIDC = $params[self::UID_COL]; |
| 277 |
$timestampC = $params[self::TIME_COL]; |
| 278 |
|
| 279 |
$res = $dbw->newSelectQueryBuilder() |
| 280 |
->select( [ $timestampC ] ) |
| 281 |
->from( $table ) |
| 282 |
->where( [ $userTextC => $this->old, $userIDC => $this->uid ] ) |
| 283 |
->orderBy( $timestampC, SelectQueryBuilder::SORT_ASC ) |
| 284 |
->caller( __METHOD__ )->fetchResultSet(); |
| 285 |
|
| 286 |
$jobParams = []; |
| 287 |
$jobParams['table'] = $table; |
| 288 |
$jobParams['column'] = $userTextC; |
| 289 |
$jobParams['uidColumn'] = $userIDC; |
| 290 |
$jobParams['timestampColumn'] = $timestampC; |
| 291 |
$jobParams['oldname'] = $this->old; |
| 292 |
$jobParams['newname'] = $this->new; |
| 293 |
$jobParams['userID'] = $this->uid; |
| 294 |
|
| 295 |
$jobParams['minTimestamp'] = '0'; |
| 296 |
$jobParams['maxTimestamp'] = '0'; |
| 297 |
$jobParams['count'] = 0; |
| 298 |
|
| 299 |
if ( isset( $params['uniqueKey'] ) ) { |
| 300 |
$jobParams['uniqueKey'] = $params['uniqueKey']; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
foreach ( $res as $row ) { |
| 305 |
|
| 306 |
if ( $jobParams['count'] === 0 ) { |
| 307 |
$jobParams['minTimestamp'] = $row->$timestampC; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
$jobParams['maxTimestamp'] = $row->$timestampC; |
| 312 |
|
| 313 |
$jobParams['count']++; |
| 314 |
|
| 315 |
if ( $jobParams['count'] >= $this->updateRowsPerJob ) { |
| 316 |
$jobs[] = new JobSpecification( 'renameUser', $jobParams, [], $oldTitle ); |
| 317 |
$jobParams['minTimestamp'] = '0'; |
| 318 |
$jobParams['maxTimestamp'] = '0'; |
| 319 |
$jobParams['count'] = 0; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
if ( $jobParams['count'] > 0 ) { |
| 324 |
$jobs[] = new JobSpecification( 'renameUser', $jobParams, [], $oldTitle ); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
$logEntry = new ManualLogEntry( 'renameuser', 'renameuser' ); |
| 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 |
| 337 |
] ); |
| 338 |
$logid = $logEntry->insert(); |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
$count = count( $jobs ); |
| 344 |
if ( $count > 0 ) { |
| 345 |
$this->jobQueueGroup->push( $jobs ); |
| 346 |
$this->debug( "Queued $count jobs for rename from {$this->old} to {$this->new}" ); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
$dbw->endAtomic( __METHOD__ ); |
| 351 |
|
| 352 |
$fname = __METHOD__; |
| 353 |
$dbw->onTransactionCommitOrIdle( |
| 354 |
function () use ( $dbw, $logEntry, $logid, $fname ) { |
| 355 |
$dbw->startAtomic( $fname ); |
| 356 |
|
| 357 |
$user = $this->userFactory->newFromId( $this->uid ); |
| 358 |
$user->load( IDBAccessObject::READ_LATEST ); |
| 359 |
|
| 360 |
$user->saveSettings(); |
| 361 |
$this->hookRunner->onRenameUserComplete( $this->uid, $this->old, $this->new ); |
| 362 |
|
| 363 |
$logEntry->publish( $logid ); |
| 364 |
$dbw->endAtomic( $fname ); |
| 365 |
}, |
| 366 |
$fname |
| 367 |
); |
| 368 |
|
| 369 |
$this->debug( "Finished rename from {$this->old} to {$this->new}" ); |
| 370 |
|
| 371 |
return true; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
private function lockUserAndGetId( $name ) { |
| 379 |
return (int)$this->dbProvider->getPrimaryDatabase()->newSelectQueryBuilder() |
| 380 |
->select( 'user_id' ) |
| 381 |
->forUpdate() |
| 382 |
->from( 'user' ) |
| 383 |
->where( [ 'user_name' => $name ] ) |
| 384 |
->caller( __METHOD__ )->fetchField(); |
| 385 |
} |
| 386 |
} |