2019-05-24 00:07:27 +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\Permissions;
|
|
|
|
|
2024-01-06 15:31:38 +00:00
|
|
|
use MediaWiki\Config\Config;
|
|
|
|
use MediaWiki\Config\ConfigException;
|
2024-10-20 07:28:03 +00:00
|
|
|
use MediaWiki\Content\ContentHandler;
|
2021-05-28 20:15:31 +00:00
|
|
|
use MediaWiki\Content\IContentHandlerFactory;
|
2024-05-19 12:45:56 +00:00
|
|
|
use MediaWiki\Context\IContextSource;
|
2024-02-25 19:26:11 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2019-07-09 16:10:31 +00:00
|
|
|
use MediaWiki\Minerva\LanguagesHelper;
|
2019-05-24 00:07:27 +00:00
|
|
|
use MediaWiki\Minerva\SkinOptions;
|
2024-03-23 21:43:33 +00:00
|
|
|
use MediaWiki\Output\OutputPage;
|
2023-07-17 17:31:07 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
2020-01-26 19:26:31 +00:00
|
|
|
use MediaWiki\Permissions\PermissionManager;
|
2023-08-19 04:23:00 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-07-17 17:31:07 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2024-02-26 07:35:55 +00:00
|
|
|
use MediaWiki\Watchlist\WatchlistManager;
|
2019-05-24 00:07:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A wrapper for all available Minerva permissions.
|
|
|
|
*/
|
|
|
|
final class MinervaPagePermissions implements IMinervaPagePermissions {
|
2024-03-28 19:15:41 +00:00
|
|
|
/** @var Title Current page title */
|
|
|
|
private ?Title $title;
|
|
|
|
/** @var Config Extension config */
|
|
|
|
private Config $config;
|
|
|
|
private Authority $performer;
|
2024-03-23 21:43:33 +00:00
|
|
|
private OutputPage $out;
|
2024-03-28 19:15:41 +00:00
|
|
|
private ContentHandler $contentHandler;
|
|
|
|
private SkinOptions $skinOptions;
|
|
|
|
private LanguagesHelper $languagesHelper;
|
|
|
|
private PermissionManager $permissionManager;
|
|
|
|
private IContentHandlerFactory $contentHandlerFactory;
|
2023-07-17 17:31:07 +00:00
|
|
|
private UserFactory $userFactory;
|
2024-02-26 07:35:55 +00:00
|
|
|
private WatchlistManager $watchlistManager;
|
|
|
|
|
2019-05-24 00:07:27 +00:00
|
|
|
/**
|
|
|
|
* Initialize internal Minerva Permissions system
|
2021-12-17 10:29:11 +00:00
|
|
|
* @param SkinOptions $skinOptions
|
2019-07-09 16:10:31 +00:00
|
|
|
* @param LanguagesHelper $languagesHelper
|
2019-08-27 20:27:57 +00:00
|
|
|
* @param PermissionManager $permissionManager
|
2021-05-28 20:15:31 +00:00
|
|
|
* @param IContentHandlerFactory $contentHandlerFactory
|
2023-07-17 17:31:07 +00:00
|
|
|
* @param UserFactory $userFactory
|
2024-02-26 07:35:55 +00:00
|
|
|
* @param WatchlistManager $watchlistManager
|
2019-05-24 00:07:27 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
SkinOptions $skinOptions,
|
2019-08-27 20:27:57 +00:00
|
|
|
LanguagesHelper $languagesHelper,
|
2021-05-28 20:15:31 +00:00
|
|
|
PermissionManager $permissionManager,
|
2023-07-17 17:31:07 +00:00
|
|
|
IContentHandlerFactory $contentHandlerFactory,
|
2024-02-26 07:35:55 +00:00
|
|
|
UserFactory $userFactory,
|
|
|
|
WatchlistManager $watchlistManager
|
2019-05-24 00:07:27 +00:00
|
|
|
) {
|
|
|
|
$this->skinOptions = $skinOptions;
|
2019-07-09 16:10:31 +00:00
|
|
|
$this->languagesHelper = $languagesHelper;
|
2019-08-27 20:27:57 +00:00
|
|
|
$this->permissionManager = $permissionManager;
|
2021-05-28 20:15:31 +00:00
|
|
|
$this->contentHandlerFactory = $contentHandlerFactory;
|
2023-07-17 17:31:07 +00:00
|
|
|
$this->userFactory = $userFactory;
|
2024-02-26 07:35:55 +00:00
|
|
|
$this->watchlistManager = $watchlistManager;
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 17:22:31 +00:00
|
|
|
/**
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @return $this
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function setContext( IContextSource $context ): self {
|
2019-10-08 17:22:31 +00:00
|
|
|
$this->title = $context->getTitle();
|
|
|
|
$this->config = $context->getConfig();
|
2023-07-17 17:31:07 +00:00
|
|
|
$this->performer = $context->getAuthority();
|
2024-03-23 21:43:33 +00:00
|
|
|
$this->out = $context->getOutput();
|
2019-10-08 17:22:31 +00:00
|
|
|
// Title may be undefined in certain contexts (T179833)
|
|
|
|
// TODO: Check if this is still true if we always pass a context instead of using global one
|
|
|
|
if ( $this->title ) {
|
2021-05-28 20:15:31 +00:00
|
|
|
$this->contentHandler = $this->contentHandlerFactory->getContentHandler(
|
|
|
|
$this->title->getContentModel()
|
|
|
|
);
|
2019-10-08 17:22:31 +00:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-05-24 00:07:27 +00:00
|
|
|
/**
|
|
|
|
* Gets whether or not the action is allowed.
|
|
|
|
*
|
|
|
|
* Actions isn't allowed when:
|
|
|
|
* <ul>
|
|
|
|
* <li>the user is on the main page</li>
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* The "edit" action is not allowed if editing is not possible on the page
|
2019-07-14 14:44:29 +00:00
|
|
|
* @see method isCurrentPageContentModelEditable
|
2019-05-24 00:07:27 +00:00
|
|
|
*
|
|
|
|
* The "switch-language" is allowed if there are interlanguage links on the page,
|
|
|
|
* or <code>$wgMinervaAlwaysShowLanguageButton</code> is truthy.
|
|
|
|
*
|
|
|
|
* @inheritDoc
|
|
|
|
* @throws ConfigException
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function isAllowed( $action ): bool {
|
2019-10-08 17:22:31 +00:00
|
|
|
if ( !$this->title ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-24 00:07:27 +00:00
|
|
|
// T206406: Enable "Talk" or "Discussion" button on Main page, also, not forgetting
|
|
|
|
// the "switch-language" button. But disable "edit" and "watch" actions.
|
|
|
|
if ( $this->title->isMainPage() ) {
|
2019-06-21 15:46:44 +00:00
|
|
|
if ( $action === self::SWITCH_LANGUAGE ) {
|
2024-02-25 19:26:11 +00:00
|
|
|
return !$this->config->get( MainConfigNames::HideInterlanguageLinks );
|
2019-06-21 15:46:44 +00:00
|
|
|
}
|
2021-10-18 21:04:03 +00:00
|
|
|
// Only the talk page is allowed on the main page provided user is registered.
|
|
|
|
// talk page permission is disabled on mobile for anons
|
|
|
|
// https://phabricator.wikimedia.org/T54165
|
2023-07-17 17:31:07 +00:00
|
|
|
return $action === self::TALK && $this->performer->isRegistered();
|
2021-10-18 21:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $action === self::TALK ) {
|
|
|
|
return (
|
|
|
|
$this->title->isTalkPage() ||
|
|
|
|
$this->title->canHaveTalkPage()
|
|
|
|
);
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $action === self::HISTORY && $this->title->exists() ) {
|
2019-08-01 16:18:18 +00:00
|
|
|
return $this->skinOptions->get( SkinOptions::HISTORY_IN_PAGE_ACTIONS );
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 16:18:18 +00:00
|
|
|
if ( $action === SkinOptions::TOOLBAR_SUBMENU ) {
|
|
|
|
return $this->skinOptions->get( SkinOptions::TOOLBAR_SUBMENU );
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 20:27:57 +00:00
|
|
|
if ( $action === self::EDIT_OR_CREATE ) {
|
|
|
|
return $this->canEditOrCreate();
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:16:10 +00:00
|
|
|
if ( $action === self::CONTENT_EDIT ) {
|
2019-05-24 00:07:27 +00:00
|
|
|
return $this->isCurrentPageContentModelEditable();
|
|
|
|
}
|
|
|
|
|
2023-08-24 08:39:33 +00:00
|
|
|
if ( $action === self::WATCHABLE || $action === self::WATCH ) {
|
2024-02-26 07:35:55 +00:00
|
|
|
$isWatchable = $this->watchlistManager->isWatchable( $this->title );
|
2023-08-24 08:39:33 +00:00
|
|
|
|
|
|
|
if ( $action === self::WATCHABLE ) {
|
|
|
|
return $isWatchable;
|
|
|
|
}
|
|
|
|
if ( $action === self::WATCH ) {
|
|
|
|
return $isWatchable &&
|
|
|
|
$this->performer->isAllowedAll( 'viewmywatchlist', 'editmywatchlist' );
|
|
|
|
}
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $action === self::SWITCH_LANGUAGE ) {
|
2024-02-25 19:26:11 +00:00
|
|
|
if ( $this->config->get( MainConfigNames::HideInterlanguageLinks ) ) {
|
2019-06-21 15:46:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-03-23 21:43:33 +00:00
|
|
|
return $this->languagesHelper->doesTitleHasLanguagesOrVariants( $this->out, $this->title ) ||
|
2019-07-09 16:10:31 +00:00
|
|
|
$this->config->get( 'MinervaAlwaysShowLanguageButton' );
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
2021-03-09 19:43:14 +00:00
|
|
|
|
|
|
|
if ( $action === self::MOVE ) {
|
|
|
|
return $this->canMove();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $action === self::DELETE ) {
|
|
|
|
return $this->canDelete();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $action === self::PROTECT ) {
|
|
|
|
return $this->canProtect();
|
|
|
|
}
|
|
|
|
|
2021-10-18 21:04:03 +00:00
|
|
|
// Unknown action has been passed.
|
|
|
|
return false;
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
public function isTalkAllowed(): bool {
|
2021-10-18 21:04:03 +00:00
|
|
|
return $this->isAllowed( self::TALK );
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the editor can handle the existing content handler type.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
protected function isCurrentPageContentModelEditable(): bool {
|
2022-10-19 16:24:15 +00:00
|
|
|
if ( !$this->contentHandler ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
$this->contentHandler->supportsDirectEditing() &&
|
|
|
|
$this->contentHandler->supportsDirectApiEditing()
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For content types with custom action=edit handlers, let them do their thing
|
|
|
|
if ( array_key_exists( 'edit', $this->contentHandler->getActionOverrides() ?? [] ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 20:27:57 +00:00
|
|
|
/**
|
|
|
|
* Returns true if $title page exists and is editable or is creatable by $user as determined by
|
|
|
|
* quick checks.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
private function canEditOrCreate(): bool {
|
2019-10-08 17:22:31 +00:00
|
|
|
if ( !$this->title ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-27 20:27:57 +00:00
|
|
|
$userQuickEditCheck =
|
2023-07-17 17:31:07 +00:00
|
|
|
$this->performer->probablyCan( 'edit', $this->title ) && (
|
2019-08-27 20:27:57 +00:00
|
|
|
$this->title->exists() ||
|
2023-07-17 17:31:07 +00:00
|
|
|
$this->performer->probablyCan( 'create', $this->title )
|
|
|
|
);
|
|
|
|
if ( $this->performer->isRegistered() ) {
|
|
|
|
$legacyUser = $this->userFactory->newFromAuthority( $this->performer );
|
|
|
|
$blocked = $this->permissionManager->isBlockedFrom(
|
|
|
|
$legacyUser, $this->title, true
|
2019-08-27 20:27:57 +00:00
|
|
|
);
|
2023-07-17 17:31:07 +00:00
|
|
|
} else {
|
|
|
|
$blocked = false;
|
|
|
|
}
|
2019-08-27 20:27:57 +00:00
|
|
|
return $this->isCurrentPageContentModelEditable() && $userQuickEditCheck && !$blocked;
|
|
|
|
}
|
2021-03-09 19:43:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the user has the permissions to move the current page.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
private function canMove(): bool {
|
2021-03-09 19:43:14 +00:00
|
|
|
if ( !$this->title ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:31:07 +00:00
|
|
|
return $this->performer->probablyCan( 'move', $this->title )
|
2021-03-09 19:43:14 +00:00
|
|
|
&& $this->title->exists();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the user has the permissions to delete the current page.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
private function canDelete(): bool {
|
2021-03-09 19:43:14 +00:00
|
|
|
if ( !$this->title ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:31:07 +00:00
|
|
|
return $this->performer->probablyCan( 'delete', $this->title )
|
2021-03-09 19:43:14 +00:00
|
|
|
&& $this->title->exists();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the user has the permissions to change the protections status of the current page.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-03-28 19:15:41 +00:00
|
|
|
private function canProtect(): bool {
|
2021-03-09 19:43:14 +00:00
|
|
|
if ( !$this->title ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:31:07 +00:00
|
|
|
return $this->performer->probablyCan( 'protect', $this->title )
|
2021-03-09 19:43:14 +00:00
|
|
|
&& $this->title->exists();
|
|
|
|
}
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|