2020-01-11 23:49:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-05-30 19:10:58 +00:00
|
|
|
use MediaWiki\Content\IContentHandlerFactory;
|
|
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
|
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
2021-03-02 21:29:46 +00:00
|
|
|
use MediaWiki\Page\PageIdentity;
|
|
|
|
|
use MediaWiki\Permissions\Authority;
|
|
|
|
|
use MediaWiki\Permissions\PermissionStatus;
|
2020-05-30 19:10:58 +00:00
|
|
|
use MediaWiki\Revision\RevisionLookup;
|
2020-04-13 00:10:07 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2021-03-02 21:29:46 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2020-01-11 23:49:41 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper class to change the content model of pages
|
|
|
|
|
*
|
|
|
|
|
* For creating new pages via the action API,
|
|
|
|
|
* use the edit api and specify the desired content model and format.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.35
|
2020-04-11 08:10:18 +00:00
|
|
|
* @author DannyS712
|
2020-01-11 23:49:41 +00:00
|
|
|
*/
|
|
|
|
|
class ContentModelChange {
|
|
|
|
|
|
2020-05-30 19:10:58 +00:00
|
|
|
/** @var IContentHandlerFactory */
|
|
|
|
|
private $contentHandlerFactory;
|
2020-01-11 23:49:41 +00:00
|
|
|
|
2020-05-30 19:10:58 +00:00
|
|
|
/** @var HookRunner */
|
|
|
|
|
private $hookRunner;
|
|
|
|
|
|
|
|
|
|
/** @var RevisionLookup */
|
|
|
|
|
private $revLookup;
|
|
|
|
|
|
2021-03-02 21:29:46 +00:00
|
|
|
/** @var UserFactory */
|
|
|
|
|
private $userFactory;
|
|
|
|
|
|
|
|
|
|
/** @var Authority making the change */
|
|
|
|
|
private $performer;
|
2020-05-30 19:10:58 +00:00
|
|
|
|
|
|
|
|
/** @var WikiPage */
|
2020-01-11 23:49:41 +00:00
|
|
|
private $page;
|
|
|
|
|
|
2020-05-30 19:10:58 +00:00
|
|
|
/** @var string */
|
2020-01-11 23:49:41 +00:00
|
|
|
private $newModel;
|
|
|
|
|
|
2020-05-30 19:10:58 +00:00
|
|
|
/** @var string[] tags to add */
|
2020-01-11 23:49:41 +00:00
|
|
|
private $tags;
|
|
|
|
|
|
2020-05-30 19:10:58 +00:00
|
|
|
/** @var Content */
|
2020-01-11 23:49:41 +00:00
|
|
|
private $newContent;
|
|
|
|
|
|
2020-05-30 19:10:58 +00:00
|
|
|
/** @var int|false latest revision id, or false if creating */
|
2020-01-11 23:49:41 +00:00
|
|
|
private $latestRevId;
|
|
|
|
|
|
2020-05-30 19:10:58 +00:00
|
|
|
/** @var string 'new' or 'change' */
|
2020-01-11 23:49:41 +00:00
|
|
|
private $logAction;
|
|
|
|
|
|
2020-05-30 19:10:58 +00:00
|
|
|
/** @var string 'apierror-' or empty string, for status messages */
|
2020-01-11 23:49:41 +00:00
|
|
|
private $msgPrefix;
|
|
|
|
|
|
2020-05-30 19:10:58 +00:00
|
|
|
/**
|
|
|
|
|
* @param IContentHandlerFactory $contentHandlerFactory
|
|
|
|
|
* @param HookContainer $hookContainer
|
|
|
|
|
* @param RevisionLookup $revLookup
|
2021-03-02 21:29:46 +00:00
|
|
|
* @param UserFactory $userFactory
|
|
|
|
|
* @param Authority $performer
|
2020-05-30 19:10:58 +00:00
|
|
|
* @param WikiPage $page
|
|
|
|
|
* @param string $newModel
|
|
|
|
|
*/
|
2020-01-11 23:49:41 +00:00
|
|
|
public function __construct(
|
2020-05-30 19:10:58 +00:00
|
|
|
IContentHandlerFactory $contentHandlerFactory,
|
|
|
|
|
HookContainer $hookContainer,
|
|
|
|
|
RevisionLookup $revLookup,
|
2021-03-02 21:29:46 +00:00
|
|
|
UserFactory $userFactory,
|
|
|
|
|
Authority $performer,
|
2020-01-11 23:49:41 +00:00
|
|
|
WikiPage $page,
|
2020-05-30 19:10:58 +00:00
|
|
|
string $newModel
|
2020-01-11 23:49:41 +00:00
|
|
|
) {
|
2020-05-30 19:10:58 +00:00
|
|
|
$this->contentHandlerFactory = $contentHandlerFactory;
|
|
|
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
|
|
|
|
$this->revLookup = $revLookup;
|
2021-03-02 21:29:46 +00:00
|
|
|
$this->userFactory = $userFactory;
|
2020-05-30 19:10:58 +00:00
|
|
|
|
2021-03-02 21:29:46 +00:00
|
|
|
$this->performer = $performer;
|
2020-01-11 23:49:41 +00:00
|
|
|
$this->page = $page;
|
|
|
|
|
$this->newModel = $newModel;
|
|
|
|
|
|
|
|
|
|
// SpecialChangeContentModel doesn't support tags
|
|
|
|
|
// api can specify tags via ::setTags, which also checks if user can add
|
|
|
|
|
// the tags specified
|
|
|
|
|
$this->tags = [];
|
|
|
|
|
|
|
|
|
|
// Requires createNewContent to be called first
|
|
|
|
|
$this->logAction = '';
|
|
|
|
|
|
|
|
|
|
// Defaults to nothing, for special page
|
|
|
|
|
$this->msgPrefix = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $msgPrefix
|
|
|
|
|
*/
|
|
|
|
|
public function setMessagePrefix( $msgPrefix ) {
|
|
|
|
|
$this->msgPrefix = $msgPrefix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-03-02 21:29:46 +00:00
|
|
|
* @param callable $authorizer ( string $action, PageIdentity $target, PermissionStatus $status )
|
|
|
|
|
* @return PermissionStatus
|
2020-01-11 23:49:41 +00:00
|
|
|
*/
|
2021-03-02 21:29:46 +00:00
|
|
|
private function authorizeInternal( callable $authorizer ): PermissionStatus {
|
2020-01-11 23:49:41 +00:00
|
|
|
$current = $this->page->getTitle();
|
|
|
|
|
$titleWithNewContentModel = clone $current;
|
|
|
|
|
$titleWithNewContentModel->setContentModel( $this->newModel );
|
|
|
|
|
|
2021-03-02 21:29:46 +00:00
|
|
|
$status = PermissionStatus::newEmpty();
|
2021-01-01 10:40:41 +00:00
|
|
|
if ( !$current->exists() ) {
|
|
|
|
|
$authorizer( 'create', $current, $status );
|
|
|
|
|
}
|
2021-03-02 21:29:46 +00:00
|
|
|
$authorizer( 'editcontentmodel', $current, $status );
|
|
|
|
|
$authorizer( 'edit', $current, $status );
|
|
|
|
|
$authorizer( 'editcontentmodel', $titleWithNewContentModel, $status );
|
|
|
|
|
$authorizer( 'edit', $titleWithNewContentModel, $status );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check whether $performer can execute the move.
|
|
|
|
|
*
|
|
|
|
|
* @note this method does not guarantee full permissions check, so it should
|
|
|
|
|
* only be used to to decide whether to show a move form. To authorize the move
|
|
|
|
|
* action use {@link self::authorizeChange} instead.
|
|
|
|
|
*
|
|
|
|
|
* @return PermissionStatus
|
|
|
|
|
*/
|
|
|
|
|
public function probablyCanChange(): PermissionStatus {
|
|
|
|
|
return $this->authorizeInternal(
|
|
|
|
|
function ( string $action, PageIdentity $target, PermissionStatus $status ) {
|
|
|
|
|
return $this->performer->probablyCan( $action, $target, $status );
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Authorize the move by $performer.
|
|
|
|
|
*
|
|
|
|
|
* @note this method should be used right before the actual move is performed.
|
|
|
|
|
* To check whether a current performer has the potential to move the page,
|
|
|
|
|
* use {@link self::probablyCanChange} instead.
|
|
|
|
|
*
|
|
|
|
|
* @return PermissionStatus
|
|
|
|
|
*/
|
|
|
|
|
public function authorizeChange(): PermissionStatus {
|
|
|
|
|
return $this->authorizeInternal(
|
|
|
|
|
function ( string $action, PageIdentity $target, PermissionStatus $status ) {
|
|
|
|
|
return $this->performer->authorizeWrite( $action, $target, $status );
|
|
|
|
|
}
|
2020-01-11 23:49:41 +00:00
|
|
|
);
|
2021-03-02 21:29:46 +00:00
|
|
|
}
|
2020-01-11 23:49:41 +00:00
|
|
|
|
2021-03-02 21:29:46 +00:00
|
|
|
/**
|
|
|
|
|
* Check user can edit and editcontentmodel before and after
|
|
|
|
|
*
|
|
|
|
|
* @deprecated since 1.36. Use ::probablyCanChange or ::authorizeChange instead.
|
|
|
|
|
* @return array from wfMergeErrorArrays
|
|
|
|
|
*/
|
|
|
|
|
public function checkPermissions() {
|
|
|
|
|
wfDeprecated( __METHOD__, '1.36' );
|
|
|
|
|
$status = $this->authorizeInternal(
|
|
|
|
|
function ( string $action, PageIdentity $target, PermissionStatus $status ) {
|
|
|
|
|
return $this->performer->definitelyCan( $action, $target, $status );
|
|
|
|
|
} );
|
|
|
|
|
return $status->toLegacyErrorArray();
|
2020-01-11 23:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Specify the tags the user wants to add, and check permissions
|
|
|
|
|
*
|
|
|
|
|
* @param string[] $tags
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
public function setTags( $tags ) {
|
2021-03-02 21:29:46 +00:00
|
|
|
$tagStatus = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->performer );
|
2020-01-11 23:49:41 +00:00
|
|
|
if ( $tagStatus->isOK() ) {
|
|
|
|
|
$this->tags = $tags;
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
} else {
|
|
|
|
|
return $tagStatus;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
private function createNewContent() {
|
2020-05-30 19:10:58 +00:00
|
|
|
$contentHandlerFactory = $this->contentHandlerFactory;
|
2020-01-11 23:49:41 +00:00
|
|
|
|
|
|
|
|
$title = $this->page->getTitle();
|
2020-05-30 19:10:58 +00:00
|
|
|
$latestRevRecord = $this->revLookup->getRevisionByTitle( $title );
|
2020-01-11 23:49:41 +00:00
|
|
|
|
2020-04-13 00:10:07 +00:00
|
|
|
if ( $latestRevRecord ) {
|
|
|
|
|
$latestContent = $latestRevRecord->getContent( SlotRecord::MAIN );
|
2020-01-11 23:49:41 +00:00
|
|
|
$latestHandler = $latestContent->getContentHandler();
|
|
|
|
|
$latestModel = $latestContent->getModel();
|
|
|
|
|
if ( !$latestHandler->supportsDirectEditing() ) {
|
|
|
|
|
// Only reachable via api
|
|
|
|
|
return Status::newFatal(
|
|
|
|
|
'apierror-changecontentmodel-nodirectediting',
|
|
|
|
|
ContentHandler::getLocalizedName( $latestModel )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$newModel = $this->newModel;
|
|
|
|
|
if ( $newModel === $latestModel ) {
|
|
|
|
|
// Only reachable via api
|
|
|
|
|
return Status::newFatal( 'apierror-nochanges' );
|
|
|
|
|
}
|
|
|
|
|
$newHandler = $contentHandlerFactory->getContentHandler( $newModel );
|
|
|
|
|
if ( !$newHandler->canBeUsedOn( $title ) ) {
|
|
|
|
|
// Only reachable via api
|
|
|
|
|
return Status::newFatal(
|
|
|
|
|
'apierror-changecontentmodel-cannotbeused',
|
|
|
|
|
ContentHandler::getLocalizedName( $newModel ),
|
|
|
|
|
Message::plaintextParam( $title->getPrefixedText() )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$newContent = $newHandler->unserializeContent(
|
|
|
|
|
$latestContent->serialize()
|
|
|
|
|
);
|
|
|
|
|
} catch ( MWException $e ) {
|
|
|
|
|
// Messages: changecontentmodel-cannot-convert,
|
|
|
|
|
// apierror-changecontentmodel-cannot-convert
|
|
|
|
|
return Status::newFatal(
|
|
|
|
|
$this->msgPrefix . 'changecontentmodel-cannot-convert',
|
|
|
|
|
Message::plaintextParam( $title->getPrefixedText() ),
|
|
|
|
|
ContentHandler::getLocalizedName( $newModel )
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-04-13 00:10:07 +00:00
|
|
|
$this->latestRevId = $latestRevRecord->getId();
|
2020-01-11 23:49:41 +00:00
|
|
|
$this->logAction = 'change';
|
|
|
|
|
} else {
|
|
|
|
|
// Page doesn't exist, create an empty content object
|
|
|
|
|
$newContent = $contentHandlerFactory
|
|
|
|
|
->getContentHandler( $this->newModel )
|
|
|
|
|
->makeEmptyContent();
|
|
|
|
|
$this->latestRevId = false;
|
|
|
|
|
$this->logAction = 'new';
|
|
|
|
|
}
|
|
|
|
|
$this->newContent = $newContent;
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-03-02 21:29:46 +00:00
|
|
|
* Handle change and logging after validation
|
2020-01-11 23:49:41 +00:00
|
|
|
*
|
|
|
|
|
* Can still be intercepted by hooks
|
|
|
|
|
*
|
|
|
|
|
* @param IContextSource $context
|
|
|
|
|
* @param string $comment
|
|
|
|
|
* @param bool $bot Mark as a bot edit if the user can
|
|
|
|
|
* @return Status
|
|
|
|
|
* @throws ThrottledError
|
|
|
|
|
*/
|
|
|
|
|
public function doContentModelChange(
|
|
|
|
|
IContextSource $context,
|
|
|
|
|
$comment,
|
|
|
|
|
$bot
|
|
|
|
|
) {
|
|
|
|
|
$status = $this->createNewContent();
|
|
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$page = $this->page;
|
|
|
|
|
$title = $page->getTitle();
|
2021-03-02 21:29:46 +00:00
|
|
|
$user = $this->userFactory->newFromAuthority( $this->performer );
|
2020-01-11 23:49:41 +00:00
|
|
|
|
2021-03-02 21:29:46 +00:00
|
|
|
// TODO: fold into authorizeChange
|
2020-01-11 23:49:41 +00:00
|
|
|
if ( $user->pingLimiter( 'editcontentmodel' ) ) {
|
|
|
|
|
throw new ThrottledError();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create log entry
|
|
|
|
|
$log = new ManualLogEntry( 'contentmodel', $this->logAction );
|
2021-03-02 21:29:46 +00:00
|
|
|
$log->setPerformer( $this->performer->getUser() );
|
2020-01-11 23:49:41 +00:00
|
|
|
$log->setTarget( $title );
|
|
|
|
|
$log->setComment( $comment );
|
|
|
|
|
$log->setParameters( [
|
|
|
|
|
'4::oldmodel' => $title->getContentModel(),
|
|
|
|
|
'5::newmodel' => $this->newModel
|
|
|
|
|
] );
|
|
|
|
|
$log->addTags( $this->tags );
|
|
|
|
|
|
|
|
|
|
$formatter = LogFormatter::newFromEntry( $log );
|
|
|
|
|
$formatter->setContext( RequestContext::newExtraneousContext( $title ) );
|
|
|
|
|
$reason = $formatter->getPlainActionText();
|
|
|
|
|
|
|
|
|
|
if ( $comment !== '' ) {
|
|
|
|
|
$reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run edit filters
|
|
|
|
|
$derivativeContext = new DerivativeContext( $context );
|
|
|
|
|
$derivativeContext->setTitle( $title );
|
|
|
|
|
$derivativeContext->setWikiPage( $page );
|
|
|
|
|
$status = new Status();
|
|
|
|
|
|
|
|
|
|
$newContent = $this->newContent;
|
|
|
|
|
|
2020-05-30 19:10:58 +00:00
|
|
|
if ( !$this->hookRunner->onEditFilterMergedContent( $derivativeContext, $newContent,
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$status, $reason, $user, false )
|
2020-01-11 23:49:41 +00:00
|
|
|
) {
|
|
|
|
|
if ( $status->isGood() ) {
|
|
|
|
|
// TODO: extensions should really specify an error message
|
|
|
|
|
$status->fatal( 'hookaborted' );
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make the edit
|
|
|
|
|
$flags = $this->latestRevId ? EDIT_UPDATE : EDIT_NEW;
|
|
|
|
|
$flags |= EDIT_INTERNAL;
|
2021-03-02 21:29:46 +00:00
|
|
|
if ( $bot && $this->performer->isAllowed( 'bot' ) ) {
|
2020-01-11 23:49:41 +00:00
|
|
|
$flags |= EDIT_FORCE_BOT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$status = $page->doEditContent(
|
|
|
|
|
$newContent,
|
|
|
|
|
$reason,
|
|
|
|
|
$flags,
|
|
|
|
|
$this->latestRevId,
|
2021-03-02 21:29:46 +00:00
|
|
|
$this->performer,
|
2020-01-11 23:49:41 +00:00
|
|
|
null,
|
|
|
|
|
$this->tags
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$logid = $log->insert();
|
|
|
|
|
$log->publish( $logid );
|
|
|
|
|
|
|
|
|
|
$values = [
|
|
|
|
|
'logid' => $logid
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return Status::newGood( $values );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|