43 private const MOVE = 0;
44 private const INPLACE_MOVE = 1;
45 private const UPPERCASE = 2;
51 private $charmap = [];
57 private $reason =
'Uppercasing title for Unicode upgrade';
63 private $seenUsers = [];
66 private $namespaces =
null;
68 private ?
string $prefix =
null;
69 private ?
string $suffix =
null;
72 private $prefixNs =
null;
75 private $tables =
null;
78 parent::__construct();
80 "Rename titles when changing behavior of Language::ucfirst().\n"
82 .
"This script skips User and User_talk pages for registered users, as renaming of users "
83 .
"is too complex to try to implement here. Use something like Extension:Renameuser to "
84 .
"clean those up; this script can provide a list of user names affected."
87 'charmap',
'Character map generated by maintenance/language/generateUcfirstOverrides.php',
91 'user',
'System user to use to do the renames. Default is "Maintenance script".',
false,
true
95 'If the username specified by --user exists, specify this to force conversion to a system user.'
98 'run',
'If not specified, the script will not actually perform any moves (i.e. it will dry-run).'
101 'prefix',
'When the new title already exists, add this prefix.',
false,
true
104 'suffix',
'When the new title already exists, add this suffix.',
false,
true
106 $this->
addOption(
'reason',
'Reason to use when moving pages.',
false,
true );
107 $this->
addOption(
'tag',
'Change tag to apply when moving pages.',
false,
true );
108 $this->
addOption(
'tables',
'Comma-separated list of database tables to process.',
false,
true );
110 'userlist',
'Filename to which to output usernames needing rename. ' .
111 'This file can then be used directly by renameInvalidUsernames.php maintenance script',
122 $username = $this->
getOption(
'user', User::MAINTENANCE_SCRIPT_USER );
123 $steal = $this->
getOption(
'steal',
false );
124 $this->user = User::newSystemUser( $username, [
'steal' => $steal ] );
125 if ( !$this->user ) {
126 $user = User::newFromName( $username );
128 $this->
fatalError(
"User $username already exists.\n"
129 .
"Use --steal if you really want to steal it from the human who currently owns it."
132 $this->
fatalError(
"Could not obtain system user $username." );
137 if ( $tables !==
null ) {
138 $this->tables = explode(
',', $tables );
142 if ( $prefix !==
null ) {
143 $title = Title::newFromText( $prefix .
'X' );
144 if ( !$title || substr( $title->getDBkey(), -1 ) !==
'X' ) {
147 if ( $title->getNamespace() <=
NS_MAIN || $title->isExternal() ) {
148 $this->
fatalError(
'Invalid --prefix. It must not be in namespace 0 and must not be external' );
150 $this->prefixNs = $title->getNamespace();
151 $this->prefix = substr( $title->getText(), 0, -1 );
153 $this->suffix = $this->
getOption(
'suffix' );
155 $this->reason = $this->
getOption(
'reason' ) ?: $this->reason;
156 $this->tags = (array)$this->
getOption(
'tag',
null );
158 $charmapFile = $this->
getOption(
'charmap' );
159 if ( !file_exists( $charmapFile ) ) {
160 $this->
fatalError(
"Charmap file $charmapFile does not exist." );
162 if ( !is_file( $charmapFile ) || !is_readable( $charmapFile ) ) {
163 $this->
fatalError(
"Charmap file $charmapFile is not readable." );
165 $this->charmap = require $charmapFile;
166 if ( !is_array( $this->charmap ) ) {
167 $this->
fatalError(
"Charmap file $charmapFile did not return a PHP array." );
169 $this->charmap = array_filter(
171 function ( $v, $k ) {
172 if ( mb_strlen( $k ) !== 1 ) {
173 $this->
error(
"Ignoring mapping from multi-character key '$k' to '$v'" );
178 ARRAY_FILTER_USE_BOTH
180 if ( !$this->charmap ) {
181 $this->
fatalError(
"Charmap file $charmapFile did not contain any usable character mappings." );
188 $db, self::INPLACE_MOVE,
'archive',
'ar_namespace',
'ar_title', [
'ar_timestamp',
'ar_id' ]
191 $db, self::INPLACE_MOVE,
'filearchive',
NS_FILE,
'fa_name', [
'fa_timestamp',
'fa_id' ]
194 $db, self::INPLACE_MOVE,
'logging',
'log_namespace',
'log_title', [
'log_id' ]
197 $db, self::INPLACE_MOVE,
'protected_titles',
'pt_namespace',
'pt_title', []
199 $this->processTable( $db, self::MOVE,
'page',
'page_namespace',
'page_title', [
'page_id' ] );
200 $this->processTable( $db, self::MOVE,
'image',
NS_FILE,
'img_name', [] );
202 $db, self::UPPERCASE,
'redirect',
'rd_namespace',
'rd_title', [
'rd_from' ]
204 $this->processUsers( $db );
214 private function getLikeBatches(
IReadableDatabase $db, $field, $batchSize = 100 ) {
217 foreach ( $this->charmap as $from => $to ) {
218 $likes[] = $db->
expr(
223 if ( count( $likes ) >= $batchSize ) {
224 $ret[] = $db->
orExpr( $likes );
229 $ret[] = $db->
orExpr( $likes );
242 private function getNamespaces() {
243 if ( $this->namespaces ===
null ) {
245 $this->namespaces = array_filter(
246 array_keys( $nsinfo->getCanonicalNamespaces() ),
247 static function ( $ns ) use ( $nsinfo ) {
248 return $nsinfo->isMovable( $ns ) && $nsinfo->isCapitalized( $ns );
251 usort( $this->namespaces,
static function ( $ns1, $ns2 ) use ( $nsinfo ) {
252 if ( $ns1 === $ns2 ) {
256 $s1 = $nsinfo->getSubject( $ns1 );
257 $s2 = $nsinfo->getSubject( $ns2 );
261 return $s1 < $s2 ? -1 : 1;
265 if ( $s1 === $ns1 ) {
268 if ( $s2 === $ns2 ) {
278 return $this->namespaces;
293 [ $base ] = explode(
'/', $title, 2 );
294 if ( !isset( $this->seenUsers[$base] ) ) {
297 ->select(
'user_id' )
299 ->where( [
'user_name' => strtr( $base,
'_',
' ' ) ] )
300 ->caller( __METHOD__ )->fetchField();
302 return $this->seenUsers[$base];
317 $munge =
'Target title\'s user exists';
320 $status = $mpFactory->newMovePage( $oldTitle, $newTitle )->isValidMove();
321 if ( !$status->isOK() && (
322 $status->hasMessage(
'articleexists' ) || $status->hasMessage(
'redirectexists' ) ) ) {
323 $munge =
'Target title exists';
330 if ( $this->prefix !==
null ) {
331 $newTitle = Title::makeTitle(
335 } elseif ( $this->suffix !==
null ) {
338 if ( $i !==
false ) {
339 $newTitle = Title::makeTitle(
341 substr( $dbkey, 0, $i ) . $this->suffix . substr( $dbkey, $i )
344 $newTitle = Title::makeTitle( $newTitle->
getNamespace(), $dbkey . $this->suffix );
348 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
349 .
"$munge and no --prefix or --suffix was given"
356 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
357 .
"$munge and munged title '{$newTitle->getPrefixedText()}' is not valid"
361 if ( $newTitle->
exists() ) {
363 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
364 .
"$munge and munged title '{$newTitle->getPrefixedText()}' also exists"
379 private function doMove(
IDatabase $db, $ns, $title ) {
380 $char = mb_substr( $title, 0, 1 );
381 if ( !array_key_exists( $char, $this->charmap ) ) {
383 "Query returned NS$ns $title, which does not begin with a character in the charmap."
388 if ( $this->isUserPage( $db, $ns, $title ) ) {
389 $this->
output(
"... Skipping user page NS$ns $title\n" );
393 $oldTitle = Title::makeTitle( $ns, $title );
394 $newTitle = Title::makeTitle( $ns, $this->charmap[$char] . mb_substr( $title, 1 ) );
395 $deletionReason = $this->shouldDelete( $db, $oldTitle, $newTitle );
396 if ( !$this->mungeTitle( $db, $oldTitle, $newTitle ) ) {
401 $mpFactory = $services->getMovePageFactory();
402 $movePage = $mpFactory->newMovePage( $oldTitle, $newTitle );
403 $status = $movePage->isValidMove();
404 if ( !$status->isOK() ) {
405 $this->
error(
"Invalid move {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}:" );
406 $this->
error( $status );
412 "Would rename {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}\n"
414 if ( $deletionReason ) {
416 "Would then delete {$newTitle->getPrefixedText()}: $deletionReason\n"
422 $status = $movePage->move( $this->user, $this->reason,
false, $this->tags );
423 if ( !$status->isOK() ) {
424 $this->
error(
"Move {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()} failed:" );
425 $this->
error( $status );
427 $this->
output(
"Renamed {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}\n" );
431 ->update(
'logging' )
433 'log_title' => $this->charmap[$char] . mb_substr( $title, 1 ),
437 'log_title' => $oldTitle->
getDBkey(),
440 ->caller( __METHOD__ )
443 if ( $deletionReason !==
null ) {
444 $page = $services->getWikiPageFactory()->newFromTitle( $newTitle );
445 $delPage = $services->getDeletePageFactory()->newDeletePage( $page, $this->user );
447 ->forceImmediate(
true )
448 ->deleteUnsafe( $deletionReason );
449 if ( !$status->isOK() ) {
450 $this->
error(
"Deletion of {$newTitle->getPrefixedText()} failed:" );
451 $this->
error( $status );
454 $this->
output(
"Deleted {$newTitle->getPrefixedText()}\n" );
476 ->select( [
'ns' =>
'rd_namespace',
'title' =>
'rd_title' ] )
478 ->join(
'redirect',
null,
'rd_from = page_id' )
479 ->where( [
'page_namespace' => $oldTitle->
getNamespace(),
'page_title' => $oldTitle->
getDBkey() ] )
480 ->caller( __METHOD__ )->fetchRow();
487 $oldRow->title === $newTitle->
getDBkey()
489 return $this->reason .
", and found that [[{$oldTitle->getPrefixedText()}]] is "
490 .
"already a redirect to [[{$newTitle->getPrefixedText()}]]";
493 ->select( [
'ns' =>
'rd_namespace',
'title' =>
'rd_title' ] )
495 ->join(
'redirect',
null,
'rd_from = page_id' )
496 ->where( [
'page_namespace' => $newTitle->
getNamespace(),
'page_title' => $newTitle->
getDBkey() ] )
497 ->caller( __METHOD__ )->fetchRow();
498 if ( $newRow && $oldRow->ns === $newRow->ns && $oldRow->title === $newRow->title ) {
499 $nt = Title::makeTitle( $newRow->ns, $newRow->title );
500 return $this->reason .
", and found that [[{$oldTitle->getPrefixedText()}]] and "
501 .
"[[{$newTitle->getPrefixedText()}]] both redirect to [[{$nt->getPrefixedText()}]].";
520 private function doUpdate(
IDatabase $db, $op, $table, $nsField, $titleField, $row ) {
521 $ns = is_int( $nsField ) ? $nsField : (int)$row->$nsField;
522 $title = $row->$titleField;
524 $char = mb_substr( $title, 0, 1 );
525 if ( !array_key_exists( $char, $this->charmap ) ) {
526 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
528 "Query returned $r, but title does not begin with a character in the charmap."
533 $oldTitle = Title::makeTitle( $ns, $title );
534 $newTitle = Title::makeTitle( $ns, $this->charmap[$char] . mb_substr( $title, 1 ) );
535 if ( $op !== self::UPPERCASE && !$this->mungeTitle( $db, $oldTitle, $newTitle ) ) {
540 $db->newUpdateQueryBuilder()
543 is_int( $nsField ) ? [] : [ $nsField => $newTitle->getNamespace() ],
544 [ $titleField => $newTitle->getDBkey() ]
546 ->where( (array)$row )
547 ->caller( __METHOD__ )
549 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
550 $this->
output(
"Set $r to {$newTitle->getPrefixedText()}\n" );
552 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
553 $this->
output(
"Would set $r to {$newTitle->getPrefixedText()}\n" );
572 private function processTable(
IDatabase $db, $op, $table, $nsField, $titleField, $pkFields ) {
573 if ( $this->tables !==
null && !in_array( $table, $this->tables,
true ) ) {
574 $this->
output(
"Skipping table `$table`, not in --tables.\n" );
579 $namespaces = $this->getNamespaces();
580 $likes = $this->getLikeBatches( $db, $titleField );
582 if ( is_int( $nsField ) ) {
583 $namespaces = array_intersect( $namespaces, [ $nsField ] );
586 if ( !$namespaces ) {
587 $this->
output(
"Skipping table `$table`, no valid namespaces.\n" );
591 $this->
output(
"Processing table `$table`...\n" );
593 $selectFields = array_merge(
594 is_int( $nsField ) ? [] : [ $nsField ],
598 $contFields = array_merge( [ $titleField ], $pkFields );
600 $lastReplicationWait = 0.0;
603 foreach ( $namespaces as $ns ) {
604 foreach ( $likes as $like ) {
608 ->select( $selectFields )
610 ->where( [
"$nsField = $ns", $like, $cont ? $db->
buildComparison(
'>', $cont ) :
'1=1' ] )
611 ->orderBy( array_merge( [ $titleField ], $pkFields ) )
612 ->limit( $batchSize )
613 ->caller( __METHOD__ )->fetchResultSet();
615 foreach ( $res as $row ) {
617 foreach ( $contFields as $field ) {
618 $cont[ $field ] = $row->$field;
621 if ( $op === self::MOVE ) {
622 $ns = is_int( $nsField ) ? $nsField : (int)$row->$nsField;
623 $ret = $this->doMove( $db, $ns, $row->$titleField );
625 $ret = $this->doUpdate( $db, $op, $table, $nsField, $titleField, $row );
627 if ( $ret ===
true ) {
629 } elseif ( $ret ===
false ) {
636 $r = $cont ? json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) :
'<end>';
637 $this->
output(
"... $table: $count renames, $errors errors at $r\n" );
644 $this->
output(
"Done processing table `$table`.\n" );
652 $userlistFile = $this->
getOption(
'userlist' );
653 if ( $userlistFile ===
null ) {
654 $this->
output(
"Not generating user list, --userlist was not specified.\n" );
658 $fh = fopen( $userlistFile,
'ab' );
660 $this->
error(
"Could not open user list file $userlistFile" );
664 $this->
output(
"Generating user list...\n" );
667 foreach ( $this->getLikeBatches( $db,
'user_name' ) as $like ) {
671 ->select( [
'user_id',
'user_name' ] )
675 ->orderBy(
'user_name' )
676 ->limit( $batchSize )
677 ->caller( __METHOD__ )->fetchResultSet();
679 if ( !$rows->numRows() ) {
683 foreach ( $rows as $row ) {
684 $char = mb_substr( $row->user_name, 0, 1 );
685 if ( !array_key_exists( $char, $this->charmap ) ) {
687 "Query returned $row->user_name, but user name does not " .
688 "begin with a character in the charmap."
692 $newName = $this->charmap[$char] . mb_substr( $row->user_name, 1 );
693 fprintf( $fh,
"%s\t%s\t%s\n", WikiMap::getCurrentWikiId(), $row->user_id, $newName );
695 $cont = [ $db->
expr(
'user_name',
'>', $row->user_name ) ];
698 $this->
output(
"... at $row->user_name, $count names so far\n" );
702 if ( !fclose( $fh ) ) {
703 $this->
error(
"fclose on $userlistFile failed" );
705 $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.