MediaWiki REL1_39
ExternalUserNames.php
Go to the documentation of this file.
1<?php
25
31
35 private $usernamePrefix;
36
40 private $assignKnownUsers;
41
45 private $triedCreations = [];
46
51 public function __construct( $usernamePrefix, $assignKnownUsers ) {
52 $this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' );
53 $this->assignKnownUsers = (bool)$assignKnownUsers;
54 }
55
63 public static function getUserLinkTitle( $userName ) {
64 $pos = strpos( $userName, '>' );
65 $services = MediaWikiServices::getInstance();
66 if ( $pos !== false ) {
67 $iw = explode( ':', substr( $userName, 0, $pos ) );
68 $firstIw = array_shift( $iw );
69 $interwikiLookup = $services->getInterwikiLookup();
70 if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
71 $title = $services->getNamespaceInfo()->getCanonicalName( NS_USER ) .
72 ':' . substr( $userName, $pos + 1 );
73 if ( $iw ) {
74 $title = implode( ':', $iw ) . ':' . $title;
75 }
76 return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
77 }
78 return null;
79 } else {
80 // Protect against invalid user names from old corrupt database rows, T232451
81 if (
82 $services->getUserNameUtils()->isIP( $userName )
83 || $services->getUserNameUtils()->isValidIPRange( $userName )
84 || $services->getUserNameUtils()->isValid( $userName )
85 ) {
86 return SpecialPage::getTitleFor( 'Contributions', $userName );
87 } else {
88 // Bad user name, no link
89 return null;
90 }
91 }
92 }
93
108 public function applyPrefix( $name ) {
109 $userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
110 if ( $userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE ) === false ) {
111 return $name;
112 }
113
114 if ( $this->assignKnownUsers ) {
115 if ( User::idFromName( $name ) ) {
116 return $name;
117 }
118
119 // See if any extension wants to create it.
120 if ( !isset( $this->triedCreations[$name] ) ) {
121 $this->triedCreations[$name] = true;
122 if ( !Hooks::runner()->onImportHandleUnknownUser( $name ) &&
123 User::idFromName( $name, User::READ_LATEST )
124 ) {
125 return $name;
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 strpos( $username, '>' ) !== false;
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}
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.
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,...
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition User.php:936
Shared interface for rigor levels when dealing with User methods.