Skip to content

Commit

Permalink
Provide a code structure for menus handling and add Advanced menu
Browse files Browse the repository at this point in the history
Changes:
 - moved all menu elements definitions from SkinMinerva into
 a separate Definitions.php file
 - moved menu building from SkinMinerva into includes/menu/Main
 folder
 - introduced Builder pattern for easy menu building
 Minerva/Menu/Main/Director takes an Minerva/Menu/Main/IBuilder
 and builds the menu. The IBuilders use definitions from
 Minerva/Menu/Definitions file, so all definitions can be shared
 across different menus
 - used ServiceWiring file to register MainMenu Director as Service
 - left class_alias for old MenuBuilder as some extensions still use it
 - The hooks system have to stay like that as some extensions
 are using it (BlueSpiceMultiUpload and GrowthExperiments).
 - introduced AdvancedMenu builder for the AMC mode

Bug: T216152
Change-Id: I210c3f1fa36bbd2f9108d728b12cbb21ee210354
  • Loading branch information
polishdeveloper committed Apr 16, 2019
1 parent 258e635 commit 1f4582c
Show file tree
Hide file tree
Showing 14 changed files with 813 additions and 339 deletions.
36 changes: 36 additions & 0 deletions includes/ServiceWiring.php
@@ -1,13 +1,49 @@
<?php <?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
*/

use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use MediaWiki\Minerva\Menu\Definitions;
use MediaWiki\Minerva\Menu\Main\AdvancedBuilder;
use MediaWiki\Minerva\Menu\Main\DefaultBuilder;
use MediaWiki\Minerva\Menu\Main\Director;
use MediaWiki\Minerva\SkinOptions; use MediaWiki\Minerva\SkinOptions;
use MediaWiki\Minerva\SkinUserPageHelper; use MediaWiki\Minerva\SkinUserPageHelper;


