2024-05-15 05:24:43 +00:00
|
|
|
<?php
|
|
|
|
|
2024-05-15 06:44:12 +00:00
|
|
|
declare( strict_types=1 );
|
2024-05-15 05:24:43 +00:00
|
|
|
|
|
|
|
namespace MediaWiki\Skins\Citizen\Components;
|
|
|
|
|
2024-05-15 06:31:00 +00:00
|
|
|
use MalformedTitleException;
|
2024-05-15 05:24:43 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use MediaWiki\Title\Title;
|
|
|
|
use MediaWiki\User\UserIdentity;
|
|
|
|
use MessageLocalizer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CitizenComponentUserInfo component
|
|
|
|
*/
|
2024-05-15 06:44:12 +00:00
|
|
|
class CitizenComponentUserInfo implements CitizenComponent {
|
2024-05-15 05:24:43 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $isRegistered;
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
private $isTemp;
|
|
|
|
|
|
|
|
/** @var MessageLocalizer */
|
|
|
|
private $localizer;
|
|
|
|
|
|
|
|
/** @var Title */
|
|
|
|
private $title;
|
|
|
|
|
|
|
|
/** @var UserIdentity */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
private $userPageData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $isRegistered
|
|
|
|
* @param bool $isTemp
|
|
|
|
* @param MessageLocalizer $localizer
|
|
|
|
* @param Title $title
|
|
|
|
* @param UserIdentity $user
|
|
|
|
* @param array $userPageData
|
|
|
|
*/
|
2024-05-15 06:43:50 +00:00
|
|
|
public function __construct(
|
2024-05-15 05:24:43 +00:00
|
|
|
bool $isRegistered,
|
|
|
|
bool $isTemp,
|
|
|
|
MessageLocalizer $localizer,
|
|
|
|
Title $title,
|
|
|
|
UserIdentity $user,
|
|
|
|
array $userPageData
|
|
|
|
) {
|
2024-05-15 06:43:50 +00:00
|
|
|
$this->isRegistered = $isRegistered;
|
|
|
|
$this->isTemp = $isTemp;
|
|
|
|
$this->localizer = $localizer;
|
|
|
|
$this->title = $title;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->userPageData = $userPageData;
|
|
|
|
}
|
2024-05-15 05:24:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user edit count
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
*/
|
2024-05-15 06:44:12 +00:00
|
|
|
private function getUserEditCount(): ?array {
|
2024-05-15 06:43:50 +00:00
|
|
|
// Return user edits
|
2024-05-15 06:44:12 +00:00
|
|
|
$edits = MediaWikiServices::getInstance()->getUserEditTracker()->getUserEditCount( $this->user );
|
2024-05-15 05:24:43 +00:00
|
|
|
|
2024-05-15 06:44:12 +00:00
|
|
|
if ( empty( $edits ) ) {
|
2024-05-15 06:43:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-05-15 05:24:43 +00:00
|
|
|
|
2024-05-17 20:43:35 +00:00
|
|
|
$edits = (string)number_format( $edits, 0 );
|
2024-05-15 14:08:20 +00:00
|
|
|
$label = $this->localizer->msg( 'usereditcount' )->numParams( $edits )->text();
|
2024-05-17 20:43:35 +00:00
|
|
|
$label = str_replace( $edits, '', $label );
|
2024-05-15 05:24:43 +00:00
|
|
|
|
2024-05-15 06:43:50 +00:00
|
|
|
return [
|
2024-05-17 20:43:35 +00:00
|
|
|
'count' => $edits,
|
2024-05-15 06:43:50 +00:00
|
|
|
'label' => $label
|
|
|
|
];
|
|
|
|
}
|
2024-05-15 05:24:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the template data for the user groups
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
*/
|
2024-05-15 06:44:12 +00:00
|
|
|
private function getUserGroups(): ?array {
|
|
|
|
$groups = MediaWikiServices::getInstance()->getUserGroupManager()->getUserGroups( $this->user );
|
2024-05-15 05:24:43 +00:00
|
|
|
|
2024-05-15 06:44:12 +00:00
|
|
|
if ( empty( $groups ) ) {
|
2024-05-15 06:43:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-05-15 05:24:43 +00:00
|
|
|
|
2024-05-15 06:43:50 +00:00
|
|
|
$listItems = [];
|
|
|
|
$msgKey = 'group-%s-member';
|
2024-05-15 06:44:12 +00:00
|
|
|
foreach ( $groups as $group ) {
|
|
|
|
$id = sprintf( $msgKey, $group );
|
|
|
|
$text = $this->localizer->msg( $id )->text();
|
2024-05-15 06:43:50 +00:00
|
|
|
try {
|
2024-05-15 06:44:12 +00:00
|
|
|
$title = $this->title->newFromTextThrow( $text, NS_PROJECT );
|
|
|
|
} catch ( MalformedTitleException $e ) {
|
2024-05-15 06:31:00 +00:00
|
|
|
// ignore
|
|
|
|
}
|
2024-05-15 05:24:43 +00:00
|
|
|
|
2024-05-15 06:44:12 +00:00
|
|
|
if ( !$text || !$title ) {
|
2024-05-15 05:24:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$link = new CitizenComponentLink(
|
2024-05-15 06:43:50 +00:00
|
|
|
$title->getLinkURL(),
|
2024-05-15 06:44:12 +00:00
|
|
|
ucfirst( $text )
|
2024-05-15 05:24:43 +00:00
|
|
|
);
|
|
|
|
|
2024-05-15 06:44:12 +00:00
|
|
|
$listItem = new CitizenComponentMenuListItem( $link, 'citizen-userInfo-usergroup', $id );
|
2024-05-15 05:24:43 +00:00
|
|
|
|
|
|
|
$listItems[] = $listItem->getTemplateData();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2024-05-15 06:43:50 +00:00
|
|
|
'array-list-items' => $listItems
|
2024-05-15 05:24:43 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the template data for the user page menu
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2024-05-15 06:44:12 +00:00
|
|
|
private function getUserPage(): array {
|
2024-05-15 05:24:43 +00:00
|
|
|
$user = $this->user;
|
|
|
|
$userPageData = $this->userPageData;
|
|
|
|
|
|
|
|
$htmlItems = $userPageData['html-items'];
|
|
|
|
$realname = $user->getRealName();
|
2024-05-15 06:44:12 +00:00
|
|
|
if ( !empty( $realname ) ) {
|
2024-05-15 05:24:43 +00:00
|
|
|
$username = $user->getName();
|
|
|
|
$innerHtml = <<<HTML
|
|
|
|
<span id="pt-userpage-realname">$realname</span>
|
|
|
|
<span id="pt-userpage-username">$username</span>
|
|
|
|
HTML;
|
|
|
|
// Dirty but it works
|
|
|
|
$htmlItems = str_replace(
|
|
|
|
">" . $username . "<",
|
|
|
|
">" . $innerHtml . "<",
|
|
|
|
$userPageData['html-items']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-15 06:44:12 +00:00
|
|
|
$menu = new CitizenComponentMenu( [
|
2024-05-15 05:24:43 +00:00
|
|
|
'id' => 'citizen-user-menu-userpage',
|
|
|
|
'class' => null,
|
|
|
|
'label' => null,
|
|
|
|
'html-items' => $htmlItems
|
2024-05-15 06:44:12 +00:00
|
|
|
] );
|
2024-05-15 05:24:43 +00:00
|
|
|
|
|
|
|
return $menu->getTemplateData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2024-05-15 06:44:12 +00:00
|
|
|
public function getTemplateData(): array {
|
2024-05-15 05:24:43 +00:00
|
|
|
$localizer = $this->localizer;
|
|
|
|
$data = [];
|
|
|
|
|
2024-05-15 06:44:12 +00:00
|
|
|
if ( $this->isRegistered ) {
|
2024-05-15 05:24:43 +00:00
|
|
|
$data = [
|
|
|
|
'data-user-page' => $this->getUserPage(),
|
|
|
|
'data-user-edit' => $this->getUserEditCount()
|
|
|
|
];
|
|
|
|
|
2024-05-15 06:44:12 +00:00
|
|
|
if ( $this->isTemp ) {
|
|
|
|
$data['text'] = $localizer->msg( 'citizen-user-info-text-temp' );
|
2024-05-15 05:24:43 +00:00
|
|
|
} else {
|
|
|
|
$data['data-user-groups'] = $this->getUserGroups();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data = [
|
2024-05-15 06:44:12 +00:00
|
|
|
'title' => $localizer->msg( 'notloggedin' ),
|
|
|
|
'text' => $localizer->msg( 'citizen-user-info-text-anon' )
|
2024-05-15 05:24:43 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|