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
{
30 private $usernamePrefix = 'imported';
31 private $assignKnownUsers = false;
32 private $triedCreations = [];
35 * @param string $usernamePrefix Prefix to apply to unknown (and possibly also known) usernames
36 * @param bool $assignKnownUsers Whether to apply the prefix to usernames that exist locally
38 public function __construct( $usernamePrefix, $assignKnownUsers ) {
39 $this->usernamePrefix
= rtrim( (string)$usernamePrefix, ':>' );
40 $this->assignKnownUsers
= (bool)$assignKnownUsers;
44 * Get a target Title to link a username.
46 * @param string $userName Username to link
50 public static function getUserLinkTitle( $userName ) {
51 $pos = strpos( $userName, '>' );
52 if ( $pos !== false ) {
53 $iw = explode( ':', substr( $userName, 0, $pos ) );
54 $firstIw = array_shift( $iw );
55 $interwikiLookup = MediaWikiServices
::getInstance()->getInterwikiLookup();
56 if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
57 $title = MWNamespace
::getCanonicalName( NS_USER
) . ':' . substr( $userName, $pos +
1 );
59 $title = implode( ':', $iw ) . ':' . $title;
61 return Title
::makeTitle( NS_MAIN
, $title, '', $firstIw );
65 return SpecialPage
::getTitleFor( 'Contributions', $userName );
70 * Add an interwiki prefix to the username, if appropriate
72 * @param string $name Name being imported
73 * @return string Name, possibly with the prefix prepended.
75 public function applyPrefix( $name ) {
76 if ( !User
::isUsableName( $name ) ) {
80 if ( $this->assignKnownUsers
) {
81 if ( User
::idFromName( $name ) ) {
85 // See if any extension wants to create it.
86 if ( !isset( $this->triedCreations
[$name] ) ) {
87 $this->triedCreations
[$name] = true;
88 if ( !Hooks
::run( 'ImportHandleUnknownUser', [ $name ] ) &&
89 User
::idFromName( $name, User
::READ_LATEST
)
96 return $this->addPrefix( $name );
100 * Add an interwiki prefix to the username regardless of circumstances
102 * @param string $name Name being imported
103 * @return string Name
105 public function addPrefix( $name ) {
106 return substr( $this->usernamePrefix
. '>' . $name, 0, 255 );
110 * Tells whether the username is external or not
112 * @param string $username Username to check
113 * @return bool true if it's external, false otherwise.
115 public static function isExternal( $username ) {
116 return strpos( $username, '>' ) !== false;
120 * Get local part of the user name
122 * @param string $username Username to get
125 public static function getLocal( $username ) {
126 if ( !self
::isExternal( $username ) ) {
130 return substr( $username, strpos( $username, '>' ) +
1 );