Merge "Add validation of the content model edited by EditPage"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 9 Jan 2014 15:02:08 +0000 (15:02 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 9 Jan 2014 15:02:08 +0000 (15:02 +0000)
includes/EditPage.php
languages/messages/MessagesEn.php
languages/messages/MessagesQqq.php
maintenance/language/messages.inc

index 1a69707..7115eab 100644 (file)
@@ -319,6 +319,18 @@ class EditPage {
                }
        }
 
+       /**
+        * 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() {
                $this->edit();
        }
@@ -613,6 +625,7 @@ class EditPage {
        /**
         * This function collects the form data and uses it to populate various member variables.
         * @param $request WebRequest
+        * @throws ErrorPageError
         */
        function importFormData( &$request ) {
                global $wgContLang, $wgUser;
@@ -777,12 +790,17 @@ class EditPage {
                $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
 
+               if ( !ContentHandler::getForModelID( $this->contentModel )->isSupportedFormat( $this->contentFormat ) ) {
+                       throw new ErrorPageError(
+                               'editpage-notsupportedcontentformat-title',
+                               'editpage-notsupportedcontentformat-text',
+                               array( $this->contentFormat, ContentHandler::getLocalizedName( $this->contentModel ) )
+                       );
+               }
                #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!
 
                $this->live = $request->getCheck( 'live' );
                $this->editintro = $request->getText( 'editintro',
@@ -2126,9 +2144,9 @@ class EditPage {
                        return $content;
                }
 
-               if ( !$this->allowNonTextContent && !( $content instanceof TextContent ) ) {
-                       throw new MWException( "This content model can not be edited as text: "
-                                                               . ContentHandler::getLocalizedName( $content->getModel() ) );
+               if ( !$this->isSupportedContentModel( $content->getModel() ) ) {
+                       throw new MWException( 'This content model is not supported: '
+                               . ContentHandler::getLocalizedName( $content->getModel() ) );
                }
 
                return $content->serialize( $this->contentFormat );
@@ -2156,8 +2174,8 @@ class EditPage {
                $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() ) );
                }
 
index 2a10a78..0cbeacc 100644 (file)
@@ -1578,6 +1578,8 @@ It already exists.',
 'content-not-allowed-here'         => '"$1" content is not allowed on page [[$2]]',
 'editwarning-warning'              => 'Leaving this page may cause you to lose any changes you have made.
 If you are logged in, you can disable this warning in the "Editing" section of your preferences.',
+'editpage-notsupportedcontentformat-title'=> 'Content format not supported',
+'editpage-notsupportedcontentformat-text' => 'The content format $1 is not supported by the content model $2.',
 
 # Content models
 'content-model-wikitext'   => 'wikitext',
index 80b8bc4..89cee68 100644 (file)
@@ -2138,6 +2138,11 @@ Parameters:
 'editwarning-warning' => "{{doc-important|Do ''not'' use <nowiki>{{int:prefs-editing}}</nowiki> for \"Editing\". It is forbidden in this message, see [[mwr:68405]].}}
 
 but you can see the text of that button here: {{msg-mw|Prefs-editing}}",
+'editpage-notsupportedcontentformat-title'=> 'Title of error page shown when using an incompatible format on EditPage',
+'editpage-notsupportedcontentformat-text' => 'Error message shown when using an incompatible format on EditPage.
+* $1 is the format id
+* $2 is the content model name
+',
 
 # Content models
 'content-model-wikitext' => 'Name for the wikitext content model, used when decribing what type of content a page contains.
index aa8f5ed..d304425 100644 (file)
@@ -766,6 +766,8 @@ $wgMessageStructure = array(
                'invalid-content-data',
                'content-not-allowed-here',
                'editwarning-warning',
+               'editpage-notsupportedcontentformat-title',
+               'editpage-notsupportedcontentformat-text',
        ),
        'contentmodels' => array(
                'content-model-wikitext',