mirror of
https://github.com/StarCitizenTools/mediawiki-skins-Citizen.git
synced 2024-11-28 00:01:05 +00:00
refactor(core): ♻️ drop polyfill related to user menu
This commit is contained in:
parent
43eae7e916
commit
a31babc297
|
@ -94,8 +94,12 @@ class SkinHooks implements
|
|||
*/
|
||||
public function onSkinTemplateNavigation__Universal( $sktemplate, &$links ): void {
|
||||
// Remove userpage from user menu since it is used in user menu info
|
||||
if ( $links['user-menu']['userpage'] ) {
|
||||
if ( isset( $links['user-menu']['userpage'] ) ) {
|
||||
unset( $links['user-menu']['userpage'] );
|
||||
}
|
||||
|
||||
if ( isset( $links['user-menu']['anonuserpage'] ) ) {
|
||||
unset( $links['user-menu']['anonuserpage'] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,29 +33,9 @@ use User;
|
|||
* Header partial of Skin Citizen
|
||||
* Generates the following partials:
|
||||
* - Personal Menu
|
||||
* - Extra Tools
|
||||
* - Search
|
||||
*/
|
||||
final class Header extends Partial {
|
||||
/**
|
||||
* Build Personal Tools menu
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function buildPersonalMenu(): array {
|
||||
$skin = $this->skin;
|
||||
|
||||
$personalTools = $skin->getPersonalToolsForMakeListItem(
|
||||
$skin->buildPersonalUrlsPublic()
|
||||
);
|
||||
|
||||
$header = $this->getPersonalHeaderData( $personalTools );
|
||||
|
||||
return [
|
||||
'data-personal-menu-header' => $header,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorate search box template data
|
||||
*
|
||||
|
@ -70,94 +50,107 @@ final class Header extends Partial {
|
|||
}
|
||||
|
||||
/**
|
||||
* Decorate the personal menu
|
||||
*
|
||||
* @param array $personalTools The original personal tools urls
|
||||
* Get the user info template data for user menu
|
||||
*
|
||||
* @param $userPageData data-portlets.data-user-page
|
||||
* @return array
|
||||
*/
|
||||
private function getPersonalHeaderData( $personalTools ): array {
|
||||
$skin = $this->skin;
|
||||
$user = $this->user;
|
||||
$header = [];
|
||||
public function getUserInfoData( $userPageData ): array {
|
||||
$isRegistered = $this->user->isRegistered();
|
||||
|
||||
if ( $user->isRegistered() ) {
|
||||
$header += [
|
||||
'userpage' => $personalTools['userpage'] ?? null,
|
||||
'usergroups' => $this->getUserGroupsData( $personalTools, $user ),
|
||||
'usercontris' => $this->getUserContributionsData( $user ),
|
||||
];
|
||||
$html = $this->getUserPageHTML( $isRegistered, $userPageData );
|
||||
|
||||
if ( $isRegistered ) {
|
||||
$html .= $this->getUserGroupsHTML();
|
||||
$html .= $this->getUserContributionsHTML();
|
||||
}
|
||||
|
||||
return $skin->getPortletData( 'personal-header', array_filter( $header ) );
|
||||
return [
|
||||
'id' => 'p-user-info',
|
||||
'html-items' => $html,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Build and return user groups data
|
||||
* Get the user page HTML
|
||||
*
|
||||
* @param array $personalTools The original personal tools urls
|
||||
* @param User $user
|
||||
*
|
||||
* @return array|null
|
||||
* @param bool $isRegistered
|
||||
* @param array $userPageData data-portlets.data-user-page
|
||||
* @return string
|
||||
*/
|
||||
private function getUserGroupsData( $personalTools, $user ): ?array {
|
||||
private function getUserPageHTML( $isRegistered, $userPageData ): ?string {
|
||||
if ( $isRegistered ) {
|
||||
$html = $userPageData['html-items'];
|
||||
} else {
|
||||
// There must be a cleaner way to do this
|
||||
$msg = $this->skin->msg( 'notloggedin' )->text();
|
||||
$tooltip = $this->skin->msg( 'tooltip-pt-anonuserpage' )->text();
|
||||
$html = <<<HTML
|
||||
<li id="pt-anonuserpage" class="mw-list-item">
|
||||
<span title="$tooltip">$msg</span>
|
||||
</li>
|
||||
HTML;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user groups HTML
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function getUserGroupsHTML(): ?string {
|
||||
// This does not return implicit groups
|
||||
$groups = MediaWikiServices::getInstance()->getUserGroupManager()->getUserGroups( $user );
|
||||
$groups = MediaWikiServices::getInstance()->getUserGroupManager()->getUserGroups( $this->user );
|
||||
|
||||
if ( empty( $groups ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$skin = $this->skin;
|
||||
$title = $this->title;
|
||||
|
||||
// Add user group
|
||||
$groupLinks = [];
|
||||
$html = '';
|
||||
$msgName = 'group-%s';
|
||||
|
||||
// There must be a cleaner way
|
||||
foreach ( $groups as $group ) {
|
||||
$groupPage = $title->newFromText(
|
||||
$skin->msg( sprintf( $msgName, $group ) )->text(),
|
||||
NS_PROJECT
|
||||
);
|
||||
|
||||
$groupLinks[$group] = [
|
||||
'msg' => sprintf( $msgName, $group ),
|
||||
// Nullpointer should not happen
|
||||
'href' => $groupPage->getLinkURL(),
|
||||
'tooltiponly' => true,
|
||||
'id' => sprintf( $msgName, $group ),
|
||||
// 'exists' => $groupPage->exists() - This will add an additional DB call
|
||||
];
|
||||
$id = sprintf( $msgName, $group );
|
||||
$msg = $this->skin->msg( $id )->text();
|
||||
// Nullpointer should not happen
|
||||
$href = $this->title->newFromText(
|
||||
$this->skin->msg( sprintf( $msgName, $group ) )->text(),
|
||||
NS_PROJECT
|
||||
)->getLinkURL();
|
||||
$html .= <<< HTML
|
||||
<li>
|
||||
<a href="$href" id="$id">$msg</a>
|
||||
</li>
|
||||
HTML;
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => 'pt-usergroups',
|
||||
'links' => $groupLinks
|
||||
];
|
||||
$html = sprintf( '<li id="pt-usergroups"><ul>%s</ul></li>', $html );
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build and return user contributions data
|
||||
* Get the user contributions HTML
|
||||
*
|
||||
* @param User $user
|
||||
*
|
||||
* @return array|null
|
||||
* @return string|null
|
||||
*/
|
||||
private function getUserContributionsData( $user ): ?array {
|
||||
private function getUserContributionsHTML(): ?string {
|
||||
// Return user edits
|
||||
$edits = MediaWikiServices::getInstance()->getUserEditTracker()->getUserEditCount( $user );
|
||||
$edits = MediaWikiServices::getInstance()->getUserEditTracker()->getUserEditCount( $this->user );
|
||||
|
||||
if ( empty( $edits ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$skin = $this->skin;
|
||||
$editsText = $this->skin->msg( 'usereditcount' )
|
||||
->numParams( sprintf( '%s', number_format( $edits, 0 ) ) );
|
||||
|
||||
return [
|
||||
'text' => $skin->msg( 'usereditcount' )
|
||||
->numParams( sprintf( '%s', number_format( $edits, 0 ) ) ),
|
||||
'id' => 'pt-usercontris'
|
||||
];
|
||||
// There must be a cleaner way
|
||||
$html = '<li id="pt-usercontris">' . $editsText . '</li>';
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -94,10 +94,8 @@ class SkinCitizen extends SkinMustache {
|
|||
// Booleans
|
||||
'toc-enabled' => $out->isTOCEnabled(),
|
||||
// Data objects
|
||||
'data-header' => [
|
||||
'data-personal-menu' => $header->buildPersonalMenu(),
|
||||
],
|
||||
'data-sitestats' => $drawer->getSiteStatsData(),
|
||||
'data-user-info' => $header->getUserInfoData( $parentData['data-portlets']['data-user-page'] ),
|
||||
// HTML strings
|
||||
'html-title-heading--formatted' => $pageTitle->buildTitle( $parentData, $title ),
|
||||
'html-citizen-jumptotop' => $this->msg( 'citizen-jumptotop' )->text() . ' [home]',
|
||||
|
@ -124,15 +122,6 @@ class SkinCitizen extends SkinMustache {
|
|||
return array_merge( $parentData, $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Change access to public, as it is used in partials
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
final public function buildPersonalUrlsPublic() {
|
||||
return parent::buildPersonalUrls();
|
||||
}
|
||||
|
||||
/**
|
||||
* Change access to public, as it is used in partials
|
||||
*
|
||||
|
@ -144,104 +133,6 @@ class SkinCitizen extends SkinMustache {
|
|||
return parent::wrapHTML( $title, $html );
|
||||
}
|
||||
|
||||
/**
|
||||
* Polyfill for 1.35 from SkinTemplate
|
||||
*
|
||||
* @since 1.36
|
||||
* @stable for overriding
|
||||
* @param string $name of the portal e.g. p-personal the name is personal.
|
||||
* @param array $items that are accepted input to Skin::makeListItem
|
||||
* @return array data that can be passed to a Mustache template that
|
||||
* represents a single menu.
|
||||
*/
|
||||
public function getPortletData( $name, array $items ) {
|
||||
// Monobook and Vector historically render this portal as an element with ID p-cactions
|
||||
// This inconsistency is regretful from a code point of view
|
||||
// However this ensures compatibility with gadgets.
|
||||
// In future we should port p-#cactions to #p-actions and drop this rename.
|
||||
if ( $name === 'actions' ) {
|
||||
$name = 'cactions';
|
||||
}
|
||||
|
||||
// user-menu is the new personal tools, without the notifications.
|
||||
// A lot of user code and gadgets relies on it being named personal.
|
||||
// This allows it to function as a drop-in replacement.
|
||||
if ( $name === 'user-menu' ) {
|
||||
$name = 'personal';
|
||||
}
|
||||
|
||||
$legacyClasses = '';
|
||||
if ( $name === 'category-normal' ) {
|
||||
// retain historic category IDs and classes
|
||||
$id = 'mw-normal-catlinks';
|
||||
$legacyClasses .= ' mw-normal-catlinks';
|
||||
} elseif ( $name === 'category-hidden' ) {
|
||||
// retain historic category IDs and classes
|
||||
$id = 'mw-hidden-catlinks';
|
||||
$legacyClasses .= ' mw-hidden-catlinks mw-hidden-cats-hidden';
|
||||
} else {
|
||||
$id = Sanitizer::escapeIdForAttribute( "p-$name" );
|
||||
}
|
||||
|
||||
$data = [
|
||||
'id' => $id,
|
||||
'class' => 'mw-portlet ' . Sanitizer::escapeClass( "mw-portlet-$name" ) . $legacyClasses,
|
||||
'html-tooltip' => Linker::tooltip( $id ),
|
||||
'html-items' => '',
|
||||
// Will be populated by SkinAfterPortlet hook.
|
||||
'html-after-portal' => '',
|
||||
'html-before-portal' => '',
|
||||
];
|
||||
// Run the SkinAfterPortlet
|
||||
// hook and if content is added appends it to the html-after-portal
|
||||
// for output.
|
||||
// Currently in production this supports the wikibase 'edit' link.
|
||||
$content = $this->getAfterPortlet( $name );
|
||||
if ( $content !== '' ) {
|
||||
$data['html-after-portal'] = Html::rawElement(
|
||||
'div',
|
||||
[
|
||||
'class' => [
|
||||
'after-portlet',
|
||||
Sanitizer::escapeClass( "after-portlet-$name" ),
|
||||
],
|
||||
],
|
||||
$content
|
||||
);
|
||||
}
|
||||
|
||||
foreach ( $items as $key => $item ) {
|
||||
$data['html-items'] .= $this->makeListItem( $key, $item );
|
||||
}
|
||||
|
||||
$data['label'] = $this->getPortletLabel( $name );
|
||||
$data['is-empty'] = count( $items ) === 0 && $content === '';
|
||||
$data['class'] .= $data['is-empty'] ? ' emptyPortlet' : '';
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Polyfill for 1.35 from SkinTemplate
|
||||
*
|
||||
* @since 1.36
|
||||
* @param string $name of the portal e.g. p-personal the name is personal.
|
||||
* @return string that is human readable corresponding to the menu
|
||||
*/
|
||||
public function getPortletLabel( $name ) {
|
||||
// For historic reasons for some menu items,
|
||||
// there is no language key corresponding with its menu key.
|
||||
$mappings = [
|
||||
'tb' => 'toolbox',
|
||||
'personal' => 'personaltools',
|
||||
'lang' => 'otherlanguages',
|
||||
];
|
||||
|
||||
$msgObj = $this->msg( $mappings[ $name ] ?? $name );
|
||||
// If no message exists fallback to plain text (T252727)
|
||||
$labelText = $msgObj->exists() ? $msgObj->text() : $name;
|
||||
return $labelText;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
|
|
|
@ -100,10 +100,6 @@
|
|||
font-weight: 500;
|
||||
}
|
||||
}
|
||||
|
||||
&-anonuserpage {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
#pt-login {
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
<div class="citizen-header__start"></div>
|
||||
<div class="citizen-header__end">
|
||||
{{#data-portlets.data-notifications}}{{>Menu}}{{/data-portlets.data-notifications}}
|
||||
{{#data-personal-menu}}{{>PersonalMenu}}{{/data-personal-menu}}
|
||||
{{>PersonalMenu}}
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
|
|
@ -10,7 +10,9 @@
|
|||
aria-labelledby="citizen-personalMenu__buttonCheckbox"
|
||||
aria-haspopup="true">
|
||||
<aside id="citizen-personalMenu__card" class="citizen-personalMenu__card mw-checkbox-hack-target">
|
||||
{{#data-personal-menu-header}}{{>Menu}}{{/data-personal-menu-header}}
|
||||
{{#data-user-info}}
|
||||
<div class="citizen-userMenu__header">{{>Menu}}</div>
|
||||
{{/data-user-info}}
|
||||
{{#data-portlets.data-user-menu}}{{>Menu}}{{/data-portlets.data-user-menu}}
|
||||
</aside>
|
||||
<label
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
object data-footer for footer template partial. see Footer.mustache for documentation.
|
||||
}}
|
||||
|
||||
{{#data-header}}{{>Header}}{{/data-header}}
|
||||
{{>Header}}
|
||||
<div class="citizen-page-container">
|
||||
<div id="siteNotice">{{{html-site-notice}}}</div>
|
||||
<main class="mw-body {{#toc-enabled}}citizen-toc-enabled{{/toc-enabled}}" id="content">
|
||||
|
|
Loading…
Reference in a new issue