Merge "Remove deprecated handling of multiple arguments by the Block constructor"
[lhc/web/wiklou.git] / includes / actions / RollbackAction.php
1 <?php
2 /**
3 * Edit rollback user interface
4 *
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.
9 *
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.
14 *
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
18 *
19 * @file
20 * @ingroup Actions
21 */
22
23 /**
24 * User interface for the rollback action
25 *
26 * @ingroup Actions
27 */
28 class RollbackAction extends FormAction {
29
30 public function getName() {
31 return 'rollback';
32 }
33
34 public function getRestriction() {
35 return 'rollback';
36 }
37
38 protected function usesOOUI() {
39 return true;
40 }
41
42 protected function getDescription() {
43 return '';
44 }
45
46 public function doesWrites() {
47 return true;
48 }
49
50 public function onSuccess() {
51 return false;
52 }
53
54 public function onSubmit( $data ) {
55 return false;
56 }
57
58 protected function alterForm( HTMLForm $form ) {
59 $form->setWrapperLegendMsg( 'confirm-rollback-top' );
60 $form->setSubmitTextMsg( 'confirm-rollback-button' );
61 $form->setTokenSalt( 'rollback' );
62
63 $from = $this->getRequest()->getVal( 'from' );
64 if ( $from === null ) {
65 throw new BadRequestError( 'rollbackfailed', 'rollback-missingparam' );
66 }
67 foreach ( [ 'from', 'bot', 'hidediff', 'summary', 'token' ] as $param ) {
68 $val = $this->getRequest()->getVal( $param );
69 if ( $val !== null ) {
70 $form->addHiddenField( $param, $val );
71 }
72 }
73 }
74
75 /**
76 * @throws ConfigException
77 * @throws ErrorPageError
78 * @throws ReadOnlyError
79 * @throws ThrottledError
80 */
81 public function show() {
82 /**
83 * FIXME
84 * Remove temporary check of DisableRollbackConfirmationFeature
85 * after release of rollback feature. See T199534
86 */
87 $config = \MediaWiki\MediaWikiServices::getInstance()->getMainConfig();
88 if ( $config->get( 'DisableRollbackConfirmationFeature' ) == true ||
89 $this->getUser()->getOption( 'showrollbackconfirmation' ) == false ||
90 $this->getRequest()->wasPosted() ) {
91 $this->handleRollbackRequest();
92 } else {
93 $this->showRollbackConfirmationForm();
94 }
95 }
96
97 public function handleRollbackRequest() {
98 $this->enableTransactionalTimelimit();
99
100 $request = $this->getRequest();
101 $user = $this->getUser();
102 $from = $request->getVal( 'from' );
103 $rev = $this->page->getRevision();
104 if ( $from === null ) {
105 throw new ErrorPageError( 'rollbackfailed', 'rollback-missingparam' );
106 }
107 if ( !$rev ) {
108 throw new ErrorPageError( 'rollbackfailed', 'rollback-missingrevision' );
109 }
110 if ( $from !== $rev->getUserText() ) {
111 throw new ErrorPageError( 'rollbackfailed', 'alreadyrolled', [
112 $this->getTitle()->getPrefixedText(),
113 $from,
114 $rev->getUserText()
115 ] );
116 }
117
118 $data = null;
119 $errors = $this->page->doRollback(
120 $from,
121 $request->getText( 'summary' ),
122 $request->getVal( 'token' ),
123 $request->getBool( 'bot' ),
124 $data,
125 $this->getUser()
126 );
127
128 if ( in_array( [ 'actionthrottledtext' ], $errors ) ) {
129 throw new ThrottledError;
130 }
131
132 if ( $this->hasRollbackRelatedErrors( $errors ) ) {
133 $this->getOutput()->setPageTitle( $this->msg( 'rollbackfailed' ) );
134 $errArray = $errors[0];
135 $errMsg = array_shift( $errArray );
136 $this->getOutput()->addWikiMsgArray( $errMsg, $errArray );
137
138 if ( isset( $data['current'] ) ) {
139 /** @var Revision $current */
140 $current = $data['current'];
141
142 if ( $current->getComment() != '' ) {
143 $this->getOutput()->addWikiMsg(
144 'editcomment',
145 Message::rawParam(
146 Linker::formatComment( $current->getComment() )
147 )
148 );
149 }
150 }
151
152 return;
153 }
154
155 # NOTE: Permission errors already handled by Action::checkExecute.
156 if ( $errors == [ [ 'readonlytext' ] ] ) {
157 throw new ReadOnlyError;
158 }
159
160 # XXX: Would be nice if ErrorPageError could take multiple errors, and/or a status object.
161 # Right now, we only show the first error
162 foreach ( $errors as $error ) {
163 throw new ErrorPageError( 'rollbackfailed', $error[0], array_slice( $error, 1 ) );
164 }
165
166 /** @var Revision $current */
167 $current = $data['current'];
168 $target = $data['target'];
169 $newId = $data['newid'];
170 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
171 $this->getOutput()->setRobotPolicy( 'noindex,nofollow' );
172
173 $old = Linker::revUserTools( $current );
174 $new = Linker::revUserTools( $target );
175 $this->getOutput()->addHTML(
176 $this->msg( 'rollback-success' )
177 ->rawParams( $old, $new )
178 ->params( $current->getUserText( Revision::FOR_THIS_USER, $user ) )
179 ->params( $target->getUserText( Revision::FOR_THIS_USER, $user ) )
180 ->parseAsBlock()
181 );
182
183 if ( $user->getBoolOption( 'watchrollback' ) ) {
184 $user->addWatch( $this->page->getTitle(), User::IGNORE_USER_RIGHTS );
185 }
186
187 $this->getOutput()->returnToMain( false, $this->getTitle() );
188
189 if ( !$request->getBool( 'hidediff', false ) &&
190 !$this->getUser()->getBoolOption( 'norollbackdiff' )
191 ) {
192 $contentHandler = $current->getContentHandler();
193 $de = $contentHandler->createDifferenceEngine(
194 $this->getContext(),
195 $current->getId(),
196 $newId,
197 false,
198 true
199 );
200 $de->showDiff( '', '' );
201 }
202 }
203
204 /**
205 * Enables transactional time limit for POST and GET requests to RollbackAction
206 * @throws ConfigException
207 */
208 private function enableTransactionalTimelimit() {
209 // If Rollbacks are made POST-only, use $this->useTransactionalTimeLimit()
210 wfTransactionalTimeLimit();
211 if ( !$this->getRequest()->wasPosted() ) {
212 /**
213 * We apply the higher POST limits on GET requests
214 * to prevent logstash.wikimedia.org from being spammed
215 */
216 $fname = __METHOD__;
217 $trxLimits = $this->context->getConfig()->get( 'TrxProfilerLimits' );
218 $trxProfiler = Profiler::instance()->getTransactionProfiler();
219 $trxProfiler->redefineExpectations( $trxLimits['POST'], $fname );
220 DeferredUpdates::addCallableUpdate( function () use ( $trxProfiler, $trxLimits, $fname
221 ) {
222 $trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname );
223 } );
224 }
225 }
226
227 private function showRollbackConfirmationForm() {
228 $form = $this->getForm();
229 if ( $form->show() ) {
230 $this->onSuccess();
231 }
232 }
233
234 protected function getFormFields() {
235 return [
236 'intro' => [
237 'type' => 'info',
238 'vertical-label' => true,
239 'raw' => true,
240 'default' => $this->msg( 'confirm-rollback-bottom' )->parse()
241 ]
242 ];
243 }
244
245 private function hasRollbackRelatedErrors( array $errors ) {
246 return isset( $errors[0][0] ) &&
247 ( $errors[0][0] == 'alreadyrolled' ||
248 $errors[0][0] == 'cantrollback'
249 );
250 }
251 }