mediawiki-extensions-Echo/includes/Formatters/EchoUserRightsPresentationModel.php
thiemowmde eaa1fea890 Fix incomplete gender support for user group changes
This affects primarily the message
"notification-header-user-rights-add-only" which is very priminently
seen by every new editor that made their first few edits and gets
promoted to the next user group a few days later. Turns out the code
was just incomplete. All the information about the user and their
gender is already there, it was just not forwarded correctly.

Notice there are two messages:
* "group-…" messages don't have gender support. This is the (ideally)
  gender neutral name of the group. Meant to be used as e.g. section
  heading for a list of users.
* "group-…-member" is the same with gender support. To be used in all
  contexts that are about a single, specific user with known gender.
  Which is exactly what's happening here.

Turns out we can even use a neat convenience function from the
Language class that does exactly what we need.

I can't tell why but the array_values is apparently critical.
Originally added via I49b5fe5. It can't hurt so I keep it.

Bug: T368249
Change-Id: I53c028375d77c93f399538fd38aa8f8af30934b0
2024-06-28 11:49:10 +02:00

127 lines
4 KiB
PHP

<?php
namespace MediaWiki\Extension\Notifications\Formatters;
use MediaWiki\Extension\Notifications\DiscussionParser;
use MediaWiki\Language\RawMessage;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\User\User;
/**
* Formatter for 'user-rights' notifications
*/
class EchoUserRightsPresentationModel extends EchoEventPresentationModel {
public function getIconType() {
return 'user-rights';
}
public function getHeaderMessage() {
[ $formattedName, $genderName ] = $this->getAgentForOutput();
$add = array_map(
[ $this->language, 'embedBidi' ],
$this->getLocalizedGroupNames( $this->event->getExtraParam( 'add', [] ), $genderName )
);
$remove = array_map(
[ $this->language, 'embedBidi' ],
$this->getLocalizedGroupNames( $this->event->getExtraParam( 'remove', [] ), $genderName )
);
$expiryChanged = array_map(
[ $this->language, 'embedBidi' ],
$this->getLocalizedGroupNames( $this->event->getExtraParam( 'expiry-changed', [] ), $genderName )
);
if ( $expiryChanged ) {
$msg = $this->msg( 'notification-header-user-rights-expiry-change' );
$msg->params( $genderName );
$msg->params( $this->language->commaList( $expiryChanged ) );
$msg->params( count( $expiryChanged ) );
$msg->params( $this->getViewingUserForGender() );
return $msg;
} elseif ( $add && !$remove ) {
$msg = $this->msg( 'notification-header-user-rights-add-only' );
$msg->params( $genderName );
$msg->params( $this->language->commaList( $add ) );
$msg->params( count( $add ) );
$msg->params( $this->getViewingUserForGender() );
return $msg;
} elseif ( !$add && $remove ) {
$msg = $this->msg( 'notification-header-user-rights-remove-only' );
$msg->params( $genderName );
$msg->params( $this->language->commaList( $remove ) );
$msg->params( count( $remove ) );
$msg->params( $this->getViewingUserForGender() );
return $msg;
} else {
$msg = $this->msg( 'notification-header-user-rights-add-and-remove' );
$msg->params( $genderName );
$msg->params( $this->language->commaList( $add ) );
$msg->params( count( $add ) );
$msg->params( $this->language->commaList( $remove ) );
$msg->params( count( $remove ) );
$msg->params( $this->getViewingUserForGender() );
return $msg;
}
}
public function getBodyMessage() {
$reason = $this->event->getExtraParam( 'reason' );
if ( $reason ) {
$text = DiscussionParser::getTextSnippet( $reason, $this->language );
return new RawMessage( "$1", [ $text ] );
}
return false;
}
/**
* @param string[] $names
* @param string $genderName
* @return string[]
*/
private function getLocalizedGroupNames( array $names, string $genderName ) {
return array_map(
fn ( $name ) => $this->language->getGroupMemberName( $name, $genderName ),
array_values( $names )
);
}
public function getPrimaryLink() {
$addedGroups = array_values( $this->event->getExtraParam( 'add', [] ) );
$removedGroups = array_values( $this->event->getExtraParam( 'remove', [] ) );
if ( $addedGroups !== [] && $removedGroups === [] ) {
$fragment = $addedGroups[0];
} elseif ( $addedGroups === [] && $removedGroups !== [] ) {
$fragment = $removedGroups[0];
} else {
$fragment = '';
}
return [
'url' => SpecialPage::getTitleFor( 'Listgrouprights', false, $fragment )->getFullURL(),
'label' => $this->msg( 'echo-learn-more' )->text()
];
}
public function getSecondaryLinks() {
return [ $this->getAgentLink(), $this->getLogLink() ];
}
private function getLogLink() {
$affectedUserPage = User::newFromId( $this->event->getExtraParam( 'user' ) )->getUserPage();
$query = [
'type' => 'rights',
'page' => $affectedUserPage->getPrefixedText(),
'user' => $this->event->getAgent()->getName(),
];
return [
'label' => $this->msg( 'echo-log' )->text(),
'url' => SpecialPage::getTitleFor( 'Log' )->getFullURL( $query ),
'description' => '',
'icon' => false,
'prioritized' => true,
];
}
protected function getSubjectMessageKey() {
return 'notification-user-rights-email-subject';
}
}