Merge "rdbms: add transaction comment to IDatabase::masterPosWait()"
[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 $trxLimits = $this->context->getConfig()->get( 'TrxProfilerLimits' );
74 $trxProfiler = Profiler::instance()->getTransactionProfiler();
75 $trxProfiler->setExpectations( $trxLimits['POST'], __METHOD__ );
76
77 $data = null;
78 $errors = $this->page->doRollback(
79 $from,
80 $request->getText( 'summary' ),
81 $request->getVal( 'token' ),
82 $request->getBool( 'bot' ),
83 $data,
84 $this->getUser()
85 );
86
87 if ( in_array( [ 'actionthrottledtext' ], $errors ) ) {
88 throw new ThrottledError;
89 }
90
91 if ( isset( $errors[0][0] ) &&
92 ( $errors[0][0] == 'alreadyrolled' || $errors[0][0] == 'cantrollback' )
93 ) {
94 $this->getOutput()->setPageTitle( $this->msg( 'rollbackfailed' ) );
95 $errArray = $errors[0];
96 $errMsg = array_shift( $errArray );
97 $this->getOutput()->addWikiMsgArray( $errMsg, $errArray );
98
99 if ( isset( $data['current'] ) ) {
100 /** @var Revision $current */
101 $current = $data['current'];
102
103 if ( $current->getComment() != '' ) {
104 $this->getOutput()->addHTML( $this->msg( 'editcomment' )->rawParams(
105 Linker::formatComment( $current->getComment() ) )->parse() );
106 }
107 }
108
109 return;
110 }
111
112 # NOTE: Permission errors already handled by Action::checkExecute.
113 if ( $errors == [ [ 'readonlytext' ] ] ) {
114 throw new ReadOnlyError;
115 }
116
117 # XXX: Would be nice if ErrorPageError could take multiple errors, and/or a status object.
118 # Right now, we only show the first error
119 foreach ( $errors as $error ) {
120 throw new ErrorPageError( 'rollbackfailed', $error[0], array_slice( $error, 1 ) );
121 }
122
123 /** @var Revision $current */
124 $current = $data['current'];
125 $target = $data['target'];
126 $newId = $data['newid'];
127 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
128 $this->getOutput()->setRobotPolicy( 'noindex,nofollow' );
129
130 $old = Linker::revUserTools( $current );
131 $new = Linker::revUserTools( $target );
132 $this->getOutput()->addHTML(
133 $this->msg( 'rollback-success' )
134 ->rawParams( $old, $new )
135 ->params( $current->getUserText( Revision::FOR_THIS_USER, $user ) )
136 ->params( $target->getUserText( Revision::FOR_THIS_USER, $user ) )
137 ->parseAsBlock()
138 );
139
140 if ( $user->getBoolOption( 'watchrollback' ) ) {
141 $user->addWatch( $this->page->getTitle(), User::IGNORE_USER_RIGHTS );
142 }
143
144 $this->getOutput()->returnToMain( false, $this->getTitle() );
145
146 if ( !$request->getBool( 'hidediff', false ) &&
147 !$this->getUser()->getBoolOption( 'norollbackdiff' )
148 ) {
149 $contentHandler = $current->getContentHandler();
150 $de = $contentHandler->createDifferenceEngine(
151 $this->getContext(),
152 $current->getId(),
153 $newId,
154 false,
155 true
156 );
157 $de->showDiff( '', '' );
158 }
159 }
160
161 protected function getDescription() {
162 return '';
163 }
164
165 public function doesWrites() {
166 return true;
167 }
168 }