This reverts commit ecf826a2ee.
Reason for revert: need to edit the patch and then it will be GTG in order to finish hard deprecating of User ::getCanonicalName, ::isUsableName, ::isCreatableName
Change-Id: I2f57f56728fcbeada96dc2228f07dc8bcaa5d4f6
167 lines
4.8 KiB
PHP
167 lines
4.8 KiB
PHP
<?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;
|
|
use MediaWiki\User\UserNameUtils;
|
|
|
|
/**
|
|
* Class to parse and build external user names
|
|
* @since 1.31
|
|
*/
|
|
class ExternalUserNames {
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $usernamePrefix;
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
private $assignKnownUsers;
|
|
|
|
/**
|
|
* @var bool[]
|
|
*/
|
|
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, '>' );
|
|
$services = MediaWikiServices::getInstance();
|
|
if ( $pos !== false ) {
|
|
$iw = explode( ':', substr( $userName, 0, $pos ) );
|
|
$firstIw = array_shift( $iw );
|
|
$interwikiLookup = $services->getInterwikiLookup();
|
|
if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
|
|
$title = $services->getNamespaceInfo()->getCanonicalName( NS_USER ) .
|
|
':' . substr( $userName, $pos + 1 );
|
|
if ( $iw ) {
|
|
$title = implode( ':', $iw ) . ':' . $title;
|
|
}
|
|
return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
|
|
}
|
|
return null;
|
|
} else {
|
|
// Protect against invalid user names from old corrupt database rows, T232451
|
|
if (
|
|
$services->getUserNameUtils()->isIP( $userName )
|
|
|| $services->getUserNameUtils()->isValidIPRange( $userName )
|
|
|| $services->getUserNameUtils()->isValid( $userName )
|
|
) {
|
|
return SpecialPage::getTitleFor( 'Contributions', $userName );
|
|
} else {
|
|
// Bad user name, no link
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add an interwiki prefix to the username, if appropriate
|
|
*
|
|
* This method does have a side-effect on SUL (single user login) wikis: Calling this calls the
|
|
* ImportHandleUnknownUser hook from the CentralAuth extension, which assigns a local ID to the
|
|
* global user name, if possible. No prefix is applied if this is successful.
|
|
*
|
|
* @see https://meta.wikimedia.org/wiki/Help:Unified_login
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ImportHandleUnknownUser
|
|
*
|
|
* @param string $name
|
|
* @return string Either the unchanged username if it's a known local user (or not a valid
|
|
* username), otherwise the name with the prefix prepended.
|
|
*/
|
|
public function applyPrefix( $name ) {
|
|
$userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
|
|
if ( $userNameUtils->getCanonical( $name, UserNameUtils::RIGOR_USABLE ) === false ) {
|
|
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::runner()->onImportHandleUnknownUser( $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
|
|
* @return string Prefixed username, using ">" as separator
|
|
*/
|
|
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 );
|
|
}
|
|
|
|
}
|