CommentParser: * Move comment formatting backend from Linker to a CommentParser service. Allow link existence and file existence to be batched. * Rename $local to $samePage since I think that is clearer. * Rename $title to $selfLinkTarget since it was unclear what the title was used for. * Rename the "autocomment" concept to "section link" in public interfaces, although the old term remains in CSS classes. * Keep unsafe HTML pass-through in separate "unsafe" methods, for easier static analysis and code review. CommentFormatter: * Add CommentFormatter and RowCommentFormatter services as a usable frontend for comment batches, and to replace the Linker static methods. * Provide fluent and parametric interfaces. Linker: * Remove Linker::makeCommentLink() without deprecation -- nothing calls it and it is obviously an internal helper. * Soft-deprecate Linker methods formatComment(), formatLinksInComment(), commentBlock() and revComment(). Caller migration: * CommentFormatter single: Linker, RollbackAction, ApiComparePages, ApiParse * CommentFormatter parametric batch: ImageHistoryPseudoPager * CommentFormatter fluent batch: ApiQueryFilearchive * RowCommentFormatter sequential: History feed, BlocklistPager, ProtectedPagesPager, ApiQueryProtectedTitles * RowCommentFormatter with index: ChangesFeed, ChangesList, ApiQueryDeletedrevs, ApiQueryLogEvents, ApiQueryRecentChanges * RevisionCommentBatch: HistoryPager, ContribsPager Bug: T285917 Change-Id: Ia3fd50a4a13138ba5003d884962da24746d562d0
301 lines
8.9 KiB
PHP
301 lines
8.9 KiB
PHP
<?php
|
|
/**
|
|
* Edit rollback user interface
|
|
*
|
|
* 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
|
|
*
|
|
* @file
|
|
* @ingroup Actions
|
|
*/
|
|
|
|
use MediaWiki\CommentFormatter\CommentFormatter;
|
|
use MediaWiki\Content\IContentHandlerFactory;
|
|
use MediaWiki\Page\RollbackPageFactory;
|
|
use MediaWiki\Revision\RevisionRecord;
|
|
use MediaWiki\Revision\SlotRecord;
|
|
use MediaWiki\User\UserOptionsLookup;
|
|
use MediaWiki\Watchlist\WatchlistManager;
|
|
|
|
/**
|
|
* User interface for the rollback action
|
|
*
|
|
* @ingroup Actions
|
|
*/
|
|
class RollbackAction extends FormAction {
|
|
|
|
/** @var IContentHandlerFactory */
|
|
private $contentHandlerFactory;
|
|
|
|
/** @var RollbackPageFactory */
|
|
private $rollbackPageFactory;
|
|
|
|
/** @var UserOptionsLookup */
|
|
private $userOptionsLookup;
|
|
|
|
/** @var WatchlistManager */
|
|
private $watchlistManager;
|
|
|
|
/** @var CommentFormatter */
|
|
private $commentFormatter;
|
|
|
|
/**
|
|
* @param Page $page
|
|
* @param IContextSource|null $context
|
|
* @param IContentHandlerFactory $contentHandlerFactory
|
|
* @param RollbackPageFactory $rollbackPageFactory
|
|
* @param UserOptionsLookup $userOptionsLookup
|
|
* @param WatchlistManager $watchlistManager
|
|
* @param CommentFormatter $commentFormatter
|
|
*/
|
|
public function __construct(
|
|
Page $page,
|
|
?IContextSource $context,
|
|
IContentHandlerFactory $contentHandlerFactory,
|
|
RollbackPageFactory $rollbackPageFactory,
|
|
UserOptionsLookup $userOptionsLookup,
|
|
WatchlistManager $watchlistManager,
|
|
CommentFormatter $commentFormatter
|
|
) {
|
|
parent::__construct( $page, $context );
|
|
$this->contentHandlerFactory = $contentHandlerFactory;
|
|
$this->rollbackPageFactory = $rollbackPageFactory;
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
|
$this->watchlistManager = $watchlistManager;
|
|
$this->commentFormatter = $commentFormatter;
|
|
}
|
|
|
|
public function getName() {
|
|
return 'rollback';
|
|
}
|
|
|
|
public function getRestriction() {
|
|
return 'rollback';
|
|
}
|
|
|
|
protected function usesOOUI() {
|
|
return true;
|
|
}
|
|
|
|
protected function getDescription() {
|
|
return '';
|
|
}
|
|
|
|
public function doesWrites() {
|
|
return true;
|
|
}
|
|
|
|
public function onSuccess() {
|
|
return false;
|
|
}
|
|
|
|
public function onSubmit( $data ) {
|
|
return false;
|
|
}
|
|
|
|
protected function alterForm( HTMLForm $form ) {
|
|
$form->setWrapperLegendMsg( 'confirm-rollback-top' );
|
|
$form->setSubmitTextMsg( 'confirm-rollback-button' );
|
|
$form->setTokenSalt( 'rollback' );
|
|
|
|
$from = $this->getRequest()->getVal( 'from' );
|
|
if ( $from === null ) {
|
|
throw new BadRequestError( 'rollbackfailed', 'rollback-missingparam' );
|
|
}
|
|
foreach ( [ 'from', 'bot', 'hidediff', 'summary', 'token' ] as $param ) {
|
|
$val = $this->getRequest()->getVal( $param );
|
|
if ( $val !== null ) {
|
|
$form->addHiddenField( $param, $val );
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @throws ErrorPageError
|
|
* @throws ReadOnlyError
|
|
* @throws ThrottledError
|
|
*/
|
|
public function show() {
|
|
if ( !$this->userOptionsLookup->getOption( $this->getUser(), 'showrollbackconfirmation' ) ||
|
|
$this->getRequest()->wasPosted()
|
|
) {
|
|
$this->handleRollbackRequest();
|
|
} else {
|
|
$this->showRollbackConfirmationForm();
|
|
}
|
|
}
|
|
|
|
public function handleRollbackRequest() {
|
|
$this->enableTransactionalTimelimit();
|
|
$this->getOutput()->addModuleStyles( 'mediawiki.interface.helpers.styles' );
|
|
|
|
$request = $this->getRequest();
|
|
$user = $this->getUser();
|
|
$from = $request->getVal( 'from' );
|
|
$rev = $this->getWikiPage()->getRevisionRecord();
|
|
if ( $from === null ) {
|
|
throw new ErrorPageError( 'rollbackfailed', 'rollback-missingparam' );
|
|
}
|
|
if ( !$rev ) {
|
|
throw new ErrorPageError( 'rollbackfailed', 'rollback-missingrevision' );
|
|
}
|
|
|
|
$revUser = $rev->getUser();
|
|
$userText = $revUser ? $revUser->getName() : '';
|
|
if ( $from !== $userText ) {
|
|
throw new ErrorPageError( 'rollbackfailed', 'alreadyrolled', [
|
|
$this->getTitle()->getPrefixedText(),
|
|
$from,
|
|
$userText
|
|
] );
|
|
}
|
|
|
|
if ( !$user->matchEditToken( $request->getVal( 'token' ), 'rollback' ) ) {
|
|
throw new ErrorPageError( 'sessionfailure-title', 'sessionfailure' );
|
|
}
|
|
|
|
// The revision has the user suppressed, so the rollback has empty 'from',
|
|
// so the check above would succeed in that case.
|
|
if ( !$revUser ) {
|
|
$revUser = $rev->getUser( RevisionRecord::RAW );
|
|
}
|
|
|
|
$rollbackResult = $this->rollbackPageFactory
|
|
->newRollbackPage( $this->getWikiPage(), $this->getContext()->getAuthority(), $revUser )
|
|
->setSummary( $request->getText( 'summary' ) )
|
|
->markAsBot( $request->getBool( 'bot' ) )
|
|
->rollbackIfAllowed();
|
|
$data = $rollbackResult->getValue();
|
|
|
|
if ( $rollbackResult->hasMessage( 'actionthrottledtext' ) ) {
|
|
throw new ThrottledError;
|
|
}
|
|
|
|
if ( $rollbackResult->hasMessage( 'alreadyrolled' ) || $rollbackResult->hasMessage( 'cantrollback' ) ) {
|
|
$this->getOutput()->setPageTitle( $this->msg( 'rollbackfailed' ) );
|
|
$errArray = $rollbackResult->getErrors()[0];
|
|
$this->getOutput()->addWikiMsgArray( $errArray['message'], $errArray['params'] );
|
|
|
|
if ( isset( $data['current-revision-record'] ) ) {
|
|
/** @var RevisionRecord $current */
|
|
$current = $data['current-revision-record'];
|
|
|
|
if ( $current->getComment() != null ) {
|
|
$this->getOutput()->addWikiMsg(
|
|
'editcomment',
|
|
Message::rawParam(
|
|
$this->commentFormatter
|
|
->format( $current->getComment()->text )
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
# NOTE: Permission errors already handled by Action::checkExecute.
|
|
if ( $rollbackResult->hasMessage( 'readonlytext' ) ) {
|
|
throw new ReadOnlyError;
|
|
}
|
|
|
|
# XXX: Would be nice if ErrorPageError could take multiple errors, and/or a status object.
|
|
# Right now, we only show the first error
|
|
foreach ( $rollbackResult->getErrors() as $error ) {
|
|
throw new ErrorPageError( 'rollbackfailed', $error['message'], $error['params'] );
|
|
}
|
|
|
|
/** @var RevisionRecord $current */
|
|
$current = $data['current-revision-record'];
|
|
$target = $data['target-revision-record'];
|
|
$newId = $data['newid'];
|
|
$this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
|
|
$this->getOutput()->setRobotPolicy( 'noindex,nofollow' );
|
|
|
|
$old = Linker::revUserTools( $current );
|
|
$new = Linker::revUserTools( $target );
|
|
|
|
$currentUser = $current->getUser( RevisionRecord::FOR_THIS_USER, $user );
|
|
$targetUser = $target->getUser( RevisionRecord::FOR_THIS_USER, $user );
|
|
$this->getOutput()->addHTML(
|
|
$this->msg( 'rollback-success' )
|
|
->rawParams( $old, $new )
|
|
->params( $currentUser ? $currentUser->getName() : '' )
|
|
->params( $targetUser ? $targetUser->getName() : '' )
|
|
->parseAsBlock()
|
|
);
|
|
|
|
if ( $this->userOptionsLookup->getBoolOption( $user, 'watchrollback' ) ) {
|
|
$this->watchlistManager->addWatchIgnoringRights( $user, $this->getTitle() );
|
|
}
|
|
|
|
$this->getOutput()->returnToMain( false, $this->getTitle() );
|
|
|
|
if ( !$request->getBool( 'hidediff', false ) &&
|
|
!$this->userOptionsLookup->getBoolOption( $this->getUser(), 'norollbackdiff' )
|
|
) {
|
|
$contentModel = $current->getSlot( SlotRecord::MAIN, RevisionRecord::RAW )
|
|
->getModel();
|
|
$contentHandler = $this->contentHandlerFactory->getContentHandler( $contentModel );
|
|
$de = $contentHandler->createDifferenceEngine(
|
|
$this->getContext(),
|
|
$current->getId(),
|
|
$newId,
|
|
false,
|
|
true
|
|
);
|
|
$de->showDiff( '', '' );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enables transactional time limit for POST and GET requests to RollbackAction
|
|
* @throws ConfigException
|
|
*/
|
|
private function enableTransactionalTimelimit() {
|
|
// If Rollbacks are made POST-only, use $this->useTransactionalTimeLimit()
|
|
wfTransactionalTimeLimit();
|
|
if ( !$this->getRequest()->wasPosted() ) {
|
|
/**
|
|
* We apply the higher POST limits on GET requests
|
|
* to prevent logstash.wikimedia.org from being spammed
|
|
*/
|
|
$fname = __METHOD__;
|
|
$trxLimits = $this->context->getConfig()->get( 'TrxProfilerLimits' );
|
|
$trxProfiler = Profiler::instance()->getTransactionProfiler();
|
|
$trxProfiler->redefineExpectations( $trxLimits['POST'], $fname );
|
|
DeferredUpdates::addCallableUpdate( static function () use ( $trxProfiler, $trxLimits, $fname
|
|
) {
|
|
$trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname );
|
|
} );
|
|
}
|
|
}
|
|
|
|
private function showRollbackConfirmationForm() {
|
|
$form = $this->getForm();
|
|
if ( $form->show() ) {
|
|
$this->onSuccess();
|
|
}
|
|
}
|
|
|
|
protected function getFormFields() {
|
|
return [
|
|
'intro' => [
|
|
'type' => 'info',
|
|
'raw' => true,
|
|
'default' => $this->msg( 'confirm-rollback-bottom' )->parse()
|
|
]
|
|
];
|
|
}
|
|
}
|