mediawiki-skins-Citizen/includes/Partials/Header.php

164 lines
3.9 KiB
PHP
Raw Normal View History

<?php
/**
* Citizen - A responsive skin developed for the Star Citizen Wiki
*
* This file is part of Citizen.
*
* Citizen is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Citizen is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Citizen. If not, see <https://www.gnu.org/licenses/>.
*
* @file
* @ingroup Skins
*/
2021-01-28 11:21:58 +00:00
declare( strict_types=1 );
namespace MediaWiki\Skins\Citizen\Partials;
use MediaWiki\MediaWikiServices;
use Skin;
use User;
/**
* Header partial of Skin Citizen
* Generates the following partials:
* - Personal Menu
* - Extra Tools
* - Search
*/
2021-01-28 19:34:46 +00:00
final class Header extends Partial {
2021-01-28 11:21:58 +00:00
/**
* Build Personal Tools menu
*
* @return array
*/
public function buildPersonalMenu(): array {
$skin = $this->skin;
$personalTools = $skin->getPersonalToolsForMakeListItem(
$skin->buildPersonalUrlsPublic()
2021-01-28 11:21:58 +00:00
);
$header = $this->getPersonalHeaderData( $personalTools );
2021-01-28 11:21:58 +00:00
return [
'data-personal-menu-header' => $header,
2021-01-28 11:21:58 +00:00
];
}
/**
* Decorate search box template data
2021-01-28 11:21:58 +00:00
*
* @param array $searchBoxData original data-search-box
2021-01-28 11:21:58 +00:00
* @return array
*/
public function decorateSearchBoxData( $searchBoxData ): array {
return $searchBoxData += [
'msg-citizen-search-toggle-shortcut' => '[/]',
2021-01-28 11:21:58 +00:00
'html-random-href' => Skin::makeSpecialUrl( 'Randompage' ),
];
}
/**
* Decorate the personal menu
2021-01-28 11:21:58 +00:00
*
* @param array $personalTools The original personal tools urls
2021-01-28 11:21:58 +00:00
*
* @return array
2021-01-28 11:21:58 +00:00
*/
private function getPersonalHeaderData( $personalTools ): array {
$skin = $this->skin;
$user = $this->user;
$header = [];
if ( $user->isRegistered() ) {
$header += [
'userpage' => $personalTools['userpage'] ?? null,
'usergroups' => $this->getUserGroupsData( $personalTools, $user ),
'usercontris' => $this->getUserContributionsData( $user ),
];
}
return $skin->getPortletData( 'personal-header', array_filter( $header ) );
}
2021-01-28 11:21:58 +00:00
/**
* Build and return user groups data
*
* @param array $personalTools The original personal tools urls
* @param User $user
*
* @return array|null
*/
private function getUserGroupsData( $personalTools, $user ): ?array {
2021-01-28 11:21:58 +00:00
// This does not return implicit groups
$groups = MediaWikiServices::getInstance()->getUserGroupManager()->getUserGroups( $user );
if ( empty( $groups ) ) {
return null;
}
2021-01-28 11:21:58 +00:00
$skin = $this->skin;
$title = $this->title;
// Add user group
$groupLinks = [];
$msgName = 'group-%s';
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
2021-01-28 11:21:58 +00:00
];
}
return [
'id' => 'pt-usergroups',
'links' => $groupLinks
];
}
/**
* Build and return user contributions data
*
* @param User $user
*
* @return array|null
*/
private function getUserContributionsData( $user ): ?array {
// Return user edits
$edits = MediaWikiServices::getInstance()->getUserEditTracker()->getUserEditCount( $user );
2021-01-28 11:21:58 +00:00
if ( empty( $edits ) ) {
return null;
2021-01-28 11:21:58 +00:00
}
$skin = $this->skin;
return [
'text' => $skin->msg( 'usereditcount' )
->numParams( sprintf( '%s', number_format( $edits, 0 ) ) ),
'id' => 'pt-usercontris'
];
2021-01-28 11:21:58 +00:00
}
}