MediaWiki  master
ExternalUserNames.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\User;
24 
25 use IDBAccessObject;
30 
36 
40  private $usernamePrefix;
41 
45  private $assignKnownUsers;
46 
50  private $triedCreations = [];
51 
56  public function __construct( $usernamePrefix, $assignKnownUsers ) {
57  $this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' );
58  $this->assignKnownUsers = (bool)$assignKnownUsers;
59  }
60 
68  public static function getUserLinkTitle( $userName ) {
69  $pos = strpos( $userName, '>' );
70  $services = MediaWikiServices::getInstance();
71  if ( $pos !== false ) {
72  $iw = explode( ':', substr( $userName, 0, $pos ) );
73  $firstIw = array_shift( $iw );
74  $interwikiLookup = $services->getInterwikiLookup();
75  if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
76  $title = $services->getNamespaceInfo()->getCanonicalName( NS_USER ) .
77  ':' . substr( $userName, $pos + 1 );
78  if ( $iw ) {
79  $title = implode( ':', $iw ) . ':' . $title;
80  }
81  return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
82  }
83  return null;
84  } else {
85  // Protect against invalid user names from old corrupt database rows, T232451
86  if (
87  $services->getUserNameUtils()->isIP( $userName )
88  || $services->getUserNameUtils()->isValidIPRange( $userName )
89  || $services->getUserNameUtils()->isValid( $userName )
90  ) {
91  return SpecialPage::getTitleFor( 'Contributions', $userName );
92  } else {
93  // Bad user name, no link
94  return null;
95  }
96  }
97  }
98 
113  public function applyPrefix( $name ) {
114  $services = MediaWikiServices::getInstance();
115  $userNameUtils = $services->getUserNameUtils();
116  if ( $userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE ) === false ) {
117  return $name;
118  }
119 
120  if ( $this->assignKnownUsers ) {
121  $userIdentityLookup = $services->getUserIdentityLookup();
122  $userIdentity = $userIdentityLookup->getUserIdentityByName( $name );
123  if ( $userIdentity && $userIdentity->isRegistered() ) {
124  return $name;
125  }
126 
127  // See if any extension wants to create it.
128  if ( !isset( $this->triedCreations[$name] ) ) {
129  $this->triedCreations[$name] = true;
130  if ( !( new HookRunner( $services->getHookContainer() ) )->onImportHandleUnknownUser( $name ) ) {
131  $userIdentity = $userIdentityLookup->getUserIdentityByName( $name, IDBAccessObject::READ_LATEST );
132  if ( $userIdentity && $userIdentity->isRegistered() ) {
133  return $name;
134  }
135  }
136  }
137  }
138 
139  return $this->addPrefix( $name );
140  }
141 
148  public function addPrefix( $name ) {
149  return substr( $this->usernamePrefix . '>' . $name, 0, 255 );
150  }
151 
158  public static function isExternal( $username ) {
159  return str_contains( $username, '>' );
160  }
161 
168  public static function getLocal( $username ) {
169  if ( !self::isExternal( $username ) ) {
170  return $username;
171  }
172 
173  return substr( $username, strpos( $username, '>' ) + 1 );
174  }
175 
176 }
177 
182 class_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...
Definition: HookRunner.php:568
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.
Definition: SpecialPage.php:65
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:76
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:624
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.
Definition: ActorCache.php:21