Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
DefaultMainMenuBuilder | |
0.00% |
0 / 42 |
|
0.00% |
0 / 7 |
380 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getDiscoveryGroup | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDonateGroup | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getInteractionToolsGroup | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSiteLinks | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSettingsGroup | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
getPersonalToolsGroup | |
0.00% |
0 / 28 |
|
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 | |
21 | namespace MediaWiki\Minerva\Menu\Main; |
22 | |
23 | use MediaWiki\Minerva\Menu\Definitions; |
24 | use MediaWiki\Minerva\Menu\Entries\SingleMenuEntry; |
25 | use MediaWiki\Minerva\Menu\Group; |
26 | use MediaWiki\Title\Title; |
27 | use MediaWiki\User\User; |
28 | use MediaWiki\User\UserIdentityUtils; |
29 | |
30 | /** |
31 | * Used to build default (available for everyone by default) main menu |
32 | */ |
33 | final 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', 'login-private' ] |
123 | ); |
124 | } |
125 | |
126 | $isTemp = $this->userIdentityUtils->isTemp( $this->user ); |
127 | if ( $isTemp ) { |
128 | $excludeKeyList[] = 'mycontris'; |
129 | } |
130 | foreach ( $personalTools as $key => $item ) { |
131 | // Default to EditWatchlist if $user has no edits |
132 | // Many users use the watchlist like a favorites list without ever editing. |
133 | // [T88270]. |
134 | if ( $key === 'watchlist' && $this->user->getEditCount() === 0 ) { |
135 | $item['href'] = Title::newFromText( 'Special:EditWatchlist' )->getLocalUrl(); |
136 | } |
137 | $href = $item['href'] ?? null; |
138 | if ( $href && !in_array( $key, $excludeKeyList ) ) { |
139 | // Substitute preference if $showMobileOptions is set. |
140 | if ( $this->showMobileOptions && $key === 'preferences' ) { |
141 | $this->definitions->insertMobileOptionsItem( $group ); |
142 | } else { |
143 | $icon = $item['icon'] ?? null; |
144 | $entry = SingleMenuEntry::create( |
145 | $key, |
146 | $item['text'], |
147 | $href, |
148 | $item['class'] ?? '', |
149 | $icon |
150 | ); |
151 | |
152 | $entry->trackClicks( $key ); |
153 | $group->insertEntry( $entry ); |
154 | } |
155 | } |
156 | } |
157 | return $group; |
158 | } |
159 | } |