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 Exception;
|
2021-01-28 20:45:37 +00:00
|
|
|
use ExtensionRegistry;
|
|
|
|
use MWException;
|
2021-01-28 11:19:50 +00:00
|
|
|
use Skin;
|
2021-01-28 20:45:37 +00:00
|
|
|
use SpecialPage;
|
2021-01-28 11:19:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Drawer partial of Skin Citizen
|
|
|
|
* Generates the following partials:
|
|
|
|
* - Logo
|
|
|
|
* - Drawer
|
|
|
|
* + Special Pages Link
|
|
|
|
* + Upload Link
|
|
|
|
*/
|
2021-01-28 19:34:46 +00:00
|
|
|
final class Drawer extends Partial {
|
2021-01-28 11:21:58 +00:00
|
|
|
/**
|
|
|
|
* Render the navigation drawer
|
2022-05-16 01:37:39 +00:00
|
|
|
* Based on getPortletsTemplateData in SkinTemplate
|
2021-01-28 11:21:58 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2022-05-16 01:37:39 +00:00
|
|
|
public function getDrawerTemplateData() {
|
2022-05-22 19:06:49 +00:00
|
|
|
$skin = $this->skin;
|
|
|
|
|
2022-05-16 01:37:39 +00:00
|
|
|
$drawer = [];
|
2022-05-22 19:06:49 +00:00
|
|
|
$drawerData = $skin->buildSidebar();
|
2022-05-18 23:33:39 +00:00
|
|
|
$portletCount = 0;
|
2021-01-28 11:21:58 +00:00
|
|
|
|
2022-05-18 21:14:24 +00:00
|
|
|
// Render portlets
|
2022-05-16 01:37:39 +00:00
|
|
|
foreach ( $drawerData as $name => $items ) {
|
|
|
|
if ( is_array( $items ) ) {
|
|
|
|
// Numeric strings gets an integer when set as key, cast back - T73639
|
|
|
|
$name = (string)$name;
|
|
|
|
switch ( $name ) {
|
2022-05-18 21:14:24 +00:00
|
|
|
// Ignore search
|
|
|
|
// Handled by Header
|
2022-05-16 01:37:39 +00:00
|
|
|
case 'SEARCH':
|
2022-05-18 21:14:24 +00:00
|
|
|
break;
|
|
|
|
// Ignore toolbox
|
|
|
|
// Handled by PageTools
|
2022-05-16 01:37:39 +00:00
|
|
|
case 'TOOLBOX':
|
|
|
|
break;
|
2022-05-18 21:14:24 +00:00
|
|
|
// Ignore language
|
|
|
|
// Handled by PageTools
|
2022-05-16 01:37:39 +00:00
|
|
|
case 'LANGUAGES':
|
|
|
|
break;
|
|
|
|
default:
|
2022-05-22 19:06:49 +00:00
|
|
|
$drawer[] = $skin->getPortletData( $name, $items );
|
2022-05-18 23:33:39 +00:00
|
|
|
// All portlets within the drawer should have a label
|
|
|
|
// to ensure it is layout nicely
|
|
|
|
$drawer[$portletCount]['has-label'] = true;
|
|
|
|
$portletCount++;
|
2022-05-16 01:37:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-01-28 11:21:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 23:33:39 +00:00
|
|
|
$drawer = $this->addSiteTools( $drawer, $portletCount );
|
2021-01-28 11:21:58 +00:00
|
|
|
|
2022-05-16 01:37:39 +00:00
|
|
|
$drawerData = [
|
2022-05-18 23:33:39 +00:00
|
|
|
'array-portlets' => $drawer,
|
|
|
|
'data-drawer-sitestats' => $this->getSiteStatsData(),
|
2021-01-28 11:21:58 +00:00
|
|
|
];
|
2021-02-03 20:00:43 +00:00
|
|
|
|
2022-05-16 01:37:39 +00:00
|
|
|
return $drawerData;
|
2021-01-28 11:21:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 23:33:39 +00:00
|
|
|
/**
|
|
|
|
* Add site-wide tools to portlet
|
2022-05-18 23:45:19 +00:00
|
|
|
*
|
2022-05-18 23:33:39 +00:00
|
|
|
* TODO: Remove this hack when Desktop Improvements separate page and site tools
|
|
|
|
* FIXME: There are no error handling if the ID does not match any existing portlet
|
|
|
|
*
|
|
|
|
* @param array $drawer
|
|
|
|
* @param int $portletCount
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function addSiteTools( $drawer, $portletCount ): array {
|
2022-05-18 23:54:08 +00:00
|
|
|
$id = $this->getConfigValue( 'CitizenSiteToolsPortlet' );
|
2022-05-18 23:33:39 +00:00
|
|
|
$html = $this->getSiteToolsHTML();
|
|
|
|
|
|
|
|
// Attach to first portlet if empty
|
|
|
|
if ( empty( $id ) ) {
|
|
|
|
$drawer[0]['html-items'] .= $html;
|
|
|
|
return $drawer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the portlet with the right ID, then add to it
|
|
|
|
for ( $i = 0; $i < $portletCount; $i++ ) {
|
|
|
|
if ( isset( $drawer[$i]['id'] ) && $drawer[$i]['id'] === $id ) {
|
|
|
|
$drawer[$i]['html-items'] .= $html;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $drawer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build site-wide tools HTML
|
|
|
|
* We removed some site-wide tools from TOOLBOX, now add it back
|
|
|
|
*
|
|
|
|
* TODO: Remove this hack when Desktop Improvements separate page and site tools
|
|
|
|
*
|
|
|
|
* @return string RawHTML
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
private function getSiteToolsHTML(): string {
|
2022-05-22 19:06:49 +00:00
|
|
|
$skin = $this->skin;
|
|
|
|
|
2022-05-18 23:33:39 +00:00
|
|
|
$html = '';
|
|
|
|
|
|
|
|
// Special pages
|
2022-05-22 19:06:49 +00:00
|
|
|
$html .= $skin->makeListItem( 'specialpages', [
|
2022-05-18 23:33:39 +00:00
|
|
|
'href' => Skin::makeSpecialUrl( 'Specialpages' ),
|
|
|
|
'id' => 't-specialpages'
|
|
|
|
] );
|
|
|
|
|
|
|
|
// Upload file
|
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'Upload Wizard' ) ) {
|
|
|
|
// Link to Upload Wizard if present
|
|
|
|
$uploadHref = SpecialPage::getTitleFor( 'UploadWizard' )->getLocalURL();
|
|
|
|
} else {
|
|
|
|
// Link to old upload form
|
|
|
|
$uploadHref = Skin::makeSpecialUrl( 'Upload' );
|
|
|
|
}
|
2022-05-22 19:06:49 +00:00
|
|
|
$html .= $skin->makeListItem( 'upload', [
|
2022-05-18 23:33:39 +00:00
|
|
|
'href' => $uploadHref,
|
|
|
|
'id' => 't-upload'
|
|
|
|
] );
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2021-03-22 21:01:08 +00:00
|
|
|
/**
|
|
|
|
* Get messages used for site stats in the drawer
|
|
|
|
*
|
|
|
|
* @return array for use in Mustache template.
|
|
|
|
*/
|
2022-05-18 23:33:39 +00:00
|
|
|
private function getSiteStatsData(): array {
|
2021-03-22 21:01:08 +00:00
|
|
|
$props = [];
|
|
|
|
|
|
|
|
if ( $this->getConfigValue( 'CitizenEnableDrawerSiteStats' ) ) {
|
2022-05-22 19:06:49 +00:00
|
|
|
$skin = $this->skin;
|
2021-03-22 21:01:08 +00:00
|
|
|
$stats = [ 'articles', 'images', 'users', 'edits' ];
|
|
|
|
$items = [];
|
2022-05-19 18:46:35 +00:00
|
|
|
$fmt = null;
|
|
|
|
|
|
|
|
// Get NumberFormatter here so that we don't have to call it for every stats
|
2022-06-14 19:56:03 +00:00
|
|
|
if ( $this->getConfigValue( 'CitizenUseNumberFormatter' ) && class_exists( \NumberFormatter::class ) ) {
|
2022-05-22 19:06:49 +00:00
|
|
|
$locale = $skin->getLanguage()->getHtmlCode() ?? 'en_US';
|
2022-05-19 18:46:35 +00:00
|
|
|
$fmt = new \NumberFormatter( $locale, \NumberFormatter::PADDING_POSITION );
|
|
|
|
$fmt->setAttribute( \NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_DOWN );
|
|
|
|
$fmt->setAttribute( \NumberFormatter::MAX_FRACTION_DIGITS, 1 );
|
|
|
|
}
|
2021-03-22 21:01:08 +00:00
|
|
|
|
|
|
|
foreach ( $stats as &$stat ) {
|
|
|
|
$items[] = [
|
|
|
|
'id' => $stat,
|
2022-05-18 23:33:39 +00:00
|
|
|
'value' => $this->getSiteStatValue( $stat, $fmt ),
|
2022-05-22 19:06:49 +00:00
|
|
|
'label' => $skin->msg( "citizen-sitestats-$stat-label" )->text(),
|
2021-03-22 21:01:08 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$props['array-drawer-sitestats-item'] = $items;
|
|
|
|
return $props;
|
|
|
|
}
|
|
|
|
|
2022-04-24 00:26:05 +00:00
|
|
|
/**
|
|
|
|
* Get and format sitestat value
|
2022-05-18 23:45:19 +00:00
|
|
|
*
|
2022-04-24 00:26:05 +00:00
|
|
|
* @param string $key
|
2022-05-19 18:46:35 +00:00
|
|
|
* @param NumberFormatter|null $fmt
|
2022-05-18 23:33:39 +00:00
|
|
|
* @return string
|
2022-04-24 00:26:05 +00:00
|
|
|
*/
|
2022-05-18 23:33:39 +00:00
|
|
|
private function getSiteStatValue( $key, $fmt ): string {
|
|
|
|
$value = call_user_func( 'SiteStats::' . $key ) ?? '';
|
2022-04-24 00:26:05 +00:00
|
|
|
|
2022-05-19 18:46:35 +00:00
|
|
|
if ( $fmt ) {
|
|
|
|
return $fmt->format( $value );
|
2022-04-24 00:26:05 +00:00
|
|
|
} else {
|
2022-05-19 18:50:30 +00:00
|
|
|
return number_format( $value );
|
2022-04-24 00:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-28 11:19:50 +00:00
|
|
|
}
|