Add message parameter type for User Groups

Bug: T278482
Change-Id: I45da5f73f8920b79b57c02776a05d0268d3480f2
This commit is contained in:
TChin 2021-10-13 14:22:26 -04:00
parent 3893aab376
commit fb4e7a803a
10 changed files with 121 additions and 9 deletions

View file

@ -2603,6 +2603,18 @@ class Language {
return $ts;
}
/**
* Gets the localized friendly name for a group, if it exists. For example,
* "Administrators" or "Bureaucrats"
*
* @param string $group Internal group name
* @return string Localized friendly group name
*/
public function getGroupName( $group ) {
$msg = $this->msg( "group-$group" );
return $msg->isBlank() ? $group : $msg->text();
}
/**
* @param string $key
* @return string|null

View file

@ -629,6 +629,26 @@ class Message implements MessageSpecifier, Serializable {
return $this;
}
/**
* Add parameters that represent user groups
*
* @since 1.38
*
* @param string|string[] ...$params User Group parameters, or a single argument that is
* an array of user group parameters.
*
* @return Message $this
*/
public function userGroupParams( ...$params ) {
if ( isset( $params[0] ) && is_array( $params[0] ) ) {
$params = $params[0];
}
foreach ( $params as $param ) {
$this->parameters[] = self::userGroupParam( $param );
}
return $this;
}
/**
* Add parameters that are times and will be passed through
* Language::time before substitution
@ -1142,6 +1162,17 @@ class Message implements MessageSpecifier, Serializable {
return [ 'time' => $time ];
}
/**
* @since 1.38
*
* @param string $userGroup
*
* @return string[] Array with a single "group" key.
*/
public static function userGroupParam( string $userGroup ) {
return [ 'group' => $userGroup ];
}
/**
* @since 1.22
*
@ -1268,6 +1299,8 @@ class Message implements MessageSpecifier, Serializable {
return [ 'before', $this->getLanguage()->date( $param['date'] ) ];
} elseif ( isset( $param['time'] ) ) {
return [ 'before', $this->getLanguage()->time( $param['time'] ) ];
} elseif ( isset( $param['group'] ) ) {
return [ 'before', $this->getLanguage()->getGroupName( $param['group'] ) ];
} elseif ( isset( $param['period'] ) ) {
return [ 'before', $this->getLanguage()->formatTimePeriod( $param['period'] ) ];
} elseif ( isset( $param['size'] ) ) {

View file

@ -199,6 +199,17 @@ class MessageValue {
return $this->textParamsOfType( ParamType::TIME, ...$values );
}
/**
* Chainable mutator which adds parameters which are a user group (ParamType::GROUP).
*
* @since 1.38
* @param string ...$values User Groups
* @return $this
*/
public function userGroupParams( ...$values ) {
return $this->textParamsOfType( ParamType::GROUP, ...$values );
}
/**
* Chainable mutator which adds parameters which are a number of bytes (ParamType::SIZE).
*

View file

@ -59,6 +59,13 @@ class ParamType {
*/
public const TIME = 'time';
/**
* @since 1.38
*
* User Group
*/
public const GROUP = 'group';
/** A number of bytes. The output will be rounded to an appropriate magnitude. */
public const SIZE = 'size';

View file

@ -112,8 +112,9 @@ class SpecialActiveUsers extends SpecialPage {
$groups = $this->userGroupManager->listAllGroups();
$options = [];
$lang = $this->getLanguage();
foreach ( $groups as $group ) {
$msg = htmlspecialchars( UserGroupMembership::getGroupName( $group ) );
$msg = htmlspecialchars( $lang->getGroupName( $group ) );
$options[$msg] = $group;
}
ksort( $options );

View file

@ -164,10 +164,10 @@ class UserGroupMembership {
*
* @param string $group Internal group name
* @return string Localized friendly group name
* @deprecated since 1.38, use Language::getGroupName or Message::userGroupParams
*/
public static function getGroupName( $group ) {
$msg = wfMessage( "group-$group" );
return $msg->isBlank() ? $group : $msg->text();
return RequestContext::getMain()->getLanguage()->getGroupName( $group );
}
/**

View file

@ -77,17 +77,30 @@ class TextFormatterTest extends MediaWikiIntegrationTestCase {
$this->assertSame( 'test a, 100 bps $2', $result );
}
public function testFormatMessage() {
$formatter = $this->createTextFormatter( 'en' );
$mv = ( new MessageValue( 'test' ) )
public function provideTestFormatMessage() {
yield [ ( new MessageValue( 'test' ) )
->params( new MessageValue( 'test2', [ 'a', 'b' ] ) )
->commaListParams( [
'x',
new ScalarParam( ParamType::BITRATE, 100 ),
new MessageValue( 'test3', [ 'c', new MessageValue( 'test4', [ 'd', 'e' ] ) ] )
] );
$result = $formatter->format( $mv );
$this->assertSame( 'test test2 a b x, 100 bps, test3 c test4 d e', $result );
] ),
'test test2 a b x(comma-separator)(bitrate-bits)(comma-separator)test3 c test4 d e'
];
yield [ ( new MessageValue( 'test' ) )
->userGroupParams( 'bot' ),
'test (group-bot) $2'
];
}
/**
* @dataProvider provideTestFormatMessage
*/
public function testFormatMessage( $message, $expected ) {
$formatter = $this->createTextFormatter( 'qqx' );
$result = $formatter->format( $message );
$this->assertSame( $expected, $result );
}
public function testFormatMessageFormatsWikitext() {

View file

@ -536,6 +536,21 @@ class MessageTest extends MediaWikiLangTestCase {
);
}
/**
* @covers Message::userGroupParam
* @covers Message::userGroupParams
*/
public function testUserGroupParams() {
$lang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'qqx' );
$msg = new RawMessage( '$1' );
$this->setUserLang( $lang );
$this->assertSame(
'(group-bot)',
$msg->userGroupParams( 'bot' )->plain(),
'user group is handled correctly'
);
}
/**
* @covers Message::timeperiodParam
* @covers Message::timeperiodParams

View file

@ -2190,4 +2190,14 @@ class LanguageIntegrationTest extends LanguageClassesTestCase {
],
];
}
/**
* @covers Language::getGroupName
*/
public function testGetGroupName() {
$lang = $this->getLang();
$groupName = $lang->getGroupName( 'bot' );
$this->assertSame( 'Bots', $groupName );
}
}

View file

@ -173,6 +173,16 @@ class MessageValueTest extends \PHPUnit\Framework\TestCase {
$this->assertSame( $mv, $mv2 );
}
public function testUserGroupParams() {
$mv = new MessageValue( 'key' );
$mv2 = $mv->userGroupParams( 'bot' );
$this->assertSame( '<message key="key">' .
"<group>bot</group>" .
'</message>',
$mv->dump() );
$this->assertSame( $mv, $mv2 );
}
public function testSizeParams() {
$mv = new MessageValue( 'key' );
$mv2 = $mv->sizeParams( 1, 2 );