MediaWiki master
ExternalUserNames.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\User;
24
30
36
37 private string $usernamePrefix;
38 private bool $assignKnownUsers;
39
43 private $triedCreations = [];
44
49 public function __construct( $usernamePrefix, $assignKnownUsers ) {
50 $this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' );
51 $this->assignKnownUsers = (bool)$assignKnownUsers;
52 }
53
61 public static function getUserLinkTitle( $userName ) {
62 $pos = strpos( $userName, '>' );
64 if ( $pos !== false ) {
65 $iw = explode( ':', substr( $userName, 0, $pos ) );
66 $firstIw = array_shift( $iw );
67 $interwikiLookup = $services->getInterwikiLookup();
68 if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
69 $title = $services->getNamespaceInfo()->getCanonicalName( NS_USER ) .
70 ':' . substr( $userName, $pos + 1 );
71 if ( $iw ) {
72 $title = implode( ':', $iw ) . ':' . $title;
73 }
74 return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
75 }
76 return null;
77 } else {
78 // Protect against invalid user names from old corrupt database rows, T232451
79 if (
80 $services->getUserNameUtils()->isIP( $userName )
81 || $services->getUserNameUtils()->isValidIPRange( $userName )
82 || $services->getUserNameUtils()->isValid( $userName )
83 ) {
84 return SpecialPage::getTitleFor( 'Contributions', $userName );
85 } else {
86 // Bad user name, no link
87 return null;
88 }
89 }
90 }
91
106 public function applyPrefix( $name ) {
107 $services = MediaWikiServices::getInstance();
108 $userNameUtils = $services->getUserNameUtils();
109 if ( $userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE ) === false ) {
110 return $name;
111 }
112
113 if ( $this->assignKnownUsers ) {
114 $userIdentityLookup = $services->getUserIdentityLookup();
115 $userIdentity = $userIdentityLookup->getUserIdentityByName( $name );
116 if ( $userIdentity && $userIdentity->isRegistered() ) {
117 return $name;
118 }
119
120 // See if any extension wants to create it.
121 if ( !isset( $this->triedCreations[$name] ) ) {
122 $this->triedCreations[$name] = true;
123 if ( !( new HookRunner( $services->getHookContainer() ) )->onImportHandleUnknownUser( $name ) ) {
124 $userIdentity = $userIdentityLookup->getUserIdentityByName( $name, IDBAccessObject::READ_LATEST );
125 if ( $userIdentity && $userIdentity->isRegistered() ) {
126 return $name;
127 }
128 }
129 }
130 }
131
132 return $this->addPrefix( $name );
133 }
134
141 public function addPrefix( $name ) {
142 return substr( $this->usernamePrefix . '>' . $name, 0, 255 );
143 }
144
151 public static function isExternal( $username ) {
152 return str_contains( $username, '>' );
153 }
154
161 public static function getLocal( $username ) {
162 if ( !self::isExternal( $username ) ) {
163 return $username;
164 }
165
166 return substr( $username, strpos( $username, '>' ) + 1 );
167 }
168
169}
170
175class_alias( ExternalUserNames::class, 'ExternalUserNames' );
const NS_USER
Definition Defines.php:66
const NS_MAIN
Definition Defines.php:64
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Represents a title within MediaWiki.
Definition Title.php:78
Class to parse and build external user names.
__construct( $usernamePrefix, $assignKnownUsers)
applyPrefix( $name)
Add an interwiki prefix to the username, if appropriate.
addPrefix( $name)
Add an interwiki prefix to the username regardless of circumstances.
static getUserLinkTitle( $userName)
Get a target Title to link a username.
static getLocal( $username)
Get local part of the user name.
static isExternal( $username)
Tells whether the username is external or not.
Interface for database access objects.
Utility class for bot passwords.