44 private const MOVE = 0;
45 private const INPLACE_MOVE = 1;
46 private const UPPERCASE = 2;
52 private $charmap = [];
58 private $reason =
'Uppercasing title for Unicode upgrade';
64 private $seenUsers = [];
67 private $namespaces =
null;
70 private $prefix =
null, $suffix =
null;
73 private $prefixNs =
null;
76 private $tables =
null;
79 parent::__construct();
81 "Rename titles when changing behavior of Language::ucfirst().\n"
83 .
"This script skips User and User_talk pages for registered users, as renaming of users "
84 .
"is too complex to try to implement here. Use something like Extension:Renameuser to "
85 .
"clean those up; this script can provide a list of user names affected."
88 'charmap',
'Character map generated by maintenance/language/generateUcfirstOverrides.php',
92 'user',
'System user to use to do the renames. Default is "Maintenance script".',
false,
true
96 'If the username specified by --user exists, specify this to force conversion to a system user.'
99 'run',
'If not specified, the script will not actually perform any moves (i.e. it will dry-run).'
102 'prefix',
'When the new title already exists, add this prefix.',
false,
true
105 'suffix',
'When the new title already exists, add this suffix.',
false,
true
107 $this->
addOption(
'reason',
'Reason to use when moving pages.',
false,
true );
108 $this->
addOption(
'tag',
'Change tag to apply when moving pages.',
false,
true );
109 $this->
addOption(
'tables',
'Comma-separated list of database tables to process.',
false,
true );
111 'userlist',
'Filename to which to output usernames needing rename. ' .
112 'This file can then be used directly by renameInvalidUsernames.php maintenance script',
120 $this->run = $this->
getOption(
'run',
false );
123 $username = $this->
getOption(
'user', User::MAINTENANCE_SCRIPT_USER );
124 $steal = $this->
getOption(
'steal',
false );
125 $this->user = User::newSystemUser( $username, [
'steal' => $steal ] );
126 if ( !$this->user ) {
127 $user = User::newFromName( $username );
129 $this->
fatalError(
"User $username already exists.\n"
130 .
"Use --steal if you really want to steal it from the human who currently owns it."
133 $this->
fatalError(
"Could not obtain system user $username." );
138 if ( $tables !==
null ) {
139 $this->tables = explode(
',', $tables );
143 if ( $prefix !==
null ) {
144 $title = Title::newFromText( $prefix .
'X' );
145 if ( !$title || substr( $title->getDBkey(), -1 ) !==
'X' ) {
148 if ( $title->getNamespace() <=
NS_MAIN || $title->isExternal() ) {
149 $this->
fatalError(
'Invalid --prefix. It must not be in namespace 0 and must not be external' );
151 $this->prefixNs = $title->getNamespace();
152 $this->prefix = substr( $title->getText(), 0, -1 );
154 $this->suffix = $this->
getOption(
'suffix' );
156 $this->reason = $this->
getOption(
'reason' ) ?: $this->reason;
157 $this->tags = (array)$this->
getOption(
'tag',
null );
159 $charmapFile = $this->
getOption(
'charmap' );
160 if ( !file_exists( $charmapFile ) ) {
161 $this->
fatalError(
"Charmap file $charmapFile does not exist." );
163 if ( !is_file( $charmapFile ) || !is_readable( $charmapFile ) ) {
164 $this->
fatalError(
"Charmap file $charmapFile is not readable." );
166 $this->charmap = require $charmapFile;
167 if ( !is_array( $this->charmap ) ) {
168 $this->
fatalError(
"Charmap file $charmapFile did not return a PHP array." );
170 $this->charmap = array_filter(
172 function ( $v, $k ) {
173 if ( mb_strlen( $k ) !== 1 ) {
174 $this->
error(
"Ignoring mapping from multi-character key '$k' to '$v'" );
179 ARRAY_FILTER_USE_BOTH
181 if ( !$this->charmap ) {
182 $this->
fatalError(
"Charmap file $charmapFile did not contain any usable character mappings." );
189 $db, self::INPLACE_MOVE,
'archive',
'ar_namespace',
'ar_title', [
'ar_timestamp',
'ar_id' ]
192 $db, self::INPLACE_MOVE,
'filearchive',
NS_FILE,
'fa_name', [
'fa_timestamp',
'fa_id' ]
195 $db, self::INPLACE_MOVE,
'logging',
'log_namespace',
'log_title', [
'log_id' ]
198 $db, self::INPLACE_MOVE,
'protected_titles',
'pt_namespace',
'pt_title', []
200 $this->processTable( $db, self::MOVE,
'page',
'page_namespace',
'page_title', [
'page_id' ] );
201 $this->processTable( $db, self::MOVE,
'image',
NS_FILE,
'img_name', [] );
203 $db, self::UPPERCASE,
'redirect',
'rd_namespace',
'rd_title', [
'rd_from' ]
205 $this->processUsers( $db );
215 private function getLikeBatches(
IReadableDatabase $db, $field, $batchSize = 100 ) {
218 foreach ( $this->charmap as $from => $to ) {
219 $likes[] = $db->
expr(
224 if ( count( $likes ) >= $batchSize ) {
243 private function getNamespaces() {
244 if ( $this->namespaces ===
null ) {
246 $this->namespaces = array_filter(
247 array_keys( $nsinfo->getCanonicalNamespaces() ),
248 static function ( $ns ) use ( $nsinfo ) {
249 return $nsinfo->isMovable( $ns ) && $nsinfo->isCapitalized( $ns );
252 usort( $this->namespaces,
static function ( $ns1, $ns2 ) use ( $nsinfo ) {
253 if ( $ns1 === $ns2 ) {
257 $s1 = $nsinfo->getSubject( $ns1 );
258 $s2 = $nsinfo->getSubject( $ns2 );
262 return $s1 < $s2 ? -1 : 1;
266 if ( $s1 === $ns1 ) {
269 if ( $s2 === $ns2 ) {
279 return $this->namespaces;
294 [ $base ] = explode(
'/', $title, 2 );
295 if ( !isset( $this->seenUsers[$base] ) ) {
298 ->select(
'user_id' )
300 ->where( [
'user_name' => strtr( $base,
'_',
' ' ) ] )
301 ->caller( __METHOD__ )->fetchField();
303 return $this->seenUsers[$base];
318 $munge =
'Target title\'s user exists';
321 $status = $mpFactory->newMovePage( $oldTitle, $newTitle )->isValidMove();
322 if ( !$status->isOK() && (
323 $status->hasMessage(
'articleexists' ) || $status->hasMessage(
'redirectexists' ) ) ) {
324 $munge =
'Target title exists';
331 if ( $this->prefix !==
null ) {
332 $newTitle = Title::makeTitle(
336 } elseif ( $this->suffix !==
null ) {
339 if ( $i !==
false ) {
340 $newTitle = Title::makeTitle(
342 substr( $dbkey, 0, $i ) . $this->suffix . substr( $dbkey, $i )
345 $newTitle = Title::makeTitle( $newTitle->
getNamespace(), $dbkey . $this->suffix );
349 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
350 .
"$munge and no --prefix or --suffix was given"
357 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
358 .
"$munge and munged title '{$newTitle->getPrefixedText()}' is not valid"
362 if ( $newTitle->
exists() ) {
364 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
365 .
"$munge and munged title '{$newTitle->getPrefixedText()}' also exists"
380 private function doMove(
IDatabase $db, $ns, $title ) {
381 $char = mb_substr( $title, 0, 1 );
382 if ( !array_key_exists( $char, $this->charmap ) ) {
384 "Query returned NS$ns $title, which does not begin with a character in the charmap."
389 if ( $this->isUserPage( $db, $ns, $title ) ) {
390 $this->
output(
"... Skipping user page NS$ns $title\n" );
394 $oldTitle = Title::makeTitle( $ns, $title );
395 $newTitle = Title::makeTitle( $ns, $this->charmap[$char] . mb_substr( $title, 1 ) );
396 $deletionReason = $this->shouldDelete( $db, $oldTitle, $newTitle );
397 if ( !$this->mungeTitle( $db, $oldTitle, $newTitle ) ) {
402 $mpFactory = $services->getMovePageFactory();
403 $movePage = $mpFactory->newMovePage( $oldTitle, $newTitle );
404 $status = $movePage->isValidMove();
405 if ( !$status->isOK() ) {
407 "Invalid move {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}: "
408 . $status->getMessage(
false,
false,
'en' )->useDatabase(
false )->plain()
415 "Would rename {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}\n"
417 if ( $deletionReason ) {
419 "Would then delete {$newTitle->getPrefixedText()}: $deletionReason\n"
425 $status = $movePage->move( $this->user, $this->reason,
false, $this->tags );
426 if ( !$status->isOK() ) {
428 "Move {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()} failed: "
429 . $status->getMessage(
false,
false,
'en' )->useDatabase(
false )->plain()
432 $this->
output(
"Renamed {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}\n" );
438 'log_title' => $this->charmap[$char] . mb_substr( $title, 1 ),
442 'log_title' => $oldTitle->
getDBkey(),
448 if ( $deletionReason !==
null ) {
449 $page = $services->getWikiPageFactory()->newFromTitle( $newTitle );
450 $delPage = $services->getDeletePageFactory()->newDeletePage( $page, $this->user );
452 ->forceImmediate(
true )
453 ->deleteUnsafe( $deletionReason );
454 if ( !$status->isOK() ) {
456 "Deletion of {$newTitle->getPrefixedText()} failed: "
457 . $status->getMessage(
false,
false,
'en' )->useDatabase(
false )->plain()
461 $this->
output(
"Deleted {$newTitle->getPrefixedText()}\n" );
483 ->select( [
'ns' =>
'rd_namespace',
'title' =>
'rd_title' ] )
485 ->join(
'redirect',
null,
'rd_from = page_id' )
486 ->where( [
'page_namespace' => $oldTitle->
getNamespace(),
'page_title' => $oldTitle->
getDBkey() ] )
487 ->caller( __METHOD__ )->fetchRow();
494 $oldRow->title === $newTitle->
getDBkey()
496 return $this->reason .
", and found that [[{$oldTitle->getPrefixedText()}]] is "
497 .
"already a redirect to [[{$newTitle->getPrefixedText()}]]";
500 ->select( [
'ns' =>
'rd_namespace',
'title' =>
'rd_title' ] )
502 ->join(
'redirect',
null,
'rd_from = page_id' )
503 ->where( [
'page_namespace' => $newTitle->
getNamespace(),
'page_title' => $newTitle->
getDBkey() ] )
504 ->caller( __METHOD__ )->fetchRow();
505 if ( $newRow && $oldRow->ns === $newRow->ns && $oldRow->title === $newRow->title ) {
506 $nt = Title::makeTitle( $newRow->ns, $newRow->title );
507 return $this->reason .
", and found that [[{$oldTitle->getPrefixedText()}]] and "
508 .
"[[{$newTitle->getPrefixedText()}]] both redirect to [[{$nt->getPrefixedText()}]].";
527 private function doUpdate(
IDatabase $db, $op, $table, $nsField, $titleField, $row ) {
528 $ns = is_int( $nsField ) ? $nsField : (int)$row->$nsField;
529 $title = $row->$titleField;
531 $char = mb_substr( $title, 0, 1 );
532 if ( !array_key_exists( $char, $this->charmap ) ) {
533 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
535 "Query returned $r, but title does not begin with a character in the charmap."
540 $oldTitle = Title::makeTitle( $ns, $title );
541 $newTitle = Title::makeTitle( $ns, $this->charmap[$char] . mb_substr( $title, 1 ) );
542 if ( $op !== self::UPPERCASE && !$this->mungeTitle( $db, $oldTitle, $newTitle ) ) {
550 is_int( $nsField ) ? [] : [ $nsField => $newTitle->getNamespace() ],
551 [ $titleField => $newTitle->getDBkey() ]
556 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
557 $this->
output(
"Set $r to {$newTitle->getPrefixedText()}\n" );
559 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
560 $this->
output(
"Would set $r to {$newTitle->getPrefixedText()}\n" );
579 private function processTable(
IDatabase $db, $op, $table, $nsField, $titleField, $pkFields ) {
580 if ( $this->tables !==
null && !in_array( $table, $this->tables,
true ) ) {
581 $this->
output(
"Skipping table `$table`, not in --tables.\n" );
586 $namespaces = $this->getNamespaces();
587 $likes = $this->getLikeBatches( $db, $titleField );
590 if ( is_int( $nsField ) ) {
591 $namespaces = array_intersect( $namespaces, [ $nsField ] );
594 if ( !$namespaces ) {
595 $this->
output(
"Skipping table `$table`, no valid namespaces.\n" );
599 $this->
output(
"Processing table `$table`...\n" );
601 $selectFields = array_merge(
602 is_int( $nsField ) ? [] : [ $nsField ],
606 $contFields = array_merge( [ $titleField ], $pkFields );
608 $lastReplicationWait = 0.0;
611 foreach ( $namespaces as $ns ) {
612 foreach ( $likes as $like ) {
616 ->select( $selectFields )
618 ->where( [
"$nsField = $ns", $like, $cont ? $db->
buildComparison(
'>', $cont ) :
'1=1' ] )
619 ->orderBy( array_merge( [ $titleField ], $pkFields ) )
620 ->limit( $batchSize )
621 ->caller( __METHOD__ )->fetchResultSet();
623 foreach ( $res as $row ) {
625 foreach ( $contFields as $field ) {
626 $cont[ $field ] = $row->$field;
629 if ( $op === self::MOVE ) {
630 $ns = is_int( $nsField ) ? $nsField : (int)$row->$nsField;
631 $ret = $this->doMove( $db, $ns, $row->$titleField );
633 $ret = $this->doUpdate( $db, $op, $table, $nsField, $titleField, $row );
635 if ( $ret ===
true ) {
637 } elseif ( $ret ===
false ) {
644 $r = $cont ? json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) :
'<end>';
645 $this->
output(
"... $table: $count renames, $errors errors at $r\n" );
646 $lbFactory->waitForReplication(
647 [
'timeout' => 30,
'ifWritesSince' => $lastReplicationWait ]
649 $lastReplicationWait = microtime(
true );
655 $this->
output(
"Done processing table `$table`.\n" );
663 $userlistFile = $this->
getOption(
'userlist' );
664 if ( $userlistFile ===
null ) {
665 $this->
output(
"Not generating user list, --userlist was not specified.\n" );
669 $fh = fopen( $userlistFile,
'ab' );
671 $this->
error(
"Could not open user list file $userlistFile" );
675 $this->
output(
"Generating user list...\n" );
678 foreach ( $this->getLikeBatches( $db,
'user_name' ) as $like ) {
682 ->select( [
'user_id',
'user_name' ] )
686 ->orderBy(
'user_name' )
687 ->limit( $batchSize )
688 ->caller( __METHOD__ )->fetchResultSet();
690 if ( !$rows->numRows() ) {
694 foreach ( $rows as $row ) {
695 $char = mb_substr( $row->user_name, 0, 1 );
696 if ( !array_key_exists( $char, $this->charmap ) ) {
698 "Query returned $row->user_name, but user name does not " .
699 "begin with a character in the charmap."
703 $newName = $this->charmap[$char] . mb_substr( $row->user_name, 1 );
704 fprintf( $fh,
"%s\t%s\t%s\n", WikiMap::getCurrentWikiId(), $row->user_id, $newName );
706 $cont = [
'user_name > ' . $db->
addQuotes( $row->user_name ) ];
709 $this->
output(
"... at $row->user_name, $count names so far\n" );
713 if ( !fclose( $fh ) ) {
714 $this->
error(
"fclose on $userlistFile failed" );
716 $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.