2019-04-10 21:43:50 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This program 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
namespace MediaWiki\Minerva;
|
|
|
|
|
2024-03-03 13:31:09 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use MediaWiki\Minerva\Hooks\HookRunner;
|
|
|
|
use MediaWiki\Minerva\Skins\SkinMinerva;
|
2024-03-24 10:49:33 +00:00
|
|
|
use MediaWiki\Minerva\Skins\SkinUserPageHelper;
|
2024-03-03 13:31:09 +00:00
|
|
|
use MobileContext;
|
2024-03-24 18:26:16 +00:00
|
|
|
use OutOfBoundsException;
|
2024-03-03 13:31:09 +00:00
|
|
|
use Skin;
|
|
|
|
|
2019-04-10 21:43:50 +00:00
|
|
|
/**
|
|
|
|
* A wrapper for all available Skin options.
|
|
|
|
*/
|
|
|
|
final class SkinOptions {
|
|
|
|
|
2020-05-19 22:43:34 +00:00
|
|
|
public const MOBILE_OPTIONS = 'mobileOptionsLink';
|
|
|
|
public const CATEGORIES = 'categories';
|
|
|
|
public const PAGE_ISSUES = 'pageIssues';
|
|
|
|
public const BETA_MODE = 'beta';
|
|
|
|
public const TALK_AT_TOP = 'talkAtTop';
|
2020-06-25 21:06:46 +00:00
|
|
|
public const SHOW_DONATE = 'donate';
|
2020-05-19 22:43:34 +00:00
|
|
|
public const HISTORY_IN_PAGE_ACTIONS = 'historyInPageActions';
|
|
|
|
public const TOOLBAR_SUBMENU = 'overflowSubmenu';
|
|
|
|
public const TABS_ON_SPECIALS = 'tabsOnSpecials';
|
|
|
|
public const MAIN_MENU_EXPANDED = 'mainMenuExpanded';
|
|
|
|
public const PERSONAL_MENU = 'personalMenu';
|
2022-12-13 01:45:57 +00:00
|
|
|
public const SINGLE_ECHO_BUTTON = 'echo';
|
2024-01-30 19:21:36 +00:00
|
|
|
public const NIGHT_MODE = 'nightMode';
|
2019-08-01 16:18:18 +00:00
|
|
|
|
2021-01-15 20:57:04 +00:00
|
|
|
/**
|
|
|
|
* Note stable skin options default to true for desktop-Minerva and are expected to be
|
|
|
|
* overridden on mobile.
|
|
|
|
* @var array skin specific options, initialized with default values
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
private array $skinOptions = [
|
2019-08-01 16:18:18 +00:00
|
|
|
self::BETA_MODE => false,
|
2020-06-25 21:06:46 +00:00
|
|
|
self::SHOW_DONATE => true,
|
2019-04-10 21:43:50 +00:00
|
|
|
/**
|
|
|
|
* Whether the main menu should include a link to
|
|
|
|
* Special:Preferences of Special:MobileOptions
|
|
|
|
*/
|
2019-08-01 16:18:18 +00:00
|
|
|
self::MOBILE_OPTIONS => false,
|
2019-04-10 21:43:50 +00:00
|
|
|
/** Whether a categories button should appear at the bottom of the skin. */
|
2019-08-01 16:18:18 +00:00
|
|
|
self::CATEGORIES => false,
|
2019-07-30 00:24:23 +00:00
|
|
|
/** requires a wiki using Template:Ambox */
|
2019-08-01 16:18:18 +00:00
|
|
|
self::PAGE_ISSUES => false,
|
2019-07-30 00:24:23 +00:00
|
|
|
/** no extension requirements */
|
|
|
|
self::TALK_AT_TOP => true,
|
|
|
|
/** no extension requirements */
|
|
|
|
self::HISTORY_IN_PAGE_ACTIONS => true,
|
|
|
|
/** no extension requirements */
|
|
|
|
self::TOOLBAR_SUBMENU => true,
|
2019-06-07 11:40:13 +00:00
|
|
|
/** Whether to show tabs on special pages */
|
2022-04-08 19:21:50 +00:00
|
|
|
self::TABS_ON_SPECIALS => true,
|
2019-08-22 20:31:31 +00:00
|
|
|
/** whether to show a personal menu */
|
|
|
|
self::PERSONAL_MENU => true,
|
|
|
|
/** whether to show a main menu with additional items */
|
|
|
|
self::MAIN_MENU_EXPANDED => true,
|
2022-12-13 01:45:57 +00:00
|
|
|
/** whether Echo should be replaced with a single button */
|
|
|
|
self::SINGLE_ECHO_BUTTON => false,
|
2024-01-30 19:21:36 +00:00
|
|
|
/** whether night mode is available to the user */
|
|
|
|
self::NIGHT_MODE => false,
|
2019-04-10 21:43:50 +00:00
|
|
|
];
|
|
|
|
|
2024-03-03 13:31:09 +00:00
|
|
|
private HookContainer $hookContainer;
|
2024-03-24 10:49:33 +00:00
|
|
|
private SkinUserPageHelper $skinUserPageHelper;
|
2024-03-03 13:31:09 +00:00
|
|
|
|
|
|
|
public function __construct(
|
2024-03-24 10:49:33 +00:00
|
|
|
HookContainer $hookContainer,
|
|
|
|
SkinUserPageHelper $skinUserPageHelper
|
2024-03-03 13:31:09 +00:00
|
|
|
) {
|
|
|
|
$this->hookContainer = $hookContainer;
|
2024-03-24 10:49:33 +00:00
|
|
|
$this->skinUserPageHelper = $skinUserPageHelper;
|
2024-03-03 13:31:09 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 21:43:50 +00:00
|
|
|
/**
|
|
|
|
* override an existing option or options with new values
|
|
|
|
* @param array $options
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function setMultiple( array $options ): void {
|
2019-07-31 20:47:27 +00:00
|
|
|
foreach ( $options as $option => $value ) {
|
|
|
|
if ( !array_key_exists( $option, $this->skinOptions ) ) {
|
2024-03-24 18:26:16 +00:00
|
|
|
throw new OutOfBoundsException( "SkinOption $option is not defined" );
|
2019-07-31 20:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-10 21:43:50 +00:00
|
|
|
$this->skinOptions = array_merge( $this->skinOptions, $options );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-01 16:18:18 +00:00
|
|
|
* Return whether a skin option is truthy. Should be one of self:* constants
|
2019-04-10 21:43:50 +00:00
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function get( string $key ): bool {
|
2019-04-10 21:43:50 +00:00
|
|
|
if ( !array_key_exists( $key, $this->skinOptions ) ) {
|
2024-03-24 18:26:16 +00:00
|
|
|
throw new OutOfBoundsException( "SkinOption $key doesn't exist" );
|
2019-04-10 21:43:50 +00:00
|
|
|
}
|
|
|
|
return $this->skinOptions[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all skin options
|
|
|
|
* @return array
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function getAll(): array {
|
2019-04-10 21:43:50 +00:00
|
|
|
return $this->skinOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether any of the skin options have been set
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function hasSkinOptions(): bool {
|
2019-04-10 21:43:50 +00:00
|
|
|
foreach ( $this->skinOptions as $key => $val ) {
|
|
|
|
if ( $val ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-03 13:31:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the skin options for Minerva
|
|
|
|
*
|
|
|
|
* @param MobileContext $mobileContext
|
|
|
|
* @param Skin $skin
|
|
|
|
*/
|
|
|
|
public function setMinervaSkinOptions(
|
|
|
|
MobileContext $mobileContext, Skin $skin
|
2024-03-28 19:15:41 +00:00
|
|
|
): void {
|
2024-03-03 13:31:09 +00:00
|
|
|
// setSkinOptions is not available
|
|
|
|
if ( $skin instanceof SkinMinerva ) {
|
|
|
|
$services = MediaWikiServices::getInstance();
|
2024-05-28 19:53:45 +00:00
|
|
|
$featuresManager = $services
|
2024-03-03 13:31:09 +00:00
|
|
|
->getService( 'MobileFrontend.FeaturesManager' );
|
|
|
|
$title = $skin->getTitle();
|
|
|
|
|
|
|
|
// T245162 - this should only apply if the context relates to a page view.
|
|
|
|
// Examples:
|
|
|
|
// - parsing wikitext during an REST response
|
|
|
|
// - a ResourceLoader response
|
|
|
|
if ( $title !== null ) {
|
|
|
|
// T232653: TALK_AT_TOP, HISTORY_IN_PAGE_ACTIONS, TOOLBAR_SUBMENU should
|
|
|
|
// be true on user pages and user talk pages for all users
|
2024-03-24 10:49:33 +00:00
|
|
|
$this->skinUserPageHelper
|
2024-03-24 09:19:59 +00:00
|
|
|
->setContext( $mobileContext )
|
|
|
|
->setTitle(
|
|
|
|
$title->inNamespace( NS_USER_TALK ) ? $title->getSubjectPage() : $title
|
|
|
|
);
|
2024-03-03 13:31:09 +00:00
|
|
|
|
2024-04-01 22:04:35 +00:00
|
|
|
$isDiffLink = $mobileContext->getRequest()->getCheck( 'diff' );
|
2024-03-24 10:49:33 +00:00
|
|
|
$isUserPage = $this->skinUserPageHelper->isUserPage();
|
|
|
|
$isUserPageAccessible = $this->skinUserPageHelper->isUserPageAccessibleToCurrentUser();
|
2024-03-03 13:31:09 +00:00
|
|
|
$isUserPageOrUserTalkPage = $isUserPage && $isUserPageAccessible;
|
2024-04-01 22:04:35 +00:00
|
|
|
$requiresHistoryLink = $isUserPageOrUserTalkPage || $isDiffLink;
|
2024-03-03 13:31:09 +00:00
|
|
|
} else {
|
|
|
|
// If no title this must be false
|
|
|
|
$isUserPageOrUserTalkPage = false;
|
2024-04-01 22:04:35 +00:00
|
|
|
$requiresHistoryLink = false;
|
2024-03-03 13:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$isBeta = $mobileContext->isBetaGroupMember();
|
|
|
|
$this->setMultiple( [
|
2024-05-28 19:53:45 +00:00
|
|
|
self::SHOW_DONATE => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaDonateLink' ),
|
2024-03-03 13:31:09 +00:00
|
|
|
self::TALK_AT_TOP => $isUserPageOrUserTalkPage ?
|
2024-05-28 19:53:45 +00:00
|
|
|
true : $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaTalkAtTop' ),
|
2024-03-03 13:31:09 +00:00
|
|
|
self::BETA_MODE
|
|
|
|
=> $isBeta,
|
|
|
|
self::CATEGORIES
|
2024-05-28 19:53:45 +00:00
|
|
|
=> $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaShowCategories' ),
|
2024-03-03 13:31:09 +00:00
|
|
|
self::PAGE_ISSUES
|
2024-05-28 19:53:45 +00:00
|
|
|
=> $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaPageIssuesNewTreatment' ),
|
2024-03-03 13:31:09 +00:00
|
|
|
self::MOBILE_OPTIONS => true,
|
2024-05-28 19:53:45 +00:00
|
|
|
self::PERSONAL_MENU => $featuresManager->isFeatureAvailableForCurrentUser(
|
2024-03-03 13:31:09 +00:00
|
|
|
'MinervaPersonalMenu'
|
|
|
|
),
|
2024-05-28 19:53:45 +00:00
|
|
|
self::MAIN_MENU_EXPANDED => $featuresManager->isFeatureAvailableForCurrentUser(
|
2024-03-03 13:31:09 +00:00
|
|
|
'MinervaAdvancedMainMenu'
|
|
|
|
),
|
|
|
|
// In mobile, always resort to single icon.
|
|
|
|
self::SINGLE_ECHO_BUTTON => true,
|
2024-04-01 22:04:35 +00:00
|
|
|
self::HISTORY_IN_PAGE_ACTIONS => $requiresHistoryLink ?
|
2024-05-28 19:53:45 +00:00
|
|
|
true : $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaHistoryInPageActions' ),
|
2024-03-03 13:31:09 +00:00
|
|
|
self::TOOLBAR_SUBMENU => $isUserPageOrUserTalkPage ?
|
2024-05-28 19:53:45 +00:00
|
|
|
true : $featuresManager->isFeatureAvailableForCurrentUser(
|
2024-03-03 13:31:09 +00:00
|
|
|
Hooks::FEATURE_OVERFLOW_PAGE_ACTIONS
|
|
|
|
),
|
|
|
|
self::TABS_ON_SPECIALS => true,
|
2024-05-28 19:53:45 +00:00
|
|
|
self::NIGHT_MODE => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaNightMode' ),
|
2024-03-03 13:31:09 +00:00
|
|
|
] );
|
|
|
|
( new HookRunner( $this->hookContainer ) )->onSkinMinervaOptionsInit( $skin, $this );
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 21:43:50 +00:00
|
|
|
}
|