Corrected grammatical error.
[lhc/web/wiklou.git] / includes / user / ExternalUserNames.php
1 <?php
2 /**
3 * Class to parse and build external user names
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * Class to parse and build external user names
27 * @since 1.31
28 */
29 class ExternalUserNames {
30
31 /**
32 * @var string
33 */
34 private $usernamePrefix = 'imported';
35
36 /**
37 * @var bool
38 */
39 private $assignKnownUsers = false;
40
41 /**
42 * @var bool[]
43 */
44 private $triedCreations = [];
45
46 /**
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
49 */
50 public function __construct( $usernamePrefix, $assignKnownUsers ) {
51 $this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' );
52 $this->assignKnownUsers = (bool)$assignKnownUsers;
53 }
54
55 /**
56 * Get a target Title to link a username.
57 *
58 * @param string $userName Username to link
59 *
60 * @return null|Title
61 */
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 );
70 if ( $iw ) {
71 $title = implode( ':', $iw ) . ':' . $title;
72 }
73 return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
74 }
75 return null;
76 } else {
77 return SpecialPage::getTitleFor( 'Contributions', $userName );
78 }
79 }
80
81 /**
82 * Add an interwiki prefix to the username, if appropriate
83 *
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.
87 *
88 * @see https://meta.wikimedia.org/wiki/Help:Unified_login
89 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ImportHandleUnknownUser
90 *
91 * @param string $name
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.
94 */
95 public function applyPrefix( $name ) {
96 if ( !User::isUsableName( $name ) ) {
97 return $name;
98 }
99
100 if ( $this->assignKnownUsers ) {
101 if ( User::idFromName( $name ) ) {
102 return $name;
103 }
104
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 )
110 ) {
111 return $name;
112 }
113 }
114 }
115
116 return $this->addPrefix( $name );
117 }
118
119 /**
120 * Add an interwiki prefix to the username regardless of circumstances
121 *
122 * @param string $name
123 * @return string Prefixed username, using ">" as separator
124 */
125 public function addPrefix( $name ) {
126 return substr( $this->usernamePrefix . '>' . $name, 0, 255 );
127 }
128
129 /**
130 * Tells whether the username is external or not
131 *
132 * @param string $username Username to check
133 * @return bool true if it's external, false otherwise.
134 */
135 public static function isExternal( $username ) {
136 return strpos( $username, '>' ) !== false;
137 }
138
139 /**
140 * Get local part of the user name
141 *
142 * @param string $username Username to get
143 * @return string
144 */
145 public static function getLocal( $username ) {
146 if ( !self::isExternal( $username ) ) {
147 return $username;
148 }
149
150 return substr( $username, strpos( $username, '>' ) + 1 );
151 }
152
153 }