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
24 * User interface for the rollback action
28 class RollbackAction
extends FormAction
{
30 public function getName() {
34 public function getRestriction() {
38 protected function usesOOUI() {
42 protected function getDescription() {
46 public function doesWrites() {
50 public function onSuccess() {
54 public function onSubmit( $data ) {
58 protected function alterForm( HTMLForm
$form ) {
59 $form->setWrapperLegendMsg( 'confirm-rollback-top' );
60 $form->setSubmitTextMsg( 'confirm-rollback-button' );
61 $form->setTokenSalt( 'rollback' );
63 $from = $this->getRequest()->getVal( 'from' );
64 if ( $from === null ) {
65 throw new BadRequestError( 'rollbackfailed', 'rollback-missingparam' );
67 foreach ( [ 'from', 'bot', 'hidediff', 'summary', 'token' ] as $param ) {
68 $val = $this->getRequest()->getVal( $param );
69 if ( $val !== null ) {
70 $form->addHiddenField( $param, $val );
76 * @throws ErrorPageError
77 * @throws ReadOnlyError
78 * @throws ThrottledError
80 public function show() {
81 if ( $this->getUser()->getOption( 'showrollbackconfirmation' ) == false ||
82 $this->getRequest()->wasPosted() ) {
83 $this->handleRollbackRequest();
85 $this->showRollbackConfirmationForm();
89 public function handleRollbackRequest() {
90 $this->enableTransactionalTimelimit();
92 $request = $this->getRequest();
93 $user = $this->getUser();
94 $from = $request->getVal( 'from' );
95 $rev = $this->page
->getRevision();
96 if ( $from === null ) {
97 throw new ErrorPageError( 'rollbackfailed', 'rollback-missingparam' );
100 throw new ErrorPageError( 'rollbackfailed', 'rollback-missingrevision' );
102 if ( $from !== $rev->getUserText() ) {
103 throw new ErrorPageError( 'rollbackfailed', 'alreadyrolled', [
104 $this->getTitle()->getPrefixedText(),
111 $errors = $this->page
->doRollback(
113 $request->getText( 'summary' ),
114 $request->getVal( 'token' ),
115 $request->getBool( 'bot' ),
120 if ( in_array( [ 'actionthrottledtext' ], $errors ) ) {
121 throw new ThrottledError
;
124 if ( $this->hasRollbackRelatedErrors( $errors ) ) {
125 $this->getOutput()->setPageTitle( $this->msg( 'rollbackfailed' ) );
126 $errArray = $errors[0];
127 $errMsg = array_shift( $errArray );
128 $this->getOutput()->addWikiMsgArray( $errMsg, $errArray );
130 if ( isset( $data['current'] ) ) {
131 /** @var Revision $current */
132 $current = $data['current'];
134 if ( $current->getComment() != '' ) {
135 $this->getOutput()->addWikiMsg(
138 Linker
::formatComment( $current->getComment() )
147 # NOTE: Permission errors already handled by Action::checkExecute.
148 if ( $errors == [ [ 'readonlytext' ] ] ) {
149 throw new ReadOnlyError
;
152 # XXX: Would be nice if ErrorPageError could take multiple errors, and/or a status object.
153 # Right now, we only show the first error
154 foreach ( $errors as $error ) {
155 throw new ErrorPageError( 'rollbackfailed', $error[0], array_slice( $error, 1 ) );
158 /** @var Revision $current */
159 $current = $data['current'];
160 $target = $data['target'];
161 $newId = $data['newid'];
162 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
163 $this->getOutput()->setRobotPolicy( 'noindex,nofollow' );
165 $old = Linker
::revUserTools( $current );
166 $new = Linker
::revUserTools( $target );
167 $this->getOutput()->addHTML(
168 $this->msg( 'rollback-success' )
169 ->rawParams( $old, $new )
170 ->params( $current->getUserText( Revision
::FOR_THIS_USER
, $user ) )
171 ->params( $target->getUserText( Revision
::FOR_THIS_USER
, $user ) )
175 if ( $user->getBoolOption( 'watchrollback' ) ) {
176 $user->addWatch( $this->page
->getTitle(), User
::IGNORE_USER_RIGHTS
);
179 $this->getOutput()->returnToMain( false, $this->getTitle() );
181 if ( !$request->getBool( 'hidediff', false ) &&
182 !$this->getUser()->getBoolOption( 'norollbackdiff' )
184 $contentHandler = $current->getContentHandler();
185 $de = $contentHandler->createDifferenceEngine(
192 $de->showDiff( '', '' );
197 * Enables transactional time limit for POST and GET requests to RollbackAction
198 * @throws ConfigException
200 private function enableTransactionalTimelimit() {
201 // If Rollbacks are made POST-only, use $this->useTransactionalTimeLimit()
202 wfTransactionalTimeLimit();
203 if ( !$this->getRequest()->wasPosted() ) {
205 * We apply the higher POST limits on GET requests
206 * to prevent logstash.wikimedia.org from being spammed
209 $trxLimits = $this->context
->getConfig()->get( 'TrxProfilerLimits' );
210 $trxProfiler = Profiler
::instance()->getTransactionProfiler();
211 $trxProfiler->redefineExpectations( $trxLimits['POST'], $fname );
212 DeferredUpdates
::addCallableUpdate( function () use ( $trxProfiler, $trxLimits, $fname
214 $trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname );
219 private function showRollbackConfirmationForm() {
220 $form = $this->getForm();
221 if ( $form->show() ) {
226 protected function getFormFields() {
230 'vertical-label' => true,
232 'default' => $this->msg( 'confirm-rollback-bottom' )->parse()
237 private function hasRollbackRelatedErrors( array $errors ) {
238 return isset( $errors[0][0] ) &&
239 ( $errors[0][0] == 'alreadyrolled' ||
240 $errors[0][0] == 'cantrollback'