MediaWiki
master
Toggle main menu visibility
ExternalUserNames.php
Go to the documentation of this file.
1
<?php
6
7
namespace
MediaWiki\User;
8
9
use MediaWiki\HookContainer\HookRunner;
10
use MediaWiki\MediaWikiServices;
11
use MediaWiki\SpecialPage\SpecialPage;
12
use MediaWiki\Title\Title;
13
use Wikimedia\Rdbms\IDBAccessObject;
14
19
class
ExternalUserNames
{
20
21
private
readonly
string
$usernamePrefix;
22
26
private
$triedCreations = [];
27
32
public
function
__construct
(
33
string
$usernamePrefix,
34
private
readonly
bool
$assignKnownUsers,
35
) {
36
$this->usernamePrefix = rtrim( $usernamePrefix,
':>'
);
37
}
38
46
public
static
function
getUserLinkTitle
( $userName ) {
47
$pos = strpos( $userName,
'>'
);
48
$services =
MediaWikiServices::getInstance
();
49
if
( $pos !==
false
) {
50
$iw = explode(
':'
, substr( $userName, 0, $pos ) );
51
$firstIw = array_shift( $iw );
52
$interwikiLookup = $services->getInterwikiLookup();
53
if
( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
54
$title = $services->getNamespaceInfo()->getCanonicalName(
NS_USER
) .
55
':'
. substr( $userName, $pos + 1 );
56
if
( $iw ) {
57
$title = implode(
':'
, $iw ) .
':'
. $title;
58
}
59
return
Title::makeTitle
(
NS_MAIN
, $title,
''
, $firstIw );
60
}
61
return
null
;
62
}
else
{
63
// Protect against invalid user names from old corrupt database rows, T232451
64
if
(
65
$services->getUserNameUtils()->isIP( $userName )
66
|| $services->getUserNameUtils()->isValidIPRange( $userName )
67
|| $services->getUserNameUtils()->isValid( $userName )
68
) {
69
return
SpecialPage::getTitleFor
(
'Contributions'
, $userName );
70
}
else
{
71
// Bad user name, no link
72
return
null
;
73
}
74
}
75
}
76
91
public
function
applyPrefix
( $name ) {
92
$services =
MediaWikiServices::getInstance
();
93
$userNameUtils = $services->getUserNameUtils();
94
if
( $userNameUtils->getCanonical( $name,
UserRigorOptions::RIGOR_USABLE
) ===
false
) {
95
return
$name;
96
}
97
98
if
( $this->assignKnownUsers ) {
99
$userIdentityLookup = $services->getUserIdentityLookup();
100
$userIdentity = $userIdentityLookup->getUserIdentityByName( $name );
101
if
( $userIdentity && $userIdentity->isRegistered() ) {
102
return
$name;
103
}
104
105
// See if any extension wants to create it.
106
if
( !isset( $this->triedCreations[$name] ) ) {
107
$this->triedCreations[$name] =
true
;
108
if
( !(
new
HookRunner
( $services->getHookContainer() ) )->onImportHandleUnknownUser( $name ) ) {
109
$userIdentity = $userIdentityLookup->getUserIdentityByName( $name, IDBAccessObject::READ_LATEST );
110
if
( $userIdentity && $userIdentity->isRegistered() ) {
111
return
$name;
112
}
113
}
114
}
115
}
116
117
return
$this->
addPrefix
( $name );
118
}
119
126
public
function
addPrefix
( $name ) {
127
return
substr( $this->usernamePrefix .
'>'
. $name, 0, 255 );
128
}
129
136
public
static
function
isExternal
( $username ) {
137
return
str_contains( $username,
'>'
);
138
}
139
146
public
static
function
getLocal
( $username ) {
147
if
( !self::isExternal( $username ) ) {
148
return
$username;
149
}
150
151
return
substr( $username, strpos( $username,
'>'
) + 1 );
152
}
153
161
public
static
function
getPrefix
( $username ) {
162
if
( !self::isExternal( $username ) ) {
163
return
false
;
164
}
165
166
return
substr( $username, 0, strpos( $username,
'>'
) );
167
}
168
169
}
170
172
class_alias( ExternalUserNames::class,
'ExternalUserNames'
);
NS_USER
const NS_USER
Definition
Defines.php:53
NS_MAIN
const NS_MAIN
Definition
Defines.php:51
MediaWiki\Deferred\LinksUpdate\GenericPageLinksTable\makeTitle
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
Definition
GenericPageLinksTable.php:129
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition
HookRunner.php:601
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition
MediaWikiServices.php:361
MediaWiki\SpecialPage\SpecialPage\getTitleFor
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,...
Definition
SpecialPage.php:129
MediaWiki\User\ExternalUserNames
Class to parse and build external user names.
Definition
ExternalUserNames.php:19
MediaWiki\User\ExternalUserNames\applyPrefix
applyPrefix( $name)
Add an interwiki prefix to the username, if appropriate.
Definition
ExternalUserNames.php:91
MediaWiki\User\ExternalUserNames\addPrefix
addPrefix( $name)
Add an interwiki prefix to the username regardless of circumstances.
Definition
ExternalUserNames.php:126
MediaWiki\User\ExternalUserNames\__construct
__construct(string $usernamePrefix, private readonly bool $assignKnownUsers,)
Definition
ExternalUserNames.php:32
MediaWiki\User\ExternalUserNames\getUserLinkTitle
static getUserLinkTitle( $userName)
Get a target Title to link a username.
Definition
ExternalUserNames.php:46
MediaWiki\User\ExternalUserNames\getPrefix
static getPrefix( $username)
Get the prefix of an external username.
Definition
ExternalUserNames.php:161
MediaWiki\User\ExternalUserNames\getLocal
static getLocal( $username)
Get local part of the user name.
Definition
ExternalUserNames.php:146
MediaWiki\User\ExternalUserNames\isExternal
static isExternal( $username)
Tells whether the username is external or not.
Definition
ExternalUserNames.php:136
MediaWiki\User\UserRigorOptions\RIGOR_USABLE
const RIGOR_USABLE
Check that a user name is valid for batch processes and login.
Definition
UserRigorOptions.php:27
includes
User
ExternalUserNames.php
Generated on
for MediaWiki by
1.18.0