2019-05-23 22:34:10 +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\PageActions;
|
|
|
|
|
|
|
|
use ExtensionRegistry;
|
2020-08-24 21:49:04 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2024-03-23 21:43:33 +00:00
|
|
|
use MediaWiki\Context\IContextSource;
|
2019-07-09 16:10:31 +00:00
|
|
|
use MediaWiki\Minerva\LanguagesHelper;
|
2019-07-01 18:06:01 +00:00
|
|
|
use MediaWiki\Minerva\Menu\Entries\IMenuEntry;
|
|
|
|
use MediaWiki\Minerva\Menu\Entries\LanguageSelectorEntry;
|
2019-11-07 21:46:55 +00:00
|
|
|
use MediaWiki\Minerva\Menu\Entries\SingleMenuEntry;
|
2019-05-23 22:34:10 +00:00
|
|
|
use MediaWiki\Minerva\Menu\Group;
|
2019-05-24 00:07:27 +00:00
|
|
|
use MediaWiki\Minerva\Permissions\IMinervaPagePermissions;
|
2019-06-24 16:15:06 +00:00
|
|
|
use MediaWiki\Minerva\SkinOptions;
|
2020-05-01 01:16:53 +00:00
|
|
|
use MediaWiki\Minerva\Skins\SkinUserPageHelper;
|
2024-01-06 15:31:38 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPage;
|
2023-08-19 04:23:00 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2024-01-06 15:31:38 +00:00
|
|
|
use MediaWiki\User\User;
|
2019-12-12 22:49:48 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2021-04-27 20:39:57 +00:00
|
|
|
use MediaWiki\Watchlist\WatchlistManager;
|
2019-05-23 22:34:10 +00:00
|
|
|
use SpecialMobileHistory;
|
|
|
|
|
|
|
|
class ToolbarBuilder {
|
|
|
|
|
2024-03-28 19:15:41 +00:00
|
|
|
/** @var Title Article title user is currently browsing */
|
|
|
|
private Title $title;
|
|
|
|
/** @var User Currently logged in user */
|
|
|
|
private User $user;
|
2024-03-23 21:43:33 +00:00
|
|
|
private IContextSource $context;
|
2024-03-28 19:15:41 +00:00
|
|
|
private IMinervaPagePermissions $permissions;
|
|
|
|
private SkinOptions $skinOptions;
|
|
|
|
private SkinUserPageHelper $relevantUserPageHelper;
|
|
|
|
private LanguagesHelper $languagesHelper;
|
|
|
|
/** @var bool Correlates to $wgWatchlistExpiry feature flag. */
|
|
|
|
private bool $watchlistExpiryEnabled;
|
|
|
|
private WatchlistManager $watchlistManager;
|
2021-04-27 20:39:57 +00:00
|
|
|
|
2020-08-24 21:49:04 +00:00
|
|
|
/**
|
|
|
|
* ServiceOptions needed.
|
|
|
|
*/
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
'WatchlistExpiry',
|
|
|
|
];
|
|
|
|
|
2019-05-23 22:34:10 +00:00
|
|
|
/**
|
|
|
|
* Build Group containing icons for toolbar
|
|
|
|
* @param Title $title Article title user is currently browsing
|
|
|
|
* @param User $user Currently logged in user
|
2024-03-23 21:43:33 +00:00
|
|
|
* @param IContextSource $context
|
2019-05-24 00:07:27 +00:00
|
|
|
* @param IMinervaPagePermissions $permissions Minerva permissions system
|
2021-12-17 10:29:11 +00:00
|
|
|
* @param SkinOptions $skinOptions
|
2019-12-12 22:49:48 +00:00
|
|
|
* @param SkinUserPageHelper $relevantUserPageHelper User Page helper. The
|
|
|
|
* UserPageHelper passed should always be specific to the user page Title. If on a
|
|
|
|
* user talk page, UserPageHelper should be instantiated with the user page
|
|
|
|
* Title and NOT with the user talk page Title.
|
2019-07-09 16:10:31 +00:00
|
|
|
* @param LanguagesHelper $languagesHelper Helper to check title languages/variants
|
2020-08-24 21:49:04 +00:00
|
|
|
* @param ServiceOptions $options
|
2021-04-27 20:39:57 +00:00
|
|
|
* @param WatchlistManager $watchlistManager
|
2019-05-23 22:34:10 +00:00
|
|
|
*/
|
2019-05-24 00:07:27 +00:00
|
|
|
public function __construct(
|
|
|
|
Title $title,
|
|
|
|
User $user,
|
2024-03-23 21:43:33 +00:00
|
|
|
IContextSource $context,
|
2019-06-24 16:15:06 +00:00
|
|
|
IMinervaPagePermissions $permissions,
|
|
|
|
SkinOptions $skinOptions,
|
2019-12-12 22:49:48 +00:00
|
|
|
SkinUserPageHelper $relevantUserPageHelper,
|
2020-08-24 21:49:04 +00:00
|
|
|
LanguagesHelper $languagesHelper,
|
2021-04-27 20:39:57 +00:00
|
|
|
ServiceOptions $options,
|
|
|
|
WatchlistManager $watchlistManager
|
2019-06-24 16:15:06 +00:00
|
|
|
) {
|
2019-05-23 22:34:10 +00:00
|
|
|
$this->title = $title;
|
|
|
|
$this->user = $user;
|
2024-03-23 21:43:33 +00:00
|
|
|
$this->context = $context;
|
2019-05-24 00:07:27 +00:00
|
|
|
$this->permissions = $permissions;
|
2019-06-24 16:15:06 +00:00
|
|
|
$this->skinOptions = $skinOptions;
|
2019-12-12 22:49:48 +00:00
|
|
|
$this->relevantUserPageHelper = $relevantUserPageHelper;
|
2019-07-09 16:10:31 +00:00
|
|
|
$this->languagesHelper = $languagesHelper;
|
2020-08-24 21:49:04 +00:00
|
|
|
$this->watchlistExpiryEnabled = $options->get( 'WatchlistExpiry' );
|
2021-04-27 20:39:57 +00:00
|
|
|
$this->watchlistManager = $watchlistManager;
|
2019-05-23 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-27 00:17:21 +00:00
|
|
|
* @param array $actions
|
|
|
|
* @param array $views
|
2019-05-23 22:34:10 +00:00
|
|
|
* @return Group
|
|
|
|
*/
|
2023-09-27 00:17:21 +00:00
|
|
|
public function getGroup( array $actions, array $views ): Group {
|
2019-10-23 18:30:55 +00:00
|
|
|
$group = new Group( 'p-views' );
|
2019-05-24 00:07:27 +00:00
|
|
|
$permissions = $this->permissions;
|
2021-04-29 20:24:13 +00:00
|
|
|
$userPageOrUserTalkPageWithOverflowMode = $this->skinOptions->get( SkinOptions::TOOLBAR_SUBMENU )
|
2019-12-12 22:49:48 +00:00
|
|
|
&& $this->relevantUserPageHelper->isUserPage();
|
2019-05-23 22:34:10 +00:00
|
|
|
|
2021-04-29 20:24:13 +00:00
|
|
|
if ( !$userPageOrUserTalkPageWithOverflowMode && $permissions->isAllowed(
|
2019-06-24 17:59:11 +00:00
|
|
|
IMinervaPagePermissions::SWITCH_LANGUAGE ) ) {
|
|
|
|
$group->insertEntry( new LanguageSelectorEntry(
|
|
|
|
$this->title,
|
2024-03-23 21:43:33 +00:00
|
|
|
$this->languagesHelper->doesTitleHasLanguagesOrVariants(
|
|
|
|
$this->context->getOutput(),
|
|
|
|
$this->title
|
|
|
|
),
|
|
|
|
$this->context,
|
2021-09-09 15:56:42 +00:00
|
|
|
true
|
2020-03-25 02:02:02 +00:00
|
|
|
) );
|
2019-05-23 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 15:57:37 +00:00
|
|
|
$watchKey = $key = isset( $actions['unwatch'] ) ? 'unwatch' : 'watch';
|
|
|
|
// The watchstar is typically not shown to anonymous users but it is in Minerva.
|
|
|
|
$watchData = $actions[ $watchKey ] ?? [
|
|
|
|
'icon' => 'star',
|
|
|
|
'class' => '',
|
|
|
|
'href' => $this->getLoginUrl( [ 'returnto' => $this->title ] ),
|
2024-03-23 21:43:33 +00:00
|
|
|
'text' => $this->context->msg( 'watch' ),
|
2023-10-06 15:57:37 +00:00
|
|
|
];
|
2023-09-27 00:17:21 +00:00
|
|
|
if ( $permissions->isAllowed( IMinervaPagePermissions::WATCHABLE ) && $watchData ) {
|
|
|
|
$group->insertEntry( $this->createWatchPageAction( $watchKey, $watchData ) );
|
2019-05-23 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
2023-09-27 00:17:21 +00:00
|
|
|
$historyView = $views[ 'history'] ?? [];
|
|
|
|
if ( $historyView && $permissions->isAllowed( IMinervaPagePermissions::HISTORY ) ) {
|
|
|
|
$group->insertEntry( $this->getHistoryPageAction( $historyView ) );
|
2019-05-23 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 19:15:41 +00:00
|
|
|
$user = $this->relevantUserPageHelper->getPageUser();
|
2021-04-29 20:24:13 +00:00
|
|
|
$isUserPageAccessible = $this->relevantUserPageHelper->isUserPageAccessibleToCurrentUser();
|
2024-03-28 19:15:41 +00:00
|
|
|
if ( $user && $isUserPageAccessible ) {
|
2019-12-12 22:49:48 +00:00
|
|
|
// T235681: Contributions icon should be added to toolbar on user pages
|
|
|
|
// and user talk pages for all users
|
|
|
|
$group->insertEntry( $this->createContributionsPageAction( $user ) );
|
2019-06-24 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2023-09-27 00:17:21 +00:00
|
|
|
// We want the edit icon/action(s) always to be the last element on the toolbar list
|
2019-08-29 14:16:10 +00:00
|
|
|
if ( $permissions->isAllowed( IMinervaPagePermissions::CONTENT_EDIT ) ) {
|
2023-09-27 00:17:21 +00:00
|
|
|
foreach ( $views as $key => $viewData ) {
|
|
|
|
if ( in_array( $key, [ 've-edit', 'viewsource', 'edit' ] ) ) {
|
|
|
|
$group->insertEntry( $this->createEditPageAction( $key, $viewData ) );
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 22:34:10 +00:00
|
|
|
}
|
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-12 22:49:48 +00:00
|
|
|
* Create Contributions page action visible on user pages or user talk pages
|
|
|
|
* for given $user
|
2019-06-24 16:15:06 +00:00
|
|
|
*
|
2021-12-17 17:16:49 +00:00
|
|
|
* @param UserIdentity $user Determines what the contribution page action will link to
|
2019-06-24 16:15:06 +00:00
|
|
|
* @return IMenuEntry
|
|
|
|
*/
|
2019-12-12 22:49:48 +00:00
|
|
|
protected function createContributionsPageAction( UserIdentity $user ): IMenuEntry {
|
2024-03-23 21:43:33 +00:00
|
|
|
$label = $this->context->msg( 'mobile-frontend-user-page-contributions' );
|
2019-06-24 16:15:06 +00:00
|
|
|
|
2019-12-09 19:46:20 +00:00
|
|
|
$entry = new SingleMenuEntry(
|
2019-06-24 16:15:06 +00:00
|
|
|
'page-actions-contributions',
|
2020-05-23 21:49:32 +00:00
|
|
|
$label->escaped(),
|
2019-12-12 22:49:48 +00:00
|
|
|
SpecialPage::getTitleFor( 'Contributions', $user->getName() )->getLocalURL() );
|
2019-11-07 21:46:55 +00:00
|
|
|
$entry->setTitle( $label )
|
|
|
|
->trackClicks( 'contributions' )
|
2023-08-17 21:44:34 +00:00
|
|
|
->setIcon( 'userContributions' );
|
2020-02-07 02:16:03 +00:00
|
|
|
|
2019-11-07 21:46:55 +00:00
|
|
|
return $entry;
|
2019-06-24 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-23 22:34:10 +00:00
|
|
|
* Creates the "edit" page action: the well-known pencil icon that, when tapped, will open an
|
|
|
|
* editor with the lead section loaded.
|
|
|
|
*
|
2023-09-27 00:17:21 +00:00
|
|
|
* @param string $key
|
|
|
|
* @param array $editAction
|
2019-11-07 21:46:55 +00:00
|
|
|
* @return IMenuEntry An edit page actions menu entry
|
2019-05-23 22:34:10 +00:00
|
|
|
*/
|
2023-09-27 00:17:21 +00:00
|
|
|
protected function createEditPageAction( string $key, array $editAction ): IMenuEntry {
|
2019-05-23 22:34:10 +00:00
|
|
|
$title = $this->title;
|
|
|
|
|
2023-09-27 00:17:21 +00:00
|
|
|
$id = $editAction['single-id'] ?? 'ca-edit';
|
2019-11-07 21:46:55 +00:00
|
|
|
$entry = new SingleMenuEntry(
|
2023-09-27 00:17:21 +00:00
|
|
|
'page-actions-' . $key,
|
|
|
|
$editAction['text'],
|
|
|
|
$editAction['href'],
|
2019-11-07 21:46:55 +00:00
|
|
|
'edit-page'
|
2019-05-23 22:34:10 +00:00
|
|
|
);
|
2023-09-27 00:17:21 +00:00
|
|
|
$iconFallback = $key === 'viewsource' ? 'editLock' : 'edit';
|
|
|
|
$icon = $editAction['icon'] ?? $iconFallback;
|
2024-10-07 22:35:31 +00:00
|
|
|
$entry->setIcon( $icon )
|
2023-09-27 00:17:21 +00:00
|
|
|
->trackClicks( $key )
|
2024-03-23 21:43:33 +00:00
|
|
|
->setTitle( $this->context->msg( 'tooltip-' . $id ) )
|
2023-09-27 00:17:21 +00:00
|
|
|
->setNodeID( $id );
|
2019-11-07 21:46:55 +00:00
|
|
|
return $entry;
|
2019-05-23 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the "watch" or "unwatch" action: the well-known star icon that, when tapped, will
|
|
|
|
* add the page to or remove the page from the user's watchlist; or, if the user is logged out,
|
|
|
|
* will direct the user's UA to Special:Login.
|
|
|
|
*
|
2023-09-27 00:17:21 +00:00
|
|
|
* @param string $watchKey either watch or unwatch
|
|
|
|
* @param array $watchData
|
2019-11-07 21:46:55 +00:00
|
|
|
* @return IMenuEntry An watch/unwatch page actions menu entry
|
2019-05-23 22:34:10 +00:00
|
|
|
*/
|
2023-09-27 00:17:21 +00:00
|
|
|
protected function createWatchPageAction( string $watchKey, array $watchData ): IMenuEntry {
|
2019-11-07 21:46:55 +00:00
|
|
|
$entry = new SingleMenuEntry(
|
2019-05-23 22:34:10 +00:00
|
|
|
'page-actions-watch',
|
2023-09-27 00:17:21 +00:00
|
|
|
$watchData['text'],
|
|
|
|
$watchData['href'],
|
2023-10-21 15:34:52 +00:00
|
|
|
$watchData[ 'class' ],
|
|
|
|
$this->permissions->isAllowed( IMinervaPagePermissions::WATCH )
|
2019-05-23 22:34:10 +00:00
|
|
|
);
|
2023-09-27 00:17:21 +00:00
|
|
|
$icon = $watchData['icon'] ?? '';
|
|
|
|
return $entry->trackClicks( $watchKey )
|
2023-09-05 17:07:13 +00:00
|
|
|
->setIcon( $icon )
|
2024-03-23 21:43:33 +00:00
|
|
|
->setTitle( $this->context->msg( $watchKey ) )
|
2019-05-23 22:34:10 +00:00
|
|
|
->setNodeID( 'ca-watch' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a history action: An icon that links to the mobile history page.
|
|
|
|
*
|
2023-09-27 00:17:21 +00:00
|
|
|
* @param array $historyAction
|
2019-11-07 21:46:55 +00:00
|
|
|
* @return IMenuEntry A menu entry object that represents a map of HTML attributes
|
2019-05-23 22:34:10 +00:00
|
|
|
* and a 'text' property to be used with the pageActionMenu.mustache template.
|
|
|
|
*/
|
2023-09-27 00:17:21 +00:00
|
|
|
protected function getHistoryPageAction( array $historyAction ): IMenuEntry {
|
2019-12-10 01:45:11 +00:00
|
|
|
$entry = new SingleMenuEntry(
|
2019-05-23 22:34:10 +00:00
|
|
|
'page-actions-history',
|
2023-09-27 00:17:21 +00:00
|
|
|
$historyAction['text'],
|
|
|
|
$historyAction['href'],
|
2019-12-10 01:45:11 +00:00
|
|
|
);
|
2023-09-27 00:17:21 +00:00
|
|
|
$icon = $historyAction['icon'] ?? 'history';
|
2024-10-07 22:35:31 +00:00
|
|
|
$entry->setIcon( $icon )
|
2019-11-07 21:46:55 +00:00
|
|
|
->trackClicks( 'history' );
|
2019-12-10 01:45:11 +00:00
|
|
|
return $entry;
|
2019-05-23 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL for the history page for the given title using Special:History
|
|
|
|
* when available.
|
|
|
|
* FIXME: temporary duplicated code, same as SkinMinerva::getHistoryUrl()
|
|
|
|
* @param Title $title The Title object of the page being viewed
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
protected function getHistoryUrl( Title $title ): string {
|
2019-05-23 22:34:10 +00:00
|
|
|
return ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) &&
|
|
|
|
SpecialMobileHistory::shouldUseSpecialHistory( $title, $this->user ) ?
|
|
|
|
SpecialPage::getTitleFor( 'History', $title )->getLocalURL() :
|
|
|
|
$title->getLocalURL( [ 'action' => 'history' ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares a url to the Special:UserLogin with query parameters
|
|
|
|
* @param array $query
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
private function getLoginUrl( $query ): string {
|
2019-05-23 22:34:10 +00:00
|
|
|
return SpecialPage::getTitleFor( 'Userlogin' )->getLocalURL( $query );
|
|
|
|
}
|
|
|
|
}
|