MediaWiki REL1_35
RollbackAction.php
Go to the documentation of this file.
1<?php
26
33
34 public function getName() {
35 return 'rollback';
36 }
37
38 public function getRestriction() {
39 return 'rollback';
40 }
41
42 protected function usesOOUI() {
43 return true;
44 }
45
46 protected function getDescription() {
47 return '';
48 }
49
50 public function doesWrites() {
51 return true;
52 }
53
54 public function onSuccess() {
55 return false;
56 }
57
58 public function onSubmit( $data ) {
59 return false;
60 }
61
62 protected function alterForm( HTMLForm $form ) {
63 $form->setWrapperLegendMsg( 'confirm-rollback-top' );
64 $form->setSubmitTextMsg( 'confirm-rollback-button' );
65 $form->setTokenSalt( 'rollback' );
66
67 $from = $this->getRequest()->getVal( 'from' );
68 if ( $from === null ) {
69 throw new BadRequestError( 'rollbackfailed', 'rollback-missingparam' );
70 }
71 foreach ( [ 'from', 'bot', 'hidediff', 'summary', 'token' ] as $param ) {
72 $val = $this->getRequest()->getVal( $param );
73 if ( $val !== null ) {
74 $form->addHiddenField( $param, $val );
75 }
76 }
77 }
78
84 public function show() {
85 $this->setHeaders();
86 // This will throw exceptions if there's a problem
87 $this->checkCanExecute( $this->getUser() );
88
89 if ( $this->getUser()->getOption( 'showrollbackconfirmation' ) == false ||
90 $this->getRequest()->wasPosted() ) {
91 $this->handleRollbackRequest();
92 } else {
94 }
95 }
96
97 public function handleRollbackRequest() {
99
100 $request = $this->getRequest();
101 $user = $this->getUser();
102 $from = $request->getVal( 'from' );
103 $rev = $this->getWikiPage()->getRevisionRecord();
104 if ( $from === null ) {
105 throw new ErrorPageError( 'rollbackfailed', 'rollback-missingparam' );
106 }
107 if ( !$rev ) {
108 throw new ErrorPageError( 'rollbackfailed', 'rollback-missingrevision' );
109 }
110
111 $revUser = $rev->getUser();
112 $userText = $revUser ? $revUser->getName() : '';
113 if ( $from !== $userText ) {
114 throw new ErrorPageError( 'rollbackfailed', 'alreadyrolled', [
115 $this->getTitle()->getPrefixedText(),
116 wfEscapeWikiText( $from ),
117 $userText
118 ] );
119 }
120
121 $data = null;
122 $errors = $this->getWikiPage()->doRollback(
123 $from,
124 $request->getText( 'summary' ),
125 $request->getVal( 'token' ),
126 $request->getBool( 'bot' ),
127 $data,
128 $this->getUser()
129 );
130
131 if ( in_array( [ 'actionthrottledtext' ], $errors ) ) {
132 throw new ThrottledError;
133 }
134
135 if ( $this->hasRollbackRelatedErrors( $errors ) ) {
136 $this->getOutput()->setPageTitle( $this->msg( 'rollbackfailed' ) );
137 $errArray = $errors[0];
138 $errMsg = array_shift( $errArray );
139 $this->getOutput()->addWikiMsgArray( $errMsg, $errArray );
140
141 if ( isset( $data['current-revision-record'] ) ) {
143 $current = $data['current-revision-record'];
144
145 if ( $current->getComment() != null ) {
146 $this->getOutput()->addWikiMsg(
147 'editcomment',
150 $current->getComment()->text
151 )
152 )
153 );
154 }
155 }
156
157 return;
158 }
159
160 # NOTE: Permission errors already handled by Action::checkExecute.
161 if ( $errors == [ [ 'readonlytext' ] ] ) {
162 throw new ReadOnlyError;
163 }
164
165 # XXX: Would be nice if ErrorPageError could take multiple errors, and/or a status object.
166 # Right now, we only show the first error
167 foreach ( $errors as $error ) {
168 throw new ErrorPageError( 'rollbackfailed', $error[0], array_slice( $error, 1 ) );
169 }
170
172 $current = $data['current-revision-record'];
173 $target = $data['target-revision-record'];
174 $newId = $data['newid'];
175 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
176 $this->getOutput()->setRobotPolicy( 'noindex,nofollow' );
177
178 $old = Linker::revUserTools( $current );
179 $new = Linker::revUserTools( $target );
180
181 $currentUser = $current->getUser( RevisionRecord::FOR_THIS_USER, $user );
182 $targetUser = $target->getUser( RevisionRecord::FOR_THIS_USER, $user );
183 $this->getOutput()->addHTML(
184 $this->msg( 'rollback-success' )
185 ->rawParams( $old, $new )
186 ->params( $currentUser ? $currentUser->getName() : '' )
187 ->params( $targetUser ? $targetUser->getName() : '' )
188 ->parseAsBlock()
189 );
190
191 if ( $user->getBoolOption( 'watchrollback' ) ) {
192 $user->addWatch( $this->getTitle(), User::IGNORE_USER_RIGHTS );
193 }
194
195 $this->getOutput()->returnToMain( false, $this->getTitle() );
196
197 if ( !$request->getBool( 'hidediff', false ) &&
198 !$this->getUser()->getBoolOption( 'norollbackdiff' )
199 ) {
200 $contentModel = $current->getSlot( SlotRecord::MAIN, RevisionRecord::RAW )
201 ->getModel();
202 $contentHandler = MediaWikiServices::getInstance()
203 ->getContentHandlerFactory()
204 ->getContentHandler( $contentModel );
205 $de = $contentHandler->createDifferenceEngine(
206 $this->getContext(),
207 $current->getId(),
208 $newId,
209 false,
210 true
211 );
212 $de->showDiff( '', '' );
213 }
214 }
215
220 private function enableTransactionalTimelimit() {
221 // If Rollbacks are made POST-only, use $this->useTransactionalTimeLimit()
223 if ( !$this->getRequest()->wasPosted() ) {
228 $fname = __METHOD__;
229 $trxLimits = $this->context->getConfig()->get( 'TrxProfilerLimits' );
230 $trxProfiler = Profiler::instance()->getTransactionProfiler();
231 $trxProfiler->redefineExpectations( $trxLimits['POST'], $fname );
232 DeferredUpdates::addCallableUpdate( function () use ( $trxProfiler, $trxLimits, $fname
233 ) {
234 $trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname );
235 } );
236 }
237 }
238
239 private function showRollbackConfirmationForm() {
240 $form = $this->getForm();
241 if ( $form->show() ) {
242 $this->onSuccess();
243 }
244 }
245
246 protected function getFormFields() {
247 return [
248 'intro' => [
249 'type' => 'info',
250 'vertical-label' => true,
251 'raw' => true,
252 'default' => $this->msg( 'confirm-rollback-bottom' )->parse()
253 ]
254 ];
255 }
256
257 private function hasRollbackRelatedErrors( array $errors ) {
258 return isset( $errors[0][0] ) &&
259 ( $errors[0][0] == 'alreadyrolled' ||
260 $errors[0][0] == 'cantrollback'
261 );
262 }
263}
wfTransactionalTimeLimit()
Set PHP's time limit to the larger of php.ini or $wgTransactionalTimeLimit.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
getWikiPage()
Get a WikiPage object.
Definition Action.php:278
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition Action.php:421
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:299
getContext()
Get the IContextSource in use here.
Definition Action.php:215
getOutput()
Get the OutputPage being used for this instance.
Definition Action.php:239
getUser()
Shortcut to get the User being used for this instance.
Definition Action.php:249
setHeaders()
Set output headers for noindexing etc.
Definition Action.php:482
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:311
getRequest()
Get the WebRequest being used for this instance.
Definition Action.php:229
An error page that emits an HTTP 400 Bad Request status code.
An error page which can definitely be safely rendered using the OutputPage.
An action which shows a form and does something based on the input from the form.
getForm()
Get the HTMLForm to control behavior Stable to override.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:135
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
addHiddenField( $name, $value, array $attribs=[])
Add a hidden field to the output.
Definition HTMLForm.php:957
setTokenSalt( $salt)
Set the salt for the edit token.
static revUserTools( $rev, $isPublic=false, $useParentheses=true)
Generate a user tool link cluster if the current user is allowed to view it.
Definition Linker.php:1152
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition Linker.php:1209
MediaWikiServices is the service locator for the application scope of MediaWiki.
Page revision base class.
Value object representing a content slot associated with a page revision.
static rawParam( $raw)
Definition Message.php:1053
Show an error when the wiki is locked/read-only and the user tries to do something that requires writ...
User interface for the rollback action.
getFormFields()
Get an HTMLForm descriptor array Stable to override.
getDescription()
Returns the description that goes below the <h1> tag.
enableTransactionalTimelimit()
Enables transactional time limit for POST and GET requests to RollbackAction.
usesOOUI()
Whether the form should use OOUI Stable to override.
onSubmit( $data)
Process the form on POST submission.
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially Stable to override.
getRestriction()
Get the permission required to perform this action.
onSuccess()
Do something exciting on successful processing of the form.
hasRollbackRelatedErrors(array $errors)
doesWrites()
Stable to override.
getName()
Return the name of the action this object responds to.
Show an error when the user hits a rate limit.
const IGNORE_USER_RIGHTS
Definition User.php:94