MediaWiki
1.40.4
ExternalUserNames.php
Go to the documentation of this file.
1
<?php
23
use
MediaWiki\MediaWikiServices
;
24
use
MediaWiki\Title\Title
;
25
use
MediaWiki\User\UserRigorOptions
;
26
31
class
ExternalUserNames
{
32
36
private
$usernamePrefix;
37
41
private
$assignKnownUsers;
42
46
private
$triedCreations = [];
47
52
public
function
__construct
( $usernamePrefix, $assignKnownUsers ) {
53
$this->usernamePrefix = rtrim( (
string
)$usernamePrefix,
':>'
);
54
$this->assignKnownUsers = (bool)$assignKnownUsers;
55
}
56
64
public
static
function
getUserLinkTitle
( $userName ) {
65
$pos = strpos( $userName,
'>'
);
66
$services = MediaWikiServices::getInstance();
67
if
( $pos !==
false
) {
68
$iw = explode(
':'
, substr( $userName, 0, $pos ) );
69
$firstIw = array_shift( $iw );
70
$interwikiLookup = $services->getInterwikiLookup();
71
if
( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
72
$title
= $services->getNamespaceInfo()->getCanonicalName(
NS_USER
) .
73
':'
. substr( $userName, $pos + 1 );
74
if
( $iw ) {
75
$title
= implode(
':'
, $iw ) .
':'
.
$title
;
76
}
77
return
Title::makeTitle(
NS_MAIN
,
$title
,
''
, $firstIw );
78
}
79
return
null
;
80
}
else
{
81
// Protect against invalid user names from old corrupt database rows, T232451
82
if
(
83
$services->getUserNameUtils()->isIP( $userName )
84
|| $services->getUserNameUtils()->isValidIPRange( $userName )
85
|| $services->getUserNameUtils()->isValid( $userName )
86
) {
87
return
SpecialPage::getTitleFor
(
'Contributions'
, $userName );
88
}
else
{
89
// Bad user name, no link
90
return
null
;
91
}
92
}
93
}
94
109
public
function
applyPrefix
( $name ) {
110
$userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
111
if
( $userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE ) ===
false
) {
112
return
$name;
113
}
114
115
if
( $this->assignKnownUsers ) {
116
$userIdentityLookup = MediaWikiServices::getInstance()->getUserIdentityLookup();
117
$userIdentity = $userIdentityLookup->getUserIdentityByName( $name );
118
if
( $userIdentity && $userIdentity->isRegistered() ) {
119
return
$name;
120
}
121
122
// See if any extension wants to create it.
123
if
( !isset( $this->triedCreations[$name] ) ) {
124
$this->triedCreations[$name] =
true
;
125
if
( !Hooks::runner()->onImportHandleUnknownUser( $name ) ) {
126
$userIdentity = $userIdentityLookup->getUserIdentityByName( $name, IDBAccessObject::READ_LATEST );
127
if
( $userIdentity && $userIdentity->isRegistered() ) {
128
return
$name;
129
}
130
}
131
}
132
}
133
134
return
$this->
addPrefix
( $name );
135
}
136
143
public
function
addPrefix
( $name ) {
144
return
substr( $this->usernamePrefix .
'>'
. $name, 0, 255 );
145
}
146
153
public
static
function
isExternal
( $username ) {
154
return
str_contains( $username,
'>'
);
155
}
156
163
public
static
function
getLocal
( $username ) {
164
if
( !self::isExternal( $username ) ) {
165
return
$username;
166
}
167
168
return
substr( $username, strpos( $username,
'>'
) + 1 );
169
}
170
171
}
NS_USER
const NS_USER
Definition
Defines.php:66
NS_MAIN
const NS_MAIN
Definition
Defines.php:64
ExternalUserNames
Class to parse and build external user names.
Definition
ExternalUserNames.php:31
ExternalUserNames\getUserLinkTitle
static getUserLinkTitle( $userName)
Get a target Title to link a username.
Definition
ExternalUserNames.php:64
ExternalUserNames\addPrefix
addPrefix( $name)
Add an interwiki prefix to the username regardless of circumstances.
Definition
ExternalUserNames.php:143
ExternalUserNames\__construct
__construct( $usernamePrefix, $assignKnownUsers)
Definition
ExternalUserNames.php:52
ExternalUserNames\isExternal
static isExternal( $username)
Tells whether the username is external or not.
Definition
ExternalUserNames.php:153
ExternalUserNames\getLocal
static getLocal( $username)
Get local part of the user name.
Definition
ExternalUserNames.php:163
ExternalUserNames\applyPrefix
applyPrefix( $name)
Add an interwiki prefix to the username, if appropriate.
Definition
ExternalUserNames.php:109
MediaWiki\MediaWikiServices
Service locator for MediaWiki core services.
Definition
MediaWikiServices.php:223
MediaWiki\Title\Title
Represents a title within MediaWiki.
Definition
Title.php:82
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:132
MediaWiki\User\UserRigorOptions
Shared interface for rigor levels when dealing with User methods.
Definition
UserRigorOptions.php:30
$title
$title
Definition
testCompression.php:39
includes
user
ExternalUserNames.php
Generated on Thu Jun 27 2024 15:52:33 for MediaWiki by
1.10.0