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