From 5d6d3aa45586747a03fa670383e8c7a136d52503 Mon Sep 17 00:00:00 2001 From: wctaiwan Date: Sun, 13 Sep 2015 06:07:36 -0400 Subject: [PATCH] Handle revisions with different content models in EditPage Disable editing and serialize using the default format if the revision has a different content model from the current one. Bug: T73163 Change-Id: If28de7a3612d414a4366e3d493598244c0754b90 --- includes/EditPage.php | 42 ++++++++++++++++++++++++++++++++++++++--- languages/i18n/en.json | 1 + languages/i18n/qqq.json | 1 + 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/includes/EditPage.php b/includes/EditPage.php index 8571cd79a9..81f35f9ea2 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -537,6 +537,20 @@ class EditPage { return; } + $revision = $this->mArticle->getRevisionFetched(); + // Disallow editing revisions with content models different from the current one + if ( $revision && $revision->getContentModel() !== $this->contentModel ) { + $this->displayViewSourcePage( + $this->getContentObject(), + wfMessage( + 'contentmodelediterror', + $revision->getContentModel(), + $this->contentModel + )->plain() + ); + return; + } + $this->isConflict = false; // css / js subpages of user pages get a special treatment $this->isCssJsSubpage = $this->mTitle->isCssJsSubpage(); @@ -647,6 +661,20 @@ class EditPage { throw new PermissionsError( $action, $permErrors ); } + $this->displayViewSourcePage( + $content, + $wgOut->formatPermissionsErrorMessage( $permErrors, 'edit' ) + ); + } + + /** + * Display a read-only View Source page + * @param Content $content content object + * @param string $errorMessage additional wikitext error message to display + */ + protected function displayViewSourcePage( Content $content, $errorMessage = '' ) { + global $wgOut; + Hooks::run( 'EditPage::showReadOnlyForm:initial', array( $this, &$wgOut ) ); $wgOut->setRobotPolicy( 'noindex,nofollow' ); @@ -658,8 +686,10 @@ class EditPage { $wgOut->addHTML( $this->editFormPageTop ); $wgOut->addHTML( $this->editFormTextTop ); - $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $permErrors, 'edit' ) ); - $wgOut->addHTML( "
\n" ); + if ( $errorMessage !== '' ) { + $wgOut->addWikiText( $errorMessage ); + $wgOut->addHTML( "
\n" ); + } # If the user made changes, preserve them when showing the markup # (This happens when a user is blocked during edit, for instance) @@ -667,7 +697,13 @@ class EditPage { $text = $this->textbox1; $wgOut->addWikiMsg( 'viewyourtext' ); } else { - $text = $this->toEditText( $content ); + try { + $text = $this->toEditText( $content ); + } catch ( MWException $e ) { + # Serialize using the default format if the content model is not supported + # (e.g. for an old revision with a different model) + $text = $content->serialize(); + } $wgOut->addWikiMsg( 'viewsourcetext' ); } diff --git a/languages/i18n/en.json b/languages/i18n/en.json index 8f4ef9638e..e2440fe4dc 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -692,6 +692,7 @@ "permissionserrors": "Permission error", "permissionserrorstext": "You do not have permission to do that, for the following {{PLURAL:$1|reason|reasons}}:", "permissionserrorstext-withaction": "You do not have permission to $2, for the following {{PLURAL:$1|reason|reasons}}:", + "contentmodelediterror": "You cannot edit this revision because its content model is $1, but the current content model of the page is $2.", "recreate-moveddeleted-warn": "Warning: You are recreating a page that was previously deleted.\n\nYou should consider whether it is appropriate to continue editing this page.\nThe deletion and move log for this page are provided here for convenience:", "moveddeleted-notice": "This page has been deleted.\nThe deletion and move log for the page are provided below for reference.", "moveddeleted-notice-recent": "Sorry, this page was recently deleted (within the last 24 hours).\nThe deletion and move log for the page are provided below for reference.", diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index 95bd205635..6a11080ae4 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -865,6 +865,7 @@ "permissionserrors": "Used as title of error message.\n\nSee also:\n* {{msg-mw|loginreqtitle}}\n{{Identical|Permission error}}", "permissionserrorstext": "This message is \"without action\" version of {{msg-mw|Permissionserrorstext-withaction}}.\n\nParameters:\n* $1 - the number of reasons that were found why ''the action'' cannot be performed", "permissionserrorstext-withaction": "This message is \"with action\" version of {{msg-mw|Permissionserrorstext}}.\n\nParameters:\n* $1 - the number of reasons that were found why the action cannot be performed\n* $2 - one of the action-* messages (for example {{msg-mw|action-edit}}) or other such messages tagged with {{tl|doc-action}} in their documentation\n\nPlease report at [[Support]] if you are unable to properly translate this message. Also see [[phab:T16246]] (now closed) for background.", + "contentmodelediterror": "Error message shown when trying to edit an old revision with a content model different from that of the current revision\n* $1 - content model of the old revision\n* $2 - content model of the current revision", "recreate-moveddeleted-warn": "Warning shown when creating a page which has already been deleted. See for example [[Test]].", "moveddeleted-notice": "Shown on top of a deleted page in normal view modus ([{{canonicalurl:Test}} example]).", "moveddeleted-notice-recent": "Shown on top of a recently deleted page in normal view modus ([{{canonicalurl:Test}} example]).", -- 2.20.1