| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
use MediaWiki\Maintenance\Maintenance; |
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use MediaWiki\User\User; |
| 28 |
use MediaWiki\WikiMap\WikiMap; |
| 29 |
use Wikimedia\Rdbms\IDatabase; |
| 30 |
use Wikimedia\Rdbms\IExpression; |
| 31 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 32 |
use Wikimedia\Rdbms\LikeValue; |
| 33 |
|
| 34 |
|
| 35 |
require_once __DIR__ . '/Maintenance.php'; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class UppercaseTitlesForUnicodeTransition extends Maintenance { |
| 45 |
|
| 46 |
private const MOVE = 0; |
| 47 |
private const INPLACE_MOVE = 1; |
| 48 |
private const UPPERCASE = 2; |
| 49 |
|
| 50 |
|
| 51 |
private $run = false; |
| 52 |
|
| 53 |
|
| 54 |
private $charmap = []; |
| 55 |
|
| 56 |
|
| 57 |
private $user; |
| 58 |
|
| 59 |
|
| 60 |
private $reason = 'Uppercasing title for Unicode upgrade'; |
| 61 |
|
| 62 |
|
| 63 |
private $tags = []; |
| 64 |
|
| 65 |
|
| 66 |
private $seenUsers = []; |
| 67 |
|
| 68 |
|
| 69 |
private $namespaces = null; |
| 70 |
|
| 71 |
private ?string $prefix = null; |
| 72 |
private ?string $suffix = null; |
| 73 |
|
| 74 |
|
| 75 |
private $prefixNs = null; |
| 76 |
|
| 77 |
|
| 78 |
private $tables = null; |
| 79 |
|
| 80 |
public function __construct() { |
| 81 |
parent::__construct(); |
| 82 |
$this->addDescription( |
| 83 |
"Rename titles when changing behavior of Language::ucfirst().\n" |
| 84 |
. "\n" |
| 85 |
. "This script skips User and User_talk pages for registered users, as renaming of users " |
| 86 |
. "is too complex to try to implement here. Use something like Extension:Renameuser to " |
| 87 |
. "clean those up; this script can provide a list of user names affected." |
| 88 |
); |
| 89 |
$this->addOption( |
| 90 |
'charmap', 'Character map generated by maintenance/language/generateUcfirstOverrides.php', |
| 91 |
true, true |
| 92 |
); |
| 93 |
$this->addOption( |
| 94 |
'user', 'System user to use to do the renames. Default is "Maintenance script".', false, true |
| 95 |
); |
| 96 |
$this->addOption( |
| 97 |
'steal', |
| 98 |
'If the username specified by --user exists, specify this to force conversion to a system user.' |
| 99 |
); |
| 100 |
$this->addOption( |
| 101 |
'run', 'If not specified, the script will not actually perform any moves (i.e. it will dry-run).' |
| 102 |
); |
| 103 |
$this->addOption( |
| 104 |
'prefix', 'When the new title already exists, add this prefix.', false, true |
| 105 |
); |
| 106 |
$this->addOption( |
| 107 |
'suffix', 'When the new title already exists, add this suffix.', false, true |
| 108 |
); |
| 109 |
$this->addOption( 'reason', 'Reason to use when moving pages.', false, true ); |
| 110 |
$this->addOption( 'tag', 'Change tag to apply when moving pages.', false, true ); |
| 111 |
$this->addOption( 'tables', 'Comma-separated list of database tables to process.', false, true ); |
| 112 |
$this->addOption( |
| 113 |
'userlist', 'Filename to which to output usernames needing rename. ' . |
| 114 |
'This file can then be used directly by renameInvalidUsernames.php maintenance script', |
| 115 |
false, |
| 116 |
true |
| 117 |
); |
| 118 |
$this->setBatchSize( 1000 ); |
| 119 |
} |
| 120 |
|
| 121 |
public function execute() { |
| 122 |
$this->run = $this->getOption( 'run', false ); |
| 123 |
|
| 124 |
if ( $this->run ) { |
| 125 |
$username = $this->getOption( 'user', User::MAINTENANCE_SCRIPT_USER ); |
| 126 |
$steal = $this->getOption( 'steal', false ); |
| 127 |
$this->user = User::newSystemUser( $username, [ 'steal' => $steal ] ); |
| 128 |
if ( !$this->user ) { |
| 129 |
$user = User::newFromName( $username ); |
| 130 |
if ( !$steal && $user && $user->isRegistered() ) { |
| 131 |
$this->fatalError( "User $username already exists.\n" |
| 132 |
. "Use --steal if you really want to steal it from the human who currently owns it." |
| 133 |
); |
| 134 |
} |
| 135 |
$this->fatalError( "Could not obtain system user $username." ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
$tables = $this->getOption( 'tables' ); |
| 140 |
if ( $tables !== null ) { |
| 141 |
$this->tables = explode( ',', $tables ); |
| 142 |
} |
| 143 |
|
| 144 |
$prefix = $this->getOption( 'prefix' ); |
| 145 |
if ( $prefix !== null ) { |
| 146 |
$title = Title::newFromText( $prefix . 'X' ); |
| 147 |
if ( !$title || substr( $title->getDBkey(), -1 ) !== 'X' ) { |
| 148 |
$this->fatalError( 'Invalid --prefix.' ); |
| 149 |
} |
| 150 |
if ( $title->getNamespace() <= NS_MAIN || $title->isExternal() ) { |
| 151 |
$this->fatalError( 'Invalid --prefix. It must not be in namespace 0 and must not be external' ); |
| 152 |
} |
| 153 |
$this->prefixNs = $title->getNamespace(); |
| 154 |
$this->prefix = substr( $title->getText(), 0, -1 ); |
| 155 |
} |
| 156 |
$this->suffix = $this->getOption( 'suffix' ); |
| 157 |
|
| 158 |
$this->reason = $this->getOption( 'reason' ) ?: $this->reason; |
| 159 |
$this->tags = (array)$this->getOption( 'tag', null ); |
| 160 |
|
| 161 |
$charmapFile = $this->getOption( 'charmap' ); |
| 162 |
if ( !file_exists( $charmapFile ) ) { |
| 163 |
$this->fatalError( "Charmap file $charmapFile does not exist." ); |
| 164 |
} |
| 165 |
if ( !is_file( $charmapFile ) || !is_readable( $charmapFile ) ) { |
| 166 |
$this->fatalError( "Charmap file $charmapFile is not readable." ); |
| 167 |
} |
| 168 |
$this->charmap = require $charmapFile; |
| 169 |
if ( !is_array( $this->charmap ) ) { |
| 170 |
$this->fatalError( "Charmap file $charmapFile did not return a PHP array." ); |
| 171 |
} |
| 172 |
$this->charmap = array_filter( |
| 173 |
$this->charmap, |
| 174 |
function ( $v, $k ) { |
| 175 |
if ( mb_strlen( $k ) !== 1 ) { |
| 176 |
$this->error( "Ignoring mapping from multi-character key '$k' to '$v'" ); |
| 177 |
return false; |
| 178 |
} |
| 179 |
return $k !== $v; |
| 180 |
}, |
| 181 |
ARRAY_FILTER_USE_BOTH |
| 182 |
); |
| 183 |
if ( !$this->charmap ) { |
| 184 |
$this->fatalError( "Charmap file $charmapFile did not contain any usable character mappings." ); |
| 185 |
} |
| 186 |
|
| 187 |
$db = $this->run ? $this->getPrimaryDB() : $this->getReplicaDB(); |
| 188 |
|
| 189 |
|
| 190 |
$this->processTable( |
| 191 |
$db, self::INPLACE_MOVE, 'archive', 'ar_namespace', 'ar_title', [ 'ar_timestamp', 'ar_id' ] |
| 192 |
); |
| 193 |
$this->processTable( |
| 194 |
$db, self::INPLACE_MOVE, 'filearchive', NS_FILE, 'fa_name', [ 'fa_timestamp', 'fa_id' ] |
| 195 |
); |
| 196 |
$this->processTable( |
| 197 |
$db, self::INPLACE_MOVE, 'logging', 'log_namespace', 'log_title', [ 'log_id' ] |
| 198 |
); |
| 199 |
$this->processTable( |
| 200 |
$db, self::INPLACE_MOVE, 'protected_titles', 'pt_namespace', 'pt_title', [] |
| 201 |
); |
| 202 |
$this->processTable( $db, self::MOVE, 'page', 'page_namespace', 'page_title', [ 'page_id' ] ); |
| 203 |
$this->processTable( $db, self::MOVE, 'image', NS_FILE, 'img_name', [] ); |
| 204 |
$this->processTable( |
| 205 |
$db, self::UPPERCASE, 'redirect', 'rd_namespace', 'rd_title', [ 'rd_from' ] |
| 206 |
); |
| 207 |
$this->processUsers( $db ); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
private function getLikeBatches( IReadableDatabase $db, $field, $batchSize = 100 ) { |
| 218 |
$ret = []; |
| 219 |
$likes = []; |
| 220 |
foreach ( $this->charmap as $from => $to ) { |
| 221 |
$likes[] = $db->expr( |
| 222 |
$field, |
| 223 |
IExpression::LIKE, |
| 224 |
new LikeValue( $from, $db->anyString() ) |
| 225 |
); |
| 226 |
if ( count( $likes ) >= $batchSize ) { |
| 227 |
$ret[] = $db->orExpr( $likes ); |
| 228 |
$likes = []; |
| 229 |
} |
| 230 |
} |
| 231 |
if ( $likes ) { |
| 232 |
$ret[] = $db->orExpr( $likes ); |
| 233 |
} |
| 234 |
return $ret; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
private function getNamespaces() { |
| 246 |
if ( $this->namespaces === null ) { |
| 247 |
$nsinfo = $this->getServiceContainer()->getNamespaceInfo(); |
| 248 |
$this->namespaces = array_filter( |
| 249 |
array_keys( $nsinfo->getCanonicalNamespaces() ), |
| 250 |
static function ( $ns ) use ( $nsinfo ) { |
| 251 |
return $nsinfo->isMovable( $ns ) && $nsinfo->isCapitalized( $ns ); |
| 252 |
} |
| 253 |
); |
| 254 |
usort( $this->namespaces, static function ( $ns1, $ns2 ) use ( $nsinfo ) { |
| 255 |
if ( $ns1 === $ns2 ) { |
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
$s1 = $nsinfo->getSubject( $ns1 ); |
| 260 |
$s2 = $nsinfo->getSubject( $ns2 ); |
| 261 |
|
| 262 |
|
| 263 |
if ( $s1 !== $s2 ) { |
| 264 |
return $s1 < $s2 ? -1 : 1; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
if ( $s1 === $ns1 ) { |
| 269 |
return -1; |
| 270 |
} |
| 271 |
if ( $s2 === $ns2 ) { |
| 272 |
return 1; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
return 0; |
| 278 |
} ); |
| 279 |
} |
| 280 |
|
| 281 |
return $this->namespaces; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
private function isUserPage( IReadableDatabase $db, $ns, $title ) { |
| 292 |
if ( $ns !== NS_USER && $ns !== NS_USER_TALK ) { |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
[ $base ] = explode( '/', $title, 2 ); |
| 297 |
if ( !isset( $this->seenUsers[$base] ) ) { |
| 298 |
|
| 299 |
$this->seenUsers[$base] = (bool)$db->newSelectQueryBuilder() |
| 300 |
->select( 'user_id' ) |
| 301 |
->from( 'user' ) |
| 302 |
->where( [ 'user_name' => strtr( $base, '_', ' ' ) ] ) |
| 303 |
->caller( __METHOD__ )->fetchField(); |
| 304 |
} |
| 305 |
return $this->seenUsers[$base]; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
private function mungeTitle( IReadableDatabase $db, Title $oldTitle, Title &$newTitle ) { |
| 316 |
$nt = $newTitle->getPrefixedText(); |
| 317 |
|
| 318 |
$munge = false; |
| 319 |
if ( $this->isUserPage( $db, $newTitle->getNamespace(), $newTitle->getText() ) ) { |
| 320 |
$munge = 'Target title\'s user exists'; |
| 321 |
} else { |
| 322 |
$mpFactory = $this->getServiceContainer()->getMovePageFactory(); |
| 323 |
$status = $mpFactory->newMovePage( $oldTitle, $newTitle )->isValidMove(); |
| 324 |
if ( !$status->isOK() && ( |
| 325 |
$status->hasMessage( 'articleexists' ) || $status->hasMessage( 'redirectexists' ) ) ) { |
| 326 |
$munge = 'Target title exists'; |
| 327 |
} |
| 328 |
} |
| 329 |
if ( !$munge ) { |
| 330 |
return true; |
| 331 |
} |
| 332 |
|
| 333 |
if ( $this->prefix !== null ) { |
| 334 |
$newTitle = Title::makeTitle( |
| 335 |
$this->prefixNs, |
| 336 |
$this->prefix . $oldTitle->getPrefixedText() . ( $this->suffix ?? '' ) |
| 337 |
); |
| 338 |
} elseif ( $this->suffix !== null ) { |
| 339 |
$dbkey = $newTitle->getText(); |
| 340 |
$i = $newTitle->getNamespace() === NS_FILE ? strrpos( $dbkey, '.' ) : false; |
| 341 |
if ( $i !== false ) { |
| 342 |
$newTitle = Title::makeTitle( |
| 343 |
$newTitle->getNamespace(), |
| 344 |
substr( $dbkey, 0, $i ) . $this->suffix . substr( $dbkey, $i ) |
| 345 |
); |
| 346 |
} else { |
| 347 |
$newTitle = Title::makeTitle( $newTitle->getNamespace(), $dbkey . $this->suffix ); |
| 348 |
} |
| 349 |
} else { |
| 350 |
$this->error( |
| 351 |
"Cannot move {$oldTitle->getPrefixedText()} → $nt: " |
| 352 |
. "$munge and no --prefix or --suffix was given" |
| 353 |
); |
| 354 |
return false; |
| 355 |
} |
| 356 |
|
| 357 |
if ( !$newTitle->canExist() ) { |
| 358 |
$this->error( |
| 359 |
"Cannot move {$oldTitle->getPrefixedText()} → $nt: " |
| 360 |
. "$munge and munged title '{$newTitle->getPrefixedText()}' is not valid" |
| 361 |
); |
| 362 |
return false; |
| 363 |
} |
| 364 |
if ( $newTitle->exists() ) { |
| 365 |
$this->error( |
| 366 |
"Cannot move {$oldTitle->getPrefixedText()} → $nt: " |
| 367 |
. "$munge and munged title '{$newTitle->getPrefixedText()}' also exists" |
| 368 |
); |
| 369 |
return false; |
| 370 |
} |
| 371 |
|
| 372 |
return true; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
private function doMove( IDatabase $db, $ns, $title ) { |
| 383 |
$char = mb_substr( $title, 0, 1 ); |
| 384 |
if ( !array_key_exists( $char, $this->charmap ) ) { |
| 385 |
$this->error( |
| 386 |
"Query returned NS$ns $title, which does not begin with a character in the charmap." |
| 387 |
); |
| 388 |
return false; |
| 389 |
} |
| 390 |
|
| 391 |
if ( $this->isUserPage( $db, $ns, $title ) ) { |
| 392 |
$this->output( "... Skipping user page NS$ns $title\n" ); |
| 393 |
return null; |
| 394 |
} |
| 395 |
|
| 396 |
$oldTitle = Title::makeTitle( $ns, $title ); |
| 397 |
$newTitle = Title::makeTitle( $ns, $this->charmap[$char] . mb_substr( $title, 1 ) ); |
| 398 |
$deletionReason = $this->shouldDelete( $db, $oldTitle, $newTitle ); |
| 399 |
if ( !$this->mungeTitle( $db, $oldTitle, $newTitle ) ) { |
| 400 |
return false; |
| 401 |
} |
| 402 |
|
| 403 |
$services = $this->getServiceContainer(); |
| 404 |
$mpFactory = $services->getMovePageFactory(); |
| 405 |
$movePage = $mpFactory->newMovePage( $oldTitle, $newTitle ); |
| 406 |
$status = $movePage->isValidMove(); |
| 407 |
if ( !$status->isOK() ) { |
| 408 |
$this->error( "Invalid move {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}:" ); |
| 409 |
$this->error( $status ); |
| 410 |
return false; |
| 411 |
} |
| 412 |
|
| 413 |
if ( !$this->run ) { |
| 414 |
$this->output( |
| 415 |
"Would rename {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}\n" |
| 416 |
); |
| 417 |
if ( $deletionReason ) { |
| 418 |
$this->output( |
| 419 |
"Would then delete {$newTitle->getPrefixedText()}: $deletionReason\n" |
| 420 |
); |
| 421 |
} |
| 422 |
return true; |
| 423 |
} |
| 424 |
|
| 425 |
$status = $movePage->move( $this->user, $this->reason, false, $this->tags ); |
| 426 |
if ( !$status->isOK() ) { |
| 427 |
$this->error( "Move {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()} failed:" ); |
| 428 |
$this->error( $status ); |
| 429 |
return false; |
| 430 |
} |
| 431 |
$this->output( "Renamed {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}\n" ); |
| 432 |
|
| 433 |
|
| 434 |
$db->newUpdateQueryBuilder() |
| 435 |
->update( 'logging' ) |
| 436 |
->set( [ |
| 437 |
'log_title' => $this->charmap[$char] . mb_substr( $title, 1 ), |
| 438 |
] ) |
| 439 |
->where( [ |
| 440 |
'log_namespace' => $oldTitle->getNamespace(), |
| 441 |
'log_title' => $oldTitle->getDBkey(), |
| 442 |
'log_page' => $newTitle->getArticleID(), |
| 443 |
] ) |
| 444 |
->caller( __METHOD__ ) |
| 445 |
->execute(); |
| 446 |
|
| 447 |
if ( $deletionReason !== null ) { |
| 448 |
$page = $services->getWikiPageFactory()->newFromTitle( $newTitle ); |
| 449 |
$delPage = $services->getDeletePageFactory()->newDeletePage( $page, $this->user ); |
| 450 |
$status = $delPage |
| 451 |
->forceImmediate( true ) |
| 452 |
->deleteUnsafe( $deletionReason ); |
| 453 |
if ( !$status->isOK() ) { |
| 454 |
$this->error( "Deletion of {$newTitle->getPrefixedText()} failed:" ); |
| 455 |
$this->error( $status ); |
| 456 |
return false; |
| 457 |
} |
| 458 |
$this->output( "Deleted {$newTitle->getPrefixedText()}\n" ); |
| 459 |
} |
| 460 |
|
| 461 |
return true; |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
private function shouldDelete( IReadableDatabase $db, Title $oldTitle, Title $newTitle ) { |
| 479 |
$oldRow = $db->newSelectQueryBuilder() |
| 480 |
->select( [ 'ns' => 'rd_namespace', 'title' => 'rd_title' ] ) |
| 481 |
->from( 'page' ) |
| 482 |
->join( 'redirect', null, 'rd_from = page_id' ) |
| 483 |
->where( [ 'page_namespace' => $oldTitle->getNamespace(), 'page_title' => $oldTitle->getDBkey() ] ) |
| 484 |
->caller( __METHOD__ )->fetchRow(); |
| 485 |
if ( !$oldRow ) { |
| 486 |
|
| 487 |
return null; |
| 488 |
} |
| 489 |
|
| 490 |
if ( (int)$oldRow->ns === $newTitle->getNamespace() && |
| 491 |
$oldRow->title === $newTitle->getDBkey() |
| 492 |
) { |
| 493 |
return $this->reason . ", and found that [[{$oldTitle->getPrefixedText()}]] is " |
| 494 |
. "already a redirect to [[{$newTitle->getPrefixedText()}]]"; |
| 495 |
} else { |
| 496 |
$newRow = $db->newSelectQueryBuilder() |
| 497 |
->select( [ 'ns' => 'rd_namespace', 'title' => 'rd_title' ] ) |
| 498 |
->from( 'page' ) |
| 499 |
->join( 'redirect', null, 'rd_from = page_id' ) |
| 500 |
->where( [ 'page_namespace' => $newTitle->getNamespace(), 'page_title' => $newTitle->getDBkey() ] ) |
| 501 |
->caller( __METHOD__ )->fetchRow(); |
| 502 |
if ( $newRow && $oldRow->ns === $newRow->ns && $oldRow->title === $newRow->title ) { |
| 503 |
$nt = Title::makeTitle( $newRow->ns, $newRow->title ); |
| 504 |
return $this->reason . ", and found that [[{$oldTitle->getPrefixedText()}]] and " |
| 505 |
. "[[{$newTitle->getPrefixedText()}]] both redirect to [[{$nt->getPrefixedText()}]]."; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
return null; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
private function doUpdate( IDatabase $db, $op, $table, $nsField, $titleField, $row ) { |
| 525 |
$ns = is_int( $nsField ) ? $nsField : (int)$row->$nsField; |
| 526 |
$title = $row->$titleField; |
| 527 |
|
| 528 |
$char = mb_substr( $title, 0, 1 ); |
| 529 |
if ( !array_key_exists( $char, $this->charmap ) ) { |
| 530 |
$r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); |
| 531 |
$this->error( |
| 532 |
"Query returned $r, but title does not begin with a character in the charmap." |
| 533 |
); |
| 534 |
return false; |
| 535 |
} |
| 536 |
|
| 537 |
$oldTitle = Title::makeTitle( $ns, $title ); |
| 538 |
$newTitle = Title::makeTitle( $ns, $this->charmap[$char] . mb_substr( $title, 1 ) ); |
| 539 |
if ( $op !== self::UPPERCASE && !$this->mungeTitle( $db, $oldTitle, $newTitle ) ) { |
| 540 |
return false; |
| 541 |
} |
| 542 |
|
| 543 |
if ( $this->run ) { |
| 544 |
$db->newUpdateQueryBuilder() |
| 545 |
->update( $table ) |
| 546 |
->set( array_merge( |
| 547 |
is_int( $nsField ) ? [] : [ $nsField => $newTitle->getNamespace() ], |
| 548 |
[ $titleField => $newTitle->getDBkey() ] |
| 549 |
) ) |
| 550 |
->where( (array)$row ) |
| 551 |
->caller( __METHOD__ ) |
| 552 |
->execute(); |
| 553 |
$r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); |
| 554 |
$this->output( "Set $r to {$newTitle->getPrefixedText()}\n" ); |
| 555 |
} else { |
| 556 |
$r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); |
| 557 |
$this->output( "Would set $r to {$newTitle->getPrefixedText()}\n" ); |
| 558 |
} |
| 559 |
|
| 560 |
return true; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
private function processTable( IDatabase $db, $op, $table, $nsField, $titleField, $pkFields ) { |
| 577 |
if ( $this->tables !== null && !in_array( $table, $this->tables, true ) ) { |
| 578 |
$this->output( "Skipping table `$table`, not in --tables.\n" ); |
| 579 |
return; |
| 580 |
} |
| 581 |
|
| 582 |
$batchSize = $this->getBatchSize(); |
| 583 |
$namespaces = $this->getNamespaces(); |
| 584 |
$likes = $this->getLikeBatches( $db, $titleField ); |
| 585 |
|
| 586 |
if ( is_int( $nsField ) ) { |
| 587 |
$namespaces = array_intersect( $namespaces, [ $nsField ] ); |
| 588 |
} |
| 589 |
|
| 590 |
if ( !$namespaces ) { |
| 591 |
$this->output( "Skipping table `$table`, no valid namespaces.\n" ); |
| 592 |
return; |
| 593 |
} |
| 594 |
|
| 595 |
$this->output( "Processing table `$table`...\n" ); |
| 596 |
|
| 597 |
$selectFields = array_merge( |
| 598 |
is_int( $nsField ) ? [] : [ $nsField ], |
| 599 |
[ $titleField ], |
| 600 |
$pkFields |
| 601 |
); |
| 602 |
$contFields = array_merge( [ $titleField ], $pkFields ); |
| 603 |
|
| 604 |
$lastReplicationWait = 0.0; |
| 605 |
$count = 0; |
| 606 |
$errors = 0; |
| 607 |
foreach ( $namespaces as $ns ) { |
| 608 |
foreach ( $likes as $like ) { |
| 609 |
$cont = []; |
| 610 |
do { |
| 611 |
$res = $db->newSelectQueryBuilder() |
| 612 |
->select( $selectFields ) |
| 613 |
->from( $table ) |
| 614 |
->where( [ "$nsField = $ns", $like, $cont ? $db->buildComparison( '>', $cont ) : '1=1' ] ) |
| 615 |
->orderBy( array_merge( [ $titleField ], $pkFields ) ) |
| 616 |
->limit( $batchSize ) |
| 617 |
->caller( __METHOD__ )->fetchResultSet(); |
| 618 |
$cont = []; |
| 619 |
foreach ( $res as $row ) { |
| 620 |
$cont = []; |
| 621 |
foreach ( $contFields as $field ) { |
| 622 |
$cont[ $field ] = $row->$field; |
| 623 |
} |
| 624 |
|
| 625 |
if ( $op === self::MOVE ) { |
| 626 |
$ns = is_int( $nsField ) ? $nsField : (int)$row->$nsField; |
| 627 |
$ret = $this->doMove( $db, $ns, $row->$titleField ); |
| 628 |
} else { |
| 629 |
$ret = $this->doUpdate( $db, $op, $table, $nsField, $titleField, $row ); |
| 630 |
} |
| 631 |
if ( $ret === true ) { |
| 632 |
$count++; |
| 633 |
} elseif ( $ret === false ) { |
| 634 |
$errors++; |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
if ( $this->run ) { |
| 639 |
|
| 640 |
$r = $cont ? json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) : '<end>'; |
| 641 |
$this->output( "... $table: $count renames, $errors errors at $r\n" ); |
| 642 |
$this->waitForReplication(); |
| 643 |
} |
| 644 |
} while ( $cont ); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
$this->output( "Done processing table `$table`.\n" ); |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
private function processUsers( IReadableDatabase $db ) { |
| 656 |
$userlistFile = $this->getOption( 'userlist' ); |
| 657 |
if ( $userlistFile === null ) { |
| 658 |
$this->output( "Not generating user list, --userlist was not specified.\n" ); |
| 659 |
return; |
| 660 |
} |
| 661 |
|
| 662 |
$fh = fopen( $userlistFile, 'ab' ); |
| 663 |
if ( !$fh ) { |
| 664 |
$this->error( "Could not open user list file $userlistFile" ); |
| 665 |
return; |
| 666 |
} |
| 667 |
|
| 668 |
$this->output( "Generating user list...\n" ); |
| 669 |
$count = 0; |
| 670 |
$batchSize = $this->getBatchSize(); |
| 671 |
foreach ( $this->getLikeBatches( $db, 'user_name' ) as $like ) { |
| 672 |
$cont = []; |
| 673 |
while ( true ) { |
| 674 |
$rows = $db->newSelectQueryBuilder() |
| 675 |
->select( [ 'user_id', 'user_name' ] ) |
| 676 |
->from( 'user' ) |
| 677 |
->where( $like ) |
| 678 |
->andWhere( $cont ) |
| 679 |
->orderBy( 'user_name' ) |
| 680 |
->limit( $batchSize ) |
| 681 |
->caller( __METHOD__ )->fetchResultSet(); |
| 682 |
|
| 683 |
if ( !$rows->numRows() ) { |
| 684 |
break; |
| 685 |
} |
| 686 |
|
| 687 |
foreach ( $rows as $row ) { |
| 688 |
$char = mb_substr( $row->user_name, 0, 1 ); |
| 689 |
if ( !array_key_exists( $char, $this->charmap ) ) { |
| 690 |
$this->error( |
| 691 |
"Query returned $row->user_name, but user name does not " . |
| 692 |
"begin with a character in the charmap." |
| 693 |
); |
| 694 |
continue; |
| 695 |
} |
| 696 |
$newName = $this->charmap[$char] . mb_substr( $row->user_name, 1 ); |
| 697 |
fprintf( $fh, "%s\t%s\t%s\n", WikiMap::getCurrentWikiId(), $row->user_id, $newName ); |
| 698 |
$count++; |
| 699 |
$cont = [ $db->expr( 'user_name', '>', $row->user_name ) ]; |
| 700 |
} |
| 701 |
|
| 702 |
$this->output( "... at $row->user_name, $count names so far\n" ); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
if ( !fclose( $fh ) ) { |
| 707 |
$this->error( "fclose on $userlistFile failed" ); |
| 708 |
} |
| 709 |
$this->output( "User list output to $userlistFile, $count users need renaming.\n" ); |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
$maintClass = UppercaseTitlesForUnicodeTransition::class; |
| 715 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 716 |
|