3 * Edit rollback user interface
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 use MediaWiki\Storage\RevisionRecord
;
26 * User interface for the rollback action
30 class RollbackAction
extends FormAction
{
32 public function getName() {
36 public function getRestriction() {
40 protected function usesOOUI() {
44 protected function getDescription() {
48 public function doesWrites() {
52 public function onSuccess() {
56 public function onSubmit( $data ) {
60 protected function alterForm( HTMLForm
$form ) {
61 $form->setWrapperLegendMsg( 'confirm-rollback-top' );
62 $form->setSubmitTextMsg( 'confirm-rollback-button' );
63 $form->setTokenSalt( 'rollback' );
65 $from = $this->getRequest()->getVal( 'from' );
66 if ( $from === null ) {
67 throw new BadRequestError( 'rollbackfailed', 'rollback-missingparam' );
69 foreach ( [ 'from', 'bot', 'hidediff', 'summary', 'token' ] as $param ) {
70 $val = $this->getRequest()->getVal( $param );
71 if ( $val !== null ) {
72 $form->addHiddenField( $param, $val );
78 * @throws ErrorPageError
79 * @throws ReadOnlyError
80 * @throws ThrottledError
82 public function show() {
83 if ( $this->getUser()->getOption( 'showrollbackconfirmation' ) == false ||
84 $this->getRequest()->wasPosted() ) {
85 $this->handleRollbackRequest();
87 $this->showRollbackConfirmationForm();
91 public function handleRollbackRequest() {
92 $this->enableTransactionalTimelimit();
94 $request = $this->getRequest();
95 $user = $this->getUser();
96 $from = $request->getVal( 'from' );
97 $rev = $this->page
->getRevision();
98 if ( $from === null ) {
99 throw new ErrorPageError( 'rollbackfailed', 'rollback-missingparam' );
102 throw new ErrorPageError( 'rollbackfailed', 'rollback-missingrevision' );
104 if ( $from !== $rev->getUserText() ) {
105 throw new ErrorPageError( 'rollbackfailed', 'alreadyrolled', [
106 $this->getTitle()->getPrefixedText(),
113 $errors = $this->page
->doRollback(
115 $request->getText( 'summary' ),
116 $request->getVal( 'token' ),
117 $request->getBool( 'bot' ),
122 if ( in_array( [ 'actionthrottledtext' ], $errors ) ) {
123 throw new ThrottledError
;
126 if ( $this->hasRollbackRelatedErrors( $errors ) ) {
127 $this->getOutput()->setPageTitle( $this->msg( 'rollbackfailed' ) );
128 $errArray = $errors[0];
129 $errMsg = array_shift( $errArray );
130 $this->getOutput()->addWikiMsgArray( $errMsg, $errArray );
132 if ( isset( $data['current'] ) ) {
133 /** @var Revision $current */
134 $current = $data['current'];
136 if ( $current->getComment() != '' ) {
137 $this->getOutput()->addWikiMsg(
140 Linker
::formatComment( $current->getComment() )
149 # NOTE: Permission errors already handled by Action::checkExecute.
150 if ( $errors == [ [ 'readonlytext' ] ] ) {
151 throw new ReadOnlyError
;
154 # XXX: Would be nice if ErrorPageError could take multiple errors, and/or a status object.
155 # Right now, we only show the first error
156 foreach ( $errors as $error ) {
157 throw new ErrorPageError( 'rollbackfailed', $error[0], array_slice( $error, 1 ) );
160 /** @var Revision $current */
161 $current = $data['current'];
162 $target = $data['target'];
163 $newId = $data['newid'];
164 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
165 $this->getOutput()->setRobotPolicy( 'noindex,nofollow' );
167 $old = Linker
::revUserTools( $current );
168 $new = Linker
::revUserTools( $target );
169 $this->getOutput()->addHTML(
170 $this->msg( 'rollback-success' )
171 ->rawParams( $old, $new )
172 ->params( $current->getUserText( RevisionRecord
::FOR_THIS_USER
, $user ) )
173 ->params( $target->getUserText( RevisionRecord
::FOR_THIS_USER
, $user ) )
177 if ( $user->getBoolOption( 'watchrollback' ) ) {
178 $user->addWatch( $this->page
->getTitle(), User
::IGNORE_USER_RIGHTS
);
181 $this->getOutput()->returnToMain( false, $this->getTitle() );
183 if ( !$request->getBool( 'hidediff', false ) &&
184 !$this->getUser()->getBoolOption( 'norollbackdiff' )
186 $contentHandler = $current->getContentHandler();
187 $de = $contentHandler->createDifferenceEngine(
194 $de->showDiff( '', '' );
199 * Enables transactional time limit for POST and GET requests to RollbackAction
200 * @throws ConfigException
202 private function enableTransactionalTimelimit() {
203 // If Rollbacks are made POST-only, use $this->useTransactionalTimeLimit()
204 wfTransactionalTimeLimit();
205 if ( !$this->getRequest()->wasPosted() ) {
207 * We apply the higher POST limits on GET requests
208 * to prevent logstash.wikimedia.org from being spammed
211 $trxLimits = $this->context
->getConfig()->get( 'TrxProfilerLimits' );
212 $trxProfiler = Profiler
::instance()->getTransactionProfiler();
213 $trxProfiler->redefineExpectations( $trxLimits['POST'], $fname );
214 DeferredUpdates
::addCallableUpdate( function () use ( $trxProfiler, $trxLimits, $fname
216 $trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname );
221 private function showRollbackConfirmationForm() {
222 $form = $this->getForm();
223 if ( $form->show() ) {
228 protected function getFormFields() {
232 'vertical-label' => true,
234 'default' => $this->msg( 'confirm-rollback-bottom' )->parse()
239 private function hasRollbackRelatedErrors( array $errors ) {
240 return isset( $errors[0][0] ) &&
241 ( $errors[0][0] == 'alreadyrolled' ||
242 $errors[0][0] == 'cantrollback'