This is a bit more interoperable than trying to roll our own by using
Title::makeTitleSafe().
We still need to handle IP addresses separately, of course.
Bug: T194916
Change-Id: Ie3900d768cbe15aef079b97d91f7fd23dc7c3e26
- $titleObj = Title::makeTitleSafe( NS_USER, $value );
-
- if ( $titleObj ) {
- $value = $titleObj->getText();
+ $name = User::getCanonicalName( $value, 'valid' );
+ if ( $name !== false ) {
+ return $name;
- !User::isValidUserName( $value ) &&
// We allow ranges as well, for blocks.
// We allow ranges as well, for blocks.
- !IP::isIPAddress( $value ) &&
+ IP::isIPAddress( $value ) ||
// See comment for User::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.
// See comment for User::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.
'/^' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.xxx$/',
$value
)
) {
'/^' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.xxx$/',
$value
)
) {
- $this->dieWithError(
- [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $value ) ],
- "baduser_{$encParamName}"
- );
+ return IP::sanitizeIP( $value );
+ $this->dieWithError(
+ [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $value ) ],
+ "baduser_{$encParamName}"
+ );
+ 'User prefixed with "User:"' => [
+ 'User:foo_bar',
+ [ ApiBase::PARAM_TYPE => 'user' ],
+ 'Foo bar',
+ [],
+ ],
'Invalid username "|"' => [
'|',
[ ApiBase::PARAM_TYPE => 'user' ],
'Invalid username "|"' => [
'|',
[ ApiBase::PARAM_TYPE => 'user' ],