MediaWiki master
ExternalUserNames.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\User;
8
14
20
21 private readonly string $usernamePrefix;
22
26 private $triedCreations = [];
27
32 public function __construct(
33 string $usernamePrefix,
34 private readonly bool $assignKnownUsers,
35 ) {
36 $this->usernamePrefix = rtrim( $usernamePrefix, ':>' );
37 }
38
46 public static function getUserLinkTitle( $userName ) {
47 $pos = strpos( $userName, '>' );
49 if ( $pos !== false ) {
50 $iw = explode( ':', substr( $userName, 0, $pos ) );
51 $firstIw = array_shift( $iw );
52 $interwikiLookup = $services->getInterwikiLookup();
53 if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
54 $title = $services->getNamespaceInfo()->getCanonicalName( NS_USER ) .
55 ':' . substr( $userName, $pos + 1 );
56 if ( $iw ) {
57 $title = implode( ':', $iw ) . ':' . $title;
58 }
59 return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
60 }
61 return null;
62 } else {
63 // Protect against invalid user names from old corrupt database rows, T232451
64 if (
65 $services->getUserNameUtils()->isIP( $userName )
66 || $services->getUserNameUtils()->isValidIPRange( $userName )
67 || $services->getUserNameUtils()->isValid( $userName )
68 ) {
69 return SpecialPage::getTitleFor( 'Contributions', $userName );
70 } else {
71 // Bad user name, no link
72 return null;
73 }
74 }
75 }
76
91 public function applyPrefix( $name ) {
93 $userNameUtils = $services->getUserNameUtils();
94 if ( $userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE ) === false ) {
95 return $name;
96 }
97
98 if ( $this->assignKnownUsers ) {
99 $userIdentityLookup = $services->getUserIdentityLookup();
100 $userIdentity = $userIdentityLookup->getUserIdentityByName( $name );
101 if ( $userIdentity && $userIdentity->isRegistered() ) {
102 return $name;
103 }
104
105 // See if any extension wants to create it.
106 if ( !isset( $this->triedCreations[$name] ) ) {
107 $this->triedCreations[$name] = true;
108 if ( !( new HookRunner( $services->getHookContainer() ) )->onImportHandleUnknownUser( $name ) ) {
109 $userIdentity = $userIdentityLookup->getUserIdentityByName( $name, IDBAccessObject::READ_LATEST );
110 if ( $userIdentity && $userIdentity->isRegistered() ) {
111 return $name;
112 }
113 }
114 }
115 }
116
117 return $this->addPrefix( $name );
118 }
119
126 public function addPrefix( $name ) {
127 return substr( $this->usernamePrefix . '>' . $name, 0, 255 );
128 }
129
136 public static function isExternal( $username ) {
137 return str_contains( $username, '>' );
138 }
139
146 public static function getLocal( $username ) {
147 if ( !self::isExternal( $username ) ) {
148 return $username;
149 }
150
151 return substr( $username, strpos( $username, '>' ) + 1 );
152 }
153
161 public static function getPrefix( $username ) {
162 if ( !self::isExternal( $username ) ) {
163 return false;
164 }
165
166 return substr( $username, 0, strpos( $username, '>' ) );
167 }
168
169}
170
172class_alias( ExternalUserNames::class, 'ExternalUserNames' );
const NS_USER
Definition Defines.php:53
const NS_MAIN
Definition Defines.php:51
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
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:69
Class to parse and build external user names.
applyPrefix( $name)
Add an interwiki prefix to the username, if appropriate.
addPrefix( $name)
Add an interwiki prefix to the username regardless of circumstances.
__construct(string $usernamePrefix, private readonly bool $assignKnownUsers,)
static getUserLinkTitle( $userName)
Get a target Title to link a username.
static getPrefix( $username)
Get the prefix of an external 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.