MediaWiki 1.40.4
ExternalUserNames.php
Go to the documentation of this file.
1<?php
26
32
36 private $usernamePrefix;
37
41 private $assignKnownUsers;
42
46 private $triedCreations = [];
47
52 public function __construct( $usernamePrefix, $assignKnownUsers ) {
53 $this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' );
54 $this->assignKnownUsers = (bool)$assignKnownUsers;
55 }
56
64 public static function getUserLinkTitle( $userName ) {
65 $pos = strpos( $userName, '>' );
66 $services = MediaWikiServices::getInstance();
67 if ( $pos !== false ) {
68 $iw = explode( ':', substr( $userName, 0, $pos ) );
69 $firstIw = array_shift( $iw );
70 $interwikiLookup = $services->getInterwikiLookup();
71 if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
72 $title = $services->getNamespaceInfo()->getCanonicalName( NS_USER ) .
73 ':' . substr( $userName, $pos + 1 );
74 if ( $iw ) {
75 $title = implode( ':', $iw ) . ':' . $title;
76 }
77 return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
78 }
79 return null;
80 } else {
81 // Protect against invalid user names from old corrupt database rows, T232451
82 if (
83 $services->getUserNameUtils()->isIP( $userName )
84 || $services->getUserNameUtils()->isValidIPRange( $userName )
85 || $services->getUserNameUtils()->isValid( $userName )
86 ) {
87 return SpecialPage::getTitleFor( 'Contributions', $userName );
88 } else {
89 // Bad user name, no link
90 return null;
91 }
92 }
93 }
94
109 public function applyPrefix( $name ) {
110 $userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
111 if ( $userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE ) === false ) {
112 return $name;
113 }
114
115 if ( $this->assignKnownUsers ) {
116 $userIdentityLookup = MediaWikiServices::getInstance()->getUserIdentityLookup();
117 $userIdentity = $userIdentityLookup->getUserIdentityByName( $name );
118 if ( $userIdentity && $userIdentity->isRegistered() ) {
119 return $name;
120 }
121
122 // See if any extension wants to create it.
123 if ( !isset( $this->triedCreations[$name] ) ) {
124 $this->triedCreations[$name] = true;
125 if ( !Hooks::runner()->onImportHandleUnknownUser( $name ) ) {
126 $userIdentity = $userIdentityLookup->getUserIdentityByName( $name, IDBAccessObject::READ_LATEST );
127 if ( $userIdentity && $userIdentity->isRegistered() ) {
128 return $name;
129 }
130 }
131 }
132 }
133
134 return $this->addPrefix( $name );
135 }
136
143 public function addPrefix( $name ) {
144 return substr( $this->usernamePrefix . '>' . $name, 0, 255 );
145 }
146
153 public static function isExternal( $username ) {
154 return str_contains( $username, '>' );
155 }
156
163 public static function getLocal( $username ) {
164 if ( !self::isExternal( $username ) ) {
165 return $username;
166 }
167
168 return substr( $username, strpos( $username, '>' ) + 1 );
169 }
170
171}
const NS_USER
Definition Defines.php:66
const NS_MAIN
Definition Defines.php:64
Class to parse and build external user names.
static getUserLinkTitle( $userName)
Get a target Title to link a username.
addPrefix( $name)
Add an interwiki prefix to the username regardless of circumstances.
__construct( $usernamePrefix, $assignKnownUsers)
static isExternal( $username)
Tells whether the username is external or not.
static getLocal( $username)
Get local part of the user name.
applyPrefix( $name)
Add an interwiki prefix to the username, if appropriate.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:82
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,...
Shared interface for rigor levels when dealing with User methods.