2021-01-28 11:19:50 +00:00
|
|
|
<?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 );
|
2021-01-28 11:19:50 +00:00
|
|
|
|
2022-05-26 20:54:52 +00:00
|
|
|
namespace MediaWiki\Skins\Citizen\Partials;
|
2021-01-28 11:19:50 +00:00
|
|
|
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use MWException;
|
|
|
|
use Skin;
|
2021-01-28 11:27:55 +00:00
|
|
|
use SpecialPage;
|
2022-05-22 19:06:49 +00:00
|
|
|
use User;
|
2021-01-28 11:19:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2022-05-22 19:06:49 +00:00
|
|
|
$skin = $this->skin;
|
|
|
|
|
|
|
|
$personalTools = $skin->getPersonalToolsForMakeListItem(
|
|
|
|
$skin->buildPersonalUrlsPublic()
|
2021-01-28 11:21:58 +00:00
|
|
|
);
|
|
|
|
|
2022-05-23 20:10:14 +00:00
|
|
|
$header = $this->getPersonalHeaderData( $personalTools );
|
|
|
|
// We need userpage for personal header
|
|
|
|
if ( isset( $personalTools['userpage'] ) ) {
|
|
|
|
unset( $personalTools['userpage'] );
|
|
|
|
}
|
|
|
|
|
2021-03-12 02:09:24 +00:00
|
|
|
// Move the Echo badges out of default list
|
2022-05-17 02:52:04 +00:00
|
|
|
// TODO: Remove notifications since MW 1.36 from buildPersonalUrls
|
2021-01-28 11:21:58 +00:00
|
|
|
if ( isset( $personalTools['notifications-alert'] ) ) {
|
|
|
|
unset( $personalTools['notifications-alert'] );
|
|
|
|
}
|
|
|
|
if ( isset( $personalTools['notifications-notice'] ) ) {
|
|
|
|
unset( $personalTools['notifications-notice'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2022-05-23 20:10:14 +00:00
|
|
|
'data-personal-menu-header' => $header,
|
|
|
|
'data-personal-menu-list' => $skin->getPortletData( 'personal', $personalTools ),
|
2021-01-28 11:21:58 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-12 02:09:24 +00:00
|
|
|
* Echo notification badges button
|
2021-01-28 11:21:58 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-05-17 02:52:04 +00:00
|
|
|
public function getNotifications(): array {
|
2022-05-22 19:06:49 +00:00
|
|
|
$skin = $this->skin;
|
|
|
|
|
|
|
|
$personalTools = $skin->getPersonalToolsForMakeListItem(
|
|
|
|
$skin->buildPersonalUrlsPublic()
|
2021-01-28 11:21:58 +00:00
|
|
|
);
|
|
|
|
|
2021-03-12 02:09:24 +00:00
|
|
|
// Create the Echo badges
|
2022-05-17 02:52:04 +00:00
|
|
|
$notifications = [];
|
2021-01-28 11:21:58 +00:00
|
|
|
if ( isset( $personalTools['notifications-alert'] ) ) {
|
2022-05-17 02:52:04 +00:00
|
|
|
$notifications['notifications-alert'] = $personalTools['notifications-alert'];
|
2021-01-28 11:21:58 +00:00
|
|
|
}
|
|
|
|
if ( isset( $personalTools['notifications-notice'] ) ) {
|
2022-05-17 02:52:04 +00:00
|
|
|
$notifications['notifications-notice'] = $personalTools['notifications-notice'];
|
2021-01-28 11:21:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 19:06:49 +00:00
|
|
|
$html = $skin->getPortletData( 'notifications', $notifications );
|
2021-01-28 11:21:58 +00:00
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the search box
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
2021-07-30 02:49:01 +00:00
|
|
|
public function buildSearchProps(): array {
|
2022-05-22 19:06:49 +00:00
|
|
|
$skin = $this->skin;
|
|
|
|
|
|
|
|
$toggleMsg = $skin->msg( 'citizen-search-toggle' )->text();
|
2021-01-28 11:21:58 +00:00
|
|
|
|
|
|
|
return [
|
2022-05-26 21:57:57 +00:00
|
|
|
// TODO: This should be called in skin.json
|
2021-01-28 11:21:58 +00:00
|
|
|
'msg-citizen-search-toggle' => $toggleMsg,
|
2021-04-12 19:09:28 +00:00
|
|
|
'msg-citizen-search-toggle-shortcut' => $toggleMsg . ' [/]',
|
2021-01-28 11:21:58 +00:00
|
|
|
'form-action' => $this->getConfigValue( 'Script' ),
|
2022-05-22 19:06:49 +00:00
|
|
|
'html-input' => $skin->makeSearchInput( [ 'id' => 'searchInput' ] ),
|
2021-01-28 11:27:55 +00:00
|
|
|
'page-title' => SpecialPage::getTitleFor( 'Search' )->getPrefixedDBkey(),
|
2021-01-28 11:21:58 +00:00
|
|
|
'html-random-href' => Skin::makeSpecialUrl( 'Randompage' ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-17 02:52:04 +00:00
|
|
|
* Decorate the personal menu
|
2021-01-28 11:21:58 +00:00
|
|
|
*
|
2022-05-17 02:52:04 +00:00
|
|
|
* @param array $personalTools The original personal tools urls
|
2021-01-28 11:21:58 +00:00
|
|
|
*
|
2022-05-18 06:56:15 +00:00
|
|
|
* @return array
|
2021-01-28 11:21:58 +00:00
|
|
|
*/
|
2022-05-23 20:10:14 +00:00
|
|
|
private function getPersonalHeaderData( $personalTools ): array {
|
|
|
|
$skin = $this->skin;
|
|
|
|
$user = $this->user;
|
2022-06-02 14:46:14 +00:00
|
|
|
$header = [];
|
2022-05-23 20:10:14 +00:00
|
|
|
|
2022-06-02 14:46:14 +00:00
|
|
|
if ( $user->isRegistered() ) {
|
|
|
|
$header += [
|
|
|
|
'userpage' => $personalTools['userpage'] ?? null,
|
|
|
|
'usergroups' => $this->getUserGroupsData( $personalTools, $user ),
|
|
|
|
'usercontris' => $this->getUserContributionsData( $user ),
|
|
|
|
];
|
|
|
|
}
|
2022-05-17 02:52:04 +00:00
|
|
|
|
2022-05-23 20:10:14 +00:00
|
|
|
return $skin->getPortletData( 'personal-header', array_filter( $header ) );
|
2022-05-17 02:52:04 +00:00
|
|
|
}
|
2021-01-28 11:21:58 +00:00
|
|
|
|
2022-05-17 02:52:04 +00:00
|
|
|
/**
|
2022-05-17 02:53:14 +00:00
|
|
|
* Build and return user groups data
|
2022-05-17 02:52:04 +00:00
|
|
|
*
|
|
|
|
* @param array $personalTools The original personal tools urls
|
|
|
|
* @param User $user
|
|
|
|
*
|
2022-05-18 06:54:16 +00:00
|
|
|
* @return array|null
|
2022-05-17 02:52:04 +00:00
|
|
|
*/
|
2022-05-18 06:54:16 +00:00
|
|
|
private function getUserGroupsData( $personalTools, $user ): ?array {
|
2021-01-28 11:21:58 +00:00
|
|
|
// This does not return implicit groups
|
2021-03-09 20:04:48 +00:00
|
|
|
$groups = MediaWikiServices::getInstance()->getUserGroupManager()->getUserGroups( $user );
|
|
|
|
|
2022-05-17 02:52:04 +00:00
|
|
|
if ( empty( $groups ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-01-28 11:21:58 +00:00
|
|
|
|
2022-05-22 19:06:49 +00:00
|
|
|
$skin = $this->skin;
|
|
|
|
$title = $this->title;
|
|
|
|
|
2021-03-12 01:43:36 +00:00
|
|
|
// Add user group
|
2022-05-17 02:52:04 +00:00
|
|
|
$groupLinks = [];
|
|
|
|
$msgName = 'group-%s';
|
|
|
|
|
|
|
|
foreach ( $groups as $group ) {
|
2022-05-22 19:06:49 +00:00
|
|
|
$groupPage = $title->newFromText(
|
|
|
|
$skin->msg( sprintf( $msgName, $group ) )->text(),
|
2022-05-17 02:52:04 +00:00
|
|
|
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
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-05-17 02:52:04 +00:00
|
|
|
return [
|
|
|
|
'id' => 'pt-usergroups',
|
|
|
|
'links' => $groupLinks
|
2021-03-09 20:04:48 +00:00
|
|
|
];
|
2022-05-17 02:52:04 +00:00
|
|
|
}
|
2021-03-09 20:04:48 +00:00
|
|
|
|
2022-05-17 02:52:04 +00:00
|
|
|
/**
|
|
|
|
* Build and return user contributions data
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*
|
2022-05-18 06:54:16 +00:00
|
|
|
* @return array|null
|
2022-05-17 02:52:04 +00:00
|
|
|
*/
|
2022-05-18 06:54:16 +00:00
|
|
|
private function getUserContributionsData( $user ): ?array {
|
2022-05-17 02:52:04 +00:00
|
|
|
// Return user edits
|
|
|
|
$edits = MediaWikiServices::getInstance()->getUserEditTracker()->getUserEditCount( $user );
|
2021-01-28 11:21:58 +00:00
|
|
|
|
2022-05-17 02:52:04 +00:00
|
|
|
if ( empty( $edits ) ) {
|
|
|
|
return null;
|
2021-01-28 11:21:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 19:06:49 +00:00
|
|
|
$skin = $this->skin;
|
|
|
|
|
2022-05-17 02:52:04 +00:00
|
|
|
return [
|
2022-05-22 19:06:49 +00:00
|
|
|
'text' => $skin->msg( 'usereditcount' )
|
2022-05-17 02:52:04 +00:00
|
|
|
->numParams( sprintf( '%s', number_format( $edits, 0 ) ) ),
|
|
|
|
'id' => 'pt-usercontris'
|
|
|
|
];
|
2021-01-28 11:21:58 +00:00
|
|
|
}
|
2021-01-28 11:19:50 +00:00
|
|
|
}
|