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
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-06-21 16:52:22 +00:00
|
|
|
public function getMenuData(): array {
|
2019-04-08 17:08:57 +00:00
|
|
|
if ( $this->menuData === null ) {
|
|
|
|
$this->menuData = $this->buildMenu();
|
|
|
|
}
|
|
|
|
return $this->menuData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the menu data array that can be passed to views/javascript
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-06-21 16:52:22 +00:00
|
|
|
private function buildMenu(): array {
|
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
|
|
|
];
|
|
|
|
foreach ( $this->builder->getGroups() as $group ) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|