MediaWiki master
ExternalUserNames.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\User;
22
28
34
35 private string $usernamePrefix;
36 private bool $assignKnownUsers;
37
41 private $triedCreations = [];
42
47 public function __construct( $usernamePrefix, $assignKnownUsers ) {
48 $this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' );
49 $this->assignKnownUsers = (bool)$assignKnownUsers;
50 }
51
59 public static function getUserLinkTitle( $userName ) {
60 $pos = strpos( $userName, '>' );
62 if ( $pos !== false ) {
63 $iw = explode( ':', substr( $userName, 0, $pos ) );
64 $firstIw = array_shift( $iw );
65 $interwikiLookup = $services->getInterwikiLookup();
66 if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
67 $title = $services->getNamespaceInfo()->getCanonicalName( NS_USER ) .
68 ':' . substr( $userName, $pos + 1 );
69 if ( $iw ) {
70 $title = implode( ':', $iw ) . ':' . $title;
71 }
72 return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
73 }
74 return null;
75 } else {
76 // Protect against invalid user names from old corrupt database rows, T232451
77 if (
78 $services->getUserNameUtils()->isIP( $userName )
79 || $services->getUserNameUtils()->isValidIPRange( $userName )
80 || $services->getUserNameUtils()->isValid( $userName )
81 ) {
82 return SpecialPage::getTitleFor( 'Contributions', $userName );
83 } else {
84 // Bad user name, no link
85 return null;
86 }
87 }
88 }
89
104 public function applyPrefix( $name ) {
105 $services = MediaWikiServices::getInstance();
106 $userNameUtils = $services->getUserNameUtils();
107 if ( $userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE ) === false ) {
108 return $name;
109 }
110
111 if ( $this->assignKnownUsers ) {
112 $userIdentityLookup = $services->getUserIdentityLookup();
113 $userIdentity = $userIdentityLookup->getUserIdentityByName( $name );
114 if ( $userIdentity && $userIdentity->isRegistered() ) {
115 return $name;
116 }
117
118 // See if any extension wants to create it.
119 if ( !isset( $this->triedCreations[$name] ) ) {
120 $this->triedCreations[$name] = true;
121 if ( !( new HookRunner( $services->getHookContainer() ) )->onImportHandleUnknownUser( $name ) ) {
122 $userIdentity = $userIdentityLookup->getUserIdentityByName( $name, IDBAccessObject::READ_LATEST );
123 if ( $userIdentity && $userIdentity->isRegistered() ) {
124 return $name;
125 }
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 str_contains( $username, '>' );
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}
168
170class_alias( ExternalUserNames::class, 'ExternalUserNames' );
const NS_USER
Definition Defines.php:67
const NS_MAIN
Definition Defines.php:65
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
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.
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:78
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.
const RIGOR_USABLE
Check that a user name is valid for batch processes and login.
Interface for database access objects.