Merge "HTML escape parameter 'text' of hook 'SkinEditSectionLinks'"
[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 $services = MediaWikiServices::getInstance();
68 $interwikiLookup = $services->getInterwikiLookup();
69 if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
70 $title = $services->getNamespaceInfo()->getCanonicalName( NS_USER ) .
71 ':' . substr( $userName, $pos + 1 );
72 if ( $iw ) {
73 $title = implode( ':', $iw ) . ':' . $title;
74 }
75 return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
76 }
77 return null;
78 } else {
79 return SpecialPage::getTitleFor( 'Contributions', $userName );
80 }
81 }
82
83 /**
84 * Add an interwiki prefix to the username, if appropriate
85 *
86 * This method does have a side-effect on SUL (single user login) wikis: Calling this calls the
87 * ImportHandleUnknownUser hook from the CentralAuth extension, which assigns a local ID to the
88 * global user name, if possible. No prefix is applied if this is successful.
89 *
90 * @see https://meta.wikimedia.org/wiki/Help:Unified_login
91 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ImportHandleUnknownUser
92 *
93 * @param string $name
94 * @return string Either the unchanged username if it's a known local user (or not a valid
95 * username), otherwise the name with the prefix prepended.
96 */
97 public function applyPrefix( $name ) {
98 if ( !User::isUsableName( $name ) ) {
99 return $name;
100 }
101
102 if ( $this->assignKnownUsers ) {
103 if ( User::idFromName( $name ) ) {
104 return $name;
105 }
106
107 // See if any extension wants to create it.
108 if ( !isset( $this->triedCreations[$name] ) ) {
109 $this->triedCreations[$name] = true;
110 if ( !Hooks::run( 'ImportHandleUnknownUser', [ $name ] ) &&
111 User::idFromName( $name, User::READ_LATEST )
112 ) {
113 return $name;
114 }
115 }
116 }
117
118 return $this->addPrefix( $name );
119 }
120
121 /**
122 * Add an interwiki prefix to the username regardless of circumstances
123 *
124 * @param string $name
125 * @return string Prefixed username, using ">" as separator
126 */
127 public function addPrefix( $name ) {
128 return substr( $this->usernamePrefix . '>' . $name, 0, 255 );
129 }
130
131 /**
132 * Tells whether the username is external or not
133 *
134 * @param string $username Username to check
135 * @return bool true if it's external, false otherwise.
136 */
137 public static function isExternal( $username ) {
138 return strpos( $username, '>' ) !== false;
139 }
140
141 /**
142 * Get local part of the user name
143 *
144 * @param string $username Username to get
145 * @return string
146 */
147 public static function getLocal( $username ) {
148 if ( !self::isExternal( $username ) ) {
149 return $username;
150 }
151
152 return substr( $username, strpos( $username, '>' ) + 1 );
153 }
154
155 }