Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
DefaultMainMenuBuilder
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 7
380
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getDiscoveryGroup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDonateGroup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getInteractionToolsGroup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSiteLinks
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSettingsGroup
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 getPersonalToolsGroup
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
110
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
21namespace MediaWiki\Minerva\Menu\Main;
22
23use MediaWiki\Minerva\Menu\Definitions;
24use MediaWiki\Minerva\Menu\Entries\SingleMenuEntry;
25use MediaWiki\Minerva\Menu\Group;
26use MediaWiki\Title\Title;
27use MediaWiki\User\User;
28use MediaWiki\User\UserIdentityUtils;
29
30/**
31 * Used to build default (available for everyone by default) main menu
32 */
33final class DefaultMainMenuBuilder implements IMainMenuBuilder {
34
35    private bool $showMobileOptions;
36    private bool $showDonateLink;
37    private User $user;
38    private Definitions $definitions;
39    private UserIdentityUtils $userIdentityUtils;
40
41    /**
42     * Initialize the Default Main Menu builder
43     *
44     * @param bool $showMobileOptions Show MobileOptions instead of Preferences
45     * @param bool $showDonateLink whether to show the donate link
46     * @param User $user The current user
47     * @param Definitions $definitions A menu items definitions set
48     * @param UserIdentityUtils $userIdentityUtils
49     */
50    public function __construct(
51        $showMobileOptions,
52        $showDonateLink,
53        User $user,
54        Definitions $definitions,
55        UserIdentityUtils $userIdentityUtils
56    ) {
57        $this->showMobileOptions = $showMobileOptions;
58        $this->showDonateLink = $showDonateLink;
59        $this->user = $user;
60        $this->definitions = $definitions;
61        $this->userIdentityUtils = $userIdentityUtils;
62    }
63
64    /**
65     * @inheritDoc
66     */
67    public function getDiscoveryGroup( array $navigationTools ): Group {
68        return BuilderUtil::getDiscoveryTools( $this->definitions, $navigationTools );
69    }
70
71    /**
72     * @inheritDoc
73     */
74    public function getDonateGroup(): Group {
75        return BuilderUtil::getDonateGroup( $this->definitions, $this->showDonateLink );
76    }
77
78    /**
79     * @inheritDoc
80     */
81    public function getInteractionToolsGroup(): Group {
82        return new Group( 'p-interaction' );
83    }
84
85    /**
86     * @inheritDoc
87     */
88    public function getSiteLinks(): Group {
89        return BuilderUtil::getSiteLinks( $this->definitions );
90    }
91
92    /**
93     * Builds the anonymous settings group.
94     *
95     * @inheritDoc
96     */
97    public function getSettingsGroup(): Group {
98        $group = new Group( 'pt-preferences' );
99        // Show settings group for anon and temp users
100        $isTemp = $this->userIdentityUtils->isTemp( $this->user );
101        if ( $this->showMobileOptions && ( !$this->user->isRegistered() || $isTemp ) ) {
102            $this->definitions->insertMobileOptionsItem( $group );
103        }
104        return $group;
105    }
106
107    /**
108     * Builds the personal tools menu item group.
109     *
110     * ... by adding the Watchlist, Settings, and Log{in,out} menu items in the given order.
111     *
112     * @inheritDoc
113     */
114    public function getPersonalToolsGroup( array $personalTools ): Group {
115        $group = new Group( 'p-personal' );
116        $excludeKeyList = [ 'betafeatures', 'mytalk', 'sandbox' ];
117
118        // For anonymous users exclude all links except login.
119        if ( !$this->user->isRegistered() ) {
120            $excludeKeyList = array_diff(
121                array_keys( $personalTools ),
122                [ 'login' ]
123            );
124        }
125        $isTemp = $this->userIdentityUtils->isTemp( $this->user );
126        if ( $isTemp ) {
127            $excludeKeyList[] = 'mycontris';
128        }
129        foreach ( $personalTools as $key => $item ) {
130            // Default to EditWatchlist if $user has no edits
131            // Many users use the watchlist like a favorites list without ever editing.
132            // [T88270].
133            if ( $key === 'watchlist' && $this->user->getEditCount() === 0 ) {
134                $item['href'] = Title::newFromText( 'Special:EditWatchlist' )->getLocalUrl();
135            }
136            $href = $item['href'] ?? null;
137            if ( $href && !in_array( $key, $excludeKeyList ) ) {
138                // Substitute preference if $showMobileOptions is set.
139                if ( $this->showMobileOptions && $key === 'preferences' ) {
140                    $this->definitions->insertMobileOptionsItem( $group );
141                } else {
142                    $icon = $item['icon'] ?? null;
143                    $entry = SingleMenuEntry::create(
144                        $key,
145                        $item['text'],
146                        $href,
147                        $item['class'] ?? '',
148                        $icon
149                    );
150
151                    $entry->trackClicks( $key );
152                    $group->insertEntry( $entry );
153                }
154            }
155        }
156        return $group;
157    }
158}