13ac6b256600cdc8bd0eba479c01e10fec28bef4
[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 private $usernamePrefix = 'imported';
31 private $assignKnownUsers = false;
32 private $triedCreations = [];
33
34 /**
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
37 */
38 public function __construct( $usernamePrefix, $assignKnownUsers ) {
39 $this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' );
40 $this->assignKnownUsers = (bool)$assignKnownUsers;
41 }
42
43 /**
44 * Get a target Title to link a username.
45 *
46 * @param string $userName Username to link
47 *
48 * @return null|Title
49 */
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 );
58 if ( $iw ) {
59 $title = join( ':', $iw ) . ':' . $title;
60 }
61 return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
62 }
63 return null;
64 } else {
65 return SpecialPage::getTitleFor( 'Contributions', $userName );
66 }
67 }
68
69 /**
70 * Add an interwiki prefix to the username, if appropriate
71 *
72 * @param string $name Name being imported
73 * @return string Name, possibly with the prefix prepended.
74 */
75 public function applyPrefix( $name ) {
76 if ( !User::isUsableName( $name ) ) {
77 return $name;
78 }
79
80 if ( $this->assignKnownUsers ) {
81 if ( User::idFromName( $name ) ) {
82 return $name;
83 }
84
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 )
90 ) {
91 return $name;
92 }
93 }
94 }
95
96 return $this->addPrefix( $name );
97 }
98
99 /**
100 * Add an interwiki prefix to the username regardless of circumstances
101 *
102 * @param string $name Name being imported
103 * @return string Name
104 */
105 public function addPrefix( $name ) {
106 return substr( $this->usernamePrefix . '>' . $name, 0, 255 );
107 }
108
109 /**
110 * Tells whether the username is external or not
111 *
112 * @param string $username Username to check
113 * @return bool true if it's external, false otherwise.
114 */
115 public static function isExternal( $username ) {
116 return strpos( $username, '>' ) !== false;
117 }
118
119 }