2022-12-07 19:31:14 +00:00
|
|
|
<?php
|
|
|
|
namespace MediaWiki\Skins\Vector\Components;
|
|
|
|
|
2023-01-06 01:45:38 +00:00
|
|
|
use Linker;
|
2023-02-01 23:38:44 +00:00
|
|
|
use MediaWiki\Skin\SkinComponentLink;
|
2023-01-06 01:45:38 +00:00
|
|
|
use MediaWiki\Skins\Vector\Constants;
|
|
|
|
use MediaWiki\Skins\Vector\VectorServices;
|
|
|
|
use Message;
|
|
|
|
use MessageLocalizer;
|
|
|
|
use Title;
|
|
|
|
use User;
|
|
|
|
|
2022-12-07 19:31:14 +00:00
|
|
|
/**
|
|
|
|
* VectorComponentUserLinks component
|
|
|
|
*/
|
|
|
|
class VectorComponentUserLinks implements VectorComponent {
|
2023-01-06 01:45:38 +00:00
|
|
|
/** @var MessageLocalizer */
|
|
|
|
private $localizer;
|
|
|
|
/** @var User */
|
|
|
|
private $user;
|
2023-02-01 23:38:44 +00:00
|
|
|
/** @var array */
|
|
|
|
private $portletData;
|
|
|
|
/** @var array */
|
|
|
|
private $linkOptions;
|
2023-01-06 01:45:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param MessageLocalizer $localizer
|
|
|
|
* @param User $user
|
2023-02-01 23:38:44 +00:00
|
|
|
* @param array $portletData
|
|
|
|
* @param array $linkOptions
|
2023-01-06 01:45:38 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
MessageLocalizer $localizer,
|
|
|
|
User $user,
|
2023-02-01 23:38:44 +00:00
|
|
|
array $portletData,
|
|
|
|
array $linkOptions
|
2023-01-06 01:45:38 +00:00
|
|
|
) {
|
|
|
|
$this->localizer = $localizer;
|
|
|
|
$this->user = $user;
|
2023-02-01 23:38:44 +00:00
|
|
|
$this->portletData = $portletData;
|
|
|
|
$this->linkOptions = $linkOptions;
|
2023-01-06 01:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @return Message
|
|
|
|
*/
|
|
|
|
private function msg( $key ): Message {
|
|
|
|
return $this->localizer->msg( $key );
|
|
|
|
}
|
2022-12-07 19:31:14 +00:00
|
|
|
|
|
|
|
/**
|
2023-02-01 23:38:44 +00:00
|
|
|
* @param bool $isDefaultAnonUserLinks
|
|
|
|
* @param bool $isAnonEditorLinksEnabled
|
|
|
|
* @return VectorComponentDropdown
|
2022-12-07 19:31:14 +00:00
|
|
|
*/
|
2023-02-01 23:38:44 +00:00
|
|
|
private function getDropdown( $isDefaultAnonUserLinks, $isAnonEditorLinksEnabled ) {
|
2023-01-06 01:45:38 +00:00
|
|
|
$user = $this->user;
|
|
|
|
$isAnon = !$user->isRegistered();
|
2023-02-01 23:38:44 +00:00
|
|
|
|
|
|
|
$class = 'vector-user-menu';
|
2023-01-06 01:45:38 +00:00
|
|
|
if ( VectorServices::getFeatureManager()->isFeatureEnabled( Constants::FEATURE_PAGE_TOOLS ) ) {
|
|
|
|
$class .= ' mw-ui-icon-flush-right';
|
|
|
|
}
|
2023-02-01 23:38:44 +00:00
|
|
|
$class .= !$isAnon ?
|
2023-01-06 01:45:38 +00:00
|
|
|
' vector-user-menu-logged-in' :
|
|
|
|
' vector-user-menu-logged-out';
|
|
|
|
|
2023-02-01 23:38:44 +00:00
|
|
|
// Hide entire user links dropdown on larger viewports if it only contains
|
|
|
|
// create account & login link, which are only shown on smaller viewports
|
|
|
|
if ( $isAnon && $isDefaultAnonUserLinks && !$isAnonEditorLinksEnabled ) {
|
|
|
|
$class .= ' user-links-collapsible-item';
|
|
|
|
}
|
|
|
|
|
2023-01-06 01:45:38 +00:00
|
|
|
$tooltip = '';
|
|
|
|
if ( $user->isTemp() ) {
|
|
|
|
$icon = 'userAnonymous';
|
2023-02-01 23:38:44 +00:00
|
|
|
} elseif ( !$isAnon ) {
|
2023-01-06 01:45:38 +00:00
|
|
|
$icon = 'userAvatar';
|
|
|
|
} else {
|
|
|
|
$icon = 'ellipsis';
|
|
|
|
// T287494 We use tooltip messages to provide title attributes on hover over certain menu icons.
|
|
|
|
// For modern Vector, the "tooltip-p-personal" key is set to "User menu" which is appropriate for
|
|
|
|
// the user icon (dropdown indicator for user links menu) for logged-in users.
|
|
|
|
// This overrides the tooltip for the user links menu icon which is an ellipsis for anonymous users.
|
|
|
|
$tooltip = Linker::tooltip( 'vector-anon-user-menu-title' ) ?? '';
|
|
|
|
}
|
2023-02-01 23:38:44 +00:00
|
|
|
|
|
|
|
return new VectorComponentDropdown(
|
2023-02-16 17:06:22 +00:00
|
|
|
'vector-user-links-dropdown', $this->msg( 'personaltools' )->text(), $class, $icon, $tooltip
|
2023-01-06 01:45:38 +00:00
|
|
|
);
|
2023-02-01 23:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $isDefaultAnonUserLinks
|
|
|
|
* @param bool $isAnonEditorLinksEnabled
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getDropdownMenus( $isDefaultAnonUserLinks, $isAnonEditorLinksEnabled ) {
|
|
|
|
$user = $this->user;
|
|
|
|
$isAnon = !$user->isRegistered();
|
|
|
|
$portletData = $this->portletData;
|
|
|
|
|
|
|
|
// Hide default user menu on larger viewports if it only contains
|
|
|
|
// create account & login link, which are only shown on smaller viewports
|
|
|
|
// FIXME: Replace array_merge with an add class helper function
|
|
|
|
$userMenuClass = $portletData[ 'data-user-menu' ][ 'class' ];
|
|
|
|
$userMenuClass = $isAnon && $isDefaultAnonUserLinks ?
|
|
|
|
$userMenuClass . ' user-links-collapsible-item' : $userMenuClass;
|
|
|
|
$dropdownMenus = [
|
|
|
|
new VectorComponentMenu( [
|
|
|
|
'label' => null,
|
|
|
|
'class' => $userMenuClass
|
|
|
|
] + $portletData[ 'data-user-menu' ] )
|
|
|
|
];
|
|
|
|
|
|
|
|
if ( $isAnon ) {
|
|
|
|
// T317789: The `anontalk` and `anoncontribs` links will not be added to
|
|
|
|
// the menu if `$wgGroupPermissions['*']['edit']` === false which can
|
|
|
|
// leave the menu empty due to our removal of other user menu items in
|
|
|
|
// `Hooks::updateUserLinksDropdownItems`. In this case, we do not want
|
|
|
|
// to render the anon "learn more" link.
|
|
|
|
if ( $isAnonEditorLinksEnabled ) {
|
|
|
|
$anonEditorLabelLinkData = [
|
|
|
|
'text' => $this->msg( 'vector-anon-user-menu-pages-learn' )->text(),
|
|
|
|
'href' => Title::newFromText( $this->msg( 'vector-intro-page' )->text() )->getLocalURL(),
|
|
|
|
'aria-label' => $this->msg( 'vector-anon-user-menu-pages-label' )->text(),
|
|
|
|
];
|
|
|
|
$anonEditorLabelLink = new SkinComponentLink(
|
|
|
|
'', $anonEditorLabelLinkData, $this->localizer, $this->linkOptions
|
|
|
|
);
|
|
|
|
$anonEditorLabelLinkHtml = $anonEditorLabelLink->getTemplateData()[ 'html' ];
|
|
|
|
$dropdownMenus[] = new VectorComponentMenu( [
|
|
|
|
'label' => $this->msg( 'vector-anon-user-menu-pages' )->text() . " " . $anonEditorLabelLinkHtml,
|
|
|
|
] + $portletData[ 'data-user-menu-anon-editor' ] );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( isset( $portletData[ 'data-user-menu-logout' ] ) ) {
|
|
|
|
$dropdownMenus[] = new VectorComponentMenu( [
|
|
|
|
'label' => null
|
|
|
|
] + $portletData[ 'data-user-menu-logout' ] );
|
|
|
|
}
|
2023-01-06 01:45:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:38:44 +00:00
|
|
|
return $dropdownMenus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getTemplateData(): array {
|
|
|
|
$portletData = $this->portletData;
|
|
|
|
$user = $this->user;
|
|
|
|
|
|
|
|
$isDefaultAnonUserLinks = count( $portletData['data-user-menu']['array-items'] ) === 2;
|
|
|
|
$isAnonEditorLinksEnabled = isset( $portletData['data-user-menu-anon-editor']['is-empty'] )
|
|
|
|
&& !$portletData['data-user-menu-anon-editor']['is-empty'];
|
|
|
|
|
|
|
|
$overflowMenu = new VectorComponentMenu( [
|
|
|
|
'label' => null,
|
|
|
|
] + $portletData[ 'data-vector-user-menu-overflow' ] );
|
|
|
|
|
|
|
|
return [
|
2023-01-06 01:45:38 +00:00
|
|
|
'is-temp-user' => $user->isTemp(),
|
|
|
|
'is-wide' => count( $overflowMenu ) > 3,
|
2023-02-01 22:09:18 +00:00
|
|
|
'data-user-links-overflow-menu' => $overflowMenu->getTemplateData(),
|
2023-02-01 23:38:44 +00:00
|
|
|
'data-user-links-dropdown' => $this->getDropdown( $isDefaultAnonUserLinks, $isAnonEditorLinksEnabled )
|
|
|
|
->getTemplateData(),
|
|
|
|
'data-user-links-dropdown-menus' => array_map( static function ( $menu ) {
|
|
|
|
return $menu->getTemplateData();
|
|
|
|
}, $this->getDropdownMenus( $isDefaultAnonUserLinks, $isAnonEditorLinksEnabled ) ),
|
2023-01-06 01:45:38 +00:00
|
|
|
];
|
2022-12-07 19:31:14 +00:00
|
|
|
}
|
|
|
|
}
|