Debian Codegraph @

skins/MinervaNeue/includes/Menu/Main/BuilderUtil.php

raw symbols SCIP
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\SpecialPage\SpecialPage;
27 use MediaWiki\Title\Title;
28
29 /**
30 * Group generators shared between menu builders.
31 *
32 * @package MediaWiki\Minerva\Menu\Main
33 */
34 final class BuilderUtil {
35 /**
36 * Prepares donate group if available
37 * @param Definitions $definitions A menu items definitions set
38 * @param bool $includeDonateLink whether to include it or not.
39 * @return Group
40 */
41 public static function getDonateGroup( Definitions $definitions, $includeDonateLink ): Group {
42 $group = new Group( 'p-donation' );
43 if ( $includeDonateLink ) {
44 $definitions->insertDonateItem( $group );
45 }
46 return $group;
47 }
48
49 /**
50 * Prepares a list of links that have the purpose of discovery in the main navigation menu
51 * @param Definitions $definitions A menu items definitions set
52 * @param array $navigationTools
53 * @return Group
54 */
55 public static function getDiscoveryTools(
56 Definitions $definitions,
57 array $navigationTools
58 ): Group {
59 $group = new Group( 'p-navigation' );
60
61 $entryDefinitions = [
62 'n-mainpage-description' => [
63 'name' => 'home',
64 'text' => $definitions->msg( 'mobile-frontend-home-button' ),
65 'icon' => 'home',
66 'class' => '',
67 'href' => Title::newMainPage()->getLocalURL(),
68 ],
69 'n-randompage' => [
70 'name' => 'random',
71 'text' => $definitions->msg( 'mobile-frontend-random-button' ),
72 'icon' => 'die',
73 'class' => '',
74 'href' => SpecialPage::getTitleFor( 'Randompage' )->getLocalURL(),
75 ],
76 ];
77 // Run through navigation tools and update if needed.
78 foreach ( $navigationTools as $item ) {
79 $id = $item['id'] ?? null;
80 if ( $id && isset( $entryDefinitions[ $id ] ) ) {
81 foreach ( [ 'icon', 'class', 'href', 'msg' ] as $overridableKey ) {
82 $override = $item[ $overridableKey ] ?? null;
83 if ( $override ) {
84 $entryDefinitions[$id][$overridableKey] = $override;
85 }
86 }
87 }
88 }
89 // Build the menu
90 foreach ( $entryDefinitions as $definition ) {
91 $msgKey = $definition['msg'] ?? null;
92 $text = null;
93 if ( $msgKey ) {
94 $msg = $definitions->msg( $msgKey );
95 $text = $msg->exists() ? $msg->text() : null;
96 }
97 if ( !$text ) {
98 $text = $definition['text'];
99 }
100
101 $entry = SingleMenuEntry::create(
102 $definition['name'],
103 $text,
104 $definition['href'],
105 $definition['class'],
106 $definition['icon']
107 );
108 $group->insertEntry( $entry );
109 }
110 $definitions->insertNearbyIfSupported( $group );
111 return $group;
112 }
113
114 /**
115 * Like <code>SkinMinerva#getDiscoveryTools</code> and <code>#getPersonalTools</code>, create
116 * a group of configuration-related menu items. Currently, only the Settings menu item is in the
117 * group.
118 * @param Definitions $definitions A menu items definitions set
119 * @param bool $showMobileOptions Show MobileOptions instead of Preferences
120 * @return Group
121 */
122 public static function getConfigurationTools(
123 Definitions $definitions, $showMobileOptions
124 ): Group {
125 $group = new Group( 'pt-preferences' );
126
127 $showMobileOptions ?
128 $definitions->insertMobileOptionsItem( $group ) :
129 $definitions->insertPreferencesItem( $group );
130
131 return $group;
132 }
133
134 /**
135 * Returns an array of sitelinks to add into the main menu footer.
136 * @param Definitions $definitions A menu items definitions set
137 * @return Group Collection of site links
138 */
139 public static function getSiteLinks( Definitions $definitions ): Group {
140 $group = new Group( 'p-minerva-sitelinks' );
141
142 $definitions->insertAboutItem( $group );
143 $definitions->insertDisclaimersItem( $group );
144 return $group;
145 }
146 }