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;
|
|
|
|
|
|
|
|
use IContextSource;
|
|
|
|
use MediaWiki\Special\SpecialPageFactory;
|
2019-07-01 18:06:01 +00:00
|
|
|
use MediaWiki\Minerva\Menu\Entries\AuthMenuEntry;
|
|
|
|
use MediaWiki\Minerva\Menu\Entries\HomeMenuEntry;
|
|
|
|
use MediaWiki\Minerva\Menu\Entries\SingleMenuEntry;
|
2019-04-26 22:09:23 +00:00
|
|
|
use Message;
|
2019-04-08 17:08:57 +00:00
|
|
|
use MinervaUI;
|
|
|
|
use MWException;
|
|
|
|
use SpecialMobileWatchlist;
|
|
|
|
use SpecialPage;
|
|
|
|
use Title;
|
|
|
|
use User;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set of all know menu items for easier building
|
|
|
|
*/
|
|
|
|
final class Definitions {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IContextSource
|
|
|
|
*/
|
|
|
|
private $context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SpecialPageFactory
|
|
|
|
*/
|
|
|
|
private $specialPageFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize definitions helper class
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @param SpecialPageFactory $factory
|
|
|
|
*/
|
|
|
|
public function __construct( IContextSource $context, SpecialPageFactory $factory ) {
|
|
|
|
$this->user = $context->getUser();
|
|
|
|
$this->context = $context;
|
|
|
|
$this->specialPageFactory = $factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts the Contributions menu item into the menu.
|
|
|
|
*
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertContributionsMenuItem( Group $group ) {
|
2019-05-14 10:40:56 +00:00
|
|
|
$group->insertEntry( new SingleMenuEntry(
|
|
|
|
'contributions',
|
|
|
|
$this->context->msg( 'mobile-frontend-main-menu-contributions' )->escaped(),
|
|
|
|
SpecialPage::getTitleFor( 'Contributions', $this->user->getName() )->getLocalURL()
|
|
|
|
|
|
|
|
) );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts the Watchlist menu item into the menu for a logged in user
|
|
|
|
*
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertWatchlistMenuItem( Group $group ) {
|
|
|
|
$watchTitle = SpecialPage::getTitleFor( 'Watchlist' );
|
|
|
|
|
|
|
|
// Watchlist link
|
|
|
|
$watchlistQuery = [];
|
|
|
|
// Avoid fatal when MobileFrontend not available (T171241)
|
|
|
|
if ( class_exists( 'SpecialMobileWatchlist' ) ) {
|
|
|
|
$view = $this->user->getOption( SpecialMobileWatchlist::VIEW_OPTION_NAME, false );
|
|
|
|
$filter = $this->user->getOption( SpecialMobileWatchlist::FILTER_OPTION_NAME, false );
|
|
|
|
if ( $view ) {
|
|
|
|
$watchlistQuery['watchlistview'] = $view;
|
|
|
|
}
|
|
|
|
if ( $filter && $view === 'feed' ) {
|
|
|
|
$watchlistQuery['filter'] = $filter;
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 10:40:56 +00:00
|
|
|
$group->insertEntry( new SingleMenuEntry(
|
|
|
|
'watchlist',
|
|
|
|
$this->context->msg( 'mobile-frontend-main-menu-watchlist' )->escaped(),
|
|
|
|
$watchTitle->getLocalURL( $watchlistQuery )
|
|
|
|
) );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a login or logout button
|
|
|
|
*
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertLogInOutMenuItem( Group $group ) {
|
2019-06-04 21:39:34 +00:00
|
|
|
$group->insertEntry( new AuthMenuEntry(
|
|
|
|
$this->user,
|
|
|
|
$this->context->getRequest(),
|
|
|
|
$this->context,
|
|
|
|
$this->context->getTitle()
|
|
|
|
) );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert Home link
|
|
|
|
* @param Group $group
|
|
|
|
*/
|
|
|
|
public function insertHomeItem( Group $group ) {
|
2019-06-27 02:04:42 +00:00
|
|
|
$group->insertEntry( new HomeMenuEntry(
|
2019-05-14 10:40:56 +00:00
|
|
|
'home',
|
|
|
|
$this->context->msg( 'mobile-frontend-home-button' )->escaped(),
|
|
|
|
Title::newMainPage()->getLocalURL()
|
|
|
|
) );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert Random link
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertRandomItem( Group $group ) {
|
2019-07-09 11:05:00 +00:00
|
|
|
$pageMsg = new Message( 'randompage-url' );
|
|
|
|
if ( !$pageMsg->exists() ) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-08 17:08:57 +00:00
|
|
|
$group->insert( 'random' )
|
|
|
|
->addComponent( $this->context->msg( 'mobile-frontend-random-button' )->escaped(),
|
2019-07-09 11:05:00 +00:00
|
|
|
Title::newFromText( $pageMsg->escaped() )->getLocalURL() . '#/random',
|
2019-04-08 17:08:57 +00:00
|
|
|
MinervaUI::iconClass( 'random', 'before' ), [
|
|
|
|
'id' => 'randomButton',
|
|
|
|
'data-event-name' => 'random',
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If Nearby is supported, build and inject the Nearby link
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertNearbyIfSupported( Group $group ) {
|
|
|
|
// Nearby link (if supported)
|
|
|
|
if ( $this->specialPageFactory->exists( 'Nearby' ) ) {
|
|
|
|
$group->insert( 'nearby', $isJSOnly = true )
|
|
|
|
->addComponent(
|
|
|
|
$this->context->msg( 'mobile-frontend-main-menu-nearby' )->escaped(),
|
|
|
|
SpecialPage::getTitleFor( 'Nearby' )->getLocalURL(),
|
|
|
|
MinervaUI::iconClass( 'nearby', 'before', 'nearby' ),
|
|
|
|
[ 'data-event-name' => 'nearby' ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert the Settings link
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertMobileOptionsItem( Group $group ) {
|
|
|
|
$title = $this->context->getTitle();
|
|
|
|
$returnToTitle = $title->getPrefixedText();
|
2019-05-14 10:40:56 +00:00
|
|
|
|
|
|
|
$group->insertEntry( new SingleMenuEntry(
|
|
|
|
'settings',
|
|
|
|
$this->context->msg( 'mobile-frontend-main-menu-settings' )->escaped(),
|
|
|
|
SpecialPage::getTitleFor( 'MobileOptions' )
|
|
|
|
->getLocalURL( [ 'returnto' => $returnToTitle ] )
|
|
|
|
) );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert the Preferences link
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertPreferencesItem( Group $group ) {
|
2019-05-14 10:40:56 +00:00
|
|
|
$group->insertEntry( new SingleMenuEntry(
|
|
|
|
'preferences',
|
|
|
|
$this->context->msg( 'preferences' )->escaped(),
|
|
|
|
SpecialPage::getTitleFor( 'Preferences' )->getLocalURL(),
|
|
|
|
true,
|
|
|
|
'settings'
|
|
|
|
) );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert About page link
|
|
|
|
* @param Group $group
|
|
|
|
*/
|
|
|
|
public function insertAboutItem( Group $group ) {
|
|
|
|
$title = Title::newFromText( $this->context->msg( 'aboutpage' )->inContentLanguage()->text() );
|
|
|
|
$msg = $this->context->msg( 'aboutsite' );
|
|
|
|
if ( $title && !$msg->isDisabled() ) {
|
|
|
|
$group->insert( 'about' )
|
2019-04-19 12:36:02 +00:00
|
|
|
->addComponent( $msg->text(), $title->getLocalURL() );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert Disclaimers link
|
|
|
|
* @param Group $group
|
|
|
|
*/
|
|
|
|
public function insertDisclaimersItem( Group $group ) {
|
|
|
|
$title = Title::newFromText( $this->context->msg( 'disclaimerpage' )
|
|
|
|
->inContentLanguage()->text() );
|
|
|
|
$msg = $this->context->msg( 'disclaimers' );
|
|
|
|
if ( $title && !$msg->isDisabled() ) {
|
|
|
|
$group->insert( 'disclaimers' )
|
2019-04-19 12:36:02 +00:00
|
|
|
->addComponent( $msg->text(), $title->getLocalURL() );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert the SpecialPages link
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertSpecialPages( Group $group ) {
|
2019-05-14 10:40:56 +00:00
|
|
|
$group->insertEntry(
|
|
|
|
new SingleMenuEntry(
|
|
|
|
'specialpages',
|
2019-04-08 17:08:57 +00:00
|
|
|
$this->context->msg( 'specialpages' )->escaped(),
|
2019-05-14 10:40:56 +00:00
|
|
|
SpecialPage::getTitleFor( 'Specialpages' )->getLocalURL()
|
|
|
|
)
|
|
|
|
);
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert the CommunityPortal link
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertCommunityPortal( Group $group ) {
|
2019-04-26 22:09:23 +00:00
|
|
|
$message = new Message( 'Portal-url' );
|
|
|
|
if ( !$message->exists() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$inContentLang = $message->inContentLanguage();
|
|
|
|
$titleName = $inContentLang->plain();
|
|
|
|
if ( $inContentLang->isDisabled() || \Http::isValidURI( $titleName ) ) {
|
2019-04-09 17:06:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$title = Title::newFromText( $titleName );
|
2019-05-03 09:19:04 +00:00
|
|
|
if ( $title === null || !$title->exists() ) {
|
2019-04-09 17:06:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-14 10:40:56 +00:00
|
|
|
$group->insertEntry( new SingleMenuEntry(
|
|
|
|
'communityportal',
|
|
|
|
$title->getText(),
|
|
|
|
$title->getLocalURL()
|
|
|
|
) );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|