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;
|
2019-07-01 18:06:01 +00:00
|
|
|
use MediaWiki\Minerva\Menu\Entries\AuthMenuEntry;
|
|
|
|
use MediaWiki\Minerva\Menu\Entries\SingleMenuEntry;
|
2022-02-26 21:08:43 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPageFactory;
|
2021-12-17 17:16:49 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2021-11-23 09:18:46 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
2022-02-16 17:42:32 +00:00
|
|
|
use Message;
|
2019-04-08 17:08:57 +00:00
|
|
|
use MWException;
|
|
|
|
use SpecialPage;
|
|
|
|
use Title;
|
|
|
|
|
|
|
|
/**
|
2022-06-21 16:20:53 +00:00
|
|
|
* Set of all known menu items for easier building
|
2019-04-08 17:08:57 +00:00
|
|
|
*/
|
|
|
|
final class Definitions {
|
|
|
|
|
|
|
|
/**
|
2021-12-17 17:16:49 +00:00
|
|
|
* @var UserIdentity
|
2019-04-08 17:08:57 +00:00
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IContextSource
|
|
|
|
*/
|
|
|
|
private $context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SpecialPageFactory
|
|
|
|
*/
|
|
|
|
private $specialPageFactory;
|
|
|
|
|
2021-11-23 09:18:46 +00:00
|
|
|
/**
|
|
|
|
* @var UserOptionsLookup
|
|
|
|
*/
|
|
|
|
private $userOptionsLookup;
|
|
|
|
|
2019-04-08 17:08:57 +00:00
|
|
|
/**
|
|
|
|
* Initialize definitions helper class
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @param SpecialPageFactory $factory
|
2021-11-23 09:18:46 +00:00
|
|
|
* @param UserOptionsLookup $userOptionsLookup
|
2019-04-08 17:08:57 +00:00
|
|
|
*/
|
2021-11-23 09:18:46 +00:00
|
|
|
public function __construct(
|
|
|
|
IContextSource $context,
|
|
|
|
SpecialPageFactory $factory,
|
|
|
|
UserOptionsLookup $userOptionsLookup
|
|
|
|
) {
|
2019-04-08 17:08:57 +00:00
|
|
|
$this->user = $context->getUser();
|
|
|
|
$this->context = $context;
|
|
|
|
$this->specialPageFactory = $factory;
|
2021-11-23 09:18:46 +00:00
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 16:20:53 +00:00
|
|
|
/**
|
|
|
|
* Builds a meny entry.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $text Entry label
|
|
|
|
* @param string $url The URL entry points to
|
|
|
|
* @param string $className Optional HTML classes
|
|
|
|
* @param string|null $icon defaults to $name if not specified
|
|
|
|
* @param bool $trackable Whether an entry will track clicks or not. Default is false.
|
|
|
|
* @throws MWException
|
|
|
|
* @return SingleMenuEntry
|
|
|
|
*/
|
|
|
|
private function buildMenuEntry( $name, $text, $url, $className = '', $icon = null, $trackable = false ) {
|
|
|
|
$entry = SingleMenuEntry::create( $name, $text, $url, $className, $icon, $trackable );
|
|
|
|
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:08:57 +00:00
|
|
|
/**
|
2019-07-22 20:58:34 +00:00
|
|
|
* Creates a login or logout button with a profile button.
|
2019-04-08 17:08:57 +00:00
|
|
|
*
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
2019-07-22 20:58:34 +00:00
|
|
|
public function insertAuthMenuItem( Group $group ) {
|
2019-06-04 21:39:34 +00:00
|
|
|
$group->insertEntry( new AuthMenuEntry(
|
2022-01-13 00:38:16 +00:00
|
|
|
$this->user,
|
|
|
|
$this->context,
|
|
|
|
$this->newLogInOutQuery( $this->newReturnToQuery() )
|
2019-06-04 21:39:34 +00:00
|
|
|
) );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-18 23:36:30 +00:00
|
|
|
* Perform message localization
|
|
|
|
* @param string $key to localize
|
2022-02-16 17:42:32 +00:00
|
|
|
* @return Message
|
2019-04-08 17:08:57 +00:00
|
|
|
*/
|
2022-01-18 23:36:30 +00:00
|
|
|
public function msg( string $key ) {
|
|
|
|
return $this->context->msg( $key );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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' ) ) {
|
2022-06-21 16:20:53 +00:00
|
|
|
$entry = $this->buildMenuEntry(
|
|
|
|
'nearby',
|
|
|
|
$this->context->msg( 'mobile-frontend-main-menu-nearby' )->text(),
|
|
|
|
SpecialPage::getTitleFor( 'Nearby' )->getLocalURL(),
|
|
|
|
'',
|
|
|
|
'mapPin',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
// Setting this feature for javascript only
|
|
|
|
$entry->setJSOnly();
|
2022-07-12 16:22:59 +00:00
|
|
|
$group->insertEntry( $entry );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert the Settings link
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertMobileOptionsItem( Group $group ) {
|
|
|
|
$title = $this->context->getTitle();
|
2020-01-07 20:29:35 +00:00
|
|
|
$config = $this->context->getConfig();
|
2019-04-08 17:08:57 +00:00
|
|
|
$returnToTitle = $title->getPrefixedText();
|
2020-01-07 20:29:35 +00:00
|
|
|
$user = $this->user;
|
|
|
|
$betaEnabled = $config->get( 'MFEnableBeta' );
|
|
|
|
/*
|
|
|
|
* to avoid linking to an empty settings page we make this jsonly when:
|
|
|
|
* - AMC and beta is disabled (if logged in there is nothing to show)
|
|
|
|
* - user is logged out and beta is disabled (beta is the only thing a non-js user can do)
|
|
|
|
* In future we might want to make this a static function on Special:MobileOptions.
|
|
|
|
*/
|
2021-12-17 10:27:35 +00:00
|
|
|
$jsonly = ( !$user->isRegistered() && !$betaEnabled ) ||
|
|
|
|
( $user->isRegistered() && !$config->get( 'MFAdvancedMobileContributions' ) &&
|
2020-01-07 20:29:35 +00:00
|
|
|
!$betaEnabled
|
|
|
|
);
|
2019-05-14 10:40:56 +00:00
|
|
|
|
2022-06-21 16:20:53 +00:00
|
|
|
$entry = $this->buildMenuEntry(
|
2019-05-14 10:40:56 +00:00
|
|
|
'settings',
|
2020-02-03 15:02:47 +00:00
|
|
|
$this->context->msg( 'mobile-frontend-main-menu-settings' )->text(),
|
2019-05-14 10:40:56 +00:00
|
|
|
SpecialPage::getTitleFor( 'MobileOptions' )
|
2022-06-21 16:20:53 +00:00
|
|
|
->getLocalURL( [ 'returnto' => $returnToTitle ] ),
|
|
|
|
'',
|
|
|
|
null,
|
|
|
|
true
|
2020-01-07 20:29:35 +00:00
|
|
|
);
|
|
|
|
if ( $jsonly ) {
|
2022-06-21 16:20:53 +00:00
|
|
|
$entry->setJSOnly();
|
2020-01-07 20:29:35 +00:00
|
|
|
}
|
2022-06-21 16:20:53 +00:00
|
|
|
$group->insertEntry( $entry );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert the Preferences link
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertPreferencesItem( Group $group ) {
|
2022-06-21 16:20:53 +00:00
|
|
|
$entry = $this->buildMenuEntry(
|
2019-05-14 10:40:56 +00:00
|
|
|
'preferences',
|
2020-02-03 15:02:47 +00:00
|
|
|
$this->context->msg( 'preferences' )->text(),
|
2022-06-21 16:20:53 +00:00
|
|
|
SpecialPage::getTitleFor( 'Preferences' )->getLocalURL(),
|
|
|
|
'',
|
|
|
|
'settings',
|
|
|
|
true
|
2019-11-07 18:04:21 +00:00
|
|
|
);
|
|
|
|
$group->insertEntry( $entry );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert About page link
|
|
|
|
* @param Group $group
|
|
|
|
*/
|
|
|
|
public function insertAboutItem( Group $group ) {
|
|
|
|
$msg = $this->context->msg( 'aboutsite' );
|
2022-03-10 22:01:50 +00:00
|
|
|
if ( $msg->isDisabled() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$title = Title::newFromText( $this->context->msg( 'aboutpage' )->inContentLanguage()->text() );
|
|
|
|
if ( !$title ) {
|
|
|
|
return;
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
2022-06-21 16:20:53 +00:00
|
|
|
$entry = $this->buildMenuEntry( 'about', $msg->text(), $title->getLocalURL() );
|
|
|
|
$group->insertEntry( $entry );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert Disclaimers link
|
|
|
|
* @param Group $group
|
|
|
|
*/
|
|
|
|
public function insertDisclaimersItem( Group $group ) {
|
2022-03-10 22:01:50 +00:00
|
|
|
$msg = $this->context->msg( 'disclaimers' );
|
|
|
|
if ( $msg->isDisabled() ) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-08 17:08:57 +00:00
|
|
|
$title = Title::newFromText( $this->context->msg( 'disclaimerpage' )
|
|
|
|
->inContentLanguage()->text() );
|
2022-03-10 22:01:50 +00:00
|
|
|
if ( !$title ) {
|
|
|
|
return;
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
2022-06-21 16:20:53 +00:00
|
|
|
$entry = $this->buildMenuEntry( 'disclaimers', $msg->text(), $title->getLocalURL() );
|
|
|
|
$group->insertEntry( $entry );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 17:27:34 +00:00
|
|
|
/**
|
|
|
|
* Build and insert the RecentChanges link
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertRecentChanges( Group $group ) {
|
2022-06-21 16:20:53 +00:00
|
|
|
$entry = $this->buildMenuEntry(
|
|
|
|
'recentchanges',
|
|
|
|
$this->context->msg( 'recentchanges' )->escaped(),
|
|
|
|
SpecialPage::getTitleFor( 'Recentchanges' )->getLocalURL(),
|
|
|
|
'',
|
|
|
|
'recentChanges',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$group->insertEntry( $entry );
|
2019-07-30 17:27:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:08:57 +00:00
|
|
|
/**
|
|
|
|
* Build and insert the SpecialPages link
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertSpecialPages( Group $group ) {
|
2022-06-21 16:20:53 +00:00
|
|
|
$entry = $this->buildMenuEntry(
|
|
|
|
'specialPages',
|
|
|
|
$this->context->msg( 'specialpages' )->text(),
|
|
|
|
SpecialPage::getTitleFor( 'Specialpages' )->getLocalURL(),
|
|
|
|
'',
|
|
|
|
null,
|
|
|
|
true
|
2019-05-14 10:40:56 +00:00
|
|
|
);
|
2022-06-21 16:20:53 +00:00
|
|
|
$group->insertEntry( $entry );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build and insert the CommunityPortal link
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function insertCommunityPortal( Group $group ) {
|
2022-03-10 22:01:50 +00:00
|
|
|
$msg = $this->context->msg( 'portal' );
|
|
|
|
if ( $msg->isDisabled() ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-08 06:26:16 +00:00
|
|
|
$title = Title::newFromText( $this->context->msg( 'portal-url' )
|
|
|
|
->inContentLanguage()->text() );
|
2022-03-10 22:01:50 +00:00
|
|
|
if ( !$title ) {
|
|
|
|
return;
|
2019-04-09 17:06:40 +00:00
|
|
|
}
|
2022-06-21 16:20:53 +00:00
|
|
|
$entry = $this->buildMenuEntry(
|
2022-03-10 22:01:50 +00:00
|
|
|
'speechBubbles',
|
|
|
|
$msg->text(),
|
2022-06-21 16:20:53 +00:00
|
|
|
$title->getLocalURL(),
|
|
|
|
'',
|
|
|
|
null,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$group->insertEntry( $entry );
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 03:06:13 +00:00
|
|
|
/**
|
|
|
|
* @param array $returnToQuery
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function newLogInOutQuery( array $returnToQuery ): array {
|
|
|
|
$ret = [];
|
|
|
|
$title = $this->context->getTitle();
|
|
|
|
if ( $title && !$title->isSpecial( 'Userlogin' ) ) {
|
|
|
|
$ret[ 'returnto' ] = $title->getPrefixedText();
|
|
|
|
}
|
2021-12-17 10:27:35 +00:00
|
|
|
if ( $this->user && !$this->user->isRegistered() ) {
|
2019-07-12 03:06:13 +00:00
|
|
|
// unset campaign on login link so as not to interfere with A/B tests
|
|
|
|
unset( $returnToQuery['campaign'] );
|
|
|
|
}
|
|
|
|
if ( !empty( $returnToQuery ) ) {
|
|
|
|
$ret['returntoquery'] = wfArrayToCgi( $returnToQuery );
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve current query parameters from Request object so system can pass those
|
|
|
|
* to the Login/logout links
|
|
|
|
* Some parameters are disabled (like title), as the returnto will be replaced with
|
|
|
|
* the current page.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function newReturnToQuery(): array {
|
|
|
|
$returnToQuery = [];
|
2022-03-10 22:01:50 +00:00
|
|
|
$request = $this->context->getRequest();
|
|
|
|
if ( !$request->wasPosted() ) {
|
|
|
|
$returnToQuery = $request->getValues();
|
2019-07-12 03:06:13 +00:00
|
|
|
unset( $returnToQuery['title'] );
|
|
|
|
unset( $returnToQuery['returnto'] );
|
|
|
|
unset( $returnToQuery['returntoquery'] );
|
|
|
|
}
|
|
|
|
return $returnToQuery;
|
|
|
|
}
|
2020-06-17 18:07:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert the Donate Link in the Mobile Menu.
|
|
|
|
*
|
|
|
|
* @param Group $group
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
2022-03-10 22:01:50 +00:00
|
|
|
public function insertDonateItem( Group $group ) {
|
|
|
|
$labelMsg = $this->context->msg( 'sitesupport' );
|
|
|
|
$urlMsg = $this->context->msg( 'sitesupport-url' );
|
|
|
|
if ( !$urlMsg->exists() || $labelMsg->isDisabled() ) {
|
2020-06-17 18:07:02 +00:00
|
|
|
return;
|
2022-03-10 22:01:50 +00:00
|
|
|
}
|
|
|
|
// Add term field to allow distinguishing from other sidebars.
|
|
|
|
// https://www.mediawiki.org/wiki/Wikimedia_Product/Analytics_Infrastructure/Schema_fragments#Campaign_Attribution
|
|
|
|
$url = wfAppendQuery(
|
|
|
|
$urlMsg->text(),
|
2020-06-25 21:06:46 +00:00
|
|
|
[ 'utm_key' => 'minerva' ]
|
|
|
|
);
|
2022-06-21 16:20:53 +00:00
|
|
|
$entry = $this->buildMenuEntry(
|
|
|
|
'donate',
|
2022-03-10 22:01:50 +00:00
|
|
|
$labelMsg->text(),
|
2020-06-17 18:07:02 +00:00
|
|
|
$url,
|
2021-09-09 15:56:42 +00:00
|
|
|
'',
|
2022-06-21 16:20:53 +00:00
|
|
|
'heart',
|
|
|
|
true
|
2022-03-10 22:01:50 +00:00
|
|
|
);
|
2022-06-21 16:20:53 +00:00
|
|
|
$group->insertEntry( $entry );
|
2022-03-10 22:01:50 +00:00
|
|
|
}
|
2019-04-08 17:08:57 +00:00
|
|
|
}
|