Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
20.37% covered (danger)
20.37%
11 / 54
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SkinOptions
20.37% covered (danger)
20.37%
11 / 54
50.00% covered (danger)
50.00%
3 / 6
201.28
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setMultiple
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 get
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getAll
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasSkinOptions
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 setMinervaSkinOptions
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
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 */
20namespace MediaWiki\Minerva;
21
22use MediaWiki\HookContainer\HookContainer;
23use MediaWiki\MediaWikiServices;
24use MediaWiki\Minerva\Hooks\HookRunner;
25use MediaWiki\Minerva\Skins\SkinMinerva;
26use MediaWiki\Minerva\Skins\SkinUserPageHelper;
27use MediaWiki\Skin\Skin;
28use MobileContext;
29use OutOfBoundsException;
30
31/**
32 * A wrapper for all available Skin options.
33 */
34final class SkinOptions {
35
36    public const MOBILE_OPTIONS = 'mobileOptionsLink';
37    public const CATEGORIES = 'categories';
38    public const PAGE_ISSUES = 'pageIssues';
39    public const TALK_AT_TOP = 'talkAtTop';
40    public const SHOW_DONATE = 'donate';
41    public const SHOW_DONATE_BANNER = 'donateBanner';
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 MINIMAL = 'minimal';
47
48    /**
49     * Note stable skin options default to true for desktop-Minerva and are expected to be
50     * overridden on mobile.
51     * @var array skin specific options, initialized with default values
52     */
53    private array $skinOptions = [
54        self::SHOW_DONATE_BANNER => false,
55        self::SHOW_DONATE => true,
56        /**
57         * Whether the main menu should include a link to
58         * Special:Preferences of Special:MobileOptions
59         */
60        self::MOBILE_OPTIONS => false,
61        /** Whether a categories button should appear at the bottom of the skin. */
62        self::CATEGORIES => false,
63        /** requires a wiki using Template:Ambox */
64        self::PAGE_ISSUES => false,
65        /** no extension requirements */
66        self::TALK_AT_TOP => true,
67        /** no extension requirements */
68        self::HISTORY_IN_PAGE_ACTIONS => true,
69        /** no extension requirements */
70        self::TOOLBAR_SUBMENU => true,
71        /** Whether to show tabs on special pages */
72        self::TABS_ON_SPECIALS => true,
73        /** whether to show a main menu with additional items */
74        self::MAIN_MENU_EXPANDED => true,
75        /** whether to show a minimal/simplified header & toolbelt */
76        self::MINIMAL => false,
77    ];
78
79    public function __construct(
80        private readonly HookContainer $hookContainer,
81        private readonly SkinUserPageHelper $skinUserPageHelper,
82    ) {
83    }
84
85    /**
86     * override an existing option or options with new values
87     */
88    public function setMultiple( array $options ): void {
89        foreach ( $options as $option => $value ) {
90            if ( !array_key_exists( $option, $this->skinOptions ) ) {
91                throw new OutOfBoundsException( "SkinOption $option is not defined" );
92            }
93        }
94        $this->skinOptions = array_merge( $this->skinOptions, $options );
95    }
96
97    /**
98     * Return whether a skin option is truthy. Should be one of self:* constants
99     * @param string $key
100     * @return bool
101     */
102    public function get( string $key ): bool {
103        if ( !array_key_exists( $key, $this->skinOptions ) ) {
104            throw new OutOfBoundsException( "SkinOption $key doesn't exist" );
105        }
106        return $this->skinOptions[$key];
107    }
108
109    /**
110     * Get all skin options
111     */
112    public function getAll(): array {
113        return $this->skinOptions;
114    }
115
116    /**
117     * Return whether any of the skin options have been set
118     */
119    public function hasSkinOptions(): bool {
120        foreach ( $this->skinOptions as $key => $val ) {
121            if ( $val ) {
122                return true;
123            }
124        }
125        return false;
126    }
127
128    /**
129     * Set the skin options for Minerva
130     *
131     * @param MobileContext $mobileContext
132     * @param Skin $skin
133     */
134    public function setMinervaSkinOptions(
135        MobileContext $mobileContext, Skin $skin
136    ): void {
137        // setSkinOptions is not available
138        if ( $skin instanceof SkinMinerva ) {
139            $featuresManager = MediaWikiServices::getInstance()
140                ->getService( 'MobileFrontend.FeaturesManager' );
141            $title = $skin->getTitle();
142
143            // T245162 - this should only apply if the context relates to a page view.
144            // Examples:
145            // - parsing wikitext during an REST response
146            // - a ResourceLoader response
147            if ( $title !== null ) {
148                // T232653: TALK_AT_TOP, HISTORY_IN_PAGE_ACTIONS, TOOLBAR_SUBMENU should
149                // be true on user pages and user talk pages for all users
150                $this->skinUserPageHelper
151                    ->setContext( $mobileContext )
152                    ->setTitle(
153                        $title->inNamespace( NS_USER_TALK ) ? $title->getSubjectPage() : $title
154                    );
155
156                $isDiffLink = $mobileContext->getRequest()->getCheck( 'diff' );
157                $isUserPage = $this->skinUserPageHelper->isUserPage();
158                $isUserPageAccessible = $this->skinUserPageHelper->isUserPageAccessibleToCurrentUser();
159                $isUserPageOrUserTalkPage = $isUserPage && $isUserPageAccessible;
160                $requiresHistoryLink = $isUserPageOrUserTalkPage || $isDiffLink;
161            } else {
162                // If no title this must be false
163                $isUserPageOrUserTalkPage = false;
164                $requiresHistoryLink = false;
165            }
166
167            $advancedMenu = $featuresManager->isFeatureAvailableForCurrentUser(
168                'MinervaAdvancedMainMenu'
169            );
170            $this->setMultiple( [
171                self::SHOW_DONATE_BANNER => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaDonateBanner' ),
172                self::SHOW_DONATE => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaDonateLink' ),
173                self::TALK_AT_TOP => $isUserPageOrUserTalkPage ?
174                    true : $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaTalkAtTop' ),
175                self::CATEGORIES
176                    => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaShowCategories' ),
177                self::PAGE_ISSUES
178                    => $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaPageIssuesNewTreatment' ),
179                self::MOBILE_OPTIONS => true,
180                self::MAIN_MENU_EXPANDED => $advancedMenu,
181                self::HISTORY_IN_PAGE_ACTIONS => $requiresHistoryLink ?
182                    true : $featuresManager->isFeatureAvailableForCurrentUser( 'MinervaHistoryInPageActions' ),
183                self::TOOLBAR_SUBMENU => $isUserPageOrUserTalkPage ?
184                    true : $featuresManager->isFeatureAvailableForCurrentUser(
185                        Hooks::FEATURE_OVERFLOW_PAGE_ACTIONS
186                    ),
187                self::TABS_ON_SPECIALS => true,
188                self::MINIMAL => false,
189            ] );
190            ( new HookRunner( $this->hookContainer ) )->onSkinMinervaOptionsInit( $skin, $this );
191        }
192    }
193}