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
|
|
|
|
2022-05-18 23:15:51 +00:00
|
|
|
use Action;
|
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;
|
2022-05-18 18:20:51 +00:00
|
|
|
use Skin;
|
2022-04-14 04:30:33 +00:00
|
|
|
use SkinTemplate;
|
2021-01-28 19:35:31 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Render page-related tools
|
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
|
|
|
*
|
2021-01-28 19:35:31 +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-04-14 04:30:33 +00:00
|
|
|
* @param array $parentData
|
2021-01-28 19:35:31 +00:00
|
|
|
* @return array html
|
|
|
|
*/
|
2022-04-14 04:30:33 +00:00
|
|
|
public function buildPageTools( $parentData ): array {
|
2022-05-22 19:06:49 +00:00
|
|
|
$skin = $this->skin;
|
|
|
|
$user = $this->user;
|
|
|
|
|
2021-01-28 19:35:31 +00:00
|
|
|
$condition = $this->getConfigValue( 'CitizenShowPageTools' );
|
2022-05-22 19:06:49 +00:00
|
|
|
$contentNavigation = $skin->buildContentNavigationUrlsPublic();
|
|
|
|
$portals = $skin->buildSidebar();
|
2021-01-28 19:35:31 +00:00
|
|
|
$props = [];
|
|
|
|
|
|
|
|
// Login-based condition, return true if condition is met
|
|
|
|
if ( $condition === 'login' ) {
|
2022-05-22 19:06:49 +00:00
|
|
|
$condition = $user->isRegistered();
|
2021-01-28 19:35:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Permission-based condition, return true if condition is met
|
|
|
|
if ( is_string( $condition ) && strpos( $condition, 'permission' ) === 0 ) {
|
|
|
|
$permission = substr( $condition, 11 );
|
|
|
|
try {
|
2022-05-22 19:06:49 +00:00
|
|
|
$title = $this->title;
|
2021-01-28 19:35:31 +00:00
|
|
|
$condition = MediaWikiServices::getInstance()->getPermissionManager()->userCan(
|
2022-05-22 19:06:49 +00:00
|
|
|
$permission, $user, $title );
|
2021-01-28 20:09:52 +00:00
|
|
|
} catch ( Exception $e ) {
|
2021-01-28 19:35:31 +00:00
|
|
|
$condition = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $condition === true ) {
|
|
|
|
|
2022-04-14 04:30:33 +00:00
|
|
|
if ( !method_exists( SkinTemplate::class, 'runOnSkinTemplateNavigationHooks' ) ) {
|
2022-05-22 19:06:49 +00:00
|
|
|
$viewshtml = $skin->getPortletData( 'views', $contentNavigation[ 'views' ] ?? [] );
|
|
|
|
$actionshtml = $skin->getPortletData( 'actions', $contentNavigation[ 'actions' ] ?? [] );
|
|
|
|
$namespaceshtml = $skin->getPortletData( 'namespaces', $contentNavigation[ 'namespaces' ] ?? [] );
|
|
|
|
$variantshtml = $skin->getPortletData( 'variants', $contentNavigation[ 'variants' ] ?? [] );
|
|
|
|
$toolboxhtml = $skin->getPortletData( 'tb', $portals['TOOLBOX'] ?? [] );
|
|
|
|
$languageshtml = $skin->getPortletData( 'lang', $portals['LANGUAGES'] ?? [] );
|
2022-04-14 04:30:33 +00:00
|
|
|
} else {
|
|
|
|
$viewshtml = $parentData['data-portlets']['data-views'];
|
|
|
|
$actionshtml = $parentData['data-portlets']['data-actions'];
|
|
|
|
$namespaceshtml = $parentData['data-portlets']['data-namespaces'];
|
|
|
|
$variantshtml = $parentData['data-portlets']['data-variants'];
|
2022-05-16 04:50:00 +00:00
|
|
|
// data-languages can be undefined index
|
|
|
|
$languageshtml = $parentData['data-portlets']['data-languages'] ?? [];
|
|
|
|
// For some reason core does not set this
|
2022-05-16 04:53:33 +00:00
|
|
|
if ( empty( $languageshtml ) ) {
|
2022-05-16 04:50:00 +00:00
|
|
|
$languageshtml['is-empty'] = true;
|
|
|
|
}
|
2022-04-30 06:54:13 +00:00
|
|
|
// Finds the toolbox in the sidebar.
|
2022-05-28 18:26:32 +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-05-16 04:50:00 +00:00
|
|
|
$toolboxhtml = [
|
|
|
|
'is-empty' => true,
|
|
|
|
];
|
2022-05-28 18:26:32 +00:00
|
|
|
foreach ( $parentData['data-portlets-sidebar']['array-portlets-rest'] as $portlet ) {
|
2022-04-30 06:54:13 +00:00
|
|
|
if ( $portlet['id'] === 'p-tb' ) {
|
|
|
|
$toolboxhtml = $portlet;
|
2022-05-16 04:50:00 +00:00
|
|
|
$toolboxhtml['is-empty'] = false;
|
2022-04-30 06:54:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 04:30:33 +00:00
|
|
|
}
|
2021-01-28 19:35:31 +00:00
|
|
|
|
2022-05-18 21:14:24 +00:00
|
|
|
// Toggle label for toolbox
|
|
|
|
if ( $toolboxhtml ) {
|
|
|
|
$toolboxhtml['has-label'] = true;
|
2021-01-28 19:35:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 21:14:24 +00:00
|
|
|
// Toggle label for variants
|
|
|
|
if ( $variantshtml ) {
|
|
|
|
$variantshtml['has-label'] = true;
|
2022-04-24 05:52:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 19:35:31 +00:00
|
|
|
$props = [
|
|
|
|
'data-page-views' => $viewshtml,
|
|
|
|
'data-page-actions' => $actionshtml,
|
2021-02-26 21:21:11 +00:00
|
|
|
'data-namespaces' => $namespaceshtml,
|
2022-05-16 04:50:00 +00:00
|
|
|
'has-languages' => $this->shouldShowLanguages( $languageshtml, $variantshtml ),
|
2022-05-17 13:42:34 +00:00
|
|
|
'is-uls-ready' => $this->shouldShowULS( $variantshtml ),
|
2022-05-16 04:50:00 +00:00
|
|
|
'data-languages' => $languageshtml,
|
2021-02-26 21:21:11 +00:00
|
|
|
'data-variants' => $variantshtml,
|
2021-01-28 19:35:31 +00:00
|
|
|
'data-page-toolbox' => $toolboxhtml,
|
2022-05-16 23:31:11 +00:00
|
|
|
'html-language-count' => $this->getLanguagesCount(),
|
2021-01-28 19:35:31 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $props;
|
|
|
|
}
|
2022-05-16 04:50:00 +00:00
|
|
|
|
|
|
|
/**
|
2022-05-16 04:53:33 +00:00
|
|
|
* Calls getLanguages with caching.
|
|
|
|
*
|
|
|
|
* Based on Vector
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-05-16 04:50:00 +00:00
|
|
|
private function getLanguagesCached(): array {
|
|
|
|
$skin = $this->skin;
|
|
|
|
|
|
|
|
if ( $this->languages === null ) {
|
|
|
|
$this->languages = $skin->getLanguages();
|
|
|
|
}
|
|
|
|
return $this->languages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This should be upstreamed to the Skin class in core once the logic is finalized.
|
|
|
|
* Returns false if the page is a special page without any languages, or if an action
|
|
|
|
* other than view is being used.
|
|
|
|
*
|
|
|
|
* Based on Vector
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function canHaveLanguages(): bool {
|
|
|
|
$skin = $this->skin;
|
|
|
|
|
2022-05-18 18:20:51 +00:00
|
|
|
if ( method_exists( Skin::class, 'getActionName' ) ) {
|
|
|
|
// >= MW 1.38
|
|
|
|
if ( $skin->getContext()->getActionName() !== 'view' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// < MW 1.38
|
2022-05-18 23:15:51 +00:00
|
|
|
if ( Action::getActionName( $skin->getContext() ) !== 'view' ) {
|
2022-05-18 18:20:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-05-16 04:50:00 +00:00
|
|
|
}
|
2022-05-18 18:20:51 +00:00
|
|
|
|
2022-05-22 19:06:49 +00:00
|
|
|
$title = $this->title;
|
2022-05-16 04:50:00 +00:00
|
|
|
// Defensive programming - if a special page has added languages explicitly, best to show it.
|
|
|
|
if ( $title && $title->isSpecialPage() && empty( $this->getLanguagesCached() ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show or hide the language button
|
|
|
|
*
|
|
|
|
* @param array $languageshtml
|
|
|
|
* @param array $variantshtml
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function shouldShowLanguages( $languageshtml, $variantshtml ): bool {
|
|
|
|
if ( !$this->canHaveLanguages() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If both language and variant menu contains nothing
|
|
|
|
if ( $languageshtml['is-empty'] && $variantshtml['is-empty'] ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
|
*
|
|
|
|
* @param array $variantshtml
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function shouldShowULS( $variantshtml ): bool {
|
|
|
|
// ULS does not support variants
|
|
|
|
if ( !$variantshtml['is-empty'] ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExtensionRegistry::getInstance()->isLoaded( 'UniversalLanguageSelector' );
|
|
|
|
}
|
|
|
|
|
2022-05-16 23:31:11 +00:00
|
|
|
/**
|
|
|
|
* Count languages avaliable
|
|
|
|
* TODO: Consider having an option to count for variants?
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getLanguagesCount(): int {
|
|
|
|
return count( $this->getLanguagesCached() );
|
|
|
|
}
|
2021-01-28 19:35:31 +00:00
|
|
|
}
|