return [ return [
'Minerva.ContentHandler' => function ( MediaWikiServices $services ) { 'Minerva.ContentHandler' => function ( MediaWikiServices $services ) {
return ContentHandler::getForTitle( RequestContext::getMain()->getTitle() ); return ContentHandler::getForTitle( RequestContext::getMain()->getTitle() );
}, },
'Minerva.Menu.MainDirector' => function ( MediaWikiServices $services ) {
$context = RequestContext::getMain();
/** @var SkinOptions $options */
$options = $services->getService( 'Minerva.SkinOptions' );
$showMobileOptions = $options->get( SkinOptions::OPTION_MOBILE_OPTIONS );
$user = $context->getUser();
$definitions = new Definitions( $context, $services->getSpecialPageFactory() );
$builder = $options->get( SkinOptions::OPTION_AMC ) ?
new AdvancedBuilder( $showMobileOptions, $user, $definitions ) :
new DefaultBuilder( $showMobileOptions, $user, $definitions );

return new Director( $builder );
},
'Minerva.SkinUserPageHelper' => function ( MediaWikiServices $services ) { 'Minerva.SkinUserPageHelper' => function ( MediaWikiServices $services ) {
return new SkinUserPageHelper( RequestContext::getMain()->getTitle() ); return new SkinUserPageHelper( RequestContext::getMain()->getTitle() );
}, },
Expand Down
332 changes: 332 additions & 0 deletions includes/menu/Definitions.php
@@ -0,0 +1,332 @@
<?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;
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 ) {
$group->insert( 'contribs' )
->addComponent(
$this->context->msg( 'mobile-frontend-main-menu-contributions' )->escaped(),
SpecialPage::getTitleFor( 'Contributions', $this->user->getName() )->getLocalUrl(),
MinervaUI::iconClass( 'contributions', 'before' ),
[ 'data-event-name' => 'contributions' ]
);
}

/**
* 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;
}
}

$group->insert( 'watchlist' )
->addComponent(
$this->context->msg( 'mobile-frontend-main-menu-watchlist' )->escaped(),
$watchTitle->getLocalURL( $watchlistQuery ),
MinervaUI::iconClass( 'watchlist', 'before' ),
[ 'data-event-name' => 'watchlist' ]
);
}

/**
* Creates a login or logout button
*
* @param Group $group
* @throws MWException
*/
public function insertLogInOutMenuItem( Group $group ) {
$query = [];
$returntoquery = [];
$request = $this->context->getRequest();

if ( !$request->wasPosted() ) {
$returntoquery = $request->getValues();
unset( $returntoquery['title'] );
unset( $returntoquery['returnto'] );
unset( $returntoquery['returntoquery'] );
}
$title = $this->context->getTitle();
// Don't ever redirect back to the login page (bug 55379)
if ( !$title->isSpecial( 'Userlogin' ) ) {
$query[ 'returnto' ] = $title->getPrefixedText();
}

if ( $this->user->isLoggedIn() ) {
if ( !empty( $returntoquery ) ) {
$query[ 'returntoquery' ] = wfArrayToCgi( $returntoquery );
}
$url = SpecialPage::getTitleFor( 'Userlogout' )->getLocalURL( $query );
$username = $this->user->getName();

$group->insert( 'auth', false )
->addComponent(
$username,
Title::newFromText( $username, NS_USER )->getLocalUrl(),
MinervaUI::iconClass( 'profile', 'before', 'truncated-text primary-action' ),
[ 'data-event-name' => 'profile' ]
)
->addComponent(
$this->context->msg( 'mobile-frontend-main-menu-logout' )->escaped(),
$url,
MinervaUI::iconClass(
'logout', 'element', 'secondary-action truncated-text' ),
[ 'data-event-name' => 'logout' ]
);
} else {
// unset campaign on login link so as not to interfere with A/B tests
unset( $returntoquery['campaign'] );
$query[ 'returntoquery' ] = wfArrayToCgi( $returntoquery );
$url = $this->getLoginUrl( $query );
$group->insert( 'auth', false )
->addComponent(
$this->context->msg( 'mobile-frontend-main-menu-login' )->escaped(),
$url,
MinervaUI::iconClass( 'login', 'before' ),
[ 'data-event-name' => 'login' ]
);
}
}

/**
* Build and insert Home link
* @param Group $group
*/
public function insertHomeItem( Group $group ) {
// Home link
$group->insert( 'home' )
->addComponent(
$this->context->msg( 'mobile-frontend-home-button' )->escaped(),
Title::newMainPage()->getLocalUrl(),
MinervaUI::iconClass( 'home', 'before' ),
[ 'data-event-name' => 'home' ]
);
}

/**
* Build and insert Random link
* @param Group $group
* @throws MWException
*/
public function insertRandomItem( Group $group ) {
// Random link
$group->insert( 'random' )
->addComponent( $this->context->msg( 'mobile-frontend-random-button' )->escaped(),
SpecialPage::getTitleFor( 'Randompage' )->getLocalUrl() . '#/random',
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();
$group->insert( 'settings' )
->addComponent(
$this->context->msg( 'mobile-frontend-main-menu-settings' )->escaped(),
SpecialPage::getTitleFor( 'MobileOptions' )->
getLocalURL( [ 'returnto' => $returnToTitle ] ),
MinervaUI::iconClass( 'settings', 'before' ),
[ 'data-event-name' => 'settings' ]
);
}

/**
* Build and insert the Preferences link
* @param Group $group
* @throws MWException
*/
public function insertPreferencesItem( Group $group ) {
$group->insert( 'preferences' )
->addComponent(
$this->context->msg( 'preferences' )->escaped(),
SpecialPage::getTitleFor( 'Preferences' )->getLocalURL(),
MinervaUI::iconClass( 'settings', 'before' ),
[ 'data-event-name' => 'preferences' ]
);
}

/**
* 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' )
->addComponent( $msg->text(), $title->getLocalUrl() );
}
}

/**
* 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' )
->addComponent( $msg->text(), $title->getLocalUrl() );
}
}

/**
* Build and insert the RecentChanges link
* @param Group $group
* @throws MWException
*/
public function insertRecentChanges( Group $group ) {
$title = SpecialPage::getTitleFor( 'Recentchanges' );

$group->insert( 'recentchanges' )
->addComponent(
$this->context->msg( 'recentchanges' )->escaped(),
$title->getLocalURL(),
MinervaUI::iconClass( 'recentchanges', 'before' ),
[ 'data-event-name' => 'recentchanges' ]
);
}

/**
* Build and insert the SpecialPages link
* @param Group $group
* @throws MWException
*/
public function insertSpecialPages( Group $group ) {
$group->insert( 'specialpages' )
->addComponent(
$this->context->msg( 'specialpages' )->escaped(),
SpecialPage::getTitleFor( 'Specialpages' )->getLocalURL(),
MinervaUI::iconClass( 'specialpages', 'before' ),
[ 'data-event-name' => 'specialpages' ]
);
}

/**
* Build and insert the CommunityPortal link
* @param Group $group
* @throws MWException
*/
public function insertCommunityPortal( Group $group ) {
/// placeholder for a follow-up patch @see T216152
}

/**
* Prepares a url to the Special:UserLogin with query parameters
*
* @param array $query
* @return string
* @throws MWException
*/
private function getLoginUrl( $query ) {
return SpecialPage::getTitleFor( 'Userlogin' )->getLocalURL( $query );
}

}

0 comments on commit 1f4582c

Please sign in to comment.