MediaWiki  master
ExternalUserNames.php
Go to the documentation of this file.
1 <?php
25 
31 
35  private $usernamePrefix;
36 
41 
45  private $triedCreations = [];
46 
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, UserNameUtils::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 }
ExternalUserNames\getLocal
static getLocal( $username)
Get local part of the user name.
Definition: ExternalUserNames.php:159
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
ExternalUserNames
Class to parse and build external user names.
Definition: ExternalUserNames.php:30
ExternalUserNames\addPrefix
addPrefix( $name)
Add an interwiki prefix to the username regardless of circumstances.
Definition: ExternalUserNames.php:139
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:107
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:651
ExternalUserNames\__construct
__construct( $usernamePrefix, $assignKnownUsers)
Definition: ExternalUserNames.php:51
ExternalUserNames\getUserLinkTitle
static getUserLinkTitle( $userName)
Get a target Title to link a username.
Definition: ExternalUserNames.php:63
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
ExternalUserNames\applyPrefix
applyPrefix( $name)
Add an interwiki prefix to the username, if appropriate.
Definition: ExternalUserNames.php:108
NS_USER
const NS_USER
Definition: Defines.php:66
ExternalUserNames\$assignKnownUsers
bool $assignKnownUsers
Definition: ExternalUserNames.php:40
User\idFromName
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition: User.php:937
MediaWiki\User\UserNameUtils
UserNameUtils service.
Definition: UserNameUtils.php:42
ExternalUserNames\$triedCreations
bool[] $triedCreations
Definition: ExternalUserNames.php:45
ExternalUserNames\$usernamePrefix
string $usernamePrefix
Definition: ExternalUserNames.php:35
ExternalUserNames\isExternal
static isExternal( $username)
Tells whether the username is external or not.
Definition: ExternalUserNames.php:149