Tpt has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/97835


Change subject: (bug 57615) Add validation of the content model edited by 
EditPage
......................................................................

(bug 57615) Add validation of the content model edited by EditPage

Change-Id: I6c1bb9774542e39bfb899a47b7afac516fea4a16
---
M includes/EditPage.php
1 file changed, 30 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/35/97835/1

diff --git a/includes/EditPage.php b/includes/EditPage.php
index ab5856a..25806fc 100644
--- a/includes/EditPage.php
+++ b/includes/EditPage.php
@@ -274,6 +274,10 @@
                $this->mTitle = $article->getTitle();
 
                $this->contentModel = $this->mTitle->getContentModel();
+               if( !$this->isSupportedContentModel( $this->contentModel ) ) {
+                       throw new MWException( 'The content model is not 
supported: '
+                               . ContentHandler::getLocalizedName( 
$this->contentModel ) );
+               }
 
                $handler = ContentHandler::getForModelID( $this->contentModel );
                $this->contentFormat = $handler->getDefaultFormat();
@@ -317,6 +321,18 @@
                } else {
                        return $this->mContextTitle;
                }
+       }
+
+       /**
+        * Returns if the given content model is editable.
+        *
+        * @param string $modelId The ID of the content model to test. Use 
CONTENT_MODEL_XXX constants.
+        * @return bool
+        * @throws MWException if $modelId has no known handler
+        */
+       public function isSupportedContentModel( $modelId ) {
+               return $this->allowNonTextContent ||
+                       ContentHandler::getForModelID( $modelId ) instanceof 
TextContentHandler;
        }
 
        function submit() {
@@ -777,12 +793,17 @@
                $this->bot = $request->getBool( 'bot', true );
                $this->nosummary = $request->getBool( 'nosummary' );
 
-               $content_handler = ContentHandler::getForTitle( $this->mTitle );
-               $this->contentModel = $request->getText( 'model', 
$content_handler->getModelID() ); #may be overridden by revision
-               $this->contentFormat = $request->getText( 'format', 
$content_handler->getDefaultFormat() ); #may be overridden by revision
+               $this->contentModel = $request->getText( 'model', 
$this->contentModel ); #may be overridden by revision
+               $this->contentFormat = $request->getText( 'format', 
$this->contentFormat ); #may be overridden by revision
 
-               #TODO: check if the desired model is allowed in this namespace, 
and if a transition from the page's current model to the new model is allowed
-               #TODO: check if the desired content model supports the given 
content format!
+               if ( !$this->isSupportedContentModel( $this->contentModel ) ) {
+                       throw new MWException( 'The content model is not 
supported: '
+                                                               . 
ContentHandler::getLocalizedName( $this->contentModel ) );
+               }
+               if ( !ContentHandler::getForModelID( $this->contentModel 
)->isSupportedFormat( $this->contentFormat ) ) {
+                       throw new MWException( 'The content format is not 
supported by the content model' );
+               }
+               #TODO: check if a transition from the page's current model to 
the new model is allowed
 
                $this->live = $request->getCheck( 'live' );
                $this->editintro = $request->getText( 'editintro',
@@ -2128,8 +2149,8 @@
                        return $content;
                }
 
-               if ( !$this->allowNonTextContent && !( $content instanceof 
TextContent ) ) {
-                       throw new MWException( "This content model can not be 
edited as text: "
+               if ( !$this->isSupportedContentModel( $content->getModel() ) ) {
+                       throw new MWException( 'This content model is not 
supported: '
                                                                . 
ContentHandler::getLocalizedName( $content->getModel() ) );
                }
 
@@ -2158,8 +2179,8 @@
                $content = ContentHandler::makeContent( $text, 
$this->getTitle(),
                        $this->contentModel, $this->contentFormat );
 
-               if ( !$this->allowNonTextContent && !( $content instanceof 
TextContent ) ) {
-                       throw new MWException( "This content model can not be 
edited as text: "
+               if ( !$this->isSupportedContentModel( $content->getModel() ) ) {
+                       throw new MWException( 'This content model is not 
supported:'
                                . ContentHandler::getLocalizedName( 
$content->getModel() ) );
                }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/97835
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6c1bb9774542e39bfb899a47b7afac516fea4a16
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Tpt <thoma...@hotmail.fr>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to