MediaWiki master
ExternalUserNames.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\User;
8
14
20
21 private string $usernamePrefix;
22 private bool $assignKnownUsers;
23
27 private $triedCreations = [];
28
33 public function __construct( $usernamePrefix, $assignKnownUsers ) {
34 $this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' );
35 $this->assignKnownUsers = (bool)$assignKnownUsers;
36 }
37
45 public static function getUserLinkTitle( $userName ) {
46 $pos = strpos( $userName, '>' );
48 if ( $pos !== false ) {
49 $iw = explode( ':', substr( $userName, 0, $pos ) );
50 $firstIw = array_shift( $iw );
51 $interwikiLookup = $services->getInterwikiLookup();
52 if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
53 $title = $services->getNamespaceInfo()->getCanonicalName( NS_USER ) .
54 ':' . substr( $userName, $pos + 1 );
55 if ( $iw ) {
56 $title = implode( ':', $iw ) . ':' . $title;
57 }
58 return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
59 }
60 return null;
61 } else {
62 // Protect against invalid user names from old corrupt database rows, T232451
63 if (
64 $services->getUserNameUtils()->isIP( $userName )
65 || $services->getUserNameUtils()->isValidIPRange( $userName )
66 || $services->getUserNameUtils()->isValid( $userName )
67 ) {
68 return SpecialPage::getTitleFor( 'Contributions', $userName );
69 } else {
70 // Bad user name, no link
71 return null;
72 }
73 }
74 }
75
90 public function applyPrefix( $name ) {
92 $userNameUtils = $services->getUserNameUtils();
93 if ( $userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE ) === false ) {
94 return $name;
95 }
96
97 if ( $this->assignKnownUsers ) {
98 $userIdentityLookup = $services->getUserIdentityLookup();
99 $userIdentity = $userIdentityLookup->getUserIdentityByName( $name );
100 if ( $userIdentity && $userIdentity->isRegistered() ) {
101 return $name;
102 }
103
104 // See if any extension wants to create it.
105 if ( !isset( $this->triedCreations[$name] ) ) {
106 $this->triedCreations[$name] = true;
107 if ( !( new HookRunner( $services->getHookContainer() ) )->onImportHandleUnknownUser( $name ) ) {
108 $userIdentity = $userIdentityLookup->getUserIdentityByName( $name, IDBAccessObject::READ_LATEST );
109 if ( $userIdentity && $userIdentity->isRegistered() ) {
110 return $name;
111 }
112 }
113 }
114 }
115
116 return $this->addPrefix( $name );
117 }
118
125 public function addPrefix( $name ) {
126 return substr( $this->usernamePrefix . '>' . $name, 0, 255 );
127 }
128
135 public static function isExternal( $username ) {
136 return str_contains( $username, '>' );
137 }
138
145 public static function getLocal( $username ) {
146 if ( !self::isExternal( $username ) ) {
147 return $username;
148 }
149
150 return substr( $username, strpos( $username, '>' ) + 1 );
151 }
152
153}
154
156class_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.
__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.