Merge "Convert Special:WithoutInterwiki to OOUI"
[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 preText() {
39 return $this->msg( 'confirm-rollback-top' )->parse();
40 }
41
42 protected function alterForm( HTMLForm $form ) {
43 $form->setSubmitTextMsg( 'confirm-rollback-button' );
44 $form->setTokenSalt( 'rollback' );
45
46 // Copy parameters from GET to confirmation form
47 $from = $this->getRequest()->getVal( 'from' );
48 if ( $from === null ) {
49 throw new BadRequestError( 'rollbackfailed', 'rollback-missingparam' );
50 }
51 foreach ( [ 'from', 'bot', 'hidediff', 'summary' ] as $param ) {
52 $val = $this->getRequest()->getVal( $param );
53 if ( $val !== null ) {
54 $form->addHiddenField( $param, $val );
55 }
56 }
57 }
58
59 /**
60 * This must return true so that HTMLForm::show() will not display the form again after
61 * submission. For rollback, display either the form or the result (success/error)
62 * not both.
63 *
64 * @return bool
65 * @throws ErrorPageError
66 */
67 public function onSubmit( $data ) {
68 $this->useTransactionalTimeLimit();
69
70 $request = $this->getRequest();
71 $user = $this->getUser();
72 $from = $request->getVal( 'from' );
73 $rev = $this->page->getRevision();
74 if ( $from === null || $from === '' ) {
75 throw new ErrorPageError( 'rollbackfailed', 'rollback-missingparam' );
76 }
77 if ( $from !== $rev->getUserText() ) {
78 throw new ErrorPageError( 'rollbackfailed', 'alreadyrolled', [
79 $this->getTitle()->getPrefixedText(),
80 $from,
81 $rev->getUserText()
82 ] );
83 }
84
85 $data = null;
86 $errors = $this->page->doRollback(
87 $from,
88 $request->getText( 'summary' ),
89 // Provided by HTMLForm
90 $request->getVal( 'wpEditToken' ),
91 $request->getBool( 'bot' ),
92 $data,
93 $this->getUser()
94 );
95
96 if ( in_array( [ 'actionthrottledtext' ], $errors ) ) {
97 throw new ThrottledError;
98 }
99
100 if ( isset( $errors[0][0] ) &&
101 ( $errors[0][0] == 'alreadyrolled' || $errors[0][0] == 'cantrollback' )
102 ) {
103 $this->getOutput()->setPageTitle( $this->msg( 'rollbackfailed' ) );
104 $errArray = $errors[0];
105 $errMsg = array_shift( $errArray );
106 $this->getOutput()->addWikiMsgArray( $errMsg, $errArray );
107
108 if ( isset( $data['current'] ) ) {
109 /** @var Revision $current */
110 $current = $data['current'];
111
112 if ( $current->getComment() != '' ) {
113 $this->getOutput()->addHTML( $this->msg( 'editcomment' )->rawParams(
114 Linker::formatComment( $current->getComment() ) )->parse() );
115 }
116 }
117
118 return true;
119 }
120
121 # NOTE: Permission errors already handled by Action::checkExecute.
122 if ( $errors == [ [ 'readonlytext' ] ] ) {
123 throw new ReadOnlyError;
124 }
125
126 # XXX: Would be nice if ErrorPageError could take multiple errors, and/or a status object.
127 # Right now, we only show the first error
128 foreach ( $errors as $error ) {
129 throw new ErrorPageError( 'rollbackfailed', $error[0], array_slice( $error, 1 ) );
130 }
131
132 /** @var Revision $current */
133 $current = $data['current'];
134 $target = $data['target'];
135 $newId = $data['newid'];
136 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
137 $this->getOutput()->setRobotPolicy( 'noindex,nofollow' );
138
139 $old = Linker::revUserTools( $current );
140 $new = Linker::revUserTools( $target );
141 $this->getOutput()->addHTML( $this->msg( 'rollback-success' )->rawParams( $old, $new )
142 ->parseAsBlock() );
143
144 if ( $user->getBoolOption( 'watchrollback' ) ) {
145 $user->addWatch( $this->page->getTitle(), User::IGNORE_USER_RIGHTS );
146 }
147
148 $this->getOutput()->returnToMain( false, $this->getTitle() );
149
150 if ( !$request->getBool( 'hidediff', false ) &&
151 !$this->getUser()->getBoolOption( 'norollbackdiff' )
152 ) {
153 $contentHandler = $current->getContentHandler();
154 $de = $contentHandler->createDifferenceEngine(
155 $this->getContext(),
156 $current->getId(),
157 $newId,
158 false,
159 true
160 );
161 $de->showDiff( '', '' );
162 }
163 return true;
164 }
165
166 public function onSuccess() {
167 // Required by parent class, but redundant because onSubmit already shows
168 // the success message when needed.
169 }
170
171 protected function getDescription() {
172 return '';
173 }
174
175 public function doesWrites() {
176 return true;
177 }
178 }