| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Minerva\Menu\PageActions; |
| 22 |
|
| 23 |
use MediaWiki\Config\ServiceOptions; |
| 24 |
use MediaWiki\Context\IContextSource; |
| 25 |
use MediaWiki\Minerva\LanguagesHelper; |
| 26 |
use MediaWiki\Minerva\Menu\Entries\IMenuEntry; |
| 27 |
use MediaWiki\Minerva\Menu\Entries\LanguageSelectorEntry; |
| 28 |
use MediaWiki\Minerva\Menu\Entries\SingleMenuEntry; |
| 29 |
use MediaWiki\Minerva\Menu\Group; |
| 30 |
use MediaWiki\Minerva\Permissions\IMinervaPagePermissions; |
| 31 |
use MediaWiki\Minerva\SkinOptions; |
| 32 |
use MediaWiki\Minerva\Skins\SkinUserPageHelper; |
| 33 |
use MediaWiki\Registration\ExtensionRegistry; |
| 34 |
use MediaWiki\SpecialPage\SpecialPage; |
| 35 |
use MediaWiki\Title\Title; |
| 36 |
use MediaWiki\User\User; |
| 37 |
use MediaWiki\User\UserIdentity; |
| 38 |
use MediaWiki\Watchlist\WatchlistManager; |
| 39 |
use SpecialMobileHistory; |
| 40 |
|
| 41 |
class ToolbarBuilder { |
| 42 |
|
| 43 |
|
| 44 |
private Title $title; |
| 45 |
|
| 46 |
private User $user; |
| 47 |
private IContextSource $context; |
| 48 |
private IMinervaPagePermissions $permissions; |
| 49 |
private SkinOptions $skinOptions; |
| 50 |
private SkinUserPageHelper $relevantUserPageHelper; |
| 51 |
private LanguagesHelper $languagesHelper; |
| 52 |
|
| 53 |
private bool $watchlistExpiryEnabled; |
| 54 |
private WatchlistManager $watchlistManager; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public const CONSTRUCTOR_OPTIONS = [ |
| 60 |
'WatchlistExpiry', |
| 61 |
]; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function __construct( |
| 79 |
Title $title, |
| 80 |
User $user, |
| 81 |
IContextSource $context, |
| 82 |
IMinervaPagePermissions $permissions, |
| 83 |
SkinOptions $skinOptions, |
| 84 |
SkinUserPageHelper $relevantUserPageHelper, |
| 85 |
LanguagesHelper $languagesHelper, |
| 86 |
ServiceOptions $options, |
| 87 |
WatchlistManager $watchlistManager |
| 88 |
) { |
| 89 |
$this->title = $title; |
| 90 |
$this->user = $user; |
| 91 |
$this->context = $context; |
| 92 |
$this->permissions = $permissions; |
| 93 |
$this->skinOptions = $skinOptions; |
| 94 |
$this->relevantUserPageHelper = $relevantUserPageHelper; |
| 95 |
$this->languagesHelper = $languagesHelper; |
| 96 |
$this->watchlistExpiryEnabled = $options->get( 'WatchlistExpiry' ); |
| 97 |
$this->watchlistManager = $watchlistManager; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function getGroup( array $actions, array $views ): Group { |
| 106 |
$group = new Group( 'p-views' ); |
| 107 |
$permissions = $this->permissions; |
| 108 |
$userPageOrUserTalkPageWithOverflowMode = $this->skinOptions->get( SkinOptions::TOOLBAR_SUBMENU ) |
| 109 |
&& $this->relevantUserPageHelper->isUserPage(); |
| 110 |
|
| 111 |
if ( !$userPageOrUserTalkPageWithOverflowMode && $permissions->isAllowed( |
| 112 |
IMinervaPagePermissions::SWITCH_LANGUAGE ) ) { |
| 113 |
$group->insertEntry( new LanguageSelectorEntry( |
| 114 |
$this->title, |
| 115 |
$this->languagesHelper->doesTitleHasLanguagesOrVariants( |
| 116 |
$this->context->getOutput(), |
| 117 |
$this->title |
| 118 |
), |
| 119 |
$this->context, |
| 120 |
true |
| 121 |
) ); |
| 122 |
} |
| 123 |
|
| 124 |
$watchKey = $key = isset( $actions['unwatch'] ) ? 'unwatch' : 'watch'; |
| 125 |
|
| 126 |
$watchData = $actions[ $watchKey ] ?? [ |
| 127 |
'icon' => 'star', |
| 128 |
'class' => '', |
| 129 |
'href' => $this->getLoginUrl( [ 'returnto' => $this->title ] ), |
| 130 |
'text' => $this->context->msg( 'watch' ), |
| 131 |
]; |
| 132 |
if ( $permissions->isAllowed( IMinervaPagePermissions::WATCHABLE ) && $watchData ) { |
| 133 |
$group->insertEntry( $this->createWatchPageAction( $watchKey, $watchData ) ); |
| 134 |
} |
| 135 |
|
| 136 |
$historyView = $views[ 'history'] ?? []; |
| 137 |
if ( $historyView && $permissions->isAllowed( IMinervaPagePermissions::HISTORY ) ) { |
| 138 |
$group->insertEntry( $this->getHistoryPageAction( $historyView ) ); |
| 139 |
} |
| 140 |
|
| 141 |
$user = $this->relevantUserPageHelper->getPageUser(); |
| 142 |
$isUserPageAccessible = $this->relevantUserPageHelper->isUserPageAccessibleToCurrentUser(); |
| 143 |
if ( $user && $isUserPageAccessible ) { |
| 144 |
|
| 145 |
|
| 146 |
$group->insertEntry( $this->createContributionsPageAction( $user ) ); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
if ( $permissions->isAllowed( IMinervaPagePermissions::CONTENT_EDIT ) ) { |
| 151 |
foreach ( $views as $key => $viewData ) { |
| 152 |
if ( in_array( $key, [ 've-edit', 'viewsource', 'edit' ] ) ) { |
| 153 |
$group->insertEntry( $this->createEditPageAction( $key, $viewData ) ); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
return $group; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
protected function createContributionsPageAction( UserIdentity $user ): IMenuEntry { |
| 168 |
$label = $this->context->msg( 'mobile-frontend-user-page-contributions' ); |
| 169 |
|
| 170 |
$entry = new SingleMenuEntry( |
| 171 |
'page-actions-contributions', |
| 172 |
$label->escaped(), |
| 173 |
SpecialPage::getTitleFor( 'Contributions', $user->getName() )->getLocalURL() ); |
| 174 |
$entry->setTitle( $label ) |
| 175 |
->trackClicks( 'contributions' ) |
| 176 |
->setIcon( 'userContributions' ); |
| 177 |
|
| 178 |
return $entry; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
protected function createEditPageAction( string $key, array $editAction ): IMenuEntry { |
| 190 |
$title = $this->title; |
| 191 |
|
| 192 |
$id = $editAction['single-id'] ?? 'ca-edit'; |
| 193 |
$entry = new SingleMenuEntry( |
| 194 |
'page-actions-' . $key, |
| 195 |
$editAction['text'], |
| 196 |
$editAction['href'], |
| 197 |
'edit-page' |
| 198 |
); |
| 199 |
$iconFallback = $key === 'viewsource' ? 'editLock' : 'edit'; |
| 200 |
$icon = $editAction['icon'] ?? $iconFallback; |
| 201 |
$entry->setIcon( $icon ) |
| 202 |
->trackClicks( $key ) |
| 203 |
->setTitle( $this->context->msg( 'tooltip-' . $id ) ) |
| 204 |
->setNodeID( $id ); |
| 205 |
return $entry; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
protected function createWatchPageAction( string $watchKey, array $watchData ): IMenuEntry { |
| 218 |
$entry = new SingleMenuEntry( |
| 219 |
'page-actions-watch', |
| 220 |
$watchData['text'], |
| 221 |
$watchData['href'], |
| 222 |
$watchData[ 'class' ], |
| 223 |
$this->permissions->isAllowed( IMinervaPagePermissions::WATCH ) |
| 224 |
); |
| 225 |
$icon = $watchData['icon'] ?? ''; |
| 226 |
return $entry->trackClicks( $watchKey ) |
| 227 |
->setIcon( $icon ) |
| 228 |
->setTitle( $this->context->msg( $watchKey ) ) |
| 229 |
->setNodeID( 'ca-watch' ); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
protected function getHistoryPageAction( array $historyAction ): IMenuEntry { |
| 240 |
$entry = new SingleMenuEntry( |
| 241 |
'page-actions-history', |
| 242 |
$historyAction['text'], |
| 243 |
$historyAction['href'], |
| 244 |
); |
| 245 |
$icon = $historyAction['icon'] ?? 'history'; |
| 246 |
$entry->setIcon( $icon ) |
| 247 |
->trackClicks( 'history' ); |
| 248 |
return $entry; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
protected function getHistoryUrl( Title $title ): string { |
| 259 |
return ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) && |
| 260 |
SpecialMobileHistory::shouldUseSpecialHistory( $title, $this->user ) ? |
| 261 |
SpecialPage::getTitleFor( 'History', $title )->getLocalURL() : |
| 262 |
$title->getLocalURL( [ 'action' => 'history' ] ); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
private function getLoginUrl( $query ): string { |
| 271 |
return SpecialPage::getTitleFor( 'Userlogin' )->getLocalURL( $query ); |
| 272 |
} |
| 273 |
} |