32 private const MOVE = 0;
33 private const INPLACE_MOVE = 1;
34 private const UPPERCASE = 2;
40 private $charmap = [];
46 private $reason =
'Uppercasing title for Unicode upgrade';
52 private $seenUsers = [];
55 private $namespaces =
null;
57 private ?
string $prefix =
null;
58 private ?
string $suffix =
null;
61 private $prefixNs =
null;
64 private $tables =
null;
67 parent::__construct();
69 "Rename titles when changing behavior of Language::ucfirst().\n"
71 .
"This script skips User and User_talk pages for registered users, as renaming of users "
72 .
"is too complex to try to implement here. Use something like Extension:Renameuser to "
73 .
"clean those up; this script can provide a list of user names affected."
76 'charmap',
'Character map generated by maintenance/language/generateUcfirstOverrides.php',
80 'user',
'System user to use to do the renames. Default is "Maintenance script".',
false,
true
84 'If the username specified by --user exists, specify this to force conversion to a system user.'
87 'run',
'If not specified, the script will not actually perform any moves (i.e. it will dry-run).'
90 'prefix',
'When the new title already exists, add this prefix.',
false,
true
93 'suffix',
'When the new title already exists, add this suffix.',
false,
true
95 $this->
addOption(
'reason',
'Reason to use when moving pages.',
false,
true );
96 $this->
addOption(
'tag',
'Change tag to apply when moving pages.',
false,
true );
97 $this->
addOption(
'tables',
'Comma-separated list of database tables to process.',
false,
true );
99 'userlist',
'Filename to which to output usernames needing rename. ' .
100 'This file can then be used directly by renameInvalidUsernames.php maintenance script',
108 $this->run = $this->
getOption(
'run',
false );
111 $username = $this->
getOption(
'user', User::MAINTENANCE_SCRIPT_USER );
112 $steal = $this->
getOption(
'steal',
false );
113 $this->user = User::newSystemUser( $username, [
'steal' => $steal ] );
114 if ( !$this->user ) {
115 $user = User::newFromName( $username );
117 $this->
fatalError(
"User $username already exists.\n"
118 .
"Use --steal if you really want to steal it from the human who currently owns it."
121 $this->
fatalError(
"Could not obtain system user $username." );
126 if ( $tables !==
null ) {
127 $this->tables = explode(
',', $tables );
131 if ( $prefix !==
null ) {
132 $title = Title::newFromText( $prefix .
'X' );
133 if ( !$title || substr( $title->getDBkey(), -1 ) !==
'X' ) {
136 if ( $title->getNamespace() <=
NS_MAIN || $title->isExternal() ) {
137 $this->
fatalError(
'Invalid --prefix. It must not be in namespace 0 and must not be external' );
139 $this->prefixNs = $title->getNamespace();
140 $this->prefix = substr( $title->getText(), 0, -1 );
142 $this->suffix = $this->
getOption(
'suffix' );
144 $this->reason = $this->
getOption(
'reason' ) ?: $this->reason;
145 $this->tags = (array)$this->
getOption(
'tag',
null );
147 $charmapFile = $this->
getOption(
'charmap' );
148 if ( !file_exists( $charmapFile ) ) {
149 $this->
fatalError(
"Charmap file $charmapFile does not exist." );
151 if ( !is_file( $charmapFile ) || !is_readable( $charmapFile ) ) {
152 $this->
fatalError(
"Charmap file $charmapFile is not readable." );
154 $this->charmap = require $charmapFile;
155 if ( !is_array( $this->charmap ) ) {
156 $this->
fatalError(
"Charmap file $charmapFile did not return a PHP array." );
158 $this->charmap = array_filter(
160 function ( $v, $k ) {
161 if ( mb_strlen( $k ) !== 1 ) {
162 $this->
error(
"Ignoring mapping from multi-character key '$k' to '$v'" );
167 ARRAY_FILTER_USE_BOTH
169 if ( !$this->charmap ) {
170 $this->
fatalError(
"Charmap file $charmapFile did not contain any usable character mappings." );
177 $db, self::INPLACE_MOVE,
'archive',
'ar_namespace',
'ar_title', [
'ar_timestamp',
'ar_id' ]
180 $db, self::INPLACE_MOVE,
'filearchive',
NS_FILE,
'fa_name', [
'fa_timestamp',
'fa_id' ]
183 $db, self::INPLACE_MOVE,
'logging',
'log_namespace',
'log_title', [
'log_id' ]
186 $db, self::INPLACE_MOVE,
'protected_titles',
'pt_namespace',
'pt_title', []
188 $this->processTable( $db, self::MOVE,
'page',
'page_namespace',
'page_title', [
'page_id' ] );
189 $this->processTable( $db, self::MOVE,
'image',
NS_FILE,
'img_name', [] );
190 $this->processTable( $db, self::MOVE,
'file',
NS_FILE,
'file_name', [
'file_id' ] );
192 $db, self::UPPERCASE,
'redirect',
'rd_namespace',
'rd_title', [
'rd_from' ]
194 $this->processUsers( $db );
204 private function getLikeBatches(
IReadableDatabase $db, $field, $batchSize = 100 ) {
207 foreach ( $this->charmap as $from => $to ) {
208 $likes[] = $db->
expr(
213 if ( count( $likes ) >= $batchSize ) {
214 $ret[] = $db->
orExpr( $likes );
219 $ret[] = $db->
orExpr( $likes );
232 private function getNamespaces() {
233 if ( $this->namespaces ===
null ) {
235 $this->namespaces = array_filter(
236 array_keys( $nsinfo->getCanonicalNamespaces() ),
237 static function ( $ns ) use ( $nsinfo ) {
238 return $nsinfo->isMovable( $ns ) && $nsinfo->isCapitalized( $ns );
241 usort( $this->namespaces,
static function ( $ns1, $ns2 ) use ( $nsinfo ) {
242 if ( $ns1 === $ns2 ) {
246 $s1 = $nsinfo->getSubject( $ns1 );
247 $s2 = $nsinfo->getSubject( $ns2 );
251 return $s1 < $s2 ? -1 : 1;
255 if ( $s1 === $ns1 ) {
258 if ( $s2 === $ns2 ) {
268 return $this->namespaces;
283 [ $base ] = explode(
'/', $title, 2 );
284 if ( !isset( $this->seenUsers[$base] ) ) {
287 ->select(
'user_id' )
289 ->where( [
'user_name' => strtr( $base,
'_',
' ' ) ] )
290 ->caller( __METHOD__ )->fetchField();
292 return $this->seenUsers[$base];
307 $munge =
'Target title\'s user exists';
310 $status = $mpFactory->newMovePage( $oldTitle, $newTitle )->isValidMove();
311 if ( !$status->isOK() && (
312 $status->hasMessage(
'articleexists' ) || $status->hasMessage(
'redirectexists' ) ) ) {
313 $munge =
'Target title exists';
320 if ( $this->prefix !==
null ) {
321 $newTitle = Title::makeTitle(
325 } elseif ( $this->suffix !==
null ) {
328 if ( $i !==
false ) {
329 $newTitle = Title::makeTitle(
331 substr( $dbkey, 0, $i ) . $this->suffix . substr( $dbkey, $i )
334 $newTitle = Title::makeTitle( $newTitle->
getNamespace(), $dbkey . $this->suffix );
338 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
339 .
"$munge and no --prefix or --suffix was given"
346 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
347 .
"$munge and munged title '{$newTitle->getPrefixedText()}' is not valid"
351 if ( $newTitle->
exists() ) {
353 "Cannot move {$oldTitle->getPrefixedText()} → $nt: "
354 .
"$munge and munged title '{$newTitle->getPrefixedText()}' also exists"
369 private function doMove(
IDatabase $db, $ns, $title ) {
370 $char = mb_substr( $title, 0, 1 );
371 if ( !array_key_exists( $char, $this->charmap ) ) {
373 "Query returned NS$ns $title, which does not begin with a character in the charmap."
378 if ( $this->isUserPage( $db, $ns, $title ) ) {
379 $this->
output(
"... Skipping user page NS$ns $title\n" );
383 $oldTitle = Title::makeTitle( $ns, $title );
384 $newTitle = Title::makeTitle( $ns, $this->charmap[$char] . mb_substr( $title, 1 ) );
385 $deletionReason = $this->shouldDelete( $db, $oldTitle, $newTitle );
386 if ( !$this->mungeTitle( $db, $oldTitle, $newTitle ) ) {
391 $mpFactory = $services->getMovePageFactory();
392 $movePage = $mpFactory->newMovePage( $oldTitle, $newTitle );
393 $status = $movePage->isValidMove();
394 if ( !$status->isOK() ) {
395 $this->
error(
"Invalid move {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}:" );
396 $this->
error( $status );
402 "Would rename {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}\n"
404 if ( $deletionReason ) {
406 "Would then delete {$newTitle->getPrefixedText()}: $deletionReason\n"
412 $status = $movePage->move( $this->user, $this->reason,
false, $this->tags );
413 if ( !$status->isOK() ) {
414 $this->
error(
"Move {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()} failed:" );
415 $this->
error( $status );
418 $this->
output(
"Renamed {$oldTitle->getPrefixedText()} → {$newTitle->getPrefixedText()}\n" );
422 ->update(
'logging' )
424 'log_title' => $this->charmap[$char] . mb_substr( $title, 1 ),
428 'log_title' => $oldTitle->
getDBkey(),
431 ->caller( __METHOD__ )
434 if ( $deletionReason !==
null ) {
435 $page = $services->getWikiPageFactory()->newFromTitle( $newTitle );
436 $delPage = $services->getDeletePageFactory()->newDeletePage( $page, $this->user );
438 ->forceImmediate(
true )
439 ->deleteUnsafe( $deletionReason );
440 if ( !$status->isOK() ) {
441 $this->
error(
"Deletion of {$newTitle->getPrefixedText()} failed:" );
442 $this->
error( $status );
445 $this->
output(
"Deleted {$newTitle->getPrefixedText()}\n" );
467 ->select( [
'ns' =>
'rd_namespace',
'title' =>
'rd_title' ] )
469 ->join(
'redirect',
null,
'rd_from = page_id' )
470 ->where( [
'page_namespace' => $oldTitle->
getNamespace(),
'page_title' => $oldTitle->
getDBkey() ] )
471 ->caller( __METHOD__ )->fetchRow();
478 $oldRow->title === $newTitle->
getDBkey()
480 return $this->reason .
", and found that [[{$oldTitle->getPrefixedText()}]] is "
481 .
"already a redirect to [[{$newTitle->getPrefixedText()}]]";
484 ->select( [
'ns' =>
'rd_namespace',
'title' =>
'rd_title' ] )
486 ->join(
'redirect',
null,
'rd_from = page_id' )
487 ->where( [
'page_namespace' => $newTitle->
getNamespace(),
'page_title' => $newTitle->
getDBkey() ] )
488 ->caller( __METHOD__ )->fetchRow();
489 if ( $newRow && $oldRow->ns === $newRow->ns && $oldRow->title === $newRow->title ) {
490 $nt = Title::makeTitle( $newRow->ns, $newRow->title );
491 return $this->reason .
", and found that [[{$oldTitle->getPrefixedText()}]] and "
492 .
"[[{$newTitle->getPrefixedText()}]] both redirect to [[{$nt->getPrefixedText()}]].";
511 private function doUpdate(
IDatabase $db, $op, $table, $nsField, $titleField, $row ) {
512 $ns = is_int( $nsField ) ? $nsField : (int)$row->$nsField;
513 $title = $row->$titleField;
515 $char = mb_substr( $title, 0, 1 );
516 if ( !array_key_exists( $char, $this->charmap ) ) {
517 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
519 "Query returned $r, but title does not begin with a character in the charmap."
524 $oldTitle = Title::makeTitle( $ns, $title );
525 $newTitle = Title::makeTitle( $ns, $this->charmap[$char] . mb_substr( $title, 1 ) );
526 if ( $op !== self::UPPERCASE && !$this->mungeTitle( $db, $oldTitle, $newTitle ) ) {
531 $db->newUpdateQueryBuilder()
534 is_int( $nsField ) ? [] : [ $nsField => $newTitle->getNamespace() ],
535 [ $titleField => $newTitle->getDBkey() ]
537 ->where( (array)$row )
538 ->caller( __METHOD__ )
540 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
541 $this->
output(
"Set $r to {$newTitle->getPrefixedText()}\n" );
543 $r = json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
544 $this->
output(
"Would set $r to {$newTitle->getPrefixedText()}\n" );
563 private function processTable(
IDatabase $db, $op, $table, $nsField, $titleField, $pkFields ) {
564 if ( $this->tables !==
null && !in_array( $table, $this->tables,
true ) ) {
565 $this->
output(
"Skipping table `$table`, not in --tables.\n" );
570 $namespaces = $this->getNamespaces();
571 $likes = $this->getLikeBatches( $db, $titleField );
573 if ( is_int( $nsField ) ) {
574 $namespaces = array_intersect( $namespaces, [ $nsField ] );
577 if ( !$namespaces ) {
578 $this->
output(
"Skipping table `$table`, no valid namespaces.\n" );
582 $this->
output(
"Processing table `$table`...\n" );
584 $selectFields = array_merge(
585 is_int( $nsField ) ? [] : [ $nsField ],
589 $contFields = array_merge( [ $titleField ], $pkFields );
591 $lastReplicationWait = 0.0;
594 foreach ( $namespaces as $ns ) {
595 foreach ( $likes as $like ) {
599 ->select( $selectFields )
601 ->where( [
"$nsField = $ns", $like, $cont ? $db->
buildComparison(
'>', $cont ) :
'1=1' ] )
602 ->orderBy( array_merge( [ $titleField ], $pkFields ) )
603 ->limit( $batchSize )
604 ->caller( __METHOD__ )->fetchResultSet();
608 foreach ( $res as $row ) {
610 foreach ( $contFields as $field ) {
611 $cont[ $field ] = $row->$field;
614 if ( $op === self::MOVE ) {
615 $ns = is_int( $nsField ) ? $nsField : (int)$row->$nsField;
616 $ret = $this->doMove( $db, $ns, $row->$titleField );
618 $ret = $this->doUpdate( $db, $op, $table, $nsField, $titleField, $row );
620 if ( $ret ===
true ) {
622 } elseif ( $ret ===
false ) {
630 $r = $cont ? json_encode( $row, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) :
'<end>';
631 $this->
output(
"... $table: $count renames, $errors errors at $r\n" );
637 $this->
output(
"Done processing table `$table`.\n" );
645 $userlistFile = $this->
getOption(
'userlist' );
646 if ( $userlistFile ===
null ) {
647 $this->
output(
"Not generating user list, --userlist was not specified.\n" );
651 $fh = fopen( $userlistFile,
'ab' );
653 $this->
error(
"Could not open user list file $userlistFile" );
657 $this->
output(
"Generating user list...\n" );
660 foreach ( $this->getLikeBatches( $db,
'user_name' ) as $like ) {
664 ->select( [
'user_id',
'user_name' ] )
668 ->orderBy(
'user_name' )
669 ->limit( $batchSize )
670 ->caller( __METHOD__ )->fetchResultSet();
672 if ( !$rows->numRows() ) {
676 foreach ( $rows as $row ) {
677 $char = mb_substr( $row->user_name, 0, 1 );
678 if ( !array_key_exists( $char, $this->charmap ) ) {
680 "Query returned $row->user_name, but user name does not " .
681 "begin with a character in the charmap."
685 $newName = $this->charmap[$char] . mb_substr( $row->user_name, 1 );
686 fprintf( $fh,
"%s\t%s\t%s\n", WikiMap::getCurrentWikiId(), $row->user_id, $newName );
688 $cont = [ $db->
expr(
'user_name',
'>', $row->user_name ) ];
691 $this->
output(
"... at $row->user_name, $count names so far\n" );
695 if ( !fclose( $fh ) ) {
696 $this->
error(
"fclose on $userlistFile failed" );
698 $this->
output(
"User list output to $userlistFile, $count users need renaming.\n" );