summaryrefslogtreecommitdiff
path: root/www/wiki/includes/user/ExternalUserNames.php
blob: f953b144d8051682504caeebd2807b08dbc6591a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
 * Class to parse and build external user names
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

use MediaWiki\MediaWikiServices;

/**
 * Class to parse and build external user names
 * @since 1.31
 */
class ExternalUserNames {
	private $usernamePrefix = 'imported';
	private $assignKnownUsers = false;
	private $triedCreations = [];

	/**
	 * @param string $usernamePrefix Prefix to apply to unknown (and possibly also known) usernames
	 * @param bool $assignKnownUsers Whether to apply the prefix to usernames that exist locally
	 */
	public function __construct( $usernamePrefix, $assignKnownUsers ) {
		$this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' );
		$this->assignKnownUsers = (bool)$assignKnownUsers;
	}

	/**
	 * Get a target Title to link a username.
	 *
	 * @param string $userName Username to link
	 *
	 * @return null|Title
	 */
	public static function getUserLinkTitle( $userName ) {
		$pos = strpos( $userName, '>' );
		if ( $pos !== false ) {
			$iw = explode( ':', substr( $userName, 0, $pos ) );
			$firstIw = array_shift( $iw );
			$interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
			if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
				$title = MWNamespace::getCanonicalName( NS_USER ) . ':' . substr( $userName, $pos + 1 );
				if ( $iw ) {
					$title = implode( ':', $iw ) . ':' . $title;
				}
				return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
			}
			return null;
		} else {
			return SpecialPage::getTitleFor( 'Contributions', $userName );
		}
	}

	/**
	 * Add an interwiki prefix to the username, if appropriate
	 *
	 * @param string $name Name being imported
	 * @return string Name, possibly with the prefix prepended.
	 */
	public function applyPrefix( $name ) {
		if ( !User::isUsableName( $name ) ) {
			return $name;
		}

		if ( $this->assignKnownUsers ) {
			if ( User::idFromName( $name ) ) {
				return $name;
			}

			// See if any extension wants to create it.
			if ( !isset( $this->triedCreations[$name] ) ) {
				$this->triedCreations[$name] = true;
				if ( !Hooks::run( 'ImportHandleUnknownUser', [ $name ] ) &&
					User::idFromName( $name, User::READ_LATEST )
				) {
					return $name;
				}
			}
		}

		return $this->addPrefix( $name );
	}

	/**
	 * Add an interwiki prefix to the username regardless of circumstances
	 *
	 * @param string $name Name being imported
	 * @return string Name
	 */
	public function addPrefix( $name ) {
		return substr( $this->usernamePrefix . '>' . $name, 0, 255 );
	}

	/**
	 * Tells whether the username is external or not
	 *
	 * @param string $username Username to check
	 * @return bool true if it's external, false otherwise.
	 */
	public static function isExternal( $username ) {
		return strpos( $username, '>' ) !== false;
	}

	/**
	 * Get local part of the user name
	 *
	 * @param string $username Username to get
	 * @return string
	 */
	public static function getLocal( $username ) {
		if ( !self::isExternal( $username ) ) {
			return $username;
		}

		return substr( $username, strpos( $username, '>' ) + 1 );
	}

}