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 $interwikiLookup = MediaWikiServices
::getInstance()->getInterwikiLookup();
68 if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
69 $title = MWNamespace
::getCanonicalName( NS_USER
) . ':' . substr( $userName, $pos +
1 );
71 $title = implode( ':', $iw ) . ':' . $title;
73 return Title
::makeTitle( NS_MAIN
, $title, '', $firstIw );
77 return SpecialPage
::getTitleFor( 'Contributions', $userName );
82 * Add an interwiki prefix to the username, if appropriate
84 * This method does have a side-effect on SUL (single user login) wikis: Calling this calls the
85 * ImportHandleUnknownUser hook from the CentralAuth extension, which assigns a local ID to the
86 * global user name, if possible. No prefix is applied if this is successful.
88 * @see https://meta.wikimedia.org/wiki/Help:Unified_login
89 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ImportHandleUnknownUser
92 * @return string Either the unchanged username if it's a known local user (or not a valid
93 * username), otherwise the name with the prefix prepended.
95 public function applyPrefix( $name ) {
96 if ( !User
::isUsableName( $name ) ) {
100 if ( $this->assignKnownUsers
) {
101 if ( User
::idFromName( $name ) ) {
105 // See if any extension wants to create it.
106 if ( !isset( $this->triedCreations
[$name] ) ) {
107 $this->triedCreations
[$name] = true;
108 if ( !Hooks
::run( 'ImportHandleUnknownUser', [ $name ] ) &&
109 User
::idFromName( $name, User
::READ_LATEST
)
116 return $this->addPrefix( $name );
120 * Add an interwiki prefix to the username regardless of circumstances
122 * @param string $name
123 * @return string Prefixed username, using ">" as separator
125 public function addPrefix( $name ) {
126 return substr( $this->usernamePrefix
. '>' . $name, 0, 255 );
130 * Tells whether the username is external or not
132 * @param string $username Username to check
133 * @return bool true if it's external, false otherwise.
135 public static function isExternal( $username ) {
136 return strpos( $username, '>' ) !== false;
140 * Get local part of the user name
142 * @param string $username Username to get
145 public static function getLocal( $username ) {
146 if ( !self
::isExternal( $username ) ) {
150 return substr( $username, strpos( $username, '>' ) +
1 );