| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace MediaWiki\Minerva; |
| 21 |
|
| 22 |
use MediaWiki\HookContainer\HookContainer; |
| 23 |
use MediaWiki\MediaWikiServices; |
| 24 |
use MediaWiki\Minerva\Hooks\HookRunner; |
| 25 |
use MediaWiki\Minerva\Skins\SkinMinerva; |
| 26 |
use MediaWiki\Minerva\Skins\SkinUserPageHelper; |
| 27 |
use MobileContext; |
| 28 |
use OutOfBoundsException; |
| 29 |
use Skin; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
final class SkinOptions { |
| 35 |
|
| 36 |
public const MOBILE_OPTIONS = 'mobileOptionsLink'; |
| 37 |
public const CATEGORIES = 'categories'; |
| 38 |
public const PAGE_ISSUES = 'pageIssues'; |
| 39 |
public const BETA_MODE = 'beta'; |
| 40 |
public const TALK_AT_TOP = 'talkAtTop'; |
| 41 |
public const SHOW_DONATE = 'donate'; |
| 42 |
public const HISTORY_IN_PAGE_ACTIONS = 'historyInPageActions'; |
| 43 |
public const TOOLBAR_SUBMENU = 'overflowSubmenu'; |
| 44 |
public const TABS_ON_SPECIALS = 'tabsOnSpecials'; |
| 45 |
public const MAIN_MENU_EXPANDED = 'mainMenuExpanded'; |
| 46 |
public const PERSONAL_MENU = 'personalMenu'; |
| 47 |
public const SINGLE_ECHO_BUTTON = 'echo'; |
| 48 |
public const NIGHT_MODE = 'nightMode'; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private array $skinOptions = [ |
| 56 |
self::BETA_MODE => false, |
| 57 |
self::SHOW_DONATE => true, |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
self::MOBILE_OPTIONS => false, |
| 63 |
|
| 64 |
self::CATEGORIES => false, |
| 65 |
|
| 66 |
self::PAGE_ISSUES => false, |
| 67 |
|
| 68 |
self::TALK_AT_TOP => true, |
| 69 |
|
| 70 |
self::HISTORY_IN_PAGE_ACTIONS => true, |
| 71 |
|
| 72 |
self::TOOLBAR_SUBMENU => true, |
| 73 |
|
| 74 |
self::TABS_ON_SPECIALS => true, |
| 75 |
|
| 76 |
self::PERSONAL_MENU => true, |
| 77 |
|
| 78 |
self::MAIN_MENU_EXPANDED => true, |
| 79 |
|
| 80 |
self::SINGLE_ECHO_BUTTON => false, |
| 81 |
|
| 82 |
self::NIGHT_MODE => false, |
| 83 |
]; |
| 84 |
|
| 85 |
private HookContainer $hookContainer; |
| 86 |
private SkinUserPageHelper $skinUserPageHelper; |
| 87 |
|
| 88 |
public function __construct( |
| 89 |
HookContainer $hookContainer, |
| 90 |
SkinUserPageHelper $skinUserPageHelper |
| 91 |
) { |
| 92 |
$this->hookContainer = $hookContainer; |
| 93 |
$this->skinUserPageHelper = $skinUserPageHelper; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function setMultiple( array $options ): void { |
| 101 |
foreach ( $options as $option => $value ) { |
| 102 |
if ( !array_key_exists( $option, $this->skinOptions ) ) { |
| 103 |
throw new OutOfBoundsException( "SkinOption $option is not defined" ); |
| 104 |
} |
| 105 |
} |
| 106 |
$this->skinOptions = array_merge( $this->skinOptions, $options ); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function get( string $key ): bool { |
| 115 |
if ( !array_key_exists( $key, $this->skinOptions ) ) { |
| 116 |
throw new OutOfBoundsException( "SkinOption $key doesn't exist" ); |
| 117 |
} |
| 118 |
return $this->skinOptions[$key]; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function getAll(): array { |
| 126 |
return $this->skinOptions; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function hasSkinOptions(): bool { |
| 134 |
foreach ( $this->skinOptions as $key => $val ) { |
| 135 |
if ( $val ) { |
| 136 |
return true; |
| 137 |
} |
| 138 |
} |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function setMinervaSkinOptions( |
| 149 |
MobileContext $mobileContext, Skin $skin |
| 150 |
): void { |
| 151 |
|
| 152 |
if ( $skin instanceof SkinMinerva ) { |
| 153 |
$services = MediaWikiServices::getInstance(); |
| 154 |
$featuresManager = $services |
| 155 |
->getService( 'MobileFrontend.FeaturesManager' ); |
| 156 |
$title = $skin->getTitle(); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
if ( $title !== null ) { |
| 163 |
|
| 164 |
|
| 165 |
$this->skinUserPageHelper |
| 166 |
->setContext( $mobileContext ) |
| 167 |
->setTitle( |
| 168 |
$title->inNamespace( NS_USER_TALK ) ? $title->getSubjectPage() : $title |
| 169 |
); |
| 170 |
|
| 171 |
$isDiffLink = $mobileContext->getRequest()->getCheck( 'diff' ); |
| 172 |
$isUserPage = $this->skinUserPageHelper->isUserPage(); |
| 173 |
$isUserPageAccessible = $this->skinUserPageHelper->isUserPageAccessibleToCurrentUser(); |
| 174 |
$isUserPageOrUserTalkPage = $isUserPage && $isUserPageAccessible; |
| 175 |
$requiresHistoryLink = $isUserPageOrUserTalkPage || $isDiffLink; |
| 176 |
} else { |
| 177 |
|
| 178 |
$isUserPageOrUserTalkPage = false; |
| 179 |
$requiresHistoryLink = false; |
| 180 |
} |
| 181 |
|
| 182 |
$isBeta = $mobileContext->isBetaGroupMember(); |
| 183 |
$this->setMultiple( [ |
| 184 |
self::SHOW_DONATE => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaDonateLink' ), |
| 185 |
self::TALK_AT_TOP => $isUserPageOrUserTalkPage ? |
| 186 |
true : $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaTalkAtTop' ), |
| 187 |
self::BETA_MODE |
| 188 |
=> $isBeta, |
| 189 |
self::CATEGORIES |
| 190 |
=> $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaShowCategories' ), |
| 191 |
self::PAGE_ISSUES |
| 192 |
=> $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaPageIssuesNewTreatment' ), |
| 193 |
self::MOBILE_OPTIONS => true, |
| 194 |
self::PERSONAL_MENU => $featuresManager->isFeatureAvailableForCurrentUser( |
| 195 |
'MinervaPersonalMenu' |
| 196 |
), |
| 197 |
self::MAIN_MENU_EXPANDED => $featuresManager->isFeatureAvailableForCurrentUser( |
| 198 |
'MinervaAdvancedMainMenu' |
| 199 |
), |
| 200 |
|
| 201 |
self::SINGLE_ECHO_BUTTON => true, |
| 202 |
self::HISTORY_IN_PAGE_ACTIONS => $requiresHistoryLink ? |
| 203 |
true : $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaHistoryInPageActions' ), |
| 204 |
self::TOOLBAR_SUBMENU => $isUserPageOrUserTalkPage ? |
| 205 |
true : $featuresManager->isFeatureAvailableForCurrentUser( |
| 206 |
Hooks::FEATURE_OVERFLOW_PAGE_ACTIONS |
| 207 |
), |
| 208 |
self::TABS_ON_SPECIALS => true, |
| 209 |
self::NIGHT_MODE => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaNightMode' ), |
| 210 |
] ); |
| 211 |
( new HookRunner( $this->hookContainer ) )->onSkinMinervaOptionsInit( $skin, $this ); |
| 212 |
} |
| 213 |
} |
| 214 |
} |