UserDef: use TitleParser instead of Title objects
Migrate away from the Title object, use TitleParser::parseTitle() which returns a TitleValue which is enough. Will be followed by switching UserDefTest to a unit test, but in a separate commit. Change-Id: Ia756964861c4e0f3edea89f6beec2643243ca741
This commit is contained in:
parent
9308ee9cf8
commit
46ef24f70a
4 changed files with 22 additions and 11 deletions
|
|
@ -3,12 +3,13 @@
|
|||
namespace MediaWiki\ParamValidator\TypeDef;
|
||||
|
||||
use ExternalUserNames;
|
||||
use MalformedTitleException;
|
||||
use MediaWiki\User\UserIdentity;
|
||||
use MediaWiki\User\UserIdentityLookup;
|
||||
use MediaWiki\User\UserIdentityValue;
|
||||
use MediaWiki\User\UserNameUtils;
|
||||
use RequestContext;
|
||||
use TitleFactory;
|
||||
use TitleParser;
|
||||
use Wikimedia\IPUtils;
|
||||
use Wikimedia\Message\MessageValue;
|
||||
use Wikimedia\ParamValidator\Callbacks;
|
||||
|
|
@ -57,8 +58,8 @@ class UserDef extends TypeDef {
|
|||
/** @var UserIdentityLookup */
|
||||
private $userIdentityLookup;
|
||||
|
||||
/** @var TitleFactory */
|
||||
private $titleFactory;
|
||||
/** @var TitleParser */
|
||||
private $titleParser;
|
||||
|
||||
/** @var UserNameUtils */
|
||||
private $userNameUtils;
|
||||
|
|
@ -66,18 +67,18 @@ class UserDef extends TypeDef {
|
|||
/**
|
||||
* @param Callbacks $callbacks
|
||||
* @param UserIdentityLookup $userIdentityLookup
|
||||
* @param TitleFactory $titleFactory
|
||||
* @param TitleParser $titleParser
|
||||
* @param UserNameUtils $userNameUtils
|
||||
*/
|
||||
public function __construct(
|
||||
Callbacks $callbacks,
|
||||
UserIdentityLookup $userIdentityLookup,
|
||||
TitleFactory $titleFactory,
|
||||
TitleParser $titleParser,
|
||||
UserNameUtils $userNameUtils
|
||||
) {
|
||||
parent::__construct( $callbacks );
|
||||
$this->userIdentityLookup = $userIdentityLookup;
|
||||
$this->titleFactory = $titleFactory;
|
||||
$this->titleParser = $titleParser;
|
||||
$this->userNameUtils = $userNameUtils;
|
||||
}
|
||||
|
||||
|
|
@ -229,9 +230,17 @@ class UserDef extends TypeDef {
|
|||
return [ '', null ];
|
||||
}
|
||||
|
||||
$t = $this->titleFactory->newFromText( $value );
|
||||
try {
|
||||
$t = $this->titleParser->parseTitle( $value );
|
||||
} catch ( MalformedTitleException $_ ) {
|
||||
$t = null;
|
||||
}
|
||||
if ( !$t || $t->getNamespace() !== NS_USER || $t->isExternal() ) { // likely
|
||||
$t = $this->titleFactory->newFromText( "User:$value" );
|
||||
try {
|
||||
$t = $this->titleParser->parseTitle( "User:$value" );
|
||||
} catch ( MalformedTitleException $_ ) {
|
||||
$t = null;
|
||||
}
|
||||
}
|
||||
if ( !$t || $t->getNamespace() !== NS_USER || $t->isExternal() ) {
|
||||
// If it wasn't a valid User-namespace title, fail.
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ class Validator {
|
|||
],
|
||||
'user' => [
|
||||
'class' => UserDef::class,
|
||||
'services' => [ 'UserIdentityLookup', 'TitleFactory', 'UserNameUtils' ]
|
||||
'services' => [ 'UserIdentityLookup', 'TitleParser', 'UserNameUtils' ]
|
||||
],
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ class ApiParamValidator {
|
|||
],
|
||||
'user' => [
|
||||
'class' => UserDef::class,
|
||||
'services' => [ 'UserIdentityLookup', 'TitleFactory', 'UserNameUtils' ]
|
||||
'services' => [ 'UserIdentityLookup', 'TitleParser', 'UserNameUtils' ]
|
||||
],
|
||||
'upload' => [ 'class' => UploadDef::class ],
|
||||
];
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ use Wikimedia\ParamValidator\TypeDef\TypeDefTestCase;
|
|||
use Wikimedia\ParamValidator\ValidationException;
|
||||
|
||||
/**
|
||||
* @TODO convert to a unit test, all dependencies are injected
|
||||
*
|
||||
* @covers MediaWiki\ParamValidator\TypeDef\UserDef
|
||||
*/
|
||||
class UserDefTest extends TypeDefTestCase {
|
||||
|
|
@ -51,7 +53,7 @@ class UserDefTest extends TypeDefTestCase {
|
|||
return new UserDef(
|
||||
$callbacks,
|
||||
$userIdentityLookup,
|
||||
MediaWikiServices::getInstance()->getTitleFactory(),
|
||||
MediaWikiServices::getInstance()->getTitleParser(),
|
||||
MediaWikiServices::getInstance()->getUserNameUtils()
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue