3 * Temporary action for MCR undos
8 use MediaWiki\MediaWikiServices
;
9 use MediaWiki\Revision\MutableRevisionRecord
;
10 use MediaWiki\Revision\RevisionRecord
;
11 use MediaWiki\Revision\SlotRecord
;
14 * Temporary action for MCR undos
16 * This is intended to go away when real MCR support is added to EditPage and
17 * the standard undo-with-edit behavior can be implemented there instead.
19 * If this were going to be kept, we'd probably want to figure out a good way
20 * to reuse the same code for generating the headers, summary box, and buttons
21 * on EditPage and here, and to better share the diffing and preview logic
22 * between the two. But doing that now would require much of the rewriting of
23 * EditPage that we're trying to put off by doing this instead.
27 * @deprecated since 1.32
29 class McrUndoAction
extends FormAction
{
31 protected $undo = 0, $undoafter = 0, $cur = 0;
33 /** @param RevisionRecord|null */
34 protected $curRev = null;
36 public function getName() {
40 public function getDescription() {
44 public function show() {
45 // Send a cookie so anons get talk message notifications
46 // (copied from SubmitAction)
47 MediaWiki\Session\SessionManager
::getGlobalSession()->persist();
49 // Some stuff copied from EditAction
50 $this->useTransactionalTimeLimit();
52 $out = $this->getOutput();
53 $out->setRobotPolicy( 'noindex,nofollow' );
54 if ( $this->getContext()->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
55 $out->addModuleStyles( [
57 'mediawiki.ui.checkbox',
61 // IP warning headers copied from EditPage
62 // (should more be copied?)
65 "<div id=\"mw-read-only-warning\">\n$1\n</div>",
66 [ 'readonlywarning', wfReadOnlyReason() ]
68 } elseif ( $this->context
->getUser()->isAnon() ) {
69 if ( !$this->getRequest()->getCheck( 'wpPreview' ) ) {
71 "<div id='mw-anon-edit-warning' class='warningbox'>\n$1\n</div>",
74 SpecialPage
::getTitleFor( 'Userlogin' )->getFullURL( [
75 'returnto' => $this->getTitle()->getPrefixedDBkey()
78 SpecialPage
::getTitleFor( 'CreateAccount' )->getFullURL( [
79 'returnto' => $this->getTitle()->getPrefixedDBkey()
84 $out->wrapWikiMsg( "<div id=\"mw-anon-preview-warning\" class=\"warningbox\">\n$1</div>",
93 protected function initFromParameters() {
94 $this->undoafter
= $this->getRequest()->getInt( 'undoafter' );
95 $this->undo
= $this->getRequest()->getInt( 'undo' );
97 if ( $this->undo
== 0 ||
$this->undoafter
== 0 ) {
98 throw new ErrorPageError( 'mcrundofailed', 'mcrundo-missingparam' );
101 $curRev = $this->page
->getRevision();
103 throw new ErrorPageError( 'mcrundofailed', 'nopagetext' );
105 $this->curRev
= $curRev->getRevisionRecord();
106 $this->cur
= $this->getRequest()->getInt( 'cur', $this->curRev
->getId() );
109 protected function checkCanExecute( User
$user ) {
110 parent
::checkCanExecute( $user );
112 $this->initFromParameters();
114 $revisionLookup = MediaWikiServices
::getInstance()->getRevisionLookup();
116 $undoRev = $revisionLookup->getRevisionById( $this->undo
);
117 $oldRev = $revisionLookup->getRevisionById( $this->undoafter
);
119 if ( $undoRev === null ||
$oldRev === null ||
120 $undoRev->isDeleted( RevisionRecord
::DELETED_TEXT
) ||
121 $oldRev->isDeleted( RevisionRecord
::DELETED_TEXT
)
123 throw new ErrorPageError( 'mcrundofailed', 'undo-norev' );
130 * @return MutableRevisionRecord
132 private function getNewRevision() {
133 $revisionLookup = MediaWikiServices
::getInstance()->getRevisionLookup();
135 $undoRev = $revisionLookup->getRevisionById( $this->undo
);
136 $oldRev = $revisionLookup->getRevisionById( $this->undoafter
);
137 $curRev = $this->curRev
;
139 $isLatest = $curRev->getId() === $undoRev->getId();
141 if ( $undoRev === null ||
$oldRev === null ||
142 $undoRev->isDeleted( RevisionRecord
::DELETED_TEXT
) ||
143 $oldRev->isDeleted( RevisionRecord
::DELETED_TEXT
)
145 throw new ErrorPageError( 'mcrundofailed', 'undo-norev' );
149 // Short cut! Undoing the current revision means we just restore the old.
150 return MutableRevisionRecord
::newFromParentRevision( $oldRev );
153 $newRev = MutableRevisionRecord
::newFromParentRevision( $curRev );
155 // Figure out the roles that need merging by first collecting all roles
156 // and then removing the ones that don't.
157 $rolesToMerge = array_unique( array_merge(
158 $oldRev->getSlotRoles(),
159 $undoRev->getSlotRoles(),
160 $curRev->getSlotRoles()
163 // Any roles with the same content in $oldRev and $undoRev can be
164 // inherited because undo won't change them.
165 $rolesToMerge = array_intersect(
166 $rolesToMerge, $oldRev->getSlots()->getRolesWithDifferentContent( $undoRev->getSlots() )
168 if ( !$rolesToMerge ) {
169 throw new ErrorPageError( 'mcrundofailed', 'undo-nochange' );
172 // Any roles with the same content in $oldRev and $curRev were already reverted
173 // and so can be inherited.
174 $rolesToMerge = array_intersect(
175 $rolesToMerge, $oldRev->getSlots()->getRolesWithDifferentContent( $curRev->getSlots() )
177 if ( !$rolesToMerge ) {
178 throw new ErrorPageError( 'mcrundofailed', 'undo-nochange' );
181 // Any roles with the same content in $undoRev and $curRev weren't
182 // changed since and so can be reverted to $oldRev.
183 $diffRoles = array_intersect(
184 $rolesToMerge, $undoRev->getSlots()->getRolesWithDifferentContent( $curRev->getSlots() )
186 foreach ( array_diff( $rolesToMerge, $diffRoles ) as $role ) {
187 if ( $oldRev->hasSlot( $role ) ) {
188 $newRev->inheritSlot( $oldRev->getSlot( $role, RevisionRecord
::RAW
) );
190 $newRev->removeSlot( $role );
193 $rolesToMerge = $diffRoles;
195 // Any slot additions or removals not handled by the above checks can't be undone.
196 // There will be only one of the three revisions missing the slot:
197 // - !old means it was added in the undone revisions and modified after.
198 // Should it be removed entirely for the undo, or should the modified version be kept?
199 // - !undo means it was removed in the undone revisions and then readded with different content.
200 // Which content is should be kept, the old or the new?
201 // - !cur means it was changed in the undone revisions and then deleted after.
202 // Did someone delete vandalized content instead of undoing (meaning we should ideally restore
203 // it), or should it stay gone?
204 foreach ( $rolesToMerge as $role ) {
205 if ( !$oldRev->hasSlot( $role ) ||
!$undoRev->hasSlot( $role ) ||
!$curRev->hasSlot( $role ) ) {
206 throw new ErrorPageError( 'mcrundofailed', 'undo-failure' );
210 // Try to merge anything that's left.
211 foreach ( $rolesToMerge as $role ) {
212 $oldContent = $oldRev->getSlot( $role, RevisionRecord
::RAW
)->getContent();
213 $undoContent = $undoRev->getSlot( $role, RevisionRecord
::RAW
)->getContent();
214 $curContent = $curRev->getSlot( $role, RevisionRecord
::RAW
)->getContent();
215 $newContent = $undoContent->getContentHandler()
216 ->getUndoContent( $curContent, $undoContent, $oldContent, $isLatest );
217 if ( !$newContent ) {
218 throw new ErrorPageError( 'mcrundofailed', 'undo-failure' );
220 $newRev->setSlot( SlotRecord
::newUnsaved( $role, $newContent ) );
226 private function generateDiffOrPreview() {
227 $newRev = $this->getNewRevision();
228 if ( $newRev->hasSameContent( $this->curRev
) ) {
229 throw new ErrorPageError( 'mcrundofailed', 'undo-nochange' );
232 $diffEngine = new DifferenceEngine( $this->context
);
233 $diffEngine->setRevisions( $this->curRev
, $newRev );
235 $oldtitle = $this->context
->msg( 'currentrev' )->parse();
236 $newtitle = $this->context
->msg( 'yourtext' )->parse();
238 if ( $this->getRequest()->getCheck( 'wpPreview' ) ) {
239 $this->showPreview( $newRev );
242 $diffText = $diffEngine->getDiff( $oldtitle, $newtitle );
243 $diffEngine->showDiffStyle();
244 return '<div id="wikiDiff">' . $diffText . '</div>';
248 private function showPreview( RevisionRecord
$rev ) {
249 // Mostly copied from EditPage::getPreviewText()
250 $out = $this->getOutput();
255 # provide a anchor link to the form
256 $continueEditing = '<span class="mw-continue-editing">' .
257 '[[#mw-mcrundo-form|' .
258 $this->context
->getLanguage()->getArrow() . ' ' .
259 $this->context
->msg( 'continue-editing' )->text() . ']]</span>';
261 $note = $this->context
->msg( 'previewnote' )->plain() . ' ' . $continueEditing;
263 $parserOptions = $this->page
->makeParserOptions( $this->context
);
264 $parserOptions->setIsPreview( true );
265 $parserOptions->setIsSectionPreview( false );
266 $parserOptions->enableLimitReport();
268 $parserOutput = MediaWikiServices
::getInstance()->getRevisionRenderer()
269 ->getRenderedRevision( $rev, $parserOptions, $this->context
->getUser() )
270 ->getRevisionParserOutput();
271 $previewHTML = $parserOutput->getText( [ 'enableSectionEditLinks' => false ] );
273 $out->addParserOutputMetadata( $parserOutput );
274 if ( count( $parserOutput->getWarnings() ) ) {
275 $note .= "\n\n" . implode( "\n\n", $parserOutput->getWarnings() );
277 } catch ( MWContentSerializationException
$ex ) {
278 $m = $this->context
->msg(
279 'content-failed-to-parse',
282 $note .= "\n\n" . $m->parse();
286 $previewhead = Html
::rawElement(
287 'div', [ 'class' => 'previewnote' ],
289 'h2', [ 'id' => 'mw-previewheader' ],
290 $this->context
->msg( 'preview' )->text()
292 $out->parseAsInterface( $note ) .
296 $pageViewLang = $this->getTitle()->getPageViewLanguage();
297 $attribs = [ 'lang' => $pageViewLang->getHtmlCode(), 'dir' => $pageViewLang->getDir(),
298 'class' => 'mw-content-' . $pageViewLang->getDir() ];
299 $previewHTML = Html
::rawElement( 'div', $attribs, $previewHTML );
301 $out->addHtml( $previewhead . $previewHTML );
304 public function onSubmit( $data ) {
305 global $wgUseRCPatrol;
307 if ( !$this->getRequest()->getCheck( 'wpSave' ) ) {
312 $updater = $this->page
->getPage()->newPageUpdater( $this->context
->getUser() );
313 $curRev = $updater->grabParentRevision();
315 throw new ErrorPageError( 'mcrundofailed', 'nopagetext' );
318 if ( $this->cur
!== $curRev->getId() ) {
319 return Status
::newFatal( 'mcrundo-changed' );
322 $newRev = $this->getNewRevision();
323 if ( !$newRev->hasSameContent( $curRev ) ) {
324 // Copy new slots into the PageUpdater, and remove any removed slots.
325 // TODO: This interface is awful, there should be a way to just pass $newRev.
326 // TODO: MCR: test this once we can store multiple slots
327 foreach ( $newRev->getSlots()->getSlots() as $slot ) {
328 $updater->setSlot( $slot );
330 foreach ( $curRev->getSlotRoles() as $role ) {
331 if ( !$newRev->hasSlot( $role ) ) {
332 $updater->removeSlot( $role );
336 $updater->setOriginalRevisionId( false );
337 $updater->setUndidRevisionId( $this->undo
);
340 if ( $wgUseRCPatrol && $this->getTitle()->userCan( 'autopatrol', $this->getUser() ) ) {
341 $updater->setRcPatrolStatus( RecentChange
::PRC_AUTOPATROLLED
);
344 $updater->saveRevision(
345 CommentStoreComment
::newUnsavedComment( trim( $this->getRequest()->getVal( 'wpSummary' ) ) ),
346 EDIT_AUTOSUMMARY | EDIT_UPDATE
349 return $updater->getStatus();
352 return Status
::newGood();
355 protected function usesOOUI() {
359 protected function getFormFields() {
360 $request = $this->getRequest();
364 'vertical-label' => true,
366 'default' => function () {
367 return $this->generateDiffOrPreview();
373 'name' => 'wpSummary',
374 'cssclass' => 'mw-summary',
375 'label-message' => 'summary',
376 'maxlength' => CommentStore
::COMMENT_CHARACTER_LIMIT
,
377 'value' => $request->getVal( 'wpSummary', '' ),
379 'spellcheck' => 'true',
381 'summarypreview' => [
383 'label-message' => 'summary-preview',
388 if ( $request->getCheck( 'wpSummary' ) ) {
389 $ret['summarypreview']['default'] = Xml
::tags( 'div', [ 'class' => 'mw-summary-preview' ],
390 Linker
::commentBlock( trim( $request->getVal( 'wpSummary' ) ), $this->getTitle(), false )
393 unset( $ret['summarypreview'] );
399 protected function alterForm( HTMLForm
$form ) {
400 $form->setWrapperLegendMsg( 'confirm-mcrundo-title' );
402 $labelAsPublish = $this->context
->getConfig()->get( 'EditSubmitButtonLabelPublish' );
404 $form->setId( 'mw-mcrundo-form' );
405 $form->setSubmitName( 'wpSave' );
406 $form->setSubmitTooltip( $labelAsPublish ?
'publish' : 'save' );
407 $form->setSubmitTextMsg( $labelAsPublish ?
'publishchanges' : 'savechanges' );
408 $form->showCancel( true );
409 $form->setCancelTarget( $this->getTitle() );
411 'name' => 'wpPreview',
413 'label-message' => 'showpreview',
414 'attribs' => Linker
::tooltipAndAccesskeyAttribs( 'preview' ),
419 'label-message' => 'showdiff',
420 'attribs' => Linker
::tooltipAndAccesskeyAttribs( 'diff' ),
423 $this->addStatePropagationFields( $form );
426 protected function addStatePropagationFields( HTMLForm
$form ) {
427 $form->addHiddenField( 'undo', $this->undo
);
428 $form->addHiddenField( 'undoafter', $this->undoafter
);
429 $form->addHiddenField( 'cur', $this->curRev
->getId() );
432 public function onSuccess() {
433 $this->getOutput()->redirect( $this->getTitle()->getFullURL() );
436 protected function preText() {
437 return '<div style="clear:both"></div>';