Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
18.03% |
11 / 61 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
| SkinOptions | |
18.03% |
11 / 61 |
|
50.00% |
3 / 6 |
217.81 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setMultiple | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
| get | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getAll | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasSkinOptions | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| setMinervaSkinOptions | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 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 MediaWiki\Skin\Skin; |
| 28 | use MobileContext; |
| 29 | use OutOfBoundsException; |
| 30 | |
| 31 | /** |
| 32 | * A wrapper for all available Skin options. |
| 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 SHOW_DONATE_BANNER = 'donateBanner'; |
| 43 | public const HISTORY_IN_PAGE_ACTIONS = 'historyInPageActions'; |
| 44 | public const TOOLBAR_SUBMENU = 'overflowSubmenu'; |
| 45 | public const TABS_ON_SPECIALS = 'tabsOnSpecials'; |
| 46 | public const MAIN_MENU_EXPANDED = 'mainMenuExpanded'; |
| 47 | public const PERSONAL_MENU = 'personalMenu'; |
| 48 | public const NIGHT_MODE = 'nightMode'; |
| 49 | |
| 50 | /** |
| 51 | * Note stable skin options default to true for desktop-Minerva and are expected to be |
| 52 | * overridden on mobile. |
| 53 | * @var array skin specific options, initialized with default values |
| 54 | */ |
| 55 | private array $skinOptions = [ |
| 56 | self::BETA_MODE => false, |
| 57 | self::SHOW_DONATE_BANNER => false, |
| 58 | self::SHOW_DONATE => true, |
| 59 | /** |
| 60 | * Whether the main menu should include a link to |
| 61 | * Special:Preferences of Special:MobileOptions |
| 62 | */ |
| 63 | self::MOBILE_OPTIONS => false, |
| 64 | /** Whether a categories button should appear at the bottom of the skin. */ |
| 65 | self::CATEGORIES => false, |
| 66 | /** requires a wiki using Template:Ambox */ |
| 67 | self::PAGE_ISSUES => false, |
| 68 | /** no extension requirements */ |
| 69 | self::TALK_AT_TOP => true, |
| 70 | /** no extension requirements */ |
| 71 | self::HISTORY_IN_PAGE_ACTIONS => true, |
| 72 | /** no extension requirements */ |
| 73 | self::TOOLBAR_SUBMENU => true, |
| 74 | /** Whether to show tabs on special pages */ |
| 75 | self::TABS_ON_SPECIALS => true, |
| 76 | /** whether to show a personal menu */ |
| 77 | self::PERSONAL_MENU => true, |
| 78 | /** whether to show a main menu with additional items */ |
| 79 | self::MAIN_MENU_EXPANDED => true, |
| 80 | /** whether night mode is available to the user */ |
| 81 | self::NIGHT_MODE => false, |
| 82 | ]; |
| 83 | |
| 84 | private HookContainer $hookContainer; |
| 85 | private SkinUserPageHelper $skinUserPageHelper; |
| 86 | |
| 87 | public function __construct( |
| 88 | HookContainer $hookContainer, |
| 89 | SkinUserPageHelper $skinUserPageHelper |
| 90 | ) { |
| 91 | $this->hookContainer = $hookContainer; |
| 92 | $this->skinUserPageHelper = $skinUserPageHelper; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * override an existing option or options with new values |
| 97 | */ |
| 98 | public function setMultiple( array $options ): void { |
| 99 | foreach ( $options as $option => $value ) { |
| 100 | if ( !array_key_exists( $option, $this->skinOptions ) ) { |
| 101 | throw new OutOfBoundsException( "SkinOption $option is not defined" ); |
| 102 | } |
| 103 | } |
| 104 | $this->skinOptions = array_merge( $this->skinOptions, $options ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Return whether a skin option is truthy. Should be one of self:* constants |
| 109 | * @param string $key |
| 110 | * @return bool |
| 111 | */ |
| 112 | public function get( string $key ): bool { |
| 113 | if ( !array_key_exists( $key, $this->skinOptions ) ) { |
| 114 | throw new OutOfBoundsException( "SkinOption $key doesn't exist" ); |
| 115 | } |
| 116 | return $this->skinOptions[$key]; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get all skin options |
| 121 | */ |
| 122 | public function getAll(): array { |
| 123 | return $this->skinOptions; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Return whether any of the skin options have been set |
| 128 | */ |
| 129 | public function hasSkinOptions(): bool { |
| 130 | foreach ( $this->skinOptions as $key => $val ) { |
| 131 | if ( $val ) { |
| 132 | return true; |
| 133 | } |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Set the skin options for Minerva |
| 140 | * |
| 141 | * @param MobileContext $mobileContext |
| 142 | * @param Skin $skin |
| 143 | */ |
| 144 | public function setMinervaSkinOptions( |
| 145 | MobileContext $mobileContext, Skin $skin |
| 146 | ): void { |
| 147 | // setSkinOptions is not available |
| 148 | if ( $skin instanceof SkinMinerva ) { |
| 149 | $services = MediaWikiServices::getInstance(); |
| 150 | $featuresManager = $services |
| 151 | ->getService( 'MobileFrontend.FeaturesManager' ); |
| 152 | $title = $skin->getTitle(); |
| 153 | |
| 154 | // T245162 - this should only apply if the context relates to a page view. |
| 155 | // Examples: |
| 156 | // - parsing wikitext during an REST response |
| 157 | // - a ResourceLoader response |
| 158 | if ( $title !== null ) { |
| 159 | // T232653: TALK_AT_TOP, HISTORY_IN_PAGE_ACTIONS, TOOLBAR_SUBMENU should |
| 160 | // be true on user pages and user talk pages for all users |
| 161 | $this->skinUserPageHelper |
| 162 | ->setContext( $mobileContext ) |
| 163 | ->setTitle( |
| 164 | $title->inNamespace( NS_USER_TALK ) ? $title->getSubjectPage() : $title |
| 165 | ); |
| 166 | |
| 167 | $isDiffLink = $mobileContext->getRequest()->getCheck( 'diff' ); |
| 168 | $isUserPage = $this->skinUserPageHelper->isUserPage(); |
| 169 | $isUserPageAccessible = $this->skinUserPageHelper->isUserPageAccessibleToCurrentUser(); |
| 170 | $isUserPageOrUserTalkPage = $isUserPage && $isUserPageAccessible; |
| 171 | $requiresHistoryLink = $isUserPageOrUserTalkPage || $isDiffLink; |
| 172 | } else { |
| 173 | // If no title this must be false |
| 174 | $isUserPageOrUserTalkPage = false; |
| 175 | $requiresHistoryLink = false; |
| 176 | } |
| 177 | |
| 178 | $isBeta = $mobileContext->isBetaGroupMember(); |
| 179 | $this->setMultiple( [ |
| 180 | self::SHOW_DONATE_BANNER => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaDonateBanner' ), |
| 181 | self::SHOW_DONATE => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaDonateLink' ), |
| 182 | self::TALK_AT_TOP => $isUserPageOrUserTalkPage ? |
| 183 | true : $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaTalkAtTop' ), |
| 184 | self::BETA_MODE |
| 185 | => $isBeta, |
| 186 | self::CATEGORIES |
| 187 | => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaShowCategories' ), |
| 188 | self::PAGE_ISSUES |
| 189 | => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaPageIssuesNewTreatment' ), |
| 190 | self::MOBILE_OPTIONS => true, |
| 191 | self::PERSONAL_MENU => $featuresManager->isFeatureAvailableForCurrentUser( |
| 192 | 'MinervaPersonalMenu' |
| 193 | ), |
| 194 | self::MAIN_MENU_EXPANDED => $featuresManager->isFeatureAvailableForCurrentUser( |
| 195 | 'MinervaAdvancedMainMenu' |
| 196 | ), |
| 197 | self::HISTORY_IN_PAGE_ACTIONS => $requiresHistoryLink ? |
| 198 | true : $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaHistoryInPageActions' ), |
| 199 | self::TOOLBAR_SUBMENU => $isUserPageOrUserTalkPage ? |
| 200 | true : $featuresManager->isFeatureAvailableForCurrentUser( |
| 201 | Hooks::FEATURE_OVERFLOW_PAGE_ACTIONS |
| 202 | ), |
| 203 | self::TABS_ON_SPECIALS => true, |
| 204 | self::NIGHT_MODE => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaNightMode' ), |
| 205 | ] ); |
| 206 | ( new HookRunner( $this->hookContainer ) )->onSkinMinervaOptionsInit( $skin, $this ); |
| 207 | } |
| 208 | } |
| 209 | } |