Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.62% |
41 / 42 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
ExternalUserNames | |
100.00% |
41 / 41 |
|
100.00% |
6 / 6 |
21 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getUserLinkTitle | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
7 | |||
applyPrefix | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
9 | |||
addPrefix | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isExternal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLocal | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
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 | namespace MediaWiki\User; |
24 | |
25 | use MediaWiki\HookContainer\HookRunner; |
26 | use MediaWiki\MediaWikiServices; |
27 | use MediaWiki\SpecialPage\SpecialPage; |
28 | use MediaWiki\Title\Title; |
29 | use Wikimedia\Rdbms\IDBAccessObject; |
30 | |
31 | /** |
32 | * Class to parse and build external user names |
33 | * @since 1.31 |
34 | */ |
35 | class ExternalUserNames { |
36 | |
37 | private string $usernamePrefix; |
38 | private bool $assignKnownUsers; |
39 | |
40 | /** |
41 | * @var bool[] |
42 | */ |
43 | private $triedCreations = []; |
44 | |
45 | /** |
46 | * @param string $usernamePrefix Prefix to apply to unknown (and possibly also known) usernames |
47 | * @param bool $assignKnownUsers Whether to apply the prefix to usernames that exist locally |
48 | */ |
49 | public function __construct( $usernamePrefix, $assignKnownUsers ) { |
50 | $this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' ); |
51 | $this->assignKnownUsers = (bool)$assignKnownUsers; |
52 | } |
53 | |
54 | /** |
55 | * Get a target Title to link a username. |
56 | * |
57 | * @param string $userName Username to link |
58 | * |
59 | * @return null|Title |
60 | */ |
61 | public static function getUserLinkTitle( $userName ) { |
62 | $pos = strpos( $userName, '>' ); |
63 | $services = MediaWikiServices::getInstance(); |
64 | if ( $pos !== false ) { |
65 | $iw = explode( ':', substr( $userName, 0, $pos ) ); |
66 | $firstIw = array_shift( $iw ); |
67 | $interwikiLookup = $services->getInterwikiLookup(); |
68 | if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) { |
69 | $title = $services->getNamespaceInfo()->getCanonicalName( NS_USER ) . |
70 | ':' . substr( $userName, $pos + 1 ); |
71 | if ( $iw ) { |
72 | $title = implode( ':', $iw ) . ':' . $title; |
73 | } |
74 | return Title::makeTitle( NS_MAIN, $title, '', $firstIw ); |
75 | } |
76 | return null; |
77 | } else { |
78 | // Protect against invalid user names from old corrupt database rows, T232451 |
79 | if ( |
80 | $services->getUserNameUtils()->isIP( $userName ) |
81 | || $services->getUserNameUtils()->isValidIPRange( $userName ) |
82 | || $services->getUserNameUtils()->isValid( $userName ) |
83 | ) { |
84 | return SpecialPage::getTitleFor( 'Contributions', $userName ); |
85 | } else { |
86 | // Bad user name, no link |
87 | return null; |
88 | } |
89 | } |
90 | } |
91 | |
92 | /** |
93 | * Add an interwiki prefix to the username, if appropriate |
94 | * |
95 | * This method does have a side-effect on SUL (single user login) wikis: Calling this calls the |
96 | * ImportHandleUnknownUser hook from the CentralAuth extension, which assigns a local ID to the |
97 | * global user name, if possible. No prefix is applied if this is successful. |
98 | * |
99 | * @see https://meta.wikimedia.org/wiki/Help:Unified_login |
100 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/ImportHandleUnknownUser |
101 | * |
102 | * @param string $name |
103 | * @return string Either the unchanged username if it's a known local user (or not a valid |
104 | * username), otherwise the name with the prefix prepended. |
105 | */ |
106 | public function applyPrefix( $name ) { |
107 | $services = MediaWikiServices::getInstance(); |
108 | $userNameUtils = $services->getUserNameUtils(); |
109 | if ( $userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE ) === false ) { |
110 | return $name; |
111 | } |
112 | |
113 | if ( $this->assignKnownUsers ) { |
114 | $userIdentityLookup = $services->getUserIdentityLookup(); |
115 | $userIdentity = $userIdentityLookup->getUserIdentityByName( $name ); |
116 | if ( $userIdentity && $userIdentity->isRegistered() ) { |
117 | return $name; |
118 | } |
119 | |
120 | // See if any extension wants to create it. |
121 | if ( !isset( $this->triedCreations[$name] ) ) { |
122 | $this->triedCreations[$name] = true; |
123 | if ( !( new HookRunner( $services->getHookContainer() ) )->onImportHandleUnknownUser( $name ) ) { |
124 | $userIdentity = $userIdentityLookup->getUserIdentityByName( $name, IDBAccessObject::READ_LATEST ); |
125 | if ( $userIdentity && $userIdentity->isRegistered() ) { |
126 | return $name; |
127 | } |
128 | } |
129 | } |
130 | } |
131 | |
132 | return $this->addPrefix( $name ); |
133 | } |
134 | |
135 | /** |
136 | * Add an interwiki prefix to the username regardless of circumstances |
137 | * |
138 | * @param string $name |
139 | * @return string Prefixed username, using ">" as separator |
140 | */ |
141 | public function addPrefix( $name ) { |
142 | return substr( $this->usernamePrefix . '>' . $name, 0, 255 ); |
143 | } |
144 | |
145 | /** |
146 | * Tells whether the username is external or not |
147 | * |
148 | * @param string $username Username to check |
149 | * @return bool true if it's external, false otherwise. |
150 | */ |
151 | public static function isExternal( $username ) { |
152 | return str_contains( $username, '>' ); |
153 | } |
154 | |
155 | /** |
156 | * Get local part of the user name |
157 | * |
158 | * @param string $username Username to get |
159 | * @return string |
160 | */ |
161 | public static function getLocal( $username ) { |
162 | if ( !self::isExternal( $username ) ) { |
163 | return $username; |
164 | } |
165 | |
166 | return substr( $username, strpos( $username, '>' ) + 1 ); |
167 | } |
168 | |
169 | } |
170 | |
171 | /** @deprecated class alias since 1.41 */ |
172 | class_alias( ExternalUserNames::class, 'ExternalUserNames' ); |