2021-01-28 19:35:31 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare( strict_types=1 );
|
|
|
|
|
2022-05-26 20:54:52 +00:00
|
|
|
namespace MediaWiki\Skins\Citizen\Partials;
|
2021-01-28 19:35:31 +00:00
|
|
|
|
2021-01-28 20:09:52 +00:00
|
|
|
use Exception;
|
2022-05-17 13:42:34 +00:00
|
|
|
use ExtensionRegistry;
|
2021-01-28 19:35:31 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
|
|
final class PageTools extends Partial {
|
2022-05-16 04:50:00 +00:00
|
|
|
/** @var null|array for caching purposes */
|
|
|
|
private $languages;
|
2021-01-28 19:35:31 +00:00
|
|
|
|
|
|
|
/**
|
2022-10-03 19:48:56 +00:00
|
|
|
* Get page-related tools template data
|
2022-05-16 04:50:00 +00:00
|
|
|
* TODO: Break this down and clean up when 1.39
|
2022-05-28 18:26:32 +00:00
|
|
|
* TODO: Use SkinTemplateNavigation::Universal instead of dirty CSS when 1.39
|
2022-05-16 04:53:33 +00:00
|
|
|
*
|
2022-04-14 04:30:33 +00:00
|
|
|
* @param array $parentData
|
2021-01-28 19:35:31 +00:00
|
|
|
* @return array html
|
|
|
|
*/
|
2022-10-03 19:48:56 +00:00
|
|
|
public function getPageToolsData( $parentData ): array {
|
|
|
|
$data = [
|
|
|
|
'data-article-tools' => $this->getArticleToolsData( $parentData['data-portlets-sidebar'] ),
|
|
|
|
'pagetools-visible' => $this->shouldShowPageTools(),
|
|
|
|
];
|
|
|
|
|
|
|
|
// There are edge cases where the menu is completely empty
|
|
|
|
if ( $data['data-article-tools']['is-empty'] === false ) {
|
|
|
|
$data['pagetools-overflow'] = true;
|
2021-01-28 19:35:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 19:48:56 +00:00
|
|
|
// NOTE: There must be a better way to handle this
|
|
|
|
// Can be undefined index
|
|
|
|
$languagesData = $parentData['data-portlets']['data-languages'] ?? [ 'is-empty' => true ];
|
|
|
|
$variantsData = $parentData['data-portlets']['data-variants'] ?? [ 'is-empty' => true ];
|
|
|
|
$hasLanguages = ( !$languagesData['is-empty'] || !$variantsData['is-empty'] );
|
2021-01-28 19:35:31 +00:00
|
|
|
|
2022-10-03 19:48:56 +00:00
|
|
|
if ( $hasLanguages ) {
|
|
|
|
$data += [
|
|
|
|
'has-languages' => $hasLanguages,
|
2023-01-15 07:26:34 +00:00
|
|
|
/*
|
|
|
|
* FIXME: ULS does not trigger for some reason, disabling it for now
|
|
|
|
* 'is-uls-ready' => $this->shouldShowULS( $variantsData ),
|
|
|
|
*/
|
|
|
|
'is-uls-ready' => false,
|
2022-05-16 23:31:11 +00:00
|
|
|
'html-language-count' => $this->getLanguagesCount(),
|
2021-01-28 19:35:31 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-10-03 19:48:56 +00:00
|
|
|
return $data;
|
2021-01-28 19:35:31 +00:00
|
|
|
}
|
2022-05-16 04:50:00 +00:00
|
|
|
|
|
|
|
/**
|
2022-10-03 19:48:56 +00:00
|
|
|
* Extract article tools from sidebar and return the data
|
2022-10-03 19:49:32 +00:00
|
|
|
*
|
2022-10-03 19:48:56 +00:00
|
|
|
* The reason we do this is because:
|
|
|
|
* 1. We removed some site-wide tools from the toolbar in Drawer.php,
|
|
|
|
* now we just want the leftovers
|
|
|
|
* 2. Toolbox is not currently avaliable as data-portlet, have to wait
|
|
|
|
* till Desktop Improvements
|
2022-10-03 19:49:32 +00:00
|
|
|
*
|
2022-10-03 19:48:56 +00:00
|
|
|
* @param array sidebarData
|
|
|
|
* @return bool
|
2022-05-16 04:53:33 +00:00
|
|
|
*/
|
2022-10-03 19:48:56 +00:00
|
|
|
private function getArticleToolsData( $sidebarData ) {
|
|
|
|
$data = [
|
|
|
|
'is-empty' => true,
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ( $sidebarData['array-portlets-rest'] as $portlet ) {
|
|
|
|
if ( $portlet['id'] === 'p-tb' ) {
|
|
|
|
$data = $portlet;
|
|
|
|
$data['is-empty'] = false;
|
|
|
|
break;
|
|
|
|
}
|
2022-05-16 04:50:00 +00:00
|
|
|
}
|
2022-10-03 19:48:56 +00:00
|
|
|
|
|
|
|
return $data;
|
2022-05-16 04:50:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-03 19:48:56 +00:00
|
|
|
* Check if views and actions should show
|
2022-05-16 04:50:00 +00:00
|
|
|
*
|
2022-10-03 19:48:56 +00:00
|
|
|
* Possible visibility conditions:
|
|
|
|
* * true: always visible (bool)
|
|
|
|
* * false: never visible (bool)
|
|
|
|
* * 'login': only visible if logged in (string)
|
|
|
|
* * 'permission-*': only visible if user has permission
|
|
|
|
* e.g. permission-edit = only visible if user can edit pages
|
2022-10-03 19:49:32 +00:00
|
|
|
*
|
2022-05-16 04:50:00 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2022-10-03 19:48:56 +00:00
|
|
|
private function shouldShowPageTools(): bool {
|
|
|
|
$condition = $this->getConfigValue( 'CitizenShowPageTools' );
|
|
|
|
$user = $this->user;
|
2022-05-16 04:50:00 +00:00
|
|
|
|
2022-10-03 19:48:56 +00:00
|
|
|
// Login-based condition, return true if condition is met
|
|
|
|
if ( $condition === 'login' ) {
|
|
|
|
$condition = $user->isRegistered();
|
2022-05-16 04:50:00 +00:00
|
|
|
}
|
2022-05-18 18:20:51 +00:00
|
|
|
|
2022-10-03 19:48:56 +00:00
|
|
|
// Permission-based condition, return true if condition is met
|
|
|
|
if ( is_string( $condition ) && strpos( $condition, 'permission' ) === 0 ) {
|
|
|
|
$permission = substr( $condition, 11 );
|
|
|
|
try {
|
|
|
|
$title = $this->title;
|
|
|
|
$condition = MediaWikiServices::getInstance()->getPermissionManager()->userCan(
|
|
|
|
$permission, $user, $title );
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
$condition = false;
|
|
|
|
}
|
2022-05-16 04:50:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 23:11:41 +00:00
|
|
|
return (bool)$condition;
|
2022-05-16 04:50:00 +00:00
|
|
|
}
|
2022-05-16 23:31:11 +00:00
|
|
|
|
2022-05-17 13:42:34 +00:00
|
|
|
/**
|
|
|
|
* Check if UniversalLanguageSelector can be used to replace the language menu
|
|
|
|
*
|
2022-10-03 19:48:56 +00:00
|
|
|
* @param array $variantsData
|
2022-05-17 13:42:34 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2022-10-03 19:48:56 +00:00
|
|
|
private function shouldShowULS( $variantsData ): bool {
|
2022-05-17 13:42:34 +00:00
|
|
|
// ULS does not support variants
|
2022-10-03 19:48:56 +00:00
|
|
|
if ( !$variantsData['is-empty'] ) {
|
2022-05-17 13:42:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExtensionRegistry::getInstance()->isLoaded( 'UniversalLanguageSelector' );
|
|
|
|
}
|
|
|
|
|
2022-05-16 23:31:11 +00:00
|
|
|
/**
|
2023-08-25 21:39:37 +00:00
|
|
|
* Count languages available
|
2022-05-16 23:31:11 +00:00
|
|
|
* TODO: Consider having an option to count for variants?
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getLanguagesCount(): int {
|
2022-10-03 19:48:56 +00:00
|
|
|
if ( $this->languages === null ) {
|
|
|
|
$this->languages = $this->skin->getLanguages();
|
|
|
|
}
|
2022-10-03 19:49:32 +00:00
|
|
|
|
2022-10-03 19:48:56 +00:00
|
|
|
return count( $this->languages );
|
2022-05-16 23:31:11 +00:00
|
|
|
}
|
2021-01-28 19:35:31 +00:00
|
|
|
}
|