3e6d4028a45196d2ab2ecd9b627d0c0342e86ce5
[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 FormlessAction {
29
30 public function getName() {
31 return 'rollback';
32 }
33
34 public function getRestriction() {
35 return 'rollback';
36 }
37
38 /**
39 * Temporarily unused message keys due to T88044/T136375:
40 * - confirm-rollback-top
41 * - confirm-rollback-button
42 * - rollbackfailed
43 * - rollback-missingparam
44 * - rollback-success-notify
45 */
46
47 /**
48 * @throws ErrorPageError
49 */
50 public function onView() {
51 // TODO: use $this->useTransactionalTimeLimit(); when POST only
52 wfTransactionalTimeLimit();
53
54 $request = $this->getRequest();
55 $user = $this->getUser();
56 $from = $request->getVal( 'from' );
57 $rev = $this->page->getRevision();
58 if ( $from === null ) {
59 throw new ErrorPageError( 'rollbackfailed', 'rollback-missingparam' );
60 }
61 if ( !$rev ) {
62 throw new ErrorPageError( 'rollbackfailed', 'rollback-missingrevision' );
63 }
64 if ( $from !== $rev->getUserText() ) {
65 throw new ErrorPageError( 'rollbackfailed', 'alreadyrolled', [
66 $this->getTitle()->getPrefixedText(),
67 $from,
68 $rev->getUserText()
69 ] );
70 }
71
72 // @TODO: remove this hack once rollback uses POST (T88044)
73 $fname = __METHOD__;
74 $trxLimits = $this->context->getConfig()->get( 'TrxProfilerLimits' );
75 $trxProfiler = Profiler::instance()->getTransactionProfiler();
76 $trxProfiler->setExpectations( $trxLimits['POST'], $fname );
77 DeferredUpdates::addCallableUpdate( function () use ( $trxProfiler, $trxLimits, $fname ) {
78 $trxProfiler->setExpectations( $trxLimits['PostSend-POST'], $fname );
79 } );
80
81 $data = null;
82 $errors = $this->page->doRollback(
83 $from,
84 $request->getText( 'summary' ),
85 $request->getVal( 'token' ),
86 $request->getBool( 'bot' ),
87 $data,
88 $this->getUser()
89 );
90
91 if ( in_array( [ 'actionthrottledtext' ], $errors ) ) {
92 throw new ThrottledError;
93 }
94
95 if ( isset( $errors[0][0] ) &&
96 ( $errors[0][0] == 'alreadyrolled' || $errors[0][0] == 'cantrollback' )
97 ) {
98 $this->getOutput()->setPageTitle( $this->msg( 'rollbackfailed' ) );
99 $errArray = $errors[0];
100 $errMsg = array_shift( $errArray );
101 $this->getOutput()->addWikiMsgArray( $errMsg, $errArray );
102
103 if ( isset( $data['current'] ) ) {
104 /** @var Revision $current */
105 $current = $data['current'];
106
107 if ( $current->getComment() != '' ) {
108 $this->getOutput()->addWikiMsg(
109 'editcomment',
110 Message::rawParam(
111 Linker::formatComment( $current->getComment() )
112 )
113 );
114 }
115 }
116
117 return;
118 }
119
120 # NOTE: Permission errors already handled by Action::checkExecute.
121 if ( $errors == [ [ 'readonlytext' ] ] ) {
122 throw new ReadOnlyError;
123 }
124
125 # XXX: Would be nice if ErrorPageError could take multiple errors, and/or a status object.
126 # Right now, we only show the first error
127 foreach ( $errors as $error ) {
128 throw new ErrorPageError( 'rollbackfailed', $error[0], array_slice( $error, 1 ) );
129 }
130
131 /** @var Revision $current */
132 $current = $data['current'];
133 $target = $data['target'];
134 $newId = $data['newid'];
135 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
136 $this->getOutput()->setRobotPolicy( 'noindex,nofollow' );
137
138 $old = Linker::revUserTools( $current );
139 $new = Linker::revUserTools( $target );
140 $this->getOutput()->addHTML(
141 $this->msg( 'rollback-success' )
142 ->rawParams( $old, $new )
143 ->params( $current->getUserText( Revision::FOR_THIS_USER, $user ) )
144 ->params( $target->getUserText( Revision::FOR_THIS_USER, $user ) )
145 ->parseAsBlock()
146 );
147
148 if ( $user->getBoolOption( 'watchrollback' ) ) {
149 $user->addWatch( $this->page->getTitle(), User::IGNORE_USER_RIGHTS );
150 }
151
152 $this->getOutput()->returnToMain( false, $this->getTitle() );
153
154 if ( !$request->getBool( 'hidediff', false ) &&
155 !$this->getUser()->getBoolOption( 'norollbackdiff' )
156 ) {
157 $contentHandler = $current->getContentHandler();
158 $de = $contentHandler->createDifferenceEngine(
159 $this->getContext(),
160 $current->getId(),
161 $newId,
162 false,
163 true
164 );
165 $de->showDiff( '', '' );
166 }
167 }
168
169 protected function getDescription() {
170 return '';
171 }
172
173 public function doesWrites() {
174 return true;
175 }
176 }