45 private const MOVE = 0;
46 private const INPLACE_MOVE = 1;
47 private const UPPERCASE = 2;
53 private $charmap = [];
59 private $reason =
'Uppercasing title for Unicode upgrade';
65 private $seenUsers = [];
68 private $namespaces =
null;
70 private ?
string $prefix =
null;
71 private ?
string $suffix =
null;
74 private $prefixNs =
null;
77 private $tables =
null;
80 parent::__construct();
82 "Rename titles when changing behavior of Language::ucfirst().\n"
84 .
"This script skips User and User_talk pages for registered users, as renaming of users "
85 .
"is too complex to try to implement here. Use something like Extension:Renameuser to "
86 .
"clean those up; this script can provide a list of user names affected."
89 'charmap',
'Character map generated by maintenance/language/generateUcfirstOverrides.php',
93 'user',
'System user to use to do the renames. Default is "Maintenance script".',
false,
true
97 'If the username specified by --user exists, specify this to force conversion to a system user.'
100 'run',
'If not specified, the script will not actually perform any moves (i.e. it will dry-run).'
103 'prefix',
'When the new title already exists, add this prefix.',
false,
true
106 'suffix',
'When the new title already exists, add this suffix.',
false,
true
108 $this->
addOption(
'reason',
'Reason to use when moving pages.',
false,
true );
109 $this->
addOption(
'tag',
'Change tag to apply when moving pages.',
false,
true );
110 $this->
addOption(
'tables',
'Comma-separated list of database tables to process.',
false,
true );
112 'userlist',
'Filename to which to output usernames needing rename. ' .
113 'This file can then be used directly by renameInvalidUsernames.php maintenance script',
124 $username = $this->
getOption(
'user', User::MAINTENANCE_SCRIPT_USER );
125 $steal = $this->
getOption(
'steal',
false );
126 $this->user = User::newSystemUser( $username, [
'steal' => $steal ] );
127 if ( !$this->user ) {
128 $user = User::newFromName( $username );
130 $this->
fatalError(
"User $username already exists.\n"
131 .
"Use --steal if you really want to steal it from the human who currently owns it."
134 $this->
fatalError(
"Could not obtain system user $username." );
139 if ( $tables !==
null ) {
140 $this->tables = explode(
',', $tables );
144 if ( $prefix !==
null ) {
145 $title = Title::newFromText( $prefix .
'X' );
146 if ( !$title || substr( $title->getDBkey(), -1 ) !==
'X' ) {
149 if ( $title->getNamespace() <=
NS_MAIN || $title->isExternal() ) {
150 $this->
fatalError(
'Invalid --prefix. It must not be in namespace 0 and must not be external' );
152 $this->prefixNs = $title->getNamespace();
153 $this->prefix = substr( $title->getText(), 0, -1 );
155 $this->suffix = $this->
getOption(
'suffix' );
157 $this->reason = $this->
getOption(
'reason' ) ?: $this->reason;
158 $this->tags = (array)$this->
getOption(
'tag',
null );
160 $charmapFile = $this->
getOption(
'charmap' );
161 if ( !file_exists( $charmapFile ) ) {
162 $this->
fatalError(
"Charmap file $charmapFile does not exist." );
164 if ( !is_file( $charmapFile ) || !is_readable( $charmapFile ) ) {
165 $this->
fatalError(
"Charmap file $charmapFile is not readable." );
167 $this->charmap = require $charmapFile;
168 if ( !is_array( $this->charmap ) ) {
169 $this->
fatalError(
"Charmap file $charmapFile did not return a PHP array." );
171 $this->charmap = array_filter(
173 function ( $v, $k ) {
174 if ( mb_strlen( $k ) !== 1 ) {
175 $this->
error(
"Ignoring mapping from multi-character key '$k' to '$v'" );
180 ARRAY_FILTER_USE_BOTH
182 if ( !$this->charmap ) {
183 $this->
fatalError(
"Charmap file $charmapFile did not contain any usable character mappings." );
190 $db, self::INPLACE_MOVE,
'archive',
'ar_namespace',
'ar_title', [
'ar_timestamp',
'ar_id' ]
193 $db, self::INPLACE_MOVE,
'filearchive',
NS_FILE,
'fa_name', [
'fa_timestamp',
'fa_id' ]
196 $db, self::INPLACE_MOVE,
'logging',
'log_namespace',
'log_title', [
'log_id' ]
199 $db, self::INPLACE_MOVE,
'protected_titles',
'pt_namespace',
'pt_title', []
201 $this->processTable( $db, self::MOVE,
'page',
'page_namespace',
'page_title', [
'page_id' ] );
202 $this->processTable( $db, self::MOVE,
'image',
NS_FILE,
'img_name', [] );
204 $db, self::UPPERCASE,
'redirect',
'rd_namespace',
'rd_title', [
'rd_from' ]
206 $this->processUsers( $db );
216 private function getLikeBatches(
IReadableDatabase $db, $field, $batchSize = 100 ) {
219 foreach ( $this->charmap as $from => $to ) {
220 $likes[] = $db->
expr(
225 if ( count( $likes ) >= $batchSize ) {
226 $ret[] = $db->
orExpr( $likes );
231 $ret[] = $db->
orExpr( $likes );
244 private function getNamespaces() {
245 if ( $this->namespaces ===
null ) {
247 $this->namespaces = array_filter(
248 array_keys( $nsinfo->getCanonicalNamespaces() ),
249 static function ( $ns ) use ( $nsinfo ) {
250 return $nsinfo->isMovable( $ns ) && $nsinfo->isCapitalized( $ns );
253 usort( $this->namespaces,
static function ( $ns1, $ns2 ) use ( $nsinfo ) {
254 if ( $ns1 === $ns2 ) {
258 $s1 = $nsinfo->getSubject( $ns1 );
259 $s2 = $nsinfo->getSubject( $ns2 );
263 return $s1 < $s2 ? -1 : 1;
267 if ( $s1 === $ns1 ) {
270 if ( $s2 === $ns2 ) {
280 return $this->namespaces;
295 [ $base ] = explode(
'/', $title, 2 );
296 if ( !isset( $this->seenUsers[$base] ) ) {
299 ->select(
'user_id' )
301 ->where( [
'user_name' => strtr( $base,
'_',
' ' ) ] )
302 ->caller( __METHOD__ )->fetchField();
304 return $this->seenUsers[$base];
319 $munge =
'Target title\'s user exists';
322 $status = $mpFactory->newMovePage( $oldTitle, $newTitle )->isValidMove();
323 if ( !$status->isOK() && (
324 $status->hasMessage(
'articleexists' ) || $status->hasMessage(
'redirectexists' ) ) ) {
325 $munge =
'Target title exists';
332 if ( $this->prefix !==
null ) {
333 $newTitle = Title::makeTitle(
337 } elseif ( $this->suffix !==
null ) {
340 if ( $i !==
false ) {
341 $newTitle = Title::makeTitle(
343 substr( $dbkey, 0, $i ) . $this->suffix . substr( $dbkey, $i )
346 $newTitle = Title::makeTitle( $newTitle->
getNamespace(), $dbkey . $this->suffix );
350 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
351 .
"$munge and no --prefix or --suffix was given"
358 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
359 .
"$munge and munged title '{$newTitle->getPrefixedText()}' is not valid"
363 if ( $newTitle->
exists() ) {
365 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
366 .
"$munge and munged title '{$newTitle->getPrefixedText()}' also exists"
381 private function doMove(
IDatabase $db, $ns, $title ) {
382 $char = mb_substr( $title, 0, 1 );
383 if ( !array_key_exists( $char, $this->charmap ) ) {
385 "Query returned NS$ns $title, which does not begin with a character in the charmap."
390 if ( $this->isUserPage( $db, $ns, $title ) ) {
391 $this->
output(
"... Skipping user page NS$ns $title\n" );
395 $oldTitle = Title::makeTitle( $ns, $title );
396 $newTitle = Title::makeTitle( $ns, $this->charmap[$char] . mb_substr( $title, 1 ) );
397 $deletionReason = $this->shouldDelete( $db, $oldTitle, $newTitle );
398 if ( !$this->mungeTitle( $db, $oldTitle, $newTitle ) ) {
403 $mpFactory = $services->getMovePageFactory();
404 $movePage = $mpFactory->newMovePage( $oldTitle, $newTitle );
405 $status = $movePage->isValidMove();
406 if ( !$status->isOK() ) {
407 $this->
error(
"Invalid move {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}:" );
408 $this->
error( $status );
414 "Would rename {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}\n"
416 if ( $deletionReason ) {
418 "Would then delete {$newTitle->getPrefixedText()}: $deletionReason\n"
424 $status = $movePage->move( $this->user, $this->reason,
false, $this->tags );
425 if ( !$status->isOK() ) {
426 $this->
error(
"Move {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()} failed:" );
427 $this->
error( $status );
429 $this->
output(
"Renamed {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}\n" );
433 ->update(
'logging' )
435 'log_title' => $this->charmap[$char] . mb_substr( $title, 1 ),
439 'log_title' => $oldTitle->
getDBkey(),
442 ->caller( __METHOD__ )
445 if ( $deletionReason !==
null ) {
446 $page = $services->getWikiPageFactory()->newFromTitle( $newTitle );
447 $delPage = $services->getDeletePageFactory()->newDeletePage( $page, $this->user );
449 ->forceImmediate(
true )
450 ->deleteUnsafe( $deletionReason );
451 if ( !$status->isOK() ) {
452 $this->
error(
"Deletion of {$newTitle->getPrefixedText()} failed:" );
453 $this->
error( $status );
456 $this->
output(
"Deleted {$newTitle->getPrefixedText()}\n" );
478 ->select( [
'ns' =>
'rd_namespace',
'title' =>
'rd_title' ] )
480 ->join(
'redirect',
null,
'rd_from = page_id' )
481 ->where( [
'page_namespace' => $oldTitle->
getNamespace(),
'page_title' => $oldTitle->
getDBkey() ] )
482 ->caller( __METHOD__ )->fetchRow();
489 $oldRow->title === $newTitle->
getDBkey()
491 return $this->reason .
", and found that [[{$oldTitle->getPrefixedText()}]] is "
492 .
"already a redirect to [[{$newTitle->getPrefixedText()}]]";
495 ->select( [
'ns' =>
'rd_namespace',
'title' =>
'rd_title' ] )
497 ->join(
'redirect',
null,
'rd_from = page_id' )
498 ->where( [
'page_namespace' => $newTitle->
getNamespace(),
'page_title' => $newTitle->
getDBkey() ] )
499 ->caller( __METHOD__ )->fetchRow();
500 if ( $newRow && $oldRow->ns === $newRow->ns && $oldRow->title === $newRow->title ) {
501 $nt = Title::makeTitle( $newRow->ns, $newRow->title );
502 return $this->reason .
", and found that [[{$oldTitle->getPrefixedText()}]] and "
503 .
"[[{$newTitle->getPrefixedText()}]] both redirect to [[{$nt->getPrefixedText()}]].";
522 private function doUpdate(
IDatabase $db, $op, $table, $nsField, $titleField, $row ) {
523 $ns = is_int( $nsField ) ? $nsField : (int)$row->$nsField;
524 $title = $row->$titleField;
526 $char = mb_substr( $title, 0, 1 );
527 if ( !array_key_exists( $char, $this->charmap ) ) {
528 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
530 "Query returned $r, but title does not begin with a character in the charmap."
535 $oldTitle = Title::makeTitle( $ns, $title );
536 $newTitle = Title::makeTitle( $ns, $this->charmap[$char] . mb_substr( $title, 1 ) );
537 if ( $op !== self::UPPERCASE && !$this->mungeTitle( $db, $oldTitle, $newTitle ) ) {
542 $db->newUpdateQueryBuilder()
545 is_int( $nsField ) ? [] : [ $nsField => $newTitle->getNamespace() ],
546 [ $titleField => $newTitle->getDBkey() ]
548 ->where( (array)$row )
549 ->caller( __METHOD__ )
551 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
552 $this->
output(
"Set $r to {$newTitle->getPrefixedText()}\n" );
554 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
555 $this->
output(
"Would set $r to {$newTitle->getPrefixedText()}\n" );
574 private function processTable(
IDatabase $db, $op, $table, $nsField, $titleField, $pkFields ) {
575 if ( $this->tables !==
null && !in_array( $table, $this->tables,
true ) ) {
576 $this->
output(
"Skipping table `$table`, not in --tables.\n" );
581 $namespaces = $this->getNamespaces();
582 $likes = $this->getLikeBatches( $db, $titleField );
584 if ( is_int( $nsField ) ) {
585 $namespaces = array_intersect( $namespaces, [ $nsField ] );
588 if ( !$namespaces ) {
589 $this->
output(
"Skipping table `$table`, no valid namespaces.\n" );
593 $this->
output(
"Processing table `$table`...\n" );
595 $selectFields = array_merge(
596 is_int( $nsField ) ? [] : [ $nsField ],
600 $contFields = array_merge( [ $titleField ], $pkFields );
602 $lastReplicationWait = 0.0;
605 foreach ( $namespaces as $ns ) {
606 foreach ( $likes as $like ) {
610 ->select( $selectFields )
612 ->where( [
"$nsField = $ns", $like, $cont ? $db->
buildComparison(
'>', $cont ) :
'1=1' ] )
613 ->orderBy( array_merge( [ $titleField ], $pkFields ) )
614 ->limit( $batchSize )
615 ->caller( __METHOD__ )->fetchResultSet();
617 foreach ( $res as $row ) {
619 foreach ( $contFields as $field ) {
620 $cont[ $field ] = $row->$field;
623 if ( $op === self::MOVE ) {
624 $ns = is_int( $nsField ) ? $nsField : (int)$row->$nsField;
625 $ret = $this->doMove( $db, $ns, $row->$titleField );
627 $ret = $this->doUpdate( $db, $op, $table, $nsField, $titleField, $row );
629 if ( $ret ===
true ) {
631 } elseif ( $ret ===
false ) {
638 $r = $cont ? json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) :
'<end>';
639 $this->
output(
"... $table: $count renames, $errors errors at $r\n" );
646 $this->
output(
"Done processing table `$table`.\n" );
654 $userlistFile = $this->
getOption(
'userlist' );
655 if ( $userlistFile ===
null ) {
656 $this->
output(
"Not generating user list, --userlist was not specified.\n" );
660 $fh = fopen( $userlistFile,
'ab' );
662 $this->
error(
"Could not open user list file $userlistFile" );
666 $this->
output(
"Generating user list...\n" );
669 foreach ( $this->getLikeBatches( $db,
'user_name' ) as $like ) {
673 ->select( [
'user_id',
'user_name' ] )
677 ->orderBy(
'user_name' )
678 ->limit( $batchSize )
679 ->caller( __METHOD__ )->fetchResultSet();
681 if ( !$rows->numRows() ) {
685 foreach ( $rows as $row ) {
686 $char = mb_substr( $row->user_name, 0, 1 );
687 if ( !array_key_exists( $char, $this->charmap ) ) {
689 "Query returned $row->user_name, but user name does not " .
690 "begin with a character in the charmap."
694 $newName = $this->charmap[$char] . mb_substr( $row->user_name, 1 );
695 fprintf( $fh,
"%s\t%s\t%s\n", WikiMap::getCurrentWikiId(), $row->user_id, $newName );
697 $cont = [ $db->
expr(
'user_name',
'>', $row->user_name ) ];
700 $this->
output(
"... at $row->user_name, $count names so far\n" );
704 if ( !fclose( $fh ) ) {
705 $this->
error(
"fclose on $userlistFile failed" );
707 $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.