2019-04-08 17:08:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
namespace MediaWiki\Minerva\Menu\Main;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Director responsible for building Main Menu
|
|
|
|
*/
|
2019-08-02 09:00:18 +00:00
|
|
|
final class MainMenuDirector {
|
2019-04-08 17:08:57 +00:00
|
|
|
|
2024-03-28 19:15:41 +00:00
|
|
|
private IMainMenuBuilder $builder;
|
|
|
|
private ?array $menuData = null;
|
2019-04-08 17:08:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Director responsible for Main Menu building
|
|
|
|
*
|
2019-08-02 09:00:18 +00:00
|
|
|
* @param IMainMenuBuilder $builder
|
2019-04-08 17:08:57 +00:00
|
|
|
*/
|
2023-02-26 12:30:07 +00:00
|
|
|
public function __construct( IMainMenuBuilder $builder ) {
|
2019-04-08 17:08:57 +00:00
|
|
|
$this->builder = $builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a data representation of the main menus
|
2022-01-13 00:38:16 +00:00
|
|
|
*
|
|
|
|
* @param array $contentNavUrls result of buildContentNavigationUrls
|
|
|
|
* @param array $sidebar
|
2019-04-08 17:08:57 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2022-01-13 00:38:16 +00:00
|
|
|
public function getMenuData( array $contentNavUrls, array $sidebar ): array {
|
2019-04-08 17:08:57 +00:00
|
|
|
if ( $this->menuData === null ) {
|
2022-01-13 00:38:16 +00:00
|
|
|
$this->menuData = $this->buildMenu(
|
|
|
|
$contentNavUrls,
|
|
|
|
$sidebar
|
|
|
|
);
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
return $this->menuData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the menu data array that can be passed to views/javascript
|
2022-01-13 00:38:16 +00:00
|
|
|
*
|
|
|
|
* @param array $contentNavUrls
|
|
|
|
* @param array $sidebar
|
2019-04-08 17:08:57 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2022-01-13 00:38:16 +00:00
|
|
|
private function buildMenu( array $contentNavUrls, array $sidebar ): array {
|
|
|
|
$builder = $this->builder;
|
2019-04-08 17:08:57 +00:00
|
|
|
$menuData = [
|
2019-06-21 16:22:17 +00:00
|
|
|
'items' => [
|
|
|
|
'groups' => [],
|
2023-02-26 12:30:07 +00:00
|
|
|
'sitelinks' => $builder->getSiteLinks()->getEntries()
|
2019-06-21 16:22:17 +00:00
|
|
|
]
|
2019-04-08 17:08:57 +00:00
|
|
|
];
|
2022-05-12 17:58:08 +00:00
|
|
|
|
2022-01-13 00:38:16 +00:00
|
|
|
$groups = [
|
2022-02-04 21:16:40 +00:00
|
|
|
// sidebar comes from MediaWiki:Sidebar so we can't assume it doesn't exist.
|
|
|
|
$builder->getDiscoveryGroup( $sidebar['navigation'] ?? [] ),
|
2022-01-13 00:38:16 +00:00
|
|
|
$builder->getInteractionToolsGroup(),
|
|
|
|
$builder->getPersonalToolsGroup( $contentNavUrls['user-menu'] ),
|
2022-05-12 17:58:08 +00:00
|
|
|
$builder->getSettingsGroup(),
|
2022-01-13 00:38:16 +00:00
|
|
|
$builder->getDonateGroup(),
|
|
|
|
];
|
|
|
|
foreach ( $groups as $group ) {
|
2019-04-08 17:08:57 +00:00
|
|
|
if ( $group->hasEntries() ) {
|
2019-10-23 18:30:55 +00:00
|
|
|
$menuData['items']['groups'][] = $group->serialize();
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $menuData;
|
|
|
|
}
|
|
|
|
}
|