| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\User; |
| 24 |
|
| 25 |
use MediaWiki\HookContainer\HookRunner; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use MediaWiki\SpecialPage\SpecialPage; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class ExternalUserNames { |
| 36 |
|
| 37 |
private string $usernamePrefix; |
| 38 |
private bool $assignKnownUsers; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private $triedCreations = []; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function __construct( $usernamePrefix, $assignKnownUsers ) { |
| 50 |
$this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' ); |
| 51 |
$this->assignKnownUsers = (bool)$assignKnownUsers; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static function getUserLinkTitle( $userName ) { |
| 62 |
$pos = strpos( $userName, '>' ); |
| 63 |
$services = MediaWikiServices::getInstance(); |
| 64 |
if ( $pos !== false ) { |
| 65 |
$iw = explode( ':', substr( $userName, 0, $pos ) ); |
| 66 |
$firstIw = array_shift( $iw ); |
| 67 |
$interwikiLookup = $services->getInterwikiLookup(); |
| 68 |
if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) { |
| 69 |
$title = $services->getNamespaceInfo()->getCanonicalName( NS_USER ) . |
| 70 |
':' . substr( $userName, $pos + 1 ); |
| 71 |
if ( $iw ) { |
| 72 |
$title = implode( ':', $iw ) . ':' . $title; |
| 73 |
} |
| 74 |
return Title::makeTitle( NS_MAIN, $title, '', $firstIw ); |
| 75 |
} |
| 76 |
return null; |
| 77 |
} else { |
| 78 |
|
| 79 |
if ( |
| 80 |
$services->getUserNameUtils()->isIP( $userName ) |
| 81 |
|| $services->getUserNameUtils()->isValidIPRange( $userName ) |
| 82 |
|| $services->getUserNameUtils()->isValid( $userName ) |
| 83 |
) { |
| 84 |
return SpecialPage::getTitleFor( 'Contributions', $userName ); |
| 85 |
} else { |
| 86 |
|
| 87 |
return null; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function applyPrefix( $name ) { |
| 107 |
$services = MediaWikiServices::getInstance(); |
| 108 |
$userNameUtils = $services->getUserNameUtils(); |
| 109 |
if ( $userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE ) === false ) { |
| 110 |
return $name; |
| 111 |
} |
| 112 |
|
| 113 |
if ( $this->assignKnownUsers ) { |
| 114 |
$userIdentityLookup = $services->getUserIdentityLookup(); |
| 115 |
$userIdentity = $userIdentityLookup->getUserIdentityByName( $name ); |
| 116 |
if ( $userIdentity && $userIdentity->isRegistered() ) { |
| 117 |
return $name; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
if ( !isset( $this->triedCreations[$name] ) ) { |
| 122 |
$this->triedCreations[$name] = true; |
| 123 |
if ( !( new HookRunner( $services->getHookContainer() ) )->onImportHandleUnknownUser( $name ) ) { |
| 124 |
$userIdentity = $userIdentityLookup->getUserIdentityByName( $name, IDBAccessObject::READ_LATEST ); |
| 125 |
if ( $userIdentity && $userIdentity->isRegistered() ) { |
| 126 |
return $name; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return $this->addPrefix( $name ); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function addPrefix( $name ) { |
| 142 |
return substr( $this->usernamePrefix . '>' . $name, 0, 255 ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public static function isExternal( $username ) { |
| 152 |
return str_contains( $username, '>' ); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public static function getLocal( $username ) { |
| 162 |
if ( !self::isExternal( $username ) ) { |
| 163 |
return $username; |
| 164 |
} |
| 165 |
|
| 166 |
return substr( $username, strpos( $username, '>' ) + 1 ); |
| 167 |
} |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
class_alias( ExternalUserNames::class, 'ExternalUserNames' ); |