25 use InvalidArgumentException;
31 use Psr\Log\LoggerInterface;
33 use Wikimedia\IPUtils;
50 'InvalidUsernameCharacters'
114 $this->hookRunner =
new HookRunner( $hookContainer );
128 public function isValid(
string $name ) : bool {
130 || $this->
isIP( $name )
131 || strpos( $name,
'/' ) !==
false
132 || strlen( $name ) > $this->options->get(
'MaxNameChars' )
133 || $name !== $this->contentLang->ucfirst( $name )
141 $title = $this->titleParser->parseTitle( $name );
148 || strcmp( $name,
$title->getText() )
155 $unicodeBlacklist =
'/[' .
156 '\x{0080}-\x{009f}' . # iso-8859-1 control chars
157 '\x{00a0}' . # non-breaking space
158 '\x{2000}-\x{200f}' . # various whitespace
159 '\x{2028}-\x{202f}' . # breaks and control chars
160 '\x{3000}' . # ideographic space
161 '\x{e000}-\x{f8ff}' . #
private use
163 if ( preg_match( $unicodeBlacklist, $name ) ) {
183 if ( !$this->
isValid( $name ) ) {
187 if ( !$this->reservedUsernames ) {
194 foreach ( $this->reservedUsernames as $reserved ) {
195 if ( substr( $reserved, 0, 4 ) ===
'msg:' ) {
196 $reserved = $this->textFormatter->format(
200 if ( $reserved === $name ) {
223 if ( strlen( $name ) > 235 ) {
224 $this->logger->debug(
225 __METHOD__ .
": '$name' uncreatable due to length"
230 $invalid = $this->options->get(
'InvalidUsernameCharacters' );
232 if ( $invalid !==
'' &&
233 preg_match(
'/[' . preg_quote( $invalid,
'/' ) .
']/', $name )
235 $this->logger->debug(
236 __METHOD__ .
": '$name' uncreatable due to wgInvalidUsernameCharacters"
258 public function getCanonical(
string $name,
string $validate = self::RIGOR_VALID ) {
260 $name = $this->contentLang->ucfirst( $name );
265 if ( strpos( $name,
'#' ) !==
false ) {
271 if ( $validate === self::RIGOR_NONE ) {
272 $name = strtr( $name,
'_',
' ' );
292 $name =
$title->getText();
295 switch ( $validate ) {
296 case self::RIGOR_VALID:
297 if ( !$this->
isValid( $name ) ) {
301 case self::RIGOR_USABLE:
306 case self::RIGOR_CREATABLE:
312 throw new InvalidArgumentException(
313 "Invalid parameter value for validation ($validate) in " .
337 public function isIP(
string $name ) : bool {
338 $anyIPv4 =
'/^\d{1,3}\.\d{1,3}\.\d{1,3}\.(?:xxx|\d{1,3})$/';
339 $validIP = IPUtils::isValid( $name );
340 return $validIP || preg_match( $anyIPv4, $name );
350 return IPUtils::isValidRange( $range );