McrUndoAction: Fix preview
authorBrad Jorsch <bjorsch@wikimedia.org>
Fri, 21 Sep 2018 14:43:48 +0000 (10:43 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Fri, 21 Sep 2018 14:50:03 +0000 (10:50 -0400)
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

includes/actions/McrUndoAction.php
languages/i18n/en.json
languages/i18n/qqq.json

index 90d1f68..15a394d 100644 (file)
@@ -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 = '<span class="mw-continue-editing">' .
+                               '[[#mw-mcrundo-form|' .
+                               $this->context->getLanguage()->getArrow() . ' ' .
+                               $this->context->msg( 'continue-editing' )->text() . ']]</span>';
+
+                       $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 = "<div class='previewnote'>\n" .
+                       '<h2 id="mw-previewheader">' . $this->context->msg( 'preview' )->escaped() . "</h2>" .
+                       $out->parse( $note, true, /* interface */true ) . "<hr /></div>\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' );
index 4d28bd6..1831b9f 100644 (file)
        "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": ";&#32;",
        "comma-separator": ",&#32;",
        "colon-separator": ":&#32;",
index cf4446d..a65c3c8 100644 (file)
        "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.",