MediaWiki
REL1_39
ExternalUserNames.php
Go to the documentation of this file.
1
<?php
23
use
MediaWiki\MediaWikiServices
;
24
use
MediaWiki\User\UserRigorOptions
;
25
30
class
ExternalUserNames
{
31
35
private
$usernamePrefix;
36
40
private
$assignKnownUsers;
41
45
private
$triedCreations = [];
46
51
public
function
__construct
( $usernamePrefix, $assignKnownUsers ) {
52
$this->usernamePrefix = rtrim( (
string
)$usernamePrefix,
':>'
);
53
$this->assignKnownUsers = (bool)$assignKnownUsers;
54
}
55
63
public
static
function
getUserLinkTitle
( $userName ) {
64
$pos = strpos( $userName,
'>'
);
65
$services = MediaWikiServices::getInstance();
66
if
( $pos !==
false
) {
67
$iw = explode(
':'
, substr( $userName, 0, $pos ) );
68
$firstIw = array_shift( $iw );
69
$interwikiLookup = $services->getInterwikiLookup();
70
if
( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
71
$title
= $services->getNamespaceInfo()->getCanonicalName(
NS_USER
) .
72
':'
. substr( $userName, $pos + 1 );
73
if
( $iw ) {
74
$title
= implode(
':'
, $iw ) .
':'
.
$title
;
75
}
76
return
Title::makeTitle(
NS_MAIN
,
$title
,
''
, $firstIw );
77
}
78
return
null
;
79
}
else
{
80
// Protect against invalid user names from old corrupt database rows, T232451
81
if
(
82
$services->getUserNameUtils()->isIP( $userName )
83
|| $services->getUserNameUtils()->isValidIPRange( $userName )
84
|| $services->getUserNameUtils()->isValid( $userName )
85
) {
86
return
SpecialPage::getTitleFor
(
'Contributions'
, $userName );
87
}
else
{
88
// Bad user name, no link
89
return
null
;
90
}
91
}
92
}
93
108
public
function
applyPrefix
( $name ) {
109
$userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
110
if
( $userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE ) ===
false
) {
111
return
$name;
112
}
113
114
if
( $this->assignKnownUsers ) {
115
if
(
User::idFromName
( $name ) ) {
116
return
$name;
117
}
118
119
// See if any extension wants to create it.
120
if
( !isset( $this->triedCreations[$name] ) ) {
121
$this->triedCreations[$name] =
true
;
122
if
( !Hooks::runner()->onImportHandleUnknownUser( $name ) &&
123
User::idFromName
( $name, User::READ_LATEST )
124
) {
125
return
$name;
126
}
127
}
128
}
129
130
return
$this->
addPrefix
( $name );
131
}
132
139
public
function
addPrefix
( $name ) {
140
return
substr( $this->usernamePrefix .
'>'
. $name, 0, 255 );
141
}
142
149
public
static
function
isExternal
( $username ) {
150
return
strpos( $username,
'>'
) !==
false
;
151
}
152
159
public
static
function
getLocal
( $username ) {
160
if
( !self::isExternal( $username ) ) {
161
return
$username;
162
}
163
164
return
substr( $username, strpos( $username,
'>'
) + 1 );
165
}
166
167
}
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:30
ExternalUserNames\getUserLinkTitle
static getUserLinkTitle( $userName)
Get a target Title to link a username.
Definition
ExternalUserNames.php:63
ExternalUserNames\addPrefix
addPrefix( $name)
Add an interwiki prefix to the username regardless of circumstances.
Definition
ExternalUserNames.php:139
ExternalUserNames\__construct
__construct( $usernamePrefix, $assignKnownUsers)
Definition
ExternalUserNames.php:51
ExternalUserNames\isExternal
static isExternal( $username)
Tells whether the username is external or not.
Definition
ExternalUserNames.php:149
ExternalUserNames\getLocal
static getLocal( $username)
Get local part of the user name.
Definition
ExternalUserNames.php:159
ExternalUserNames\applyPrefix
applyPrefix( $name)
Add an interwiki prefix to the username, if appropriate.
Definition
ExternalUserNames.php:108
MediaWiki\MediaWikiServices
Service locator for MediaWiki core services.
Definition
MediaWikiServices.php:212
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
User\idFromName
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition
User.php:936
MediaWiki\User\UserRigorOptions
Shared interface for rigor levels when dealing with User methods.
Definition
UserRigorOptions.php:30
$title
$title
Definition
testCompression.php:38
includes
user
ExternalUserNames.php
Generated on Fri Dec 20 2024 15:35:38 for MediaWiki by
1.10.0