42 private const MOVE = 0;
43 private const INPLACE_MOVE = 1;
44 private const UPPERCASE = 2;
50 private $charmap = [];
56 private $reason =
'Uppercasing title for Unicode upgrade';
62 private $seenUsers = [];
65 private $namespaces =
null;
68 private $prefix =
null, $suffix =
null;
71 private $prefixNs =
null;
74 private $tables =
null;
77 parent::__construct();
79 "Rename titles when changing behavior of Language::ucfirst().\n"
81 .
"This script skips User and User_talk pages for registered users, as renaming of users "
82 .
"is too complex to try to implement here. Use something like Extension:Renameuser to "
83 .
"clean those up; this script can provide a list of user names affected."
86 'charmap',
'Character map generated by maintenance/language/generateUcfirstOverrides.php',
90 'user',
'System user to use to do the renames. Default is "Maintenance script".',
false,
true
94 'If the username specified by --user exists, specify this to force conversion to a system user.'
97 'run',
'If not specified, the script will not actually perform any moves (i.e. it will dry-run).'
100 'prefix',
'When the new title already exists, add this prefix.',
false,
true
103 'suffix',
'When the new title already exists, add this suffix.',
false,
true
105 $this->
addOption(
'reason',
'Reason to use when moving pages.',
false,
true );
106 $this->
addOption(
'tag',
'Change tag to apply when moving pages.',
false,
true );
107 $this->
addOption(
'tables',
'Comma-separated list of database tables to process.',
false,
true );
109 'userlist',
'Filename to which to output usernames needing rename. ' .
110 'This file can then be used directly by renameInvalidUsernames.php maintenance script',
118 $this->run = $this->
getOption(
'run',
false );
122 $steal = $this->
getOption(
'steal',
false );
124 if ( !$this->user ) {
127 $this->
fatalError(
"User $username already exists.\n"
128 .
"Use --steal if you really want to steal it from the human who currently owns it."
131 $this->
fatalError(
"Could not obtain system user $username." );
136 if ( $tables !==
null ) {
137 $this->tables = explode(
',', $tables );
141 if ( $prefix !==
null ) {
142 $title = Title::newFromText( $prefix .
'X' );
143 if ( !
$title || substr(
$title->getDBkey(), -1 ) !==
'X' ) {
147 $this->
fatalError(
'Invalid --prefix. It must not be in namespace 0 and must not be external' );
149 $this->prefixNs =
$title->getNamespace();
150 $this->prefix = substr(
$title->getText(), 0, -1 );
152 $this->suffix = $this->
getOption(
'suffix' );
154 $this->reason = $this->
getOption(
'reason' ) ?: $this->reason;
155 $this->tags = (array)$this->
getOption(
'tag',
null );
157 $charmapFile = $this->
getOption(
'charmap' );
158 if ( !file_exists( $charmapFile ) ) {
159 $this->
fatalError(
"Charmap file $charmapFile does not exist." );
161 if ( !is_file( $charmapFile ) || !is_readable( $charmapFile ) ) {
162 $this->
fatalError(
"Charmap file $charmapFile is not readable." );
164 $this->charmap = require $charmapFile;
165 if ( !is_array( $this->charmap ) ) {
166 $this->
fatalError(
"Charmap file $charmapFile did not return a PHP array." );
168 $this->charmap = array_filter(
170 function ( $v, $k ) {
171 if ( mb_strlen( $k ) !== 1 ) {
172 $this->
error(
"Ignoring mapping from multi-character key '$k' to '$v'" );
177 ARRAY_FILTER_USE_BOTH
179 if ( !$this->charmap ) {
180 $this->
fatalError(
"Charmap file $charmapFile did not contain any usable character mappings." );
187 $db, self::INPLACE_MOVE,
'archive',
'ar_namespace',
'ar_title', [
'ar_timestamp',
'ar_id' ]
190 $db, self::INPLACE_MOVE,
'filearchive',
NS_FILE,
'fa_name', [
'fa_timestamp',
'fa_id' ]
193 $db, self::INPLACE_MOVE,
'logging',
'log_namespace',
'log_title', [
'log_id' ]
196 $db, self::INPLACE_MOVE,
'protected_titles',
'pt_namespace',
'pt_title', []
198 $this->processTable( $db, self::MOVE,
'page',
'page_namespace',
'page_title', [
'page_id' ] );
199 $this->processTable( $db, self::MOVE,
'image',
NS_FILE,
'img_name', [] );
201 $db, self::UPPERCASE,
'redirect',
'rd_namespace',
'rd_title', [
'rd_from' ]
203 $this->processUsers( $db );
213 private function getLikeBatches(
ISQLPlatform $db, $field, $batchSize = 100 ) {
216 foreach ( $this->charmap as $from => $to ) {
218 if ( count( $likes ) >= $batchSize ) {
219 $ret[] = $db->
makeList( $likes, $db::LIST_OR );
224 $ret[] = $db->
makeList( $likes, $db::LIST_OR );
237 private function getNamespaces() {
238 if ( $this->namespaces ===
null ) {
239 $nsinfo = MediaWikiServices::getInstance()->getNamespaceInfo();
240 $this->namespaces = array_filter(
241 array_keys( $nsinfo->getCanonicalNamespaces() ),
242 static function ( $ns ) use ( $nsinfo ) {
243 return $nsinfo->isMovable( $ns ) && $nsinfo->isCapitalized( $ns );
246 usort( $this->namespaces,
static function ( $ns1, $ns2 ) use ( $nsinfo ) {
247 if ( $ns1 === $ns2 ) {
251 $s1 = $nsinfo->getSubject( $ns1 );
252 $s2 = $nsinfo->getSubject( $ns2 );
256 return $s1 < $s2 ? -1 : 1;
260 if ( $s1 === $ns1 ) {
263 if ( $s2 === $ns2 ) {
273 return $this->namespaces;
289 if ( !isset( $this->seenUsers[
$base] ) ) {
294 [
'user_name' => strtr(
$base,
'_',
' ' ) ],
298 return $this->seenUsers[
$base];
313 $munge =
'Target title\'s user exists';
315 $mpFactory = MediaWikiServices::getInstance()->getMovePageFactory();
316 $status = $mpFactory->newMovePage( $oldTitle, $newTitle )->isValidMove();
317 if ( !$status->isOK() && (
318 $status->hasMessage(
'articleexists' ) || $status->hasMessage(
'redirectexists' ) ) ) {
319 $munge =
'Target title exists';
326 if ( $this->prefix !==
null ) {
327 $newTitle = Title::makeTitle(
331 } elseif ( $this->suffix !==
null ) {
334 if ( $i !==
false ) {
335 $newTitle = Title::makeTitle(
337 substr( $dbkey, 0, $i ) . $this->suffix . substr( $dbkey, $i )
340 $newTitle = Title::makeTitle( $newTitle->
getNamespace(), $dbkey . $this->suffix );
344 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
345 .
"$munge and no --prefix or --suffix was given"
352 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
353 .
"$munge and munged title '{$newTitle->getPrefixedText()}' is not valid"
357 if ( $newTitle->
exists() ) {
359 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
360 .
"$munge and munged title '{$newTitle->getPrefixedText()}' also exists"
376 $char = mb_substr(
$title, 0, 1 );
377 if ( !array_key_exists( $char, $this->charmap ) ) {
379 "Query returned NS$ns $title, which does not begin with a character in the charmap."
384 if ( $this->isUserPage( $db, $ns,
$title ) ) {
385 $this->
output(
"... Skipping user page NS$ns $title\n" );
389 $oldTitle = Title::makeTitle( $ns,
$title );
390 $newTitle = Title::makeTitle( $ns, $this->charmap[$char] . mb_substr(
$title, 1 ) );
391 $deletionReason = $this->shouldDelete( $db, $oldTitle, $newTitle );
392 if ( !$this->mungeTitle( $db, $oldTitle, $newTitle ) ) {
396 $services = MediaWikiServices::getInstance();
397 $mpFactory = $services->getMovePageFactory();
398 $movePage = $mpFactory->newMovePage( $oldTitle, $newTitle );
399 $status = $movePage->isValidMove();
400 if ( !$status->isOK() ) {
402 "Invalid move {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}: "
403 . $status->getMessage(
false,
false,
'en' )->useDatabase(
false )->plain()
410 "Would rename {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}\n"
412 if ( $deletionReason ) {
414 "Would then delete {$newTitle->getPrefixedText()}: $deletionReason\n"
420 $status = $movePage->move( $this->user, $this->reason,
false, $this->tags );
421 if ( !$status->isOK() ) {
423 "Move {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()} failed: "
424 . $status->getMessage(
false,
false,
'en' )->useDatabase(
false )->plain()
427 $this->
output(
"Renamed {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}\n" );
433 'log_title' => $this->charmap[$char] . mb_substr(
$title, 1 ),
437 'log_title' => $oldTitle->
getDBkey(),
443 if ( $deletionReason !==
null ) {
444 $page = $services->getWikiPageFactory()->newFromTitle( $newTitle );
446 $status = $page->doDeleteArticleReal(
457 if ( !$status->isOK() ) {
459 "Deletion of {$newTitle->getPrefixedText()} failed: "
460 . $status->getMessage(
false,
false,
'en' )->useDatabase(
false )->plain()
464 $this->
output(
"Deleted {$newTitle->getPrefixedText()}\n" );
486 [
'page',
'redirect' ],
487 [
'ns' =>
'rd_namespace',
'title' =>
'rd_title' ],
491 [
'redirect' => [
'JOIN',
'rd_from = page_id' ] ]
499 $oldRow->title === $newTitle->
getDBkey()
501 return $this->reason .
", and found that [[{$oldTitle->getPrefixedText()}]] is "
502 .
"already a redirect to [[{$newTitle->getPrefixedText()}]]";
505 [
'page',
'redirect' ],
506 [
'ns' =>
'rd_namespace',
'title' =>
'rd_title' ],
510 [
'redirect' => [
'JOIN',
'rd_from = page_id' ] ]
512 if ( $newRow && $oldRow->ns === $newRow->ns && $oldRow->title === $newRow->title ) {
513 $nt = Title::makeTitle( $newRow->ns, $newRow->title );
514 return $this->reason .
", and found that [[{$oldTitle->getPrefixedText()}]] and "
515 .
"[[{$newTitle->getPrefixedText()}]] both redirect to [[{$nt->getPrefixedText()}]].";
534 private function doUpdate(
IDatabase $db, $op, $table, $nsField, $titleField, $row ) {
535 $ns = is_int( $nsField ) ? $nsField : (int)$row->$nsField;
536 $title = $row->$titleField;
538 $char = mb_substr(
$title, 0, 1 );
539 if ( !array_key_exists( $char, $this->charmap ) ) {
540 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
542 "Query returned $r, but title does not begin with a character in the charmap."
547 $oldTitle = Title::makeTitle( $ns,
$title );
548 $newTitle = Title::makeTitle( $ns, $this->charmap[$char] . mb_substr(
$title, 1 ) );
549 if ( $op !== self::UPPERCASE && !$this->mungeTitle( $db, $oldTitle, $newTitle ) ) {
557 is_int( $nsField ) ? [] : [ $nsField => $newTitle->getNamespace() ],
558 [ $titleField => $newTitle->getDBkey() ]
563 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
564 $this->
output(
"Set $r to {$newTitle->getPrefixedText()}\n" );
566 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
567 $this->
output(
"Would set $r to {$newTitle->getPrefixedText()}\n" );
586 private function processTable(
IDatabase $db, $op, $table, $nsField, $titleField, $pkFields ) {
587 if ( $this->tables !==
null && !in_array( $table, $this->tables,
true ) ) {
588 $this->
output(
"Skipping table `$table`, not in --tables.\n" );
593 $namespaces = $this->getNamespaces();
594 $likes = $this->getLikeBatches( $db, $titleField );
595 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
597 if ( is_int( $nsField ) ) {
598 $namespaces = array_intersect( $namespaces, [ $nsField ] );
601 if ( !$namespaces ) {
602 $this->
output(
"Skipping table `$table`, no valid namespaces.\n" );
606 $this->
output(
"Processing table `$table`...\n" );
608 $selectFields = array_merge(
609 is_int( $nsField ) ? [] : [ $nsField ],
613 $contFields = array_merge( [ $titleField ], $pkFields );
615 $lastReplicationWait = 0.0;
618 foreach ( $namespaces as $ns ) {
619 foreach ( $likes as $like ) {
625 [
"$nsField = $ns", $like, $cont ? $db->
buildComparison(
'>', $cont ) :
'1=1' ],
627 [
'ORDER BY' => array_merge( [ $titleField ], $pkFields ),
'LIMIT' => $batchSize ]
630 foreach (
$res as $row ) {
632 foreach ( $contFields as $field ) {
633 $cont[ $field ] = $row->$field;
636 if ( $op === self::MOVE ) {
637 $ns = is_int( $nsField ) ? $nsField : (int)$row->$nsField;
638 $ret = $this->doMove( $db, $ns, $row->$titleField );
640 $ret = $this->doUpdate( $db, $op, $table, $nsField, $titleField, $row );
642 if ( $ret ===
true ) {
644 } elseif ( $ret ===
false ) {
651 $r = $cont ? json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) :
'<end>';
652 $this->
output(
"... $table: $count renames, $errors errors at $r\n" );
653 $lbFactory->waitForReplication(
654 [
'timeout' => 30,
'ifWritesSince' => $lastReplicationWait ]
656 $lastReplicationWait = microtime(
true );
662 $this->
output(
"Done processing table `$table`.\n" );
670 $userlistFile = $this->
getOption(
'userlist' );
671 if ( $userlistFile ===
null ) {
672 $this->
output(
"Not generating user list, --userlist was not specified.\n" );
676 $fh = fopen( $userlistFile,
'ab' );
678 $this->
error(
"Could not open user list file $userlistFile" );
682 $this->
output(
"Generating user list...\n" );
685 foreach ( $this->getLikeBatches( $db,
'user_name' ) as $like ) {
690 [
'user_id',
'user_name' ],
691 array_merge( [ $like ], $cont ),
693 [
'ORDER BY' =>
'user_name',
'LIMIT' => $batchSize ]
696 if ( !$rows->numRows() ) {
700 foreach ( $rows as $row ) {
701 $char = mb_substr( $row->user_name, 0, 1 );
702 if ( !array_key_exists( $char, $this->charmap ) ) {
704 "Query returned $row->user_name, but user name does not " .
705 "begin with a character in the charmap."
709 $newName = $this->charmap[$char] . mb_substr( $row->user_name, 1 );
710 fprintf( $fh,
"%s\t%s\t%s\n", WikiMap::getCurrentWikiId(), $row->user_id, $newName );
712 $cont = [
'user_name > ' . $db->
addQuotes( $row->user_name ) ];
715 $this->
output(
"... at $row->user_name, $count names so far\n" );
719 if ( !fclose( $fh ) ) {
720 $this->
error(
"fclose on $userlistFile failed" );
722 $this->
output(
"User list output to $userlistFile, $count users need renaming.\n" );
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.