| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use MediaWiki\CommentFormatter\CommentFormatter; |
| 24 |
use MediaWiki\Config\ConfigException; |
| 25 |
use MediaWiki\Content\IContentHandlerFactory; |
| 26 |
use MediaWiki\Context\IContextSource; |
| 27 |
use MediaWiki\Deferred\DeferredUpdates; |
| 28 |
use MediaWiki\HTMLForm\HTMLForm; |
| 29 |
use MediaWiki\Linker\Linker; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MediaWiki\MediaWikiServices; |
| 32 |
use MediaWiki\Message\Message; |
| 33 |
use MediaWiki\Page\RollbackPageFactory; |
| 34 |
use MediaWiki\Revision\RevisionRecord; |
| 35 |
use MediaWiki\Revision\SlotRecord; |
| 36 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 37 |
use MediaWiki\Watchlist\WatchlistManager; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class RollbackAction extends FormAction { |
| 45 |
|
| 46 |
private IContentHandlerFactory $contentHandlerFactory; |
| 47 |
private RollbackPageFactory $rollbackPageFactory; |
| 48 |
private UserOptionsLookup $userOptionsLookup; |
| 49 |
private WatchlistManager $watchlistManager; |
| 50 |
private CommentFormatter $commentFormatter; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function __construct( |
| 62 |
Article $article, |
| 63 |
IContextSource $context, |
| 64 |
IContentHandlerFactory $contentHandlerFactory, |
| 65 |
RollbackPageFactory $rollbackPageFactory, |
| 66 |
UserOptionsLookup $userOptionsLookup, |
| 67 |
WatchlistManager $watchlistManager, |
| 68 |
CommentFormatter $commentFormatter |
| 69 |
) { |
| 70 |
parent::__construct( $article, $context ); |
| 71 |
$this->contentHandlerFactory = $contentHandlerFactory; |
| 72 |
$this->rollbackPageFactory = $rollbackPageFactory; |
| 73 |
$this->userOptionsLookup = $userOptionsLookup; |
| 74 |
$this->watchlistManager = $watchlistManager; |
| 75 |
$this->commentFormatter = $commentFormatter; |
| 76 |
} |
| 77 |
|
| 78 |
public function getName() { |
| 79 |
return 'rollback'; |
| 80 |
} |
| 81 |
|
| 82 |
public function getRestriction() { |
| 83 |
return 'rollback'; |
| 84 |
} |
| 85 |
|
| 86 |
protected function usesOOUI() { |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
protected function getDescription() { |
| 91 |
return ''; |
| 92 |
} |
| 93 |
|
| 94 |
public function doesWrites() { |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
public function onSuccess() { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
public function onSubmit( $data ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
protected function alterForm( HTMLForm $form ) { |
| 107 |
$form->setWrapperLegendMsg( 'confirm-rollback-top' ); |
| 108 |
$form->setSubmitTextMsg( 'confirm-rollback-button' ); |
| 109 |
$form->setTokenSalt( 'rollback' ); |
| 110 |
|
| 111 |
$from = $this->getRequest()->getVal( 'from' ); |
| 112 |
if ( $from === null ) { |
| 113 |
throw new BadRequestError( 'rollbackfailed', 'rollback-missingparam' ); |
| 114 |
} |
| 115 |
foreach ( [ 'from', 'bot', 'hidediff', 'summary', 'token' ] as $param ) { |
| 116 |
$val = $this->getRequest()->getVal( $param ); |
| 117 |
if ( $val !== null ) { |
| 118 |
$form->addHiddenField( $param, $val ); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function show() { |
| 129 |
$this->setHeaders(); |
| 130 |
|
| 131 |
$this->checkCanExecute( $this->getUser() ); |
| 132 |
|
| 133 |
if ( !$this->userOptionsLookup->getOption( $this->getUser(), 'showrollbackconfirmation' ) || |
| 134 |
$this->getRequest()->wasPosted() |
| 135 |
) { |
| 136 |
$this->handleRollbackRequest(); |
| 137 |
} else { |
| 138 |
$this->showRollbackConfirmationForm(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public function handleRollbackRequest() { |
| 143 |
$this->enableTransactionalTimelimit(); |
| 144 |
$this->getOutput()->addModuleStyles( 'mediawiki.interface.helpers.styles' ); |
| 145 |
|
| 146 |
$request = $this->getRequest(); |
| 147 |
$user = $this->getUser(); |
| 148 |
$from = $request->getVal( 'from' ); |
| 149 |
$rev = $this->getWikiPage()->getRevisionRecord(); |
| 150 |
if ( $from === null ) { |
| 151 |
throw new ErrorPageError( 'rollbackfailed', 'rollback-missingparam' ); |
| 152 |
} |
| 153 |
if ( !$rev ) { |
| 154 |
throw new ErrorPageError( 'rollbackfailed', 'rollback-missingrevision' ); |
| 155 |
} |
| 156 |
|
| 157 |
$revUser = $rev->getUser(); |
| 158 |
$userText = $revUser ? $revUser->getName() : ''; |
| 159 |
if ( $from !== $userText ) { |
| 160 |
throw new ErrorPageError( 'rollbackfailed', 'alreadyrolled', [ |
| 161 |
$this->getTitle()->getPrefixedText(), |
| 162 |
wfEscapeWikiText( $from ), |
| 163 |
$userText |
| 164 |
] ); |
| 165 |
} |
| 166 |
|
| 167 |
if ( !$user->matchEditToken( $request->getVal( 'token' ), 'rollback' ) ) { |
| 168 |
throw new ErrorPageError( 'sessionfailure-title', 'sessionfailure' ); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
if ( !$revUser ) { |
| 175 |
if ( $this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) { |
| 176 |
$revUser = $rev->getUser( RevisionRecord::RAW ); |
| 177 |
} else { |
| 178 |
$userFactory = MediaWikiServices::getInstance()->getUserFactory(); |
| 179 |
$revUser = $userFactory->newFromName( $this->context->msg( 'rev-deleted-user' )->plain() ); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
$rollbackResult = $this->rollbackPageFactory |
| 184 |
|
| 185 |
->newRollbackPage( $this->getWikiPage(), $this->getAuthority(), $revUser ) |
| 186 |
->setSummary( $request->getText( 'summary' ) ) |
| 187 |
->markAsBot( $request->getBool( 'bot' ) ) |
| 188 |
->rollbackIfAllowed(); |
| 189 |
$data = $rollbackResult->getValue(); |
| 190 |
|
| 191 |
if ( $rollbackResult->hasMessage( 'actionthrottledtext' ) ) { |
| 192 |
throw new ThrottledError; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
if ( $rollbackResult->hasMessage( 'readonlytext' ) ) { |
| 197 |
throw new ReadOnlyError; |
| 198 |
} |
| 199 |
|
| 200 |
if ( $rollbackResult->getMessages() ) { |
| 201 |
$this->getOutput()->setPageTitleMsg( $this->msg( 'rollbackfailed' ) ); |
| 202 |
|
| 203 |
foreach ( $rollbackResult->getMessages() as $msg ) { |
| 204 |
$this->getOutput()->addWikiMsg( $msg ); |
| 205 |
} |
| 206 |
|
| 207 |
if ( |
| 208 |
( $rollbackResult->hasMessage( 'alreadyrolled' ) || $rollbackResult->hasMessage( 'cantrollback' ) ) |
| 209 |
&& isset( $data['current-revision-record'] ) |
| 210 |
) { |
| 211 |
|
| 212 |
$current = $data['current-revision-record']; |
| 213 |
|
| 214 |
if ( $current->getComment() != null ) { |
| 215 |
$this->getOutput()->addWikiMsg( |
| 216 |
'editcomment', |
| 217 |
Message::rawParam( |
| 218 |
$this->commentFormatter |
| 219 |
->format( $current->getComment()->text ) |
| 220 |
) |
| 221 |
); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
$current = $data['current-revision-record']; |
| 230 |
$target = $data['target-revision-record']; |
| 231 |
$newId = $data['newid']; |
| 232 |
$this->getOutput()->setPageTitleMsg( $this->msg( 'actioncomplete' ) ); |
| 233 |
$this->getOutput()->setRobotPolicy( 'noindex,nofollow' ); |
| 234 |
|
| 235 |
$old = Linker::revUserTools( $current ); |
| 236 |
$new = Linker::revUserTools( $target ); |
| 237 |
|
| 238 |
$currentUser = $current->getUser( RevisionRecord::FOR_THIS_USER, $user ); |
| 239 |
$targetUser = $target->getUser( RevisionRecord::FOR_THIS_USER, $user ); |
| 240 |
$this->getOutput()->addHTML( |
| 241 |
$this->msg( 'rollback-success' ) |
| 242 |
->rawParams( $old, $new ) |
| 243 |
->params( $currentUser ? $currentUser->getName() : '' ) |
| 244 |
->params( $targetUser ? $targetUser->getName() : '' ) |
| 245 |
->parseAsBlock() |
| 246 |
); |
| 247 |
|
| 248 |
|
| 249 |
$this->getOutput()->addModules( 'mediawiki.misc-authed-curate' ); |
| 250 |
|
| 251 |
|
| 252 |
$this->getOutput()->addJsConfigVars( [ |
| 253 |
'wgRollbackSuccess' => true, |
| 254 |
|
| 255 |
|
| 256 |
'wgPostEditConfirmationDisabled' => true, |
| 257 |
] ); |
| 258 |
|
| 259 |
if ( $this->userOptionsLookup->getBoolOption( $user, 'watchrollback' ) ) { |
| 260 |
$this->watchlistManager->addWatchIgnoringRights( $user, $this->getTitle() ); |
| 261 |
} |
| 262 |
|
| 263 |
$this->getOutput()->returnToMain( false, $this->getTitle() ); |
| 264 |
|
| 265 |
if ( !$request->getBool( 'hidediff', false ) && |
| 266 |
!$this->userOptionsLookup->getBoolOption( $this->getUser(), 'norollbackdiff' ) |
| 267 |
) { |
| 268 |
$contentModel = $current->getSlot( SlotRecord::MAIN, RevisionRecord::RAW ) |
| 269 |
->getModel(); |
| 270 |
$contentHandler = $this->contentHandlerFactory->getContentHandler( $contentModel ); |
| 271 |
$de = $contentHandler->createDifferenceEngine( |
| 272 |
$this->getContext(), |
| 273 |
$current->getId(), |
| 274 |
$newId, |
| 275 |
0, |
| 276 |
true |
| 277 |
); |
| 278 |
$de->showDiff( '', '' ); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
private function enableTransactionalTimelimit() { |
| 287 |
|
| 288 |
wfTransactionalTimeLimit(); |
| 289 |
if ( !$this->getRequest()->wasPosted() ) { |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
$fname = __METHOD__; |
| 295 |
$trxLimits = $this->context->getConfig()->get( MainConfigNames::TrxProfilerLimits ); |
| 296 |
$trxProfiler = Profiler::instance()->getTransactionProfiler(); |
| 297 |
$trxProfiler->redefineExpectations( $trxLimits['POST'], $fname ); |
| 298 |
DeferredUpdates::addCallableUpdate( static function () use ( $trxProfiler, $trxLimits, $fname |
| 299 |
) { |
| 300 |
$trxProfiler->redefineExpectations( $trxLimits['PostSend-POST'], $fname ); |
| 301 |
} ); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
private function showRollbackConfirmationForm() { |
| 306 |
$form = $this->getForm(); |
| 307 |
if ( $form->show() ) { |
| 308 |
$this->onSuccess(); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
protected function getFormFields() { |
| 313 |
return [ |
| 314 |
'intro' => [ |
| 315 |
'type' => 'info', |
| 316 |
'raw' => true, |
| 317 |
'default' => $this->msg( 'confirm-rollback-bottom' )->parse() |
| 318 |
] |
| 319 |
]; |
| 320 |
} |
| 321 |
} |