3 * Class to parse and build external user names
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
26 * Class to parse and build external user names
29 class ExternalUserNames
{
34 private $usernamePrefix = 'imported';
39 private $assignKnownUsers = false;
44 private $triedCreations = [];
47 * @param string $usernamePrefix Prefix to apply to unknown (and possibly also known) usernames
48 * @param bool $assignKnownUsers Whether to apply the prefix to usernames that exist locally
50 public function __construct( $usernamePrefix, $assignKnownUsers ) {
51 $this->usernamePrefix
= rtrim( (string)$usernamePrefix, ':>' );
52 $this->assignKnownUsers
= (bool)$assignKnownUsers;
56 * Get a target Title to link a username.
58 * @param string $userName Username to link
62 public static function getUserLinkTitle( $userName ) {
63 $pos = strpos( $userName, '>' );
64 if ( $pos !== false ) {
65 $iw = explode( ':', substr( $userName, 0, $pos ) );
66 $firstIw = array_shift( $iw );
67 $services = MediaWikiServices
::getInstance();
68 $interwikiLookup = $services->getInterwikiLookup();
69 if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
70 $title = $services->getNamespaceInfo()->getCanonicalName( NS_USER
) .
71 ':' . substr( $userName, $pos +
1 );
73 $title = implode( ':', $iw ) . ':' . $title;
75 return Title
::makeTitle( NS_MAIN
, $title, '', $firstIw );
79 return SpecialPage
::getTitleFor( 'Contributions', $userName );
84 * Add an interwiki prefix to the username, if appropriate
86 * This method does have a side-effect on SUL (single user login) wikis: Calling this calls the
87 * ImportHandleUnknownUser hook from the CentralAuth extension, which assigns a local ID to the
88 * global user name, if possible. No prefix is applied if this is successful.
90 * @see https://meta.wikimedia.org/wiki/Help:Unified_login
91 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ImportHandleUnknownUser
94 * @return string Either the unchanged username if it's a known local user (or not a valid
95 * username), otherwise the name with the prefix prepended.
97 public function applyPrefix( $name ) {
98 if ( !User
::isUsableName( $name ) ) {
102 if ( $this->assignKnownUsers
) {
103 if ( User
::idFromName( $name ) ) {
107 // See if any extension wants to create it.
108 if ( !isset( $this->triedCreations
[$name] ) ) {
109 $this->triedCreations
[$name] = true;
110 if ( !Hooks
::run( 'ImportHandleUnknownUser', [ $name ] ) &&
111 User
::idFromName( $name, User
::READ_LATEST
)
118 return $this->addPrefix( $name );
122 * Add an interwiki prefix to the username regardless of circumstances
124 * @param string $name
125 * @return string Prefixed username, using ">" as separator
127 public function addPrefix( $name ) {
128 return substr( $this->usernamePrefix
. '>' . $name, 0, 255 );
132 * Tells whether the username is external or not
134 * @param string $username Username to check
135 * @return bool true if it's external, false otherwise.
137 public static function isExternal( $username ) {
138 return strpos( $username, '>' ) !== false;
142 * Get local part of the user name
144 * @param string $username Username to get
147 public static function getLocal( $username ) {
148 if ( !self
::isExternal( $username ) ) {
152 return substr( $username, strpos( $username, '>' ) +
1 );