| 1 |
<?php |
| 2 |
namespace MediaWiki\Skins\Vector\Components; |
| 3 |
|
| 4 |
use MediaWiki\Linker\Linker; |
| 5 |
use MediaWiki\Message\Message; |
| 6 |
use MediaWiki\Skin\SkinComponentLink; |
| 7 |
use MediaWiki\Title\MalformedTitleException; |
| 8 |
use MediaWiki\Title\Title; |
| 9 |
use MediaWiki\User\UserIdentity; |
| 10 |
use MessageLocalizer; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class VectorComponentUserLinks implements VectorComponent { |
| 16 |
|
| 17 |
private const BUTTON_CLASSES = 'cdx-button cdx-button--fake-button ' |
| 18 |
. 'cdx-button--fake-button--enabled cdx-button--weight-quiet'; |
| 19 |
private const ICON_ONLY_BUTTON_CLASS = 'cdx-button--icon-only'; |
| 20 |
|
| 21 |
|
| 22 |
private $localizer; |
| 23 |
|
| 24 |
private $user; |
| 25 |
|
| 26 |
private $portletData; |
| 27 |
|
| 28 |
private $linkOptions; |
| 29 |
|
| 30 |
private $userIcon; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function __construct( |
| 40 |
MessageLocalizer $localizer, |
| 41 |
UserIdentity $user, |
| 42 |
array $portletData, |
| 43 |
array $linkOptions, |
| 44 |
string $userIcon = 'userAvatar' |
| 45 |
) { |
| 46 |
$this->localizer = $localizer; |
| 47 |
$this->user = $user; |
| 48 |
$this->portletData = $portletData; |
| 49 |
$this->linkOptions = $linkOptions; |
| 50 |
$this->userIcon = $userIcon; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private function msg( $key ): Message { |
| 58 |
return $this->localizer->msg( $key ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private function getDropdown( $isDefaultAnonUserLinks, $isAnonEditorLinksEnabled, $userLinksCount ) { |
| 68 |
$user = $this->user; |
| 69 |
$isAnon = !$user->isRegistered(); |
| 70 |
|
| 71 |
$class = 'vector-user-menu'; |
| 72 |
$class .= ' vector-button-flush-right'; |
| 73 |
$class .= !$isAnon ? |
| 74 |
' vector-user-menu-logged-in' : |
| 75 |
' vector-user-menu-logged-out'; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
if ( $isAnon && $isDefaultAnonUserLinks && !$isAnonEditorLinksEnabled ) { |
| 80 |
$linkclass = ' user-links-collapsible-item'; |
| 81 |
|
| 82 |
if ( $userLinksCount === 0 ) { |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
$linkclass .= '--none'; |
| 87 |
} |
| 88 |
|
| 89 |
$class .= $linkclass; |
| 90 |
} |
| 91 |
|
| 92 |
$tooltip = ''; |
| 93 |
$icon = $this->userIcon; |
| 94 |
if ( $icon === '' && $userLinksCount ) { |
| 95 |
$icon = 'ellipsis'; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
$tooltip = Linker::tooltip( 'vector-anon-user-menu-title' ) ?? ''; |
| 101 |
} |
| 102 |
|
| 103 |
return new VectorComponentDropdown( |
| 104 |
'vector-user-links-dropdown', $this->msg( 'personaltools' )->text(), $class, $icon, $tooltip |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
private function getMenus( $isDefaultAnonUserLinks, $isAnonEditorLinksEnabled ) { |
| 114 |
$user = $this->user; |
| 115 |
$isAnon = !$user->isRegistered(); |
| 116 |
$portletData = $this->portletData; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
$userMenuClass = $portletData[ 'data-user-menu' ][ 'class' ]; |
| 122 |
$userMenuClass = $isAnon && $isDefaultAnonUserLinks ? |
| 123 |
$userMenuClass . ' user-links-collapsible-item' : $userMenuClass; |
| 124 |
$dropdownMenus = [ |
| 125 |
new VectorComponentMenu( [ |
| 126 |
'label' => null, |
| 127 |
'class' => $userMenuClass |
| 128 |
] + $portletData[ 'data-user-menu' ] ) |
| 129 |
]; |
| 130 |
|
| 131 |
if ( $isAnon ) { |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if ( $isAnonEditorLinksEnabled ) { |
| 138 |
$anonUserMenuData = $portletData[ 'data-user-menu-anon-editor' ]; |
| 139 |
try { |
| 140 |
$anonEditorLabelLinkData = [ |
| 141 |
'text' => $this->msg( 'vector-anon-user-menu-pages-learn' )->text(), |
| 142 |
'href' => Title::newFromTextThrow( $this->msg( 'vector-intro-page' )->text() )->getLocalURL(), |
| 143 |
'aria-label' => $this->msg( 'vector-anon-user-menu-pages-label' )->text(), |
| 144 |
]; |
| 145 |
$anonEditorLabelLink = new SkinComponentLink( |
| 146 |
'', $anonEditorLabelLinkData, $this->localizer, $this->linkOptions |
| 147 |
); |
| 148 |
$anonEditorLabelLinkHtml = $anonEditorLabelLink->getTemplateData()[ 'html' ]; |
| 149 |
$anonUserMenuData['html-label'] = $this->msg( 'vector-anon-user-menu-pages' )->escaped() . |
| 150 |
" " . $anonEditorLabelLinkHtml; |
| 151 |
$anonUserMenuData['label'] = null; |
| 152 |
} catch ( MalformedTitleException $e ) { |
| 153 |
|
| 154 |
} |
| 155 |
$dropdownMenus[] = new VectorComponentMenu( $anonUserMenuData ); |
| 156 |
} |
| 157 |
} else { |
| 158 |
|
| 159 |
$isLogoutLinkEnabled = isset( $portletData[ 'data-user-menu-logout' ][ 'is-empty' ] ) && |
| 160 |
!$portletData[ 'data-user-menu-logout'][ 'is-empty' ]; |
| 161 |
if ( $isLogoutLinkEnabled ) { |
| 162 |
$dropdownMenus[] = new VectorComponentMenu( [ |
| 163 |
'label' => null |
| 164 |
] + $portletData[ 'data-user-menu-logout' ] ); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return $dropdownMenus; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
private static function stripIcons( array $arrayListItems ) { |
| 178 |
return array_map( static function ( $item ) { |
| 179 |
$item['array-links'] = array_map( static function ( $link ) { |
| 180 |
$link['icon'] = null; |
| 181 |
return $link; |
| 182 |
}, $item['array-links'] ); |
| 183 |
return $item; |
| 184 |
}, $arrayListItems ); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private static function makeLinksButtons( $arrayListItems, $iconOnlyButton = true, $exceptions = [] ) { |
| 196 |
return array_map( static function ( $item ) use ( $iconOnlyButton, $exceptions ) { |
| 197 |
if ( in_array( $item[ 'name'], $exceptions ) ) { |
| 198 |
return $item; |
| 199 |
} |
| 200 |
$item['array-links'] = array_map( static function ( $link ) use ( $iconOnlyButton ) { |
| 201 |
$link['array-attributes'] = array_map( static function ( $attribute ) use ( $iconOnlyButton ) { |
| 202 |
if ( $attribute['key'] === 'class' ) { |
| 203 |
$newClass = $attribute['value'] . ' ' . self::BUTTON_CLASSES; |
| 204 |
if ( $iconOnlyButton ) { |
| 205 |
$newClass .= ' ' . self::ICON_ONLY_BUTTON_CLASS; |
| 206 |
} |
| 207 |
$attribute['value'] = $newClass; |
| 208 |
} |
| 209 |
return $attribute; |
| 210 |
}, $link['array-attributes'] ); |
| 211 |
return $link; |
| 212 |
}, $item['array-links'] ); |
| 213 |
return $item; |
| 214 |
}, $arrayListItems ); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
private static function makeItemsCollapsible( $arrayListItems ) { |
| 224 |
return array_map( static function ( $item ) { |
| 225 |
$item['class'] .= ' user-links-collapsible-item'; |
| 226 |
return $item; |
| 227 |
}, $arrayListItems ); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
private static function getOverflowMenuClass( $arrayListItems ) { |
| 237 |
$overflowMenuClass = 'mw-portlet'; |
| 238 |
if ( count( $arrayListItems ) === 0 ) { |
| 239 |
$overflowMenuClass .= ' emptyPortlet'; |
| 240 |
} |
| 241 |
return $overflowMenuClass; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public function getTemplateData(): array { |
| 248 |
$portletData = $this->portletData; |
| 249 |
|
| 250 |
$userLinksCount = count( $portletData['data-user-menu']['array-items'] ); |
| 251 |
$isDefaultAnonUserLinks = $userLinksCount <= 3; |
| 252 |
$isAnonEditorLinksEnabled = isset( $portletData['data-user-menu-anon-editor']['is-empty'] ) |
| 253 |
&& !$portletData['data-user-menu-anon-editor']['is-empty']; |
| 254 |
|
| 255 |
$userInterfacePreferences = $this->makeLinksButtons( |
| 256 |
$this->makeItemsCollapsible( |
| 257 |
$portletData[ 'data-user-interface-preferences' ]['array-items'] ?? [] |
| 258 |
), |
| 259 |
false |
| 260 |
); |
| 261 |
$userPage = $this->makeItemsCollapsible( |
| 262 |
$this->stripIcons( $portletData[ 'data-user-page' ]['array-items'] ?? [] ) |
| 263 |
); |
| 264 |
$notifications = $this->makeLinksButtons( |
| 265 |
$portletData[ 'data-notifications' ]['array-items'] ?? [], |
| 266 |
true, |
| 267 |
[ 'talk-alert' ] |
| 268 |
); |
| 269 |
|
| 270 |
$overflow = $this->makeItemsCollapsible( |
| 271 |
array_map( |
| 272 |
static function ( $item ) { |
| 273 |
|
| 274 |
$item['id'] .= '-2'; |
| 275 |
|
| 276 |
if ( $item['name'] === 'watchlist' ) { |
| 277 |
$item['icon'] = 'watchlist'; |
| 278 |
} |
| 279 |
return $item; |
| 280 |
}, |
| 281 |
|
| 282 |
array_values( |
| 283 |
array_filter( |
| 284 |
$portletData['data-user-menu']['array-items'] ?? [], |
| 285 |
static function ( $item ) { |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
$name = $item['name']; |
| 291 |
return in_array( $name, [ 'watchlist', 'createaccount', 'login', 'login-private' ] ); |
| 292 |
} |
| 293 |
) |
| 294 |
) |
| 295 |
) |
| 296 |
); |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
if ( $this->user->isRegistered() ) { |
| 301 |
$overflow = $this->makeLinksButtons( $overflow ); |
| 302 |
} |
| 303 |
|
| 304 |
$preferencesMenu = new VectorComponentMenu( [ |
| 305 |
'id' => 'p-vector-user-menu-preferences', |
| 306 |
'class' => self::getOverflowMenuClass( $userInterfacePreferences ), |
| 307 |
'label' => null, |
| 308 |
'html-items' => null, |
| 309 |
'array-list-items' => $userInterfacePreferences, |
| 310 |
] ); |
| 311 |
$userPageMenu = new VectorComponentMenu( [ |
| 312 |
'id' => 'p-vector-user-menu-userpage', |
| 313 |
'class' => self::getOverflowMenuClass( $userPage ), |
| 314 |
'label' => null, |
| 315 |
'html-items' => null, |
| 316 |
'array-list-items' => $userPage, |
| 317 |
] ); |
| 318 |
$notificationsMenu = new VectorComponentMenu( [ |
| 319 |
'id' => 'p-vector-user-menu-notifications', |
| 320 |
'class' => self::getOverflowMenuClass( $notifications ), |
| 321 |
'label' => null, |
| 322 |
'html-items' => null, |
| 323 |
'array-list-items' => $notifications, |
| 324 |
] ); |
| 325 |
$overflowMenu = new VectorComponentMenu( [ |
| 326 |
'id' => 'p-vector-user-menu-overflow', |
| 327 |
'class' => self::getOverflowMenuClass( $overflow ), |
| 328 |
'label' => null, |
| 329 |
'html-items' => null, |
| 330 |
'array-list-items' => $overflow, |
| 331 |
] ); |
| 332 |
|
| 333 |
return [ |
| 334 |
'is-wide' => array_filter( |
| 335 |
[ $overflow, $notifications, $userPage, $userInterfacePreferences ] |
| 336 |
) !== [], |
| 337 |
'data-user-links-notifications' => $notificationsMenu->getTemplateData(), |
| 338 |
'data-user-links-overflow' => $overflowMenu->getTemplateData(), |
| 339 |
'data-user-links-preferences' => $preferencesMenu->getTemplateData(), |
| 340 |
'data-user-links-user-page' => $userPageMenu->getTemplateData(), |
| 341 |
'data-user-links-dropdown' => $this->getDropdown( |
| 342 |
$isDefaultAnonUserLinks, $isAnonEditorLinksEnabled, $userLinksCount )->getTemplateData(), |
| 343 |
'data-user-links-menus' => array_map( static function ( $menu ) { |
| 344 |
return $menu->getTemplateData(); |
| 345 |
}, $this->getMenus( $isDefaultAnonUserLinks, $isAnonEditorLinksEnabled ) ), |
| 346 |
]; |
| 347 |
} |
| 348 |
} |