From: Brad Jorsch Date: Fri, 21 Sep 2018 14:43:48 +0000 (-0400) Subject: McrUndoAction: Fix preview X-Git-Tag: 1.34.0-rc.0~4002^2~1 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=2e345debf2382480ff2d02d9824524eb15521890;p=lhc%2Fweb%2Fwiklou.git McrUndoAction: Fix preview Apparently DifferenceEngine::renderNewRevision() never really worked right, it only functions when diffing *saved* revisions. Replace the preview code with code adapted from EditPage::getPreviewText(), using RevisionRenderer for the actual rendering. Bug: T205101 Change-Id: I416ede84c8d10fc28866e608c9cfc3a2a7410e48 --- diff --git a/includes/actions/McrUndoAction.php b/includes/actions/McrUndoAction.php index 90d1f686cd..15a394da09 100644 --- a/includes/actions/McrUndoAction.php +++ b/includes/actions/McrUndoAction.php @@ -219,7 +219,7 @@ class McrUndoAction extends FormAction { return $newRev; } - private function generateDiff() { + private function generateDiffOrPreview() { $newRev = $this->getNewRevision(); if ( $newRev->hasSameContent( $this->curRev ) ) { throw new ErrorPageError( 'mcrundofailed', 'undo-nochange' ); @@ -232,7 +232,7 @@ class McrUndoAction extends FormAction { $newtitle = $this->context->msg( 'yourtext' )->parse(); if ( $this->getRequest()->getCheck( 'wpPreview' ) ) { - $diffEngine->renderNewRevision(); + $this->showPreview( $newRev ); return ''; } else { $diffText = $diffEngine->getDiff( $oldtitle, $newtitle ); @@ -241,6 +241,56 @@ class McrUndoAction extends FormAction { } } + private function showPreview( RevisionRecord $rev ) { + // Mostly copied from EditPage::getPreviewText() + $out = $this->getOutput(); + + try { + $previewHTML = ''; + + # provide a anchor link to the form + $continueEditing = '' . + '[[#mw-mcrundo-form|' . + $this->context->getLanguage()->getArrow() . ' ' . + $this->context->msg( 'continue-editing' )->text() . ']]'; + + $note = $this->context->msg( 'previewnote' )->plain() . ' ' . $continueEditing; + + $parserOptions = $this->page->makeParserOptions( $this->context ); + $parserOptions->setIsPreview( true ); + $parserOptions->setIsSectionPreview( false ); + $parserOptions->enableLimitReport(); + + $parserOutput = MediaWikiServices::getInstance()->getRevisionRenderer() + ->getRenderedRevision( $rev, $parserOptions, $this->context->getUser() ) + ->getRevisionParserOutput(); + $previewHTML = $parserOutput->getText( [ 'enableSectionEditLinks' => false ] ); + + $out->addParserOutputMetadata( $parserOutput ); + if ( count( $parserOutput->getWarnings() ) ) { + $note .= "\n\n" . implode( "\n\n", $parserOutput->getWarnings() ); + } + } catch ( MWContentSerializationException $ex ) { + $m = $this->context->msg( + 'content-failed-to-parse', + $ex->getMessage() + ); + $note .= "\n\n" . $m->parse(); + $previewHTML = ''; + } + + $previewhead = "
\n" . + '

' . $this->context->msg( 'preview' )->escaped() . "

" . + $out->parse( $note, true, /* interface */true ) . "
\n"; + + $pageViewLang = $this->getTitle()->getPageViewLanguage(); + $attribs = [ 'lang' => $pageViewLang->getHtmlCode(), 'dir' => $pageViewLang->getDir(), + 'class' => 'mw-content-' . $pageViewLang->getDir() ]; + $previewHTML = Html::rawElement( 'div', $attribs, $previewHTML ); + + $out->addHtml( $previewhead . $previewHTML ); + } + public function onSubmit( $data ) { global $wgUseRCPatrol; @@ -306,7 +356,7 @@ class McrUndoAction extends FormAction { 'vertical-label' => true, 'raw' => true, 'default' => function () { - return $this->generateDiff(); + return $this->generateDiffOrPreview(); } ], 'summary' => [ @@ -343,6 +393,7 @@ class McrUndoAction extends FormAction { $labelAsPublish = $this->context->getConfig()->get( 'EditSubmitButtonLabelPublish' ); + $form->setId( 'mw-mcrundo-form' ); $form->setSubmitName( 'wpSave' ); $form->setSubmitTooltip( $labelAsPublish ? 'publish' : 'save' ); $form->setSubmitTextMsg( $labelAsPublish ? 'publishchanges' : 'savechanges' ); diff --git a/languages/i18n/en.json b/languages/i18n/en.json index 4d28bd6354..1831b9fc8e 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -3674,6 +3674,7 @@ "mcrundofailed": "Undo failed", "mcrundo-missingparam": "Missing required parameters on request.", "mcrundo-changed": "The page has been changed since you viewed the diff. Please review the new change.", + "mcrundo-parse-failed": "Failed to parse the new revision: $1", "semicolon-separator": "; ", "comma-separator": ", ", "colon-separator": ": ", diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index cf4446db59..a65c3c84d1 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -3876,6 +3876,7 @@ "mcrundofailed": "Title of the error page when an editless undo fails.", "mcrundo-missingparam": "Error displayed when parameters for action=mcrundo are missing", "mcrundo-changed": "Message displayed when the page has been edited between the loading and submission of an editless undo.", + "mcrundo-parse-failed": "Error message indicating that the page's content can not be parsed because it is syntactically invalid. This may occurr for content types using serialization or a strict markup syntax.\n\nParameters:\n* $1 – specific error message", "semicolon-separator": "{{optional}}", "comma-separator": "{{optional}}\n\nWarning: languages have different usages of punctuation, and sometimes they are swapped (e.g. openining and closing quotation marks, or full stop and colon in Armenian), or change their form (the full stop in Chinese and Japanese, the prefered \"colon\" in Armenian used in fact as the regular full stop, the comma in Arabic, Armenian, and Chinese...)\n\nTheir spacing (before or after) may also vary across languages (for example French requires a non-breaking space, preferably narrow if the browser supports NNBSP, on the inner side of some punctuations like quotation/question/exclamation marks, colon, and semicolons).", "colon-separator": "{{optional}}\nChange it only if your language uses another character for ':' or it needs an extra space before the colon.",