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;
|
|
|
|
|
2020-01-26 19:26:31 +00:00
|
|
|
use MediaWiki\Special\SpecialPageFactory;
|
2019-06-21 16:22:17 +00:00
|
|
|
use MessageLocalizer;
|
|
|
|
|
2019-04-08 17:08:57 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
/**
|
2019-08-02 09:00:18 +00:00
|
|
|
* @var IMainMenuBuilder
|
2019-04-08 17:08:57 +00:00
|
|
|
*/
|
|
|
|
private $builder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $menuData;
|
|
|
|
|
2019-06-21 16:22:17 +00:00
|
|
|
/**
|
|
|
|
* @var MessageLocalizer
|
|
|
|
*/
|
|
|
|
private $msgLocalizer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SpecialPageFactory
|
|
|
|
*/
|
|
|
|
private $specialPageFactory;
|
|
|
|
|
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-06-21 16:22:17 +00:00
|
|
|
* @param MessageLocalizer $messageLocalizer Used for translating texts in menu toggle
|
|
|
|
* @param SpecialPageFactory $specialPageFactory Used to check for MobileMenu special page
|
|
|
|
* existence
|
2019-04-08 17:08:57 +00:00
|
|
|
*/
|
2019-06-21 16:22:17 +00:00
|
|
|
public function __construct(
|
2019-08-02 09:00:18 +00:00
|
|
|
IMainMenuBuilder $builder,
|
2019-06-21 16:22:17 +00:00
|
|
|
MessageLocalizer $messageLocalizer,
|
|
|
|
SpecialPageFactory $specialPageFactory
|
|
|
|
) {
|
2019-04-08 17:08:57 +00:00
|
|
|
$this->builder = $builder;
|
2019-06-21 16:22:17 +00:00
|
|
|
$this->msgLocalizer = $messageLocalizer;
|
|
|
|
$this->specialPageFactory = $specialPageFactory;
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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' => [],
|
|
|
|
'sitelinks' => $this->builder->getSiteLinks()->getEntries()
|
|
|
|
]
|
2019-04-08 17:08:57 +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'] ),
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